diff src/number.s @ 98:6837d10b67fb

Add integer <-> float conversion routines and combine some code for parsing
author William Astle <lost@l-w.ca>
date Sun, 22 Oct 2023 23:54:24 -0600
parents 25b44f1ac2aa
children 6db72a92ff7a
line wrap: on
line diff
--- a/src/number.s	Sun Oct 22 21:15:14 2023 -0600
+++ b/src/number.s	Sun Oct 22 23:54:24 2023 -0600
@@ -267,60 +267,27 @@
                 lda fpa0+fpa.exp                ; put the bias into the exponent
                 adda #64
                 sta fpa0+fpa.exp
-                ldy #val0+val.value             ; normalize/round and return the result
-                jmp fps_normalize
+                jsr fps_normalizea0             ; normalize fpa0
+                ldd fpa0+fpa.sig                ; copy result to the right place
+                std val0+val.fpssig
+                ldd fpa0+fpa.sig+2
+                std val0+val.fpssig+2
+                lda fpa0+fpa.sig+4
+                sta val0+val.fpssig+4
+                lda fpa0+fpa.exp
+                sta val0+val.fpsexp
+                lda fpa0+fpa.sign
+                sta val0+val.fpssign
+                rts
 val_parsenum14  lda #valtype_int                ; set value type to integer
                 sta val0+val.type
-                ldb #9                          ; exponent needed for decimal point to the right of significand
-                subb fpa0+fpa.exp               ; number of digit shifts needed to denormalize
-                beq val_parsenum16              ; brif already denormalized
-                lslb                            ; do 4 shifts per digit
-                lslb
-val_parsenum15  lsr fpa0+fpa.sig                ; shift a digit right
-                ror fpa0+fpa.sig+1
-                ror fpa0+fpa.sig+2
-                ror fpa0+fpa.sig+3
-                ror fpa0+fpa.sig+4
-                decb                            ; done all shifts?
-                bne val_parsenum15
-; Now convert BCD digit sequence in fpa0 significand to binary value in val0
-val_parsenum16  ldb #32                         ; 40 bit shifts needed for whole significand
-                stb fpa0+fpa.extra              ; use extra precision byte as counter
-val_parsenum17  lsr fpa0+fpa.sig                ; shift a bit into the binary result
-                ror fpa0+fpa.sig+1
-                ror fpa0+fpa.sig+2
-                ror fpa0+fpa.sig+3
-                ror fpa0+fpa.sig+4
-                ror val0+val.int
-                ror val0+val.int+1
-                ror val0+val.int+2
-                ror val0+val.int+3
-                ldx #fpa0+fpa.sig               ; point to BCD digits
-val_parsenum18  lda ,x                          ; get byte to check
-                beq val_parsenum20              ; short circuit check if digits are 0
-                anda #0x88                      ; keep bit 3 of each digit; adjustment on >= 8
-                lsra                            ; shift over and mulply by adjustment factor
-                lsra
-                lsra
-                ldb #3                          ; the adjustment is a subtraction by 3
-                mul
-                negb                            ; now subtract from digit
-                addb ,x
-                stb ,x+
-val_parsenum18a cmpx #fpa0+fpa.sig+5            ; done all 5 bytes?
-                blo val_parsenum18              ; brif not
-                dec fpa0+fpa.extra              ; done all bits?
-                bne val_parsenum17              ; brif not
-                ldb fpa0+fpa.sign               ; do we want negative?
-                bpl val_parsenum19              ; brif not
-                ldd zero                        ; negate the value through subtracting from 0
-                subd val0+val.int+2
+                ldb fpa0+fpa.exp                ; add the exponent bias in
+                addb #64
+                stb fpa0+fpa.exp
+                jsr fps_toint1                  ; go convert to an integer
+                ldd fpa0+fpa.sig+1              ; copy result to val0
+                std val0+val.int
+                ldd fpa0+fpa.sig+3
                 std val0+val.int+2
-                ldd zero
-                sbcb val0+val.int+1
-                sbca val0+val.int
-                std val0+val.int
-val_parsenum19  rts
-val_parsenum20  leax 1,x                        ; move to next digit
-                bra val_parsenum18a             ; go back to mainline
+                rts
                 *pragmapop list