-
Notifications
You must be signed in to change notification settings - Fork 11
/
f32tou16.z80
69 lines (61 loc) · 956 Bytes
/
f32tou16.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
#ifndef included_f32tou16
#define included_f32tou16
f32tou16:
;Inputs: HL points to an f32 float
;Outputs: HL is the unsigned 16-bit integer part of the input (rounded down)
;Special cases:
; NaN ==> 0
; greater than 65535 ==> 65535
; less than 0 ==> 0
push de
push bc
push af
ld c,(hl)
inc hl
ld e,(hl)
inc hl
ld a,(hl)
rlca
scf
rra
ld d,a
inc hl
ld a,(hl)
adc a,a
ccf
jr nc,f32tou16_return_carry
or a
jr z,f32tou16_return_carry
inc a
jr z,f32tou16_infnan
add a,256-143
jr c,f32tou16_return_carry
add a,15
jr nc,f32tou16_return_carry
ld b,a
inc b
ex de,hl
xor a
ld e,a
add hl,hl
rl e
rla
djnz $-4
ld l,e
ld h,a
.db $01 ; start of `ld bc,**` to skip the next two bytes
f32tou16_return_carry:
sbc hl,hl
f32tou16_return:
pop af
pop bc
pop de
ret
f32tou16_infnan:
ld a,d
add a,a
or e
or c
sub 1
jr f32tou16_return_carry
#endif