Skip to content

Commit 9fa78e8

Browse files
committed
Optimized frexpf slightly
1 parent eb5b446 commit 9fa78e8

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

src/libc/frexpf.src

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ else
1515
_frexp:
1616
_frexpf:
1717
ld iy, 0
18+
lea de, iy - 127 ; bias ; $FFFF81
1819
add iy, sp
1920
ld hl, (iy + 3)
2021
add hl, hl
@@ -29,7 +30,7 @@ _frexpf:
2930
rr b
3031
sbc hl, hl
3132
ld l, a
32-
ld de, -127 ; bias
33+
; ld de, -127 ; bias
3334
add hl, de
3435
.ret_subnormal:
3536
res 7, (iy + 5)
@@ -47,22 +48,23 @@ _frexpf:
4748
jr .ret_self
4849

4950
.maybe_subnormal:
50-
add hl, bc
51-
or a, a
52-
sbc hl, bc
53-
jr z, .ret_zero
51+
ld e, d ; ld de, -1
52+
add hl, de
53+
inc hl ; restore HL
54+
jr nc, .ret_zero
5455
; input: HL output: A
5556
call __ictlz
5657
ld c, a
5758
call __ishl
5859
ld (iy + 3), hl
59-
scf
60-
sbc hl, hl
60+
sub a, 131 ; (127 + 3) + 1? idk where this magic number comes from
6161
cpl
62-
add a, 131 ; 127 + 3 + 1? idk where this magic number comes from
62+
ex de, hl ; DE was -1 from before
63+
; sbc hl, hl ; Carry is set here
6364
ld l, a
64-
ld a, b ; exponent
65-
xor a, $3F
65+
; copy exponent of 0.5f
66+
ld a, b
67+
xor a, $3F
6668
ld b, a
6769
jr .ret_subnormal
6870

test/floating_point/float32_frexp/src/main.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ size_t run_test(void) {
3131
result.flt = frexpf(input[i], &expon);
3232
if (result.bin != output[i].frac.bin || expon != output[i].expon) {
3333
if (!(isnan(result.flt) && isnan(output[i].frac.flt))) {
34-
// printf("G: %08lX %d\n", result.bin, expon);
35-
// printf("T: %08lX %d\n", output[i].frac.bin, output[i].expon);
34+
#if 1
35+
printf("G: %08lX %d\n", result.bin, expon);
36+
printf("T: %08lX %d\n", output[i].frac.bin, output[i].expon);
37+
#endif
3638
return i;
3739
}
3840
}

0 commit comments

Comments
 (0)