Skip to content

Commit d8dabdc

Browse files
committed
Optimized the remaining long double classification routines to return bool
1 parent 691f87e commit d8dabdc

File tree

8 files changed

+51
-71
lines changed

8 files changed

+51
-71
lines changed

src/libc/include/__math_def.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ bool _isfinitef(float) __NOEXCEPT_CONST;
5151
bool _iszerof(float) __NOEXCEPT_CONST;
5252
bool _issubnormalf(float) __NOEXCEPT_CONST;
5353

54-
int _isinfl(long double) __NOEXCEPT_CONST;
55-
int _isnanl(long double) __NOEXCEPT_CONST;
54+
bool _isinfl(long double) __NOEXCEPT_CONST;
55+
bool _isnanl(long double) __NOEXCEPT_CONST;
5656
bool _isnormall(long double) __NOEXCEPT_CONST;
5757
bool _isfinitel(long double) __NOEXCEPT_CONST;
58-
int _iszerol(long double) __NOEXCEPT_CONST;
59-
int _issubnormall(long double) __NOEXCEPT_CONST;
58+
bool _iszerol(long double) __NOEXCEPT_CONST;
59+
bool _issubnormall(long double) __NOEXCEPT_CONST;
6060

6161
#if 0
6262
/* disabled until builtin is optimized */

src/libc/isinfl.src

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,17 @@
77
; int _isinfl(long double)
88
__isinfl:
99
pop bc, hl, de
10-
or a, a
10+
xor a, a
1111
adc hl, de
1212
pop de
1313
push bc, bc, bc, bc
14-
jr nz, .mant_nonzero
15-
jr c, .mant_carry
14+
ret nz ; mant_nonzero
15+
ret c ; mant_carry
1616
ld a, e
17-
xor a, $F0
18-
ret nz ; mantissa is non-zero
19-
; A is zero here
17+
xor a, $F0 ; sets A to zero if the bit pattern matches infinity
2018
set 7, d
21-
inc d
22-
ret nz
23-
inc hl
24-
ret
25-
26-
.mant_nonzero:
27-
.mant_carry:
28-
or a, a
29-
sbc hl, hl
19+
inc d ; set d to zero if it matches infinity
20+
or a, d
21+
sub a, 1
22+
sbc a, a
3023
ret

src/libc/isnanl.src

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,15 @@ __isnanl:
1515
jr nz, .mant_nonzero ; normal, subnormal, or NaN
1616
jr c, .mant_nonzero ; normal, subnormal, or NaN
1717
; common NaN, inf, normal, or subnormal
18-
add a, 15 ; overflows 0xF1
19-
jr c, .skip_exp_check
20-
ret
21-
.mant_nonzero:
2218
or a, a
23-
sbc hl, hl
19+
ret z
20+
dec a
21+
.mant_nonzero:
2422
add a, 16 ; overflows 0xF0
25-
ret nc
26-
.skip_exp_check:
27-
; carry is set here
23+
sbc a, a
24+
ret z
2825
ld a, d
29-
rla ; A should be all ones if D was all ones
30-
inc a
31-
ret nz
32-
inc hl
26+
add a, a
27+
add a, 2
28+
sbc a, a
3329
ret

src/libc/issubnormall.src

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ __issubnormall:
2121
.mant_carry:
2222
ld a, e
2323
and a, $F0
24-
sbc hl, hl
25-
rra
2624
res 7, d
27-
add a, d
28-
ret nz
29-
inc hl
25+
or a, d
26+
sub a, 1
27+
sbc a, a
3028
ret

src/libc/iszerol.src

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,16 @@
77
; int iszerol(long double)
88
__iszerol:
99
pop bc, hl, de
10-
or a, a
10+
xor a, a
1111
adc hl, de
1212
pop de
1313
push bc, bc, bc, bc
1414
; return if mantissa is non-zero
15-
jr nz, .mant_nonzero
16-
jr c, .mant_carry
15+
ret nz ; mant_nonzero
16+
ret c ; mant_carry
1717
ld a, d
1818
rla ; clear the signbit
1919
or a, e
20-
inc hl
21-
ret z
22-
; upper 16 bits non-zero
23-
; dec hl
24-
; ret
25-
.mant_nonzero:
26-
.mant_carry:
27-
or a, a
28-
sbc hl, hl
20+
sub a, 1
21+
sbc a, a
2922
ret

src/libcxx/include/cmath

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ inline constexpr bool signbit(float __x) {
1616
if (__builtin_constant_p(__x)) {
1717
return (__builtin_copysign(1.0f, __x) < 0.0f);
1818
}
19-
return static_cast<bool>(_signbitf(__x));
19+
return _signbitf(__x);
2020
}
2121
inline constexpr bool signbit(double __x) {
2222
if (__builtin_constant_p(__x)) {
2323
return (__builtin_copysign(1.0, __x) < 0.0);
2424
}
25-
return static_cast<bool>(_signbitf(__x));
25+
return _signbitf(__x);
2626
}
2727
inline constexpr bool signbit(long double __x) {
2828
if (__builtin_constant_p(__x)) {
2929
return (__builtin_copysign(1.0L, __x) < 0.0L);
3030
}
31-
return static_cast<bool>(_signbitl(__x));
31+
return _signbitl(__x);
3232
}
3333
template<typename _Tp> inline constexpr
3434
__cmath_enable_if_t<__cmath_is_integral_v<_Tp>, bool>
@@ -38,19 +38,19 @@ inline constexpr bool isinf(float __x) {
3838
if (__builtin_constant_p(__x)) {
3939
return __builtin_isinf(__x);
4040
}
41-
return static_cast<bool>(_isinff(__x));
41+
return _isinff(__x);
4242
}
4343
inline constexpr bool isinf(double __x) {
4444
if (__builtin_constant_p(__x)) {
4545
return __builtin_isinf(__x);
4646
}
47-
return static_cast<bool>(_isinff(__x));
47+
return _isinff(__x);
4848
}
4949
inline constexpr bool isinf(long double __x) {
5050
if (__builtin_constant_p(__x)) {
5151
return __builtin_isinf(__x);
5252
}
53-
return static_cast<bool>(_isinfl(__x));
53+
return _isinfl(__x);
5454
}
5555
template<typename _Tp> inline constexpr
5656
__cmath_enable_if_t<__cmath_is_integral_v<_Tp>, bool>
@@ -60,19 +60,19 @@ inline constexpr bool isnan(float __x) {
6060
if (__builtin_constant_p(__x)) {
6161
return __builtin_isnan(__x);
6262
}
63-
return static_cast<bool>(_isnanf(__x));
63+
return _isnanf(__x);
6464
}
6565
inline constexpr bool isnan(double __x) {
6666
if (__builtin_constant_p(__x)) {
6767
return __builtin_isnan(__x);
6868
}
69-
return static_cast<bool>(_isnanf(__x));
69+
return _isnanf(__x);
7070
}
7171
inline constexpr bool isnan(long double __x) {
7272
if (__builtin_constant_p(__x)) {
7373
return __builtin_isnan(__x);
7474
}
75-
return static_cast<bool>(_isnanl(__x));
75+
return _isnanl(__x);
7676
}
7777
template<typename _Tp> inline constexpr
7878
__cmath_enable_if_t<__cmath_is_integral_v<_Tp>, bool>
@@ -82,19 +82,19 @@ inline constexpr bool isnormal(float __x) {
8282
if (__builtin_constant_p(__x)) {
8383
return __builtin_isnormal(__x);
8484
}
85-
return static_cast<bool>(_isnormalf(__x));
85+
return _isnormalf(__x);
8686
}
8787
inline constexpr bool isnormal(double __x) {
8888
if (__builtin_constant_p(__x)) {
8989
return __builtin_isnormal(__x);
9090
}
91-
return static_cast<bool>(_isnormalf(__x));
91+
return _isnormalf(__x);
9292
}
9393
inline constexpr bool isnormal(long double __x) {
9494
if (__builtin_constant_p(__x)) {
9595
return __builtin_isnormal(__x);
9696
}
97-
return static_cast<bool>(_isnormall(__x));
97+
return _isnormall(__x);
9898
}
9999
template<typename _Tp> inline constexpr
100100
__cmath_enable_if_t<__cmath_is_integral_v<_Tp>, bool>
@@ -104,19 +104,19 @@ inline constexpr bool isfinite(float __x) {
104104
if (__builtin_constant_p(__x)) {
105105
return __builtin_isfinite(__x);
106106
}
107-
return static_cast<bool>(_isfinitef(__x));
107+
return _isfinitef(__x);
108108
}
109109
inline constexpr bool isfinite(double __x) {
110110
if (__builtin_constant_p(__x)) {
111111
return __builtin_isfinite(__x);
112112
}
113-
return static_cast<bool>(_isfinitef(__x));
113+
return _isfinitef(__x);
114114
}
115115
inline constexpr bool isfinite(long double __x) {
116116
if (__builtin_constant_p(__x)) {
117117
return __builtin_isfinite(__x);
118118
}
119-
return static_cast<bool>(_isfinitel(__x));
119+
return _isfinitel(__x);
120120
}
121121
template<typename _Tp> inline constexpr
122122
__cmath_enable_if_t<__cmath_is_integral_v<_Tp>, bool>
@@ -126,19 +126,19 @@ inline constexpr bool iszero(float __x) {
126126
if (__builtin_constant_p(__x)) {
127127
return (__x == 0.0f);
128128
}
129-
return static_cast<bool>(_iszerof(__x));
129+
return _iszerof(__x);
130130
}
131131
inline constexpr bool iszero(double __x) {
132132
if (__builtin_constant_p(__x)) {
133133
return (__x == 0.0);
134134
}
135-
return static_cast<bool>(_iszerof(__x));
135+
return _iszerof(__x);
136136
}
137137
inline constexpr bool iszero(long double __x) {
138138
if (__builtin_constant_p(__x)) {
139139
return (__x == 0.0L);
140140
}
141-
return static_cast<bool>(_iszerol(__x));
141+
return _iszerol(__x);
142142
}
143143
template<typename _Tp> inline constexpr
144144
__cmath_enable_if_t<__cmath_is_integral_v<_Tp>, bool>
@@ -148,19 +148,19 @@ inline constexpr bool issubnormal(float __x) {
148148
if (__builtin_constant_p(__x)) {
149149
return (FP_SUBNORMAL == __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, __x));
150150
}
151-
return static_cast<bool>(_issubnormalf(__x));
151+
return _issubnormalf(__x);
152152
}
153153
inline constexpr bool issubnormal(double __x) {
154154
if (__builtin_constant_p(__x)) {
155155
return (FP_SUBNORMAL == __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, __x));
156156
}
157-
return static_cast<bool>(_issubnormalf(__x));
157+
return _issubnormalf(__x);
158158
}
159159
inline constexpr bool issubnormal(long double __x) {
160160
if (__builtin_constant_p(__x)) {
161161
return (FP_SUBNORMAL == __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, __x));
162162
}
163-
return static_cast<bool>(_issubnormall(__x));
163+
return _issubnormall(__x);
164164
}
165165
template<typename _Tp> inline constexpr
166166
__cmath_enable_if_t<__cmath_is_integral_v<_Tp>, bool>

test/floating_point/float32_classification/src/main.c

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

test/floating_point/float64_classification/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ int main(void) {
139139
const uint32_t* failed_index_split = (const uint32_t*)((const void*)&(ret.failed_index));
140140
printf(
141141
"Failed test:\n0x%08lX%08lX\nTruth: %06X_%d\nGuess: %06X_%d",
142-
failed_index_split[1], failed_index_split[0], ret.guess, ret.guess_fp, ret.truth, ret.truth_fp
142+
failed_index_split[1], failed_index_split[0], ret.truth, ret.truth_fp, ret.guess, ret.guess_fp
143143
);
144144
}
145145

0 commit comments

Comments
 (0)