Skip to content

Commit

Permalink
conversion routines between f32 to and from f16,f24, f64, and extended
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeda committed Apr 25, 2021
1 parent e8b07ae commit 3f591ab
Show file tree
Hide file tree
Showing 6 changed files with 535 additions and 2 deletions.
81 changes: 81 additions & 0 deletions conversion/f16tof32.z80
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#ifndef included_f16tof32
#define included_f16tof32
#include "pushpop.z80"

f16tof32:
;Inputs:
; HL is the f16 input
; BC is where to write the f32 float
;Destroys:
; none
call pushpop
;check for inf or NaN
ld a,h
and %01111100
jr z,f16tof32_zero
cp %01111100
jr z,f16tof32_inf_nan
;it is not a special value

; we need to shift in the sign and exponent from HL
; We need to make roomfor 3 more bits of exponent, and we need to subtract 15
; for the original bias, and add 127 for the new bias, for a net of +112
; We'll init A to this so that the RLA instructions will pre-add
ld a,%01100000
add hl,hl
rla ;A = 1100000s
rla ;A = 100000s0
rla ;A = 00000s00 + 1
rla ;A = 0000s000 + 3
add hl,hl
rla ;A = 000s000e + 6
inc a ;A = 000s000e + 7
add hl,hl
rla ;A = 00s000ee + 14
add hl,hl
rla ;A = 0s000eee + 28
add hl,hl
rla ;A = s000eeee + 56

ex de,hl
ld h,b
ld l,c
ld (hl),0
inc hl
ld (hl),e
inc hl
ld (hl),d
inc hl
ld (hl),a
ret


f16tof32_zero:
ld (bc),a
inc bc
ld (bc),a
inc bc
ld (bc),a
inc bc
ld a,h
and %10000000
ld (bc),a
ret

f16tof32_inf_NaN:
ld a,h
and %00000011
or l
ld (bc),a
inc bc
ld (bc),a
inc bc
or %10000000
ld (bc),a
inc bc
ld a,h
or %01111111
ld (bc),a
ret

#endif
76 changes: 76 additions & 0 deletions conversion/f32tof16.z80
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
14 changes: 12 additions & 2 deletions conversion/f32tof24.z80
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ f32tof24:
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:
Expand All @@ -56,9 +65,10 @@ f32tof24_return_inf:

f32tof24_return_0:
xor a
rl c
f32tof24_return_0_noA:
ld h,a ; Not necessary, but nice
ld l,a ;
ld h,a
ld l,a
rra ; shift the sign back in
ret
#endif
117 changes: 117 additions & 0 deletions conversion/f32tof64.z80
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#ifndef included_f32tof64
#define included_f32tof64
#include "pushpop.z80"

;bias is 1023, so 0x03FF is exponent of 0
;sign is 1 bit
;exponent is 11 bits
;mantissa is 53 bits (top bit is implicit)


f32tof64:
;convert an IEEE-754 binary32 to an IEEE-754 binary64 float.
;Input: HL points to the input float, BC points to where to output
;Destroys: None
call pushpop
xor a
ld (bc),a
inc bc
ld (bc),a
inc bc
ld (bc),a
inc bc

ld e,(hl)
inc hl
ld d,(hl)
inc hl
ld a,(hl)
inc hl
ld h,(hl)
ld l,a
ex de,hl
; The float is in DEHL
ld a,e
add a,a
ld a,d
adc a,a
jr z,f32tof64_zero
inc a
jr z,f32tof64_infnan

; to the exponent, substract 127 and add 1023 for a net of +896
ld a,%01100000
add hl,hl
rl e
rl d
rla ;1110000s + 0
rla ;110000s0 + 0
rla ;10000s00 + 1
rla ;0000s000 + 3
add hl,hl
rl e
rl d
rla ;000s000e + 6
inc a ;000s000e + 7
add hl,hl
rl e
rl d
rla ;00s000ee + 14
add hl,hl
rl e
rl d
rla ;0s000eee + 28
add hl,hl
rl e
rl d
rla ;s000eeee + 56


push hl
ld h,b
ld l,c
pop bc
ld (hl),c
inc hl
ld (hl),b
inc hl
ld (hl),e
inc hl
ld (hl),d
inc hl
ld (hl),a
ret

f32tof64_zero:
ld (bc),a
inc bc
ld (bc),a
inc bc
ld (bc),a
inc bc
ld (bc),a
inc bc
rra
ld (bc),a
ret

f32tof64_infnan:
xor a
ld (bc),a
inc bc
ld (bc),a
inc bc
ld a,e
add a,a
or h
or l
ld (bc),a
inc bc
or %11110000
ld (bc),a
inc bc
ld a,d
or %01111111
ld (bc),a
ret
#endif
Loading

1 comment on commit 3f591ab

@Zeda
Copy link
Owner

@Zeda Zeda commented on 3f591ab Apr 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addresses issue #29

Please sign in to comment.