266 changes: 266 additions & 0 deletions libc/test/src/__support/FPUtil/fpbits_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
//===-- Unittests for the DyadicFloat class -------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/__support/FPUtil/FPBits.h"
#include "test/UnitTest/Test.h"

using __llvm_libc::fputil::FPBits;

TEST(LlvmLibcFPBitsTest, FloatType) {
EXPECT_STREQ(FPBits<float>::inf().str().c_str(), "(+Infinity)");
EXPECT_STREQ(FPBits<float>::neg_inf().str().c_str(), "(-Infinity)");
EXPECT_STREQ(FPBits<float>(FPBits<float>::build_nan(1)).str().c_str(),
"(NaN)");

FPBits<float> zero(0.0f);
EXPECT_EQ(zero.get_sign(), false);
EXPECT_EQ(zero.get_unbiased_exponent(), static_cast<uint16_t>(0));
EXPECT_EQ(zero.get_mantissa(), static_cast<uint32_t>(0));
EXPECT_EQ(zero.uintval(), static_cast<uint32_t>(0x00000000));
EXPECT_STREQ(zero.str().c_str(),
"0x00000000 = (S: 0, E: 0x0000, M: 0x00000000)");

FPBits<float> negzero(-0.0f);
EXPECT_EQ(negzero.get_sign(), true);
EXPECT_EQ(negzero.get_unbiased_exponent(), static_cast<uint16_t>(0));
EXPECT_EQ(negzero.get_mantissa(), static_cast<uint32_t>(0));
EXPECT_EQ(negzero.uintval(), static_cast<uint32_t>(0x80000000));
EXPECT_STREQ(negzero.str().c_str(),
"0x80000000 = (S: 1, E: 0x0000, M: 0x00000000)");

FPBits<float> one(1.0f);
EXPECT_EQ(one.get_sign(), false);
EXPECT_EQ(one.get_unbiased_exponent(), static_cast<uint16_t>(0x7F));
EXPECT_EQ(one.get_mantissa(), static_cast<uint32_t>(0));
EXPECT_EQ(one.uintval(), static_cast<uint32_t>(0x3F800000));
EXPECT_STREQ(one.str().c_str(),
"0x3F800000 = (S: 0, E: 0x007F, M: 0x00000000)");

FPBits<float> negone(-1.0f);
EXPECT_EQ(negone.get_sign(), true);
EXPECT_EQ(negone.get_unbiased_exponent(), static_cast<uint16_t>(0x7F));
EXPECT_EQ(negone.get_mantissa(), static_cast<uint32_t>(0));
EXPECT_EQ(negone.uintval(), static_cast<uint32_t>(0xBF800000));
EXPECT_STREQ(negone.str().c_str(),
"0xBF800000 = (S: 1, E: 0x007F, M: 0x00000000)");

FPBits<float> num(1.125f);
EXPECT_EQ(num.get_sign(), false);
EXPECT_EQ(num.get_unbiased_exponent(), static_cast<uint16_t>(0x7F));
EXPECT_EQ(num.get_mantissa(), static_cast<uint32_t>(0x00100000));
EXPECT_EQ(num.uintval(), static_cast<uint32_t>(0x3F900000));
EXPECT_STREQ(num.str().c_str(),
"0x3F900000 = (S: 0, E: 0x007F, M: 0x00100000)");

FPBits<float> negnum(-1.125f);
EXPECT_EQ(negnum.get_sign(), true);
EXPECT_EQ(negnum.get_unbiased_exponent(), static_cast<uint16_t>(0x7F));
EXPECT_EQ(negnum.get_mantissa(), static_cast<uint32_t>(0x00100000));
EXPECT_EQ(negnum.uintval(), static_cast<uint32_t>(0xBF900000));
EXPECT_STREQ(negnum.str().c_str(),
"0xBF900000 = (S: 1, E: 0x007F, M: 0x00100000)");
}

TEST(LlvmLibcFPBitsTest, DoubleType) {
EXPECT_STREQ(FPBits<double>::inf().str().c_str(), "(+Infinity)");
EXPECT_STREQ(FPBits<double>::neg_inf().str().c_str(), "(-Infinity)");
EXPECT_STREQ(FPBits<double>(FPBits<double>::build_nan(1)).str().c_str(),
"(NaN)");

FPBits<double> zero(0.0);
EXPECT_EQ(zero.get_sign(), false);
EXPECT_EQ(zero.get_unbiased_exponent(), static_cast<uint16_t>(0x0000));
EXPECT_EQ(zero.get_mantissa(), static_cast<uint64_t>(0x0000000000000000));
EXPECT_EQ(zero.uintval(), static_cast<uint64_t>(0x0000000000000000));
EXPECT_STREQ(zero.str().c_str(),
"0x0000000000000000 = (S: 0, E: 0x0000, M: 0x0000000000000000)");

FPBits<double> negzero(-0.0);
EXPECT_EQ(negzero.get_sign(), true);
EXPECT_EQ(negzero.get_unbiased_exponent(), static_cast<uint16_t>(0x0000));
EXPECT_EQ(negzero.get_mantissa(), static_cast<uint64_t>(0x0000000000000000));
EXPECT_EQ(negzero.uintval(), static_cast<uint64_t>(0x8000000000000000));
EXPECT_STREQ(negzero.str().c_str(),
"0x8000000000000000 = (S: 1, E: 0x0000, M: 0x0000000000000000)");

FPBits<double> one(1.0);
EXPECT_EQ(one.get_sign(), false);
EXPECT_EQ(one.get_unbiased_exponent(), static_cast<uint16_t>(0x03FF));
EXPECT_EQ(one.get_mantissa(), static_cast<uint64_t>(0x0000000000000000));
EXPECT_EQ(one.uintval(), static_cast<uint64_t>(0x3FF0000000000000));
EXPECT_STREQ(one.str().c_str(),
"0x3FF0000000000000 = (S: 0, E: 0x03FF, M: 0x0000000000000000)");

FPBits<double> negone(-1.0);
EXPECT_EQ(negone.get_sign(), true);
EXPECT_EQ(negone.get_unbiased_exponent(), static_cast<uint16_t>(0x03FF));
EXPECT_EQ(negone.get_mantissa(), static_cast<uint64_t>(0x0000000000000000));
EXPECT_EQ(negone.uintval(), static_cast<uint64_t>(0xBFF0000000000000));
EXPECT_STREQ(negone.str().c_str(),
"0xBFF0000000000000 = (S: 1, E: 0x03FF, M: 0x0000000000000000)");

FPBits<double> num(1.125);
EXPECT_EQ(num.get_sign(), false);
EXPECT_EQ(num.get_unbiased_exponent(), static_cast<uint16_t>(0x03FF));
EXPECT_EQ(num.get_mantissa(), static_cast<uint64_t>(0x0002000000000000));
EXPECT_EQ(num.uintval(), static_cast<uint64_t>(0x3FF2000000000000));
EXPECT_STREQ(num.str().c_str(),
"0x3FF2000000000000 = (S: 0, E: 0x03FF, M: 0x0002000000000000)");

FPBits<double> negnum(-1.125);
EXPECT_EQ(negnum.get_sign(), true);
EXPECT_EQ(negnum.get_unbiased_exponent(), static_cast<uint16_t>(0x03FF));
EXPECT_EQ(negnum.get_mantissa(), static_cast<uint64_t>(0x0002000000000000));
EXPECT_EQ(negnum.uintval(), static_cast<uint64_t>(0xBFF2000000000000));
EXPECT_STREQ(negnum.str().c_str(),
"0xBFF2000000000000 = (S: 1, E: 0x03FF, M: 0x0002000000000000)");
}

#ifdef LIBC_TARGET_ARCH_IS_X86
TEST(LlvmLibcFPBitsTest, X86LongDoubleType) {
if constexpr (sizeof(long double) == sizeof(double))
return; // The tests for the "double" type cover for this case.

EXPECT_STREQ(FPBits<long double>::inf().str().c_str(), "(+Infinity)");
EXPECT_STREQ(FPBits<long double>::neg_inf().str().c_str(), "(-Infinity)");
EXPECT_STREQ(
FPBits<long double>(FPBits<long double>::build_nan(1)).str().c_str(),
"(NaN)");

FPBits<long double> zero(0.0l);
EXPECT_EQ(zero.get_sign(), false);
EXPECT_EQ(zero.get_unbiased_exponent(), static_cast<uint16_t>(0x0000));
EXPECT_EQ(zero.get_mantissa(), static_cast<UInt128>(0x0000000000000000)
<< 64);
EXPECT_EQ(zero.uintval(), static_cast<UInt128>(0x0000000000000000) << 64);
EXPECT_STREQ(
zero.str().c_str(),
"0x00000000000000000000000000000000 = "
"(S: 0, E: 0x0000, I: 0, M: 0x00000000000000000000000000000000)");

FPBits<long double> negzero(-0.0l);
EXPECT_EQ(negzero.get_sign(), true);
EXPECT_EQ(negzero.get_unbiased_exponent(), static_cast<uint16_t>(0x0000));
EXPECT_EQ(negzero.get_mantissa(), static_cast<UInt128>(0x0000000000000000)
<< 64);
EXPECT_EQ(negzero.uintval(), static_cast<UInt128>(0x1) << 79);
EXPECT_STREQ(
negzero.str().c_str(),
"0x00000000000080000000000000000000 = "
"(S: 1, E: 0x0000, I: 0, M: 0x00000000000000000000000000000000)");

FPBits<long double> one(1.0l);
EXPECT_EQ(one.get_sign(), false);
EXPECT_EQ(one.get_unbiased_exponent(), static_cast<uint16_t>(0x3FFF));
EXPECT_EQ(one.get_mantissa(), static_cast<UInt128>(0x0000000000000000) << 64);
EXPECT_EQ(one.uintval(), static_cast<UInt128>(0x3FFF8) << 60);
EXPECT_STREQ(
one.str().c_str(),
"0x0000000000003FFF8000000000000000 = "
"(S: 0, E: 0x3FFF, I: 1, M: 0x00000000000000000000000000000000)");

FPBits<long double> negone(-1.0l);
EXPECT_EQ(negone.get_sign(), true);
EXPECT_EQ(negone.get_unbiased_exponent(), static_cast<uint16_t>(0x3FFF));
EXPECT_EQ(negone.get_mantissa(), static_cast<UInt128>(0x0000000000000000)
<< 64);
EXPECT_EQ(negone.uintval(), static_cast<UInt128>(0xBFFF8) << 60);
EXPECT_STREQ(
negone.str().c_str(),
"0x000000000000BFFF8000000000000000 = "
"(S: 1, E: 0x3FFF, I: 1, M: 0x00000000000000000000000000000000)");

FPBits<long double> num(1.125l);
EXPECT_EQ(num.get_sign(), false);
EXPECT_EQ(num.get_unbiased_exponent(), static_cast<uint16_t>(0x3FFF));
EXPECT_EQ(num.get_mantissa(), static_cast<UInt128>(0x1) << 60);
EXPECT_EQ(num.uintval(), static_cast<UInt128>(0x3FFF9) << 60);
EXPECT_STREQ(
num.str().c_str(),
"0x0000000000003FFF9000000000000000 = "
"(S: 0, E: 0x3FFF, I: 1, M: 0x00000000000000001000000000000000)");

FPBits<long double> negnum(-1.125l);
EXPECT_EQ(negnum.get_sign(), true);
EXPECT_EQ(negnum.get_unbiased_exponent(), static_cast<uint16_t>(0x3FFF));
EXPECT_EQ(negnum.get_mantissa(), static_cast<UInt128>(0x1) << 60);
EXPECT_EQ(negnum.uintval(), static_cast<UInt128>(0xBFFF9) << 60);
EXPECT_STREQ(
negnum.str().c_str(),
"0x000000000000BFFF9000000000000000 = "
"(S: 1, E: 0x3FFF, I: 1, M: 0x00000000000000001000000000000000)");
}
#else
TEST(LlvmLibcFPBitsTest, LongDoubleType) {
if constexpr (sizeof(long double) == sizeof(double))
return; // The tests for the "double" type cover for this case.

EXPECT_STREQ(FPBits<long double>::inf().str().c_str(), "(+Infinity)");
EXPECT_STREQ(FPBits<long double>::neg_inf().str().c_str(), "(-Infinity)");
EXPECT_STREQ(
FPBits<long double>(FPBits<long double>::build_nan(1)).str().c_str(),
"(NaN)");

FPBits<long double> zero(0.0l);
EXPECT_EQ(zero.get_sign(), false);
EXPECT_EQ(zero.get_unbiased_exponent(), static_cast<uint16_t>(0x0000));
EXPECT_EQ(zero.get_mantissa(), static_cast<UInt128>(0x0000000000000000)
<< 64);
EXPECT_EQ(zero.uintval(), static_cast<UInt128>(0x0000000000000000) << 64);
EXPECT_STREQ(zero.str().c_str(),
"0x00000000000000000000000000000000 = "
"(S: 0, E: 0x0000, M: 0x00000000000000000000000000000000)");

FPBits<long double> negzero(-0.0l);
EXPECT_EQ(negzero.get_sign(), true);
EXPECT_EQ(negzero.get_unbiased_exponent(), static_cast<uint16_t>(0x0000));
EXPECT_EQ(negzero.get_mantissa(), static_cast<UInt128>(0x0000000000000000)
<< 64);
EXPECT_EQ(negzero.uintval(), static_cast<UInt128>(0x1) << 127);
EXPECT_STREQ(negzero.str().c_str(),
"0x80000000000000000000000000000000 = "
"(S: 1, E: 0x0000, M: 0x00000000000000000000000000000000)");

FPBits<long double> one(1.0l);
EXPECT_EQ(one.get_sign(), false);
EXPECT_EQ(one.get_unbiased_exponent(), static_cast<uint16_t>(0x3FFF));
EXPECT_EQ(one.get_mantissa(), static_cast<UInt128>(0x0000000000000000) << 64);
EXPECT_EQ(one.uintval(), static_cast<UInt128>(0x3FFF) << 112);
EXPECT_STREQ(one.str().c_str(),
"0x3FFF0000000000000000000000000000 = "
"(S: 0, E: 0x3FFF, M: 0x00000000000000000000000000000000)");

FPBits<long double> negone(-1.0l);
EXPECT_EQ(negone.get_sign(), true);
EXPECT_EQ(negone.get_unbiased_exponent(), static_cast<uint16_t>(0x3FFF));
EXPECT_EQ(negone.get_mantissa(), static_cast<UInt128>(0x0000000000000000)
<< 64);
EXPECT_EQ(negone.uintval(), static_cast<UInt128>(0xBFFF) << 112);
EXPECT_STREQ(negone.str().c_str(),
"0xBFFF0000000000000000000000000000 = "
"(S: 1, E: 0x3FFF, M: 0x00000000000000000000000000000000)");

FPBits<long double> num(1.125l);
EXPECT_EQ(num.get_sign(), false);
EXPECT_EQ(num.get_unbiased_exponent(), static_cast<uint16_t>(0x3FFF));
EXPECT_EQ(num.get_mantissa(), static_cast<UInt128>(0x2) << 108);
EXPECT_EQ(num.uintval(), static_cast<UInt128>(0x3FFF2) << 108);
EXPECT_STREQ(num.str().c_str(),
"0x3FFF2000000000000000000000000000 = "
"(S: 0, E: 0x3FFF, M: 0x00002000000000000000000000000000)");

FPBits<long double> negnum(-1.125l);
EXPECT_EQ(negnum.get_sign(), true);
EXPECT_EQ(negnum.get_unbiased_exponent(), static_cast<uint16_t>(0x3FFF));
EXPECT_EQ(negnum.get_mantissa(), static_cast<UInt128>(0x2) << 108);
EXPECT_EQ(negnum.uintval(), static_cast<UInt128>(0xBFFF2) << 108);
EXPECT_STREQ(negnum.str().c_str(),
"0xBFFF2000000000000000000000000000 = "
"(S: 1, E: 0x3FFF, M: 0x00002000000000000000000000000000)");
}
#endif
49 changes: 24 additions & 25 deletions libc/utils/MPFRWrapper/MPFRUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -703,12 +703,11 @@ void explain_unary_operation_single_output_error(Operation op, T input,
MPFRNumber mpfrMatchValue(matchValue);
tlog << "Match value not within tolerance value of MPFR result:\n"
<< " Input decimal: " << mpfrInput.str() << '\n';
__llvm_libc::fputil::testing::describeValue(" Input bits: ", input);
tlog << " Input bits: " << FPBits<T>(input).str() << '\n';
tlog << '\n' << " Match decimal: " << mpfrMatchValue.str() << '\n';
__llvm_libc::fputil::testing::describeValue(" Match bits: ", matchValue);
tlog << " Match bits: " << FPBits<T>(matchValue).str() << '\n';
tlog << '\n' << " MPFR result: " << mpfr_result.str() << '\n';
__llvm_libc::fputil::testing::describeValue(" MPFR rounded: ",
mpfr_result.as<T>());
tlog << " MPFR rounded: " << FPBits<T>(mpfr_result.as<T>()).str() << '\n';
tlog << '\n';
tlog << " ULP error: "
<< mpfr_result.ulp_as_mpfr_number(matchValue).str() << '\n';
Expand Down Expand Up @@ -748,13 +747,13 @@ void explain_unary_operation_two_outputs_error(
tlog << " Input decimal: " << mpfrInput.str() << "\n\n";

tlog << "Libc floating point value: " << mpfrMatchValue.str() << '\n';
__llvm_libc::fputil::testing::describeValue(" Libc floating point bits: ",
libc_result.f);
tlog << " Libc floating point bits: " << FPBits<T>(libc_result.f).str()
<< '\n';
tlog << "\n\n";

tlog << " MPFR result: " << mpfr_result.str() << '\n';
__llvm_libc::fputil::testing::describeValue(" MPFR rounded: ",
mpfr_result.as<T>());
tlog << " MPFR rounded: " << FPBits<T>(mpfr_result.as<T>()).str()
<< '\n';
tlog << '\n'
<< " ULP error: "
<< mpfr_result.ulp_as_mpfr_number(libc_result.f).str() << '\n';
Expand Down Expand Up @@ -786,10 +785,10 @@ void explain_binary_operation_two_outputs_error(
<< "Libc integral result: " << libc_result.i << '\n'
<< "Libc floating point result: " << mpfrMatchValue.str() << '\n'
<< " MPFR result: " << mpfr_result.str() << '\n';
__llvm_libc::fputil::testing::describeValue(
"Libc floating point result bits: ", libc_result.f);
__llvm_libc::fputil::testing::describeValue(
" MPFR rounded bits: ", mpfr_result.as<T>());
tlog << "Libc floating point result bits: " << FPBits<T>(libc_result.f).str()
<< '\n';
tlog << " MPFR rounded bits: "
<< FPBits<T>(mpfr_result.as<T>()).str() << '\n';
tlog << "ULP error: " << mpfr_result.ulp_as_mpfr_number(libc_result.f).str()
<< '\n';
}
Expand Down Expand Up @@ -820,15 +819,15 @@ void explain_binary_operation_one_output_error(Operation op,
MPFRNumber mpfrMatchValue(libc_result);

tlog << "Input decimal: x: " << mpfrX.str() << " y: " << mpfrY.str() << '\n';
__llvm_libc::fputil::testing::describeValue("First input bits: ", input.x);
__llvm_libc::fputil::testing::describeValue("Second input bits: ", input.y);
tlog << "First input bits: " << FPBits<T>(input.x).str() << '\n';
tlog << "Second input bits: " << FPBits<T>(input.y).str() << '\n';

tlog << "Libc result: " << mpfrMatchValue.str() << '\n'
<< "MPFR result: " << mpfr_result.str() << '\n';
__llvm_libc::fputil::testing::describeValue(
"Libc floating point result bits: ", libc_result);
__llvm_libc::fputil::testing::describeValue(
" MPFR rounded bits: ", mpfr_result.as<T>());
tlog << "Libc floating point result bits: " << FPBits<T>(libc_result).str()
<< '\n';
tlog << " MPFR rounded bits: "
<< FPBits<T>(mpfr_result.as<T>()).str() << '\n';
tlog << "ULP error: " << mpfr_result.ulp_as_mpfr_number(libc_result).str()
<< '\n';
}
Expand Down Expand Up @@ -860,16 +859,16 @@ void explain_ternary_operation_one_output_error(Operation op,

tlog << "Input decimal: x: " << mpfrX.str() << " y: " << mpfrY.str()
<< " z: " << mpfrZ.str() << '\n';
__llvm_libc::fputil::testing::describeValue("First input bits: ", input.x);
__llvm_libc::fputil::testing::describeValue("Second input bits: ", input.y);
__llvm_libc::fputil::testing::describeValue("Third input bits: ", input.z);
tlog << " First input bits: " << FPBits<T>(input.x).str() << '\n';
tlog << "Second input bits: " << FPBits<T>(input.y).str() << '\n';
tlog << " Third input bits: " << FPBits<T>(input.z).str() << '\n';

tlog << "Libc result: " << mpfrMatchValue.str() << '\n'
<< "MPFR result: " << mpfr_result.str() << '\n';
__llvm_libc::fputil::testing::describeValue(
"Libc floating point result bits: ", libc_result);
__llvm_libc::fputil::testing::describeValue(
" MPFR rounded bits: ", mpfr_result.as<T>());
tlog << "Libc floating point result bits: " << FPBits<T>(libc_result).str()
<< '\n';
tlog << " MPFR rounded bits: "
<< FPBits<T>(mpfr_result.as<T>()).str() << '\n';
tlog << "ULP error: " << mpfr_result.ulp_as_mpfr_number(libc_result).str()
<< '\n';
}
Expand Down