From ca5db83e8f5e0528e75ee91ae735d2abbfd3ebf7 Mon Sep 17 00:00:00 2001
From: Brion Vibber <brion@pobox.com>
Date: Sat, 28 Jan 2023 16:31:55 -0800
Subject: [PATCH] rethought the rounding function

realized the top bits are not enough, that was a brainfart
doing comparisons now
---
 mandel.s | 44 +++++++++++++++++++++-----------------------
 1 file changed, 21 insertions(+), 23 deletions(-)

diff --git a/mandel.s b/mandel.s
index 30ed07d..ff402ad 100644
--- a/mandel.s
+++ b/mandel.s
@@ -359,35 +359,33 @@ positive_result:
 
 .macro round16 arg
     ; Round top 16 bits of 32-bit fixed-point number in-place
-    .local positive
-    .local negative
-    .local no_carry
+    .local increment
+    .local high_half
+    .local check_sign
     .local next
 
-    ; positive input:
-    ;   0 rounds down, no change - 5 cyc
-    ;   1 rounds up, add one - 15-20 cyc
-    ; negative input:
-    ;   0 rounds up, no change - 5 cyc
-    ;   1 rounds down, sub one - 23-28 cyc
+    ; low word > $8000: round up
+    ;          = $8000: round up   if positive
+    ;                   round down if negative
+    ;          < $8000: round down
 
-    ; check 16th bit down
-    lda arg + 1   ; 3 cyc
-    bpl next      ; 2 cyc
+    lda arg + 1
+    cmp #$80
+    beq high_half
+    bpl increment
+    bmi next
 
-    ; check sign bit
-    lda arg + 3  ; 3 cyc
-    bpl positive ; 2 cyc
+high_half:
+    lda arg
+    beq check_sign
+    bpl increment
+    bmi next
 
-negative:        ; 13-18 cyc
-    lda arg + 2  ; 3 cyc
-    bne no_carry ; 2 cyc
-    dec arg + 3  ; 5 cyc
-no_carry:
-    dec arg + 2  ; 5 cyc
-    jmp next     ; 3 cyc
+check_sign:
+    lda arg + 3
+    bmi next
 
-positive:        ; 5-10 cyc
+increment:       ; 5-10 cyc
     inc arg + 2  ; 5 cyc
     bne next     ; 2 cyc
     inc arg + 3  ; 5 cyc