Skip to content

[libc] Fix the remaining isnan and isinf in tests. #100969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions libc/fuzzing/math/RemQuoDiff.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,22 @@ void RemQuoDiff(RemQuoFunc<T> func1, RemQuoFunc<T> func2, const uint8_t *data,
T remainder1 = func1(x, y, &q1);
T remainder2 = func2(x, y, &q2);

if (isnan(remainder1)) {
if (!isnan(remainder2))
LIBC_NAMESPACE::fputil::FPBits<T> bits1(remainder1);
LIBC_NAMESPACE::fputil::FPBits<T> bits2(remainder2);

if (bit1.is_nan()) {
if (!bit2.is_nan())
__builtin_trap();
return;
}

if (isinf(remainder2) != isinf(remainder1))
if (bit1.is_inf() != bit2.is_inf())
__builtin_trap();

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

LIBC_NAMESPACE::fputil::FPBits<T> bits1(remainder1);
LIBC_NAMESPACE::fputil::FPBits<T> bits2(remainder2);
if (bits1.uintval() != bits2.uintval())
__builtin_trap();
}
Expand Down
19 changes: 12 additions & 7 deletions libc/fuzzing/stdlib/strtofloat_fuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
__builtin_trap();
// If any result is NaN, all of them should be NaN. We can't use the usual
// comparisons because NaN != NaN.
if (isnan(float_result) ^ isnan(strtof_result))
if (FPBits<float>(float_result).is_nan() !=
FPBits<float>(strtof_result).is_nan())
__builtin_trap();
if (!isnan(float_result) && float_result != strtof_result)
if (!FPBits<float>(float_result).is_nan() && float_result != strtof_result)
__builtin_trap();
mpfr_clear(mpfr_float);
}
Expand All @@ -136,10 +137,12 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
ptrdiff_t strtod_strlen = out_ptr - str_ptr;
if (result_strlen != strtod_strlen)
__builtin_trap();
if (isnan(double_result) ^ isnan(strtod_result) ||
isnan(double_result) ^ isnan(atof_result))
if (FPBits<double>(double_result).is_nan() !=
FPBits<double>(strtod_result).is_nan() ||
FPBits<double>(double_result).is_nan() !=
FPBits<double>(atof_result).is_nan())
__builtin_trap();
if (!isnan(double_result) &&
if (!FPBits<double>(double_result).is_nan() &&
(double_result != strtod_result || double_result != atof_result))
__builtin_trap();
mpfr_clear(mpfr_double);
Expand All @@ -156,9 +159,11 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
ptrdiff_t strtold_strlen = out_ptr - str_ptr;
if (result_strlen != strtold_strlen)
__builtin_trap();
if (isnan(long_double_result) ^ isnan(strtold_result))
if (FPBits<long double>(long_double_result).is_nan() ^
FPBits<long double>(strtold_result).is_nan())
__builtin_trap();
if (!isnan(long_double_result) && long_double_result != strtold_result)
if (!FPBits<long double>(long_double_result).is_nan() &&
long_double_result != strtold_result)
__builtin_trap();
mpfr_clear(mpfr_long_double);
}
Expand Down
8 changes: 4 additions & 4 deletions libc/test/src/math/cbrt_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ using LIBC_NAMESPACE::testing::tlog;

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

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

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

double result = LIBC_NAMESPACE::cbrt(x);
++total;
if (isnan(result) || isinf(result))
if (FPBits(result).is_inf_or_nan())
continue;

++tested;
Expand Down
12 changes: 0 additions & 12 deletions libc/test/utils/FPUtil/x86_long_double_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ TEST(LlvmLibcX86LongDoubleTest, is_nan) {
// If exponent has the max value and the implicit bit is 0,
// then the number is a NaN for all values of mantissa.
bits.set_mantissa(i);
long double nan = bits.get_val();
ASSERT_NE(static_cast<int>(isnan(nan)), 0);
ASSERT_TRUE(bits.is_nan());
}

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

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

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

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

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