Skip to content

Commit dfdef2c

Browse files
authored
[libc] Fix the remaining isnan and isinf in tests. (#100969)
Fixes #100964
1 parent 5cddc31 commit dfdef2c

File tree

4 files changed

+22
-28
lines changed

4 files changed

+22
-28
lines changed

libc/fuzzing/math/RemQuoDiff.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,22 @@ void RemQuoDiff(RemQuoFunc<T> func1, RemQuoFunc<T> func2, const uint8_t *data,
3131
T remainder1 = func1(x, y, &q1);
3232
T remainder2 = func2(x, y, &q2);
3333

34-
if (isnan(remainder1)) {
35-
if (!isnan(remainder2))
34+
LIBC_NAMESPACE::fputil::FPBits<T> bits1(remainder1);
35+
LIBC_NAMESPACE::fputil::FPBits<T> bits2(remainder2);
36+
37+
if (bit1.is_nan()) {
38+
if (!bit2.is_nan())
3639
__builtin_trap();
3740
return;
3841
}
3942

40-
if (isinf(remainder2) != isinf(remainder1))
43+
if (bit1.is_inf() != bit2.is_inf())
4144
__builtin_trap();
4245

4346
// Compare only the 3 LS bits of the quotient.
4447
if ((q1 & 0x7) != (q2 & 0x7))
4548
__builtin_trap();
4649

47-
LIBC_NAMESPACE::fputil::FPBits<T> bits1(remainder1);
48-
LIBC_NAMESPACE::fputil::FPBits<T> bits2(remainder2);
4950
if (bits1.uintval() != bits2.uintval())
5051
__builtin_trap();
5152
}

libc/fuzzing/stdlib/strtofloat_fuzz.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
118118
__builtin_trap();
119119
// If any result is NaN, all of them should be NaN. We can't use the usual
120120
// comparisons because NaN != NaN.
121-
if (isnan(float_result) ^ isnan(strtof_result))
121+
if (FPBits<float>(float_result).is_nan() !=
122+
FPBits<float>(strtof_result).is_nan())
122123
__builtin_trap();
123-
if (!isnan(float_result) && float_result != strtof_result)
124+
if (!FPBits<float>(float_result).is_nan() && float_result != strtof_result)
124125
__builtin_trap();
125126
mpfr_clear(mpfr_float);
126127
}
@@ -136,10 +137,12 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
136137
ptrdiff_t strtod_strlen = out_ptr - str_ptr;
137138
if (result_strlen != strtod_strlen)
138139
__builtin_trap();
139-
if (isnan(double_result) ^ isnan(strtod_result) ||
140-
isnan(double_result) ^ isnan(atof_result))
140+
if (FPBits<double>(double_result).is_nan() !=
141+
FPBits<double>(strtod_result).is_nan() ||
142+
FPBits<double>(double_result).is_nan() !=
143+
FPBits<double>(atof_result).is_nan())
141144
__builtin_trap();
142-
if (!isnan(double_result) &&
145+
if (!FPBits<double>(double_result).is_nan() &&
143146
(double_result != strtod_result || double_result != atof_result))
144147
__builtin_trap();
145148
mpfr_clear(mpfr_double);
@@ -156,9 +159,11 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
156159
ptrdiff_t strtold_strlen = out_ptr - str_ptr;
157160
if (result_strlen != strtold_strlen)
158161
__builtin_trap();
159-
if (isnan(long_double_result) ^ isnan(strtold_result))
162+
if (FPBits<long double>(long_double_result).is_nan() ^
163+
FPBits<long double>(strtold_result).is_nan())
160164
__builtin_trap();
161-
if (!isnan(long_double_result) && long_double_result != strtold_result)
165+
if (!FPBits<long double>(long_double_result).is_nan() &&
166+
long_double_result != strtold_result)
162167
__builtin_trap();
163168
mpfr_clear(mpfr_long_double);
164169
}

libc/test/src/math/cbrt_test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ using LIBC_NAMESPACE::testing::tlog;
2121

2222
TEST_F(LlvmLibcCbrtTest, InDoubleRange) {
2323
constexpr uint64_t COUNT = 123'451;
24-
uint64_t START = LIBC_NAMESPACE::fputil::FPBits<double>(1.0).uintval();
25-
uint64_t STOP = LIBC_NAMESPACE::fputil::FPBits<double>(8.0).uintval();
24+
uint64_t START = FPBits(1.0).uintval();
25+
uint64_t STOP = FPBits(8.0).uintval();
2626
uint64_t STEP = (STOP - START) / COUNT;
2727

2828
auto test = [&](mpfr::RoundingMode rounding_mode) {
@@ -38,12 +38,12 @@ TEST_F(LlvmLibcCbrtTest, InDoubleRange) {
3838

3939
for (uint64_t i = 0, v = START; i <= COUNT; ++i, v += STEP) {
4040
double x = FPBits(v).get_val();
41-
if (isnan(x) || isinf(x))
41+
if (FPBits(x).is_inf_or_nan())
4242
continue;
4343

4444
double result = LIBC_NAMESPACE::cbrt(x);
4545
++total;
46-
if (isnan(result) || isinf(result))
46+
if (FPBits(result).is_inf_or_nan())
4747
continue;
4848

4949
++tested;

libc/test/utils/FPUtil/x86_long_double_test.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ TEST(LlvmLibcX86LongDoubleTest, is_nan) {
2727
// If exponent has the max value and the implicit bit is 0,
2828
// then the number is a NaN for all values of mantissa.
2929
bits.set_mantissa(i);
30-
long double nan = bits.get_val();
31-
ASSERT_NE(static_cast<int>(isnan(nan)), 0);
3230
ASSERT_TRUE(bits.is_nan());
3331
}
3432

@@ -38,8 +36,6 @@ TEST(LlvmLibcX86LongDoubleTest, is_nan) {
3836
// then the number is a NaN for all non-zero values of mantissa.
3937
// Note the initial value of |i| of 1 to avoid a zero mantissa.
4038
bits.set_mantissa(i);
41-
long double nan = bits.get_val();
42-
ASSERT_NE(static_cast<int>(isnan(nan)), 0);
4339
ASSERT_TRUE(bits.is_nan());
4440
}
4541

@@ -49,8 +45,6 @@ TEST(LlvmLibcX86LongDoubleTest, is_nan) {
4945
// If exponent is non-zero and also not max, and the implicit bit is 0,
5046
// then the number is a NaN for all values of mantissa.
5147
bits.set_mantissa(i);
52-
long double nan = bits.get_val();
53-
ASSERT_NE(static_cast<int>(isnan(nan)), 0);
5448
ASSERT_TRUE(bits.is_nan());
5549
}
5650

@@ -60,8 +54,6 @@ TEST(LlvmLibcX86LongDoubleTest, is_nan) {
6054
// If exponent is non-zero and also not max, and the implicit bit is 1,
6155
// then the number is normal value for all values of mantissa.
6256
bits.set_mantissa(i);
63-
long double valid = bits.get_val();
64-
ASSERT_EQ(static_cast<int>(isnan(valid)), 0);
6557
ASSERT_FALSE(bits.is_nan());
6658
}
6759

@@ -70,8 +62,6 @@ TEST(LlvmLibcX86LongDoubleTest, is_nan) {
7062
for (unsigned int i = 0; i < COUNT; ++i) {
7163
// If exponent is zero, then the number is a valid but denormal value.
7264
bits.set_mantissa(i);
73-
long double valid = bits.get_val();
74-
ASSERT_EQ(static_cast<int>(isnan(valid)), 0);
7565
ASSERT_FALSE(bits.is_nan());
7666
}
7767

@@ -80,8 +70,6 @@ TEST(LlvmLibcX86LongDoubleTest, is_nan) {
8070
for (unsigned int i = 0; i < COUNT; ++i) {
8171
// If exponent is zero, then the number is a valid but denormal value.
8272
bits.set_mantissa(i);
83-
long double valid = bits.get_val();
84-
ASSERT_EQ(static_cast<int>(isnan(valid)), 0);
8573
ASSERT_FALSE(bits.is_nan());
8674
}
8775
}

0 commit comments

Comments
 (0)