-
Notifications
You must be signed in to change notification settings - Fork 10
/
f64tox.z80
107 lines (95 loc) · 1.67 KB
/
f64tox.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
#ifndef included_f64tox
#define included_f64tox
#include "routines/rl64.z80"
#include "mov.z80"
#include "pushpop.z80"
f64tox:
;Inputs:
; HL points to the input double-precision float
; BC points to where to output the result
;Outputs:
; The double-precision float (binary64) is converted to an extended-precision
; float at BC.
;
call pushpop
f64tox_nopush:
ld d,b
ld e,c
;bottom 8 bits are 0
xor a
ld (de),a
inc de
;load the next 7 bytes
call mov7
;load the upper 8 bits of the sign/exponent into c
ld c,(hl)
;now shift the mantissa up 4 times
;lower 4 bits of BC contain the mantissa, upper 12 are the exponent and sign
ld hl,-7
ex de,hl
add hl,de
inc e
rlc c ;\
srl c ; |
sbc a,a ; | Get the sign where it needs to be
and 8 ; | also resets carry, helpfully
ld b,a ;/
call rl56 \ rl c \ rl b
add hl,de \ or a \ call rl56 \ rl c \ rl b
add hl,de \ or a \ call rl56 \ rl c \ rl b
;now the top bit of the mantissa contains the bottom bit of the exponent.
;we also need to replace that top bit with a 1
ld a,(hl)
rlca
scf
rra
ld (hl),a
rl c
rl b
; BC is the input exponent, check if it is special
ld a,b
add a,a
or c
jr z,f64tox_zero
ld a,c
inc a
jr nz,+_
ld a,b
inc a
and 7
jr z,f64tox_infnan
;now we need to subtract 1023 and add 16384
ld a,(16384-1023)&255
add a,c
inc hl
ld (hl),a
ld a,(16384-1023)>>8
adc a,b
inc hl
ld (hl),a
ret
f64tox_zero:
xor a
ld (hl),a
inc hl
ld (hl),a
inc hl
ld (hl),b
ret
f64tox_infnan:
or (hl)
ld a,$80
jr nz,+_
scf
_:
rra
ld (hl),a
xor a
inc hl
ld (hl),a
inc hl
rl b
rra
ld (hl),a
ret
#endif