-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathf32mul.z80
235 lines (218 loc) · 2.73 KB
/
f32mul.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#ifndef included_f32mul
#define included_f32mul
#include "pushpop.z80"
#include "mul24_stack_based.z80"
f32mul:
;x * y ==> z
;
call pushpop
push bc
push de
ld e,(hl)
inc hl
ld d,(hl)
inc hl
ld b,(hl)
ld a,b
add a,a
inc hl
ld a,(hl)
ld c,a
adc a,a
pop hl
jp z,f32mul_0_op2
inc a
jp z,f32mul_infnan_op2
dec a
push af ;exponent of the first operand
push de
ld e,(hl)
inc hl
ld d,(hl)
inc hl
ld c,(hl)
ld a,c
add a,a
inc hl
ld a,(hl)
adc a,a
jr z,f32mul_op1_0
inc a
jr z,f32mul_op1_infnan
pop hl
ex de,hl
push af ;exponent of the second operand
;BDE*CHL
set 7,b
set 7,c
call mul24
;BHLDEA
pop af
rr e
bit 7,b
jr nz,+_
dec a
sla d
adc hl,hl
rl b
_:
sla d
ex de,hl
jr nc,+_
inc e
jr nz,+_
inc d
jr nz,+_
inc b
jr nz,+_
inc a
_:
ld h,a ; first exponent
pop af ; A is the second exponent
rr l ; top 2 bits are the signs
; ultimately need H+A-0x7F
add a,h
ld h,0
rl h
sub $7E
dec a
jr nc,$+3
dec h
; If H is 1, we have an inf, -1 we have 0
dec h
jp z,f32mul_inf_l
inc h
jr z,$+3
xor a
sla b
ld c,a ;the exponent
ld a,l
and %11000000
jp pe,$+4
scf
f32mul_return2:
rr c
rr b
f32mul_return:
pop hl
ld (hl),e
inc hl
ld (hl),d
inc hl
ld (hl),b
inc hl
ld (hl),c
ret
f32mul_op1_0:
pop bc
rra
ld c,a
pop af
rra
xor c
and %10000000
ld c,a
ld b,0
jr f32mul_return
f32mul_op1_infnan:
pop hl
rra
ld b,a
pop af
rra
xor b ;top bit is sign
ld h,a
ld a,c
add a,a
or d
or e
ld b,a
add hl,hl
ld c,-1
jr f32mul_return2
f32mul_0_op2:
;if OP2 is inf or NaN, retun NaN, else return 0
rra
ld b,a ;save the sign
ld e,(hl)
inc hl
ld d,(hl)
inc hl
ld a,(hl)
add a,a
ld c,a
inc hl
ld a,(hl)
ld h,a
adc a,a
inc a
jr z,f32mul_return_NaN
rra
xor b
and %10000000
ld c,a
ld b,0
jr f32mul_return
f32mul_return_NaN:
ld bc,$FF7F
jr f32mul_return
f32mul_infnan_op2:
;if OP2 is 0 or NaN, return NaN, else return OP1 with adjusted sign
rra
ld c,a ;save the sign
ld a,b
add a,a
or d
or e
jr nz,f32mul_return_NaN
f32mul_inf_op2:
inc hl
inc hl
ld a,(hl)
inc hl
add a,a
ld a,(hl)
adc a,a
jr z,f32mul_return_NaN
inc a
jr z,f32mul_check_NaN
rra
xor c
and %10000000
or %01111111
ld c,a
jr f32mul_return
f32mul_check_NaN:
rra
xor c
and %10000000
or %01111111
ld c,a
dec hl
ld a,(hl)
add a,a
dec hl
or (hl)
dec hl
or (hl)
jr nz,f32mul_return_NaN
jr f32mul_return
f32mul_inf_l:
ld a,l
and %11000000
jp pe,$+4
scf
f32mul_inf:
;carry is sign
pop hl
ld (hl),0
inc hl
ld (hl),0
inc hl
ld (hl),%10000000
inc hl
ld a,-1
rra
ld (hl),a
ret
#endif