-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathf24div.z80
210 lines (188 loc) · 2.69 KB
/
f24div.z80
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#ifndef included_f24div
#define included_f24div
#define included_f24inv
f24inv:
ld c,a
ex de,hl
ld a,$3F
ld hl,0
f24div:
;AHL * CDE ==> AHL
;Destroys BC,DE
;
;put the output sign in B
ld b,a
xor c
add a,a
ld a,b
rla
rrca
ld b,a
;check for special values
;NaN/x ==> NaN
;0/fin ==> 0
; 0/0 ==> NaN
;inf/inf ==> NaN
;inf/x ==> inf
;x/NaN ==> NaN
;x/inf ==> 0
;x/0 ==> NaN
and $7F
jp z,f24div_0_x
inc a
jp m,f24div_infnan_x
ld a,c
and $7F
jr nz,+_
dec a
ld h,a
ld l,a
ret
_:
inc a
jp m,f24div_x_infnan
;upper bit of B is the output sign
;first approximation of the exponent is
; (B&7F) - (C&7F) + 63
res 7,c
ld a,b
and $7F
add a,63
sub c
jr nc,$+4
xor a ;underflowed, so return 0
ret
cp $7F
jr c,+_
f24div_return_inf:
ld a,b
or %01111111
ld hl,0 ;overflow so return inf
ret
_:
;now compute (1.HL / 1.DE)
; = (1+.HL)/(1+.DE)
; want 1.HL>1.DE, because then result is going to be 1.x
;so we can end up doing (.HL-.DE)/(1.DE) to 16 bits precision
or a
ld c,0 ;top bit of 1.HL-1.DE
sbc hl,de
jr nc,f24div_ready
;if carry is set, then DE was the larger of the two
;so we need to decrement the exponent and do
;(HL+DE)*2-DE
dec a ;decrement exponent
ret z ;return 0 if underflowed
add hl,de
add hl,hl
rl c
inc c
sbc hl,de
jr nc,f24div_ready
dec c
f24div_ready:
;C.HL is the numerator, 1.DE is the denominator
;A is the exponent, B[7] is the sign
;save the exponent and sign
push bc
push af
;we can now commence 16 iterations of this division
call fdiv24_div16
pop de
pop bc
adc a,d
jp p,+_
f24div_return_NaN:
dec a
ld h,a
ld l,a
_:
xor b
and $7F
xor b
ret
fdiv24_div16:
;negate the divisior for more efficient division
;(16-bit addition is cheaper than subtraction)
xor a
sub e
ld e,a
ld a,0
sbc a,d
ld d,a
sbc a,a
dec a
ld b,a
ld a,c
call fdiv24_div8
rl c
push bc
call fdiv24_div8
rl c
;check if 2*A.HL>1.DE
add hl,hl
adc a,a
add hl,de
adc a,b
pop hl
ld h,l
ld l,c
ld bc,0
ld a,b
adc hl,bc
ret
fdiv24_div8:
call fdiv24_div4
fdiv24_div4:
call fdiv24_div2
fdiv24_div2:
call fdiv24_div1
fdiv24_div1:
rl c
add hl,hl
adc a,a
ret z
add hl,de
adc a,b
ret c
sbc hl,de
sbc a,b
ret
f24div_0_x:
;make sure we aren't trying 0/NaN or 0/0
ld a,c
and $7F
jr z,f24div_return_NaN
inc a
jp m,+_
xor a
ret
_:
ld a,d
or e
ret z
ld a,c
ex de,hl
ret
f24div_x_infnan:
ld a,d
or e
ret z
ld a,c
_:
ex de,hl
ret
f24div_infnan_x:
ld a,h
or l
ld a,b
ret nz
;make sure x is not inf NaN or 0
ld a,c
and $7F
jr z,f24div_return_NaN
inc a
jp m,f24div_return_NaN
ld a,b
ret
#endif