Skip to content

Commit

Permalink
added integer ==> f32 conversion routines, issue #29
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeda committed Apr 22, 2021
1 parent 3e6f391 commit 5a973d9
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 0 deletions.
55 changes: 55 additions & 0 deletions conversion/i16tof32.z80
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#ifndef included_i16tof32
#define included_i16tof32

i16tof32:
;Inputs:
; HL holds a 16-bit signed integer, (-32768 to 32767)
; BC points to where to write the float
;Outputs:
; Converts A to an f32 float at BC
;
call pushpop

xor a
ld (bc),a
inc bc

ld a,l
or h
ld d,a
jr z,i16tof32_finish

ld a,h
or a
jp p,$+10
xor a
sub l
ld l,a
sbc a,a
sub h
ld h,a
scf

rla ; save the sign
ld d,$7F+16 ;Initial exponent

dec d
add hl,hl
jr nc,$-2

rra ; shift out the sign
rr d ; shift the exponent down, shifting in the sign
rr h ; shift the lsb of the exponent into the significand
rr l

ld a,l
i16tof32_finish:
ld (bc),a
inc bc
ld a,h
ld (bc),a
inc bc
ld a,d
ld (bc),a
ret
#endif
64 changes: 64 additions & 0 deletions conversion/i8tof32.z80
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#ifndef included_i8tof32
#define included_i8tof32

i8tof32:
;Inputs:
; A holds a 8-bit signed integer, (-128 to 127)
; BC points to where to write the float
;Outputs:
; Converts A to an f32 float at BC
;
push hl
push af

or a
jp p,$+6
neg
scf

; #ifndef included_u8tof32
; ##define included_u8tof32
; .db $21 ; start of `ld hl,*` to skip the next byte
; u8tof32:
; push hl
; push af
; #else
; .echo "Hint: it looks like you are using u8tof32 as well as i8tof32. i8tof32 already has code for u8tof32, so you can save bytes by including it first."
; #endif
;
; Begin writing the float
ld h,b
ld l,c
ld (hl),0
inc hl
ld (hl),0
inc hl

jr nz,$+8
ld (hl),a
inc hl
ld (hl),a
pop af
pop hl
ret

push bc
rl c ; save the sign
ld b,$7F+8 ;Initial exponent

dec b
add a,a
jr nc,$-2

rr c ; shift in a 1 and shift out the sign
rr b ; shift the exponent down, shifting in the sign
rra ; shift the lsb of the exponent into the significand
ld (hl),a
inc hl
ld (hl),b

pop bc
pop af
pop hl
ret
#endif
42 changes: 42 additions & 0 deletions conversion/u16tof32.z80
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef included_u16tof32
#define included_u16tof32

u16tof32:
;Inputs:
; HL holds a 16-bit unsigned integer, (0 to 65535)
; BC points to where to write the float
;Outputs:
; Converts HL (unsigned) to an f32 float at BC
;
call pushpop

xor a
ld (bc),a
inc bc

ld a,l
or h
ld d,a
jr z,u16tof32_finish

ld d,$7F+16 ;Initial exponent

dec d
add hl,hl
jr nc,$-2

srl d ; shift the exponent down, shifting in the sign
rr h ; shift the lsb of the exponent into the significand
rr l

ld a,l
u16tof32_finish:
ld (bc),a
inc bc
ld a,h
ld (bc),a
inc bc
ld a,d
ld (bc),a
ret
#endif
50 changes: 50 additions & 0 deletions conversion/u8tof32.z80
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#ifndef included_u8tof32
#define included_u8tof32

u8tof32:
;Inputs:
; A holds a 8-bit unsigned integer, (0 to 255)
; BC points to where to write the float
;Outputs:
; Converts A to an f32 float at BC
;
push hl
push af

; Begin writing the float
ld h,b
ld l,c
ld (hl),0
inc hl
ld (hl),0
inc hl

or a
jr nz,$+8
ld (hl),a
inc hl
ld (hl),a
pop af
pop hl
ret

push bc
rl c ; save the sign
ld b,$7F+8 ;Initial exponent

dec b
add a,a
jr nc,$-2

rr c ; shift in a 1 and shift out the sign
rr b ; shift the exponent down, shifting in the sign
rra ; shift the lsb of the exponent into the significand
ld (hl),a
inc hl
ld (hl),b

pop bc
pop af
pop hl
ret
#endif

0 comments on commit 5a973d9

Please sign in to comment.