-
Notifications
You must be signed in to change notification settings - Fork 11
/
f32tof24.z80
74 lines (68 loc) · 967 Bytes
/
f32tof24.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
#ifndef included_f32tof24
#define included_f32tof24
f32tof24:
;convert an IEEE-754 binary32 to this library's "f24" format.
;Input: HL points to the input float
;Output: AHL is the f24 float
;Destroys: DE,BC
ld a,(hl)
ld c,a
add a,a
inc hl
ld a,(hl)
rla
ld e,a
inc hl
ld a,(hl)
rla
ld d,a
; DE is the significand of the output
inc hl
ld a,(hl)
adc a,a
jr z,f32tof24_return_0_noA
inc a
jr z,f32tof24_return_infnan
rr c ; save the sign
sub 65
jr c,f32tof24_return_0
cp 127
jr nc,f32tof24_return_inf
add a,a
rl c
rra
ex de,hl
; round
bit 6,c
ret z
inc l
ret z
inc h
ret z
inc a
ret
f32tof24_return_infnan:
dec a
rra
ld b,a ; save exponent/sign
ld a,c
or d
or e
ld d,a
ld a,b
ex de,hl
ret
f32tof24_return_inf:
ld a,c
or %01111111
ld hl,0
ret
f32tof24_return_0:
xor a
rl c
f32tof24_return_0_noA:
ld h,a
ld l,a
rra ; shift the sign back in
ret
#endif