File tree Expand file tree Collapse file tree 2 files changed +71
-40
lines changed Expand file tree Collapse file tree 2 files changed +71
-40
lines changed Original file line number Diff line number Diff line change @@ -11,70 +11,91 @@ f32toi16:
1111 push de
1212 push bc
1313 push af
14- ld e,(hl)
14+
15+ ld c,(hl)
1516 inc hl
16- ld d ,(hl)
17+ ld e ,(hl)
1718 inc hl
1819 ld a,(hl)
1920 rlca
2021 scf
2122 rra
22- ld c ,a
23+ ld d ,a
2324 inc hl
2425 ld a,(hl)
2526 adc a,a
26-
27- ; carry flag is sign, CDE is the significand, A is the exponent
28- push af ; carry flag is a sign
29-
27+ ; carry flag is sign, DEC is the significand, A is the exponent
3028 call f32toi16_get_int
31-
32- pop af ; get the carry flag
33- ld a,h
34- jr nc,f32toi16_return
35- ; -HL-1
36- ld a,l
37- cpl
38- ld l,a
39- ld a,h
40- cpl
41- ld h,a
4229f32toi16_return:
4330 pop af
4431 pop bc
4532 pop de
4633 ret
4734
4835f32toi16_get_int:
49- ld hl,32767
36+ rl b ; save the sign
5037 cp 142
51- ret nc
52- sub h ;
38+ jr nc,f32toi16_return_early
39+ sub 127
5340 ld hl,0
5441 ret c
55- ld b,a
5642
5743 ; if the exponent is 128, return 0 if NaN, else inf
5844 jp p,f32toi16_extract
59- ld a,l
45+ ld a,c
6046 or e
6147 or d
6248 ret nz
49+ f32toi16_return_early:
6350 ld hl,32767
51+ rr b
52+ ret nc
53+ inc hl
6454 ret
55+
6556f32toi16_extract:
57+ ex de,hl ; significand is in HLC now, but we don't need to track C
58+ ;DE is 0
59+ cp 7
60+ jr c,$+7
61+ ;shift up by 8
62+ sub 8
63+ ld e,h
64+ ld h,l
65+ ld l,d ; 0
66+
67+ ld d,b ; save sign
68+ ld b,a
6669 inc b
67- ld l,d
68- ld h,c
6970 xor a
70- ld e,a
71+ ;AE.HLC
7172
7273 add hl,hl
7374 rl e
7475 rla
7576 djnz $-4
7677
78+ ld b,d ; save sign again
79+ ld d,a
80+ ex de,hl
81+ ; HL is the result, DEC has any fractional bits
82+
83+ rrc b ; if carry is reset, then we are done
84+ ret nc
85+ ; otherwise the number is negative, so if the fractional part is non-zero,
86+ ; need to round down
87+
88+ xor a
89+ sub l
90+ ld l,a
91+ sbc a,a
92+ sub h
7793 ld h,a
78- ld l,e
94+
95+ ld a,c
96+ or d
97+ or e
98+ ret z
99+ dec hl
79100 ret
80101#endif
Original file line number Diff line number Diff line change @@ -12,9 +12,10 @@ f32toi8:
1212 push hl
1313 push de
1414 push bc
15- ld e ,(hl)
15+ ld a ,(hl)
1616 inc hl
17- ld d,(hl)
17+ or (hl)
18+ ld d,a ; save the OR of the bottom two bytes of the significand
1819 inc hl
1920 ld a,(hl)
2021 rlca
@@ -25,42 +26,51 @@ f32toi8:
2526 ld a,(hl)
2627 adc a,a
2728
28- ; carry flag is sign, CDE is the significand, A is the exponent
29- push af ; carry flag is a sign
29+ rr e ; save the sign
30+ ; E has the sign, C is the portion of the significand that matters,
31+ ; A is the exponent
3032
3133 call f32toi8_get_int
3234
33- pop af ; get the carry flag
34- ; if carry is set, return -H-1, else return H
35- sbc a,a
36- xor h
3735f32toi8_return:
3836 pop bc
3937 pop de
4038 pop hl
4139 ret
4240
4341f32toi8_get_int:
44- ld h,127
4542 cp 134
46- ret nc
47- sub h
43+ jr nc,f32toi8_return_inf
44+ sub 127
4845 ld h,0
4946 ret c
50- ld l,c ; upper 8 bits of the significand
51- ld b,a
5247
5348 ; if the exponent is 128, return inf
5449 jp p,f32toi8_extract
5550 ld a,l
5651 or e
5752 or d
5853 ret nz
54+ f32toi8_return_inf:
5955 ld h,127
56+ rl e
57+ ret nc
58+ inc h
6059 ret
60+
6161f32toi8_extract:
62+ ld l,c ; upper 8 bits of the significand, H is 0
63+ ld b,a
6264 inc b
6365 add hl,hl
6466 djnz $-1
67+ rl e
68+ ret nc
69+ ld a,l
70+ or d
71+ add a,255
72+ sbc a,a
73+ sub h
74+ ld h,a
6575 ret
6676#endif
You can’t perform that action at this time.
0 commit comments