rethought the rounding function

realized the top bits are not enough, that was a brainfart
doing comparisons now
This commit is contained in:
Brooke Vibber 2023-01-28 16:31:55 -08:00
parent b55015ff87
commit ca5db83e8f

View file

@ -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