Skip to content

Commit 0d18903

Browse files
committed
Optimized float classification routines and changed return type to bool
1 parent 2e57bc8 commit 0d18903

File tree

10 files changed

+74
-79
lines changed

10 files changed

+74
-79
lines changed

src/libc/include/__math_def.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ extern "C" {
4343
int _fpclassifyf(float n);
4444
int _fpclassifyl(long double n);
4545

46-
int _isinff(float n);
47-
int _isnanf(float n);
48-
int _isnormalf(float n);
49-
int _isfinitef(float n);
50-
int _iszerof(float n);
51-
int _issubnormalf(float n);
46+
bool _isinff(float n);
47+
bool _isnanf(float n);
48+
bool _isnormalf(float n);
49+
bool _isfinitef(float n);
50+
bool _iszerof(float n);
51+
bool _issubnormalf(float n);
5252

5353
int _isinfl(long double n);
5454
int _isnanl(long double n);
55-
int _isnormall(long double n);
56-
int _isfinitel(long double n);
55+
bool _isnormall(long double n);
56+
bool _isfinitel(long double n);
5757
int _iszerol(long double n);
5858
int _issubnormall(long double n);
5959

src/libc/isfinitef.src

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
assume adl=1
22

33
section .text
4+
45
public __isfinitef
6+
7+
; bool _isfinitef(float)
58
__isfinitef:
6-
pop bc
7-
pop hl
8-
pop de
9-
push de
10-
push hl
11-
push bc
12-
add hl,hl
13-
ld a,e
14-
rla
15-
add a,1
16-
sbc hl,hl
17-
inc hl
9+
ld hl, 5
10+
add hl, sp
11+
ld hl, (hl)
12+
add hl, hl
13+
ld a, h
14+
sub a, $FF ; NaN/inf exponent won't underflow
15+
sbc a, a
1816
ret
1917

src/libc/isfinitel.src

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
public __isfinitel
66

7-
; int _isfinitel(long double)
7+
; bool _isfinitel(long double)
88
__isfinitel:
99
ld hl, 8
1010
add hl, sp
1111
ld hl, (hl) ; load the upper 24bits
1212
add hl, hl ; clears signbit
1313
ld de, $2000 ; (1 << (5 + 8))
1414
add hl, de ; attempts to overflow the exponent
15-
sbc hl, hl
16-
inc hl
15+
sbc a, a
16+
inc a
1717
ret

src/libc/isinff.src

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
assume adl=1
22

33
section .text
4+
45
public __isinff
6+
7+
; bool _isinff(float)
58
__isinff:
6-
ld iy,0
7-
add iy,sp
8-
ld hl,(iy+3)
9-
adc hl,hl
10-
jr nz,.finite
11-
ld a,(iy+6)
9+
xor a, a
10+
ld iy, 0
11+
add iy, sp
12+
ld hl, (iy+3)
13+
adc hl, hl
14+
ret nz ; finite or NaN
15+
; HL is zero
16+
ld a, (iy+6)
1217
rla
13-
inc a
14-
inc hl
15-
ret z
16-
.finite:
17-
or a,a
18-
sbc hl,hl
18+
add a, 1 ; attempt to overflow the exponent
19+
sbc a, a
1920
ret

src/libc/isnanf.src

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
assume adl=1
22

33
section .text
4-
4+
55
public __isnanf
6-
6+
7+
; bool _isnanf(float)
78
__isnanf:
8-
ld iy,0
9-
add iy,sp
10-
ld hl,(iy+3)
11-
adc hl,hl
12-
jr z,.l
13-
ld hl,(iy+5)
14-
add hl,hl
15-
.l:
16-
inc h
17-
ld hl,0
18-
ret nz
19-
inc hl
9+
xor a, a
10+
ld iy, 0
11+
add iy, sp
12+
ld hl, (iy+3)
13+
adc hl, hl
14+
ret z ; infinity
15+
ld a, (iy+6)
16+
rla
17+
add a, 1 ; attempt to overflow the exponent
18+
sbc a, a
2019
ret

src/libc/isnormalf.src

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
public __isnormalf
66

7-
; int _isnormalf(float)
7+
; bool _isnormalf(float)
88
__isnormalf:
99
ld hl, 5
1010
add hl, sp
@@ -13,6 +13,5 @@ __isnormalf:
1313
ld a, h
1414
dec a
1515
cp a, $FE ; tests for zero/denormal/inf/NaN exponents
16-
sbc hl, hl ; -1 if normal, 0 otherwise
16+
sbc a, a ; -1 if normal, 0 otherwise
1717
ret
18-

src/libc/isnormall.src

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
public __isnormall
66

7-
; int _isnormall(long double)
7+
; bool _isnormall(long double)
88
__isnormall:
99
; load upper 24bits
1010
ld hl, 8
@@ -24,6 +24,6 @@ __isnormall:
2424

2525
.exp_all_ones:
2626
.exp_all_zero:
27-
sbc hl, hl
28-
inc hl
27+
sbc a, a
28+
inc a
2929
ret

src/libc/issubnormalf.src

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44

55
public __issubnormalf
66

7-
; int _issubnormalf(float)
7+
; bool _issubnormalf(float)
88
__issubnormalf:
9-
pop bc, hl, de
10-
push de, hl, bc
11-
or a, a
9+
xor a, a
10+
pop bc, hl
1211
adc hl, hl
13-
ret z ; zero mantissa is not subnormal
14-
ld a, e
15-
adc a, a
16-
ret z ; zero exponent is subnormal
17-
or a, a
18-
sbc hl, hl
12+
ex (sp), hl
13+
push hl, bc
14+
ret z ; zero mantissa is not subnormal
15+
adc hl, hl
16+
sub a, l
17+
sbc a, a
18+
inc a
1919
ret

src/libc/iszerof.src

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
assume adl=1
22

33
section .text
4-
4+
55
public __iszerof
66

7-
; int iszerof(float)
7+
; bool _iszerof(float)
88
__iszerof:
9-
pop bc, hl, de
10-
push de, hl, bc
11-
or a, a
9+
xor a, a
10+
pop bc, hl
1211
adc hl, hl
13-
jr nz, .not_zero_f32
14-
rl e
15-
jr nz, .not_zero_f32
16-
inc hl
17-
ret
18-
.not_zero_f32:
19-
or a, a
20-
sbc hl, hl
12+
ex (sp), hl
13+
push hl, bc
14+
ret nz ; non-zero mantissa
15+
ld a, l
16+
adc a, a
17+
sub a, 1
18+
sbc a, a
2119
ret

test/floating_point/float32_classification/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ int main(void) {
136136
printf("All tests passed");
137137
} else {
138138
printf(
139-
"Failed test:\n0x%08lX\nTruth: %X_%d\nGuess: %X_%d",
139+
"Failed test:\n0x%08lX\nTruth: %06X_%d\nGuess: %06X_%d",
140140
ret.failed_index, ret.guess, ret.guess_fp, ret.truth, ret.truth_fp
141141
);
142142
}

0 commit comments

Comments
 (0)