-
Notifications
You must be signed in to change notification settings - Fork 10
/
f32tof16.z80
76 lines (70 loc) · 1.21 KB
/
f32tof16.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
#ifndef included_f32tof16
#define included_f32tof16
f32tof16:
;convert an IEEE-754 binary32 to an IEEE-754 binary16 format.
;Input: HL points to the input float
;Output: HL is the f16 float
;Destroys: AF, BC, DE
ld c,(hl)
inc hl
ld e,(hl)
inc hl
ld a,(hl)
ld d,a
add a,a
inc hl
ld a,(hl)
adc a,a
jr z,f32tof16_return_0_noA
inc a
jr z,f32tof16_return_infnan
rr c ; save the sign, we no longer need C
sub 113
jr c,f32tof16_return_0
cp 31
jr nc,f32tof16_return_inf
;A is the exponent
;(DE>>5)&%0000001111111111 encodes the significand
ex de,hl
add hl,hl
; rla ; we want to omit the top bit of the significand
add hl,hl
rla
add hl,hl
rla
; now get the sign
add a,a
rl c
rra
; we'll round
sla l
ld l,h
ld h,a
ret nc
inc hl
ret
f32tof16_return_infnan:
ld a,%11111000
rra
ld h,a ; sign and exponent set
ld a,d
add a,a
or e
or c
ld l,a ; if the input was NaN, this will be non-zero, else zero
ret
f32tof16_return_inf:
ld a,c
or %01111100
ld l,0
ld h,a
ret
f32tof16_return_0:
xor a
rl c
f32tof16_return_0_noA:
; ld l,a ; Not necessary, just need the exponent = 0
rra ; shift the sign back in
ld h,a
ret
#endif