From 985371433b016b4412f115c852fd44e0cf6dba69 Mon Sep 17 00:00:00 2001
From: Piotr Fusik <fox@scene.pl>
Date: Tue, 5 Nov 2019 20:26:02 +0100
Subject: [PATCH 089/125] Swap the positive/negative paths to save a branch.
---
libsrc/runtime/imul8x8r16.s | 55 +++++++++++++++++++++----------------
1 file changed, 31 insertions(+), 24 deletions(-)
diff --git a/libsrc/runtime/imul8x8r16.s b/libsrc/runtime/imul8x8r16.s
index 72e6d03a..fe939f66 100644
--- a/libsrc/runtime/imul8x8r16.s
+++ b/libsrc/runtime/imul8x8r16.s
@@ -32,24 +32,7 @@ imul8x8r16m:
ldy #<$0000 ; Clear .XY accumulator
ldx #>$0000
lda ptr1
- bmi NegMult
- 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
+ bpl PosStart
; The multiplier is negative.
; Therefore, make it positive; and, subtract when multiplying.
@@ -57,9 +40,10 @@ NegMult:
eor #%11111111
sta ptr1
inc ptr1
- bnz @L2 ; Branch always
+ bnz NegStart ; Branch always
-@L0: tya ; Subtract current multiplicand
+NegAdd:
+ tya ; Subtract current multiplicand
; sec
sbc ptr3
tay
@@ -67,11 +51,34 @@ NegMult:
sbc ptr3+1
tax
-@L1: asl ptr3
+NegShift:
+ asl ptr3
+ rol ptr3+1
+NegStart:
+ lsr ptr1 ; Get next bit of Right-Hand Side into carry
+ 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
-@L2: lsr ptr1 ; Get next bit of Right-Hand Side into carry
- bcs @L0
- bnz @L1 ; Loop if more one-bits in multiplier
+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
rts
--
2.23.0