changeset 102:baead5689afc

Fix logic errors in floating point addition routine
author William Astle <lost@l-w.ca>
date Mon, 30 Oct 2023 20:48:29 -0600
parents b0422868a7b1
children 2f97bfecffab
files src/fps.s
diffstat 1 files changed, 5 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/fps.s	Mon Oct 23 23:15:55 2023 -0600
+++ b/src/fps.s	Mon Oct 30 20:48:29 2023 -0600
@@ -121,7 +121,8 @@
                 std fpa1+fpa.sig+5
                 std fpa1+fpa.sig+7
                 sta fpa1+fpa.sig+9
-                subb fpa0+fpa.exp               ; calculate exponent difference
+                ldb fpa0+fpa.exp                ; fetch exponent of second argument
+                subb fpa1+fpa.exp               ; calculate exponent difference
                 lbeq fps_add8                   ; brif same exponent - no need to denormalize
                 bcs fps_add4                    ; brif second operand is bigger
                 ldx #fpa1                       ; point to second operand to denormalize
@@ -129,11 +130,11 @@
                 bhs fps_add0                    ; brif so - return first operand
                 bra fps_add5                    ; go shift operand right by B places
 fps_add4        negb                            ; get positive number of shifts
+                cmpb #10                        ; shifting more than precision?
+                bhs fps_add2                    ; brif so - return second operand
                 ldx #fpa0                       ; shift left operand right B places
                 lda fpa1+fpa.exp                ; set exponent of result to larger of two
                 sta fpa0+fpa.exp
-                cmpb #10                        ; shifting more than precision?
-                bhs fps_add2                    ; brif so - return second operand
 fps_add5        subb #2                         ; do we need at least two places?
                 bcs fps_add6                    ; brif not
                 lda fpa.sig+8,x                 ; shift right into extra digits
@@ -220,7 +221,7 @@
                 cmpa fpa1+fpa.sig+4
                 bne fps_add9
                 lda fpa0+fpa.sig+5
-                cmpa fpa1+fpa.sig+5             ; don't have to check other extras; only one set will be set
+                cmpa fpa1+fpa.sig+5             ; don't have to check other extras; the first extra will be decisive
 fps_add9        bhs fps_add10                   ; brif first operand is bigger
                 ldx #fpa0                       ; point to first operand as smaller
                 lda fpa1+fpa.sign               ; set result sign to that of larger operand