1
0
mirror of https://github.com/cc65/cc65.git synced 2025-08-13 23:25:29 +00:00

Swap the positive/negative paths to save a branch.

This commit is contained in:
Piotr Fusik
2019-11-05 20:26:02 +01:00
committed by Oliver Schmidt
parent 421c3f2b4f
commit 985371433b

View File

@@ -32,24 +32,7 @@ imul8x8r16m:
ldy #<$0000 ; Clear .XY accumulator ldy #<$0000 ; Clear .XY accumulator
ldx #>$0000 ldx #>$0000
lda ptr1 lda ptr1
bmi NegMult bpl PosStart
bpl @L2 ; Branch always
@L0: tya ; Add current multiplicand
add ptr3
tay
txa
adc ptr3+1
tax
@L1: asl ptr3
rol ptr3+1
@L2: lsr ptr1 ; Get next bit of Right-Hand Side into carry
bcs @L0
bnz @L1 ; Loop if more one-bits in multiplier
tya ; Put result into cc65's accumulator
rts
; The multiplier is negative. ; The multiplier is negative.
; Therefore, make it positive; and, subtract when multiplying. ; Therefore, make it positive; and, subtract when multiplying.
@@ -57,9 +40,10 @@ NegMult:
eor #%11111111 eor #%11111111
sta ptr1 sta ptr1
inc ptr1 inc ptr1
bnz @L2 ; Branch always bnz NegStart ; Branch always
@L0: tya ; Subtract current multiplicand NegAdd:
tya ; Subtract current multiplicand
; sec ; sec
sbc ptr3 sbc ptr3
tay tay
@@ -67,11 +51,34 @@ NegMult:
sbc ptr3+1 sbc ptr3+1
tax tax
@L1: asl ptr3 NegShift:
asl ptr3
rol ptr3+1 rol ptr3+1
@L2: lsr ptr1 ; Get next bit of Right-Hand Side into carry NegStart:
bcs @L0 lsr ptr1 ; Get next bit of Right-Hand Side into carry
bnz @L1 ; Loop if more one-bits in multiplier bcs NegAdd
bnz NegShift ; Loop if more one-bits in multiplier
tya ; Put result into cc65's accumulator
rts
; The multiplier is positive.
PosAdd:
tya ; Add current multiplicand
add ptr3
tay
txa
adc ptr3+1
tax
PosShift:
asl ptr3
rol ptr3+1
PosStart:
lsr ptr1 ; Get next bit of Right-Hand Side into carry
bcs PosAdd
bnz PosShift ; Loop if more one-bits in multiplier
tya ; Put result into cc65's accumulator tya ; Put result into cc65's accumulator
rts rts