From 60509623c46e8d8122dcc1ee62fecae0a6383139 Mon Sep 17 00:00:00 2001 From: Siva Chandra Reddy Date: Thu, 23 Dec 2021 22:59:14 +0000 Subject: [PATCH] [libc][obvious] Fix style of MPFRWrapper. --- libc/test/src/math/RIntTest.h | 28 +- libc/test/src/math/RoundToIntegerTest.h | 18 +- libc/utils/MPFRWrapper/MPFRUtils.cpp | 392 ++++++++++++------------ libc/utils/MPFRWrapper/MPFRUtils.h | 149 ++++----- 4 files changed, 298 insertions(+), 289 deletions(-) diff --git a/libc/test/src/math/RIntTest.h b/libc/test/src/math/RIntTest.h index 7dc677059f19e..438a92338dfa2 100644 --- a/libc/test/src/math/RIntTest.h +++ b/libc/test/src/math/RIntTest.h @@ -70,12 +70,12 @@ class RIntTestTemplate : public __llvm_libc::testing::Test { for (int mode : ROUNDING_MODES) { __llvm_libc::fputil::set_round(mode); mpfr::RoundingMode mpfr_mode = to_mpfr_rounding_mode(mode); - ASSERT_FP_EQ(func(T(1.0)), mpfr::Round(T(1.0), mpfr_mode)); - ASSERT_FP_EQ(func(T(-1.0)), mpfr::Round(T(-1.0), mpfr_mode)); - ASSERT_FP_EQ(func(T(10.0)), mpfr::Round(T(10.0), mpfr_mode)); - ASSERT_FP_EQ(func(T(-10.0)), mpfr::Round(T(-10.0), mpfr_mode)); - ASSERT_FP_EQ(func(T(1234.0)), mpfr::Round(T(1234.0), mpfr_mode)); - ASSERT_FP_EQ(func(T(-1234.0)), mpfr::Round(T(-1234.0), mpfr_mode)); + ASSERT_FP_EQ(func(T(1.0)), mpfr::round(T(1.0), mpfr_mode)); + ASSERT_FP_EQ(func(T(-1.0)), mpfr::round(T(-1.0), mpfr_mode)); + ASSERT_FP_EQ(func(T(10.0)), mpfr::round(T(10.0), mpfr_mode)); + ASSERT_FP_EQ(func(T(-10.0)), mpfr::round(T(-10.0), mpfr_mode)); + ASSERT_FP_EQ(func(T(1234.0)), mpfr::round(T(1234.0), mpfr_mode)); + ASSERT_FP_EQ(func(T(-1234.0)), mpfr::round(T(-1234.0), mpfr_mode)); } } @@ -83,12 +83,12 @@ class RIntTestTemplate : public __llvm_libc::testing::Test { for (int mode : ROUNDING_MODES) { __llvm_libc::fputil::set_round(mode); mpfr::RoundingMode mpfr_mode = to_mpfr_rounding_mode(mode); - ASSERT_FP_EQ(func(T(0.5)), mpfr::Round(T(0.5), mpfr_mode)); - ASSERT_FP_EQ(func(T(-0.5)), mpfr::Round(T(-0.5), mpfr_mode)); - ASSERT_FP_EQ(func(T(0.115)), mpfr::Round(T(0.115), mpfr_mode)); - ASSERT_FP_EQ(func(T(-0.115)), mpfr::Round(T(-0.115), mpfr_mode)); - ASSERT_FP_EQ(func(T(0.715)), mpfr::Round(T(0.715), mpfr_mode)); - ASSERT_FP_EQ(func(T(-0.715)), mpfr::Round(T(-0.715), mpfr_mode)); + ASSERT_FP_EQ(func(T(0.5)), mpfr::round(T(0.5), mpfr_mode)); + ASSERT_FP_EQ(func(T(-0.5)), mpfr::round(T(-0.5), mpfr_mode)); + ASSERT_FP_EQ(func(T(0.115)), mpfr::round(T(0.115), mpfr_mode)); + ASSERT_FP_EQ(func(T(-0.115)), mpfr::round(T(-0.115), mpfr_mode)); + ASSERT_FP_EQ(func(T(0.715)), mpfr::round(T(0.715), mpfr_mode)); + ASSERT_FP_EQ(func(T(-0.715)), mpfr::round(T(-0.715), mpfr_mode)); } } @@ -102,7 +102,7 @@ class RIntTestTemplate : public __llvm_libc::testing::Test { for (int mode : ROUNDING_MODES) { __llvm_libc::fputil::set_round(mode); mpfr::RoundingMode mpfr_mode = to_mpfr_rounding_mode(mode); - ASSERT_FP_EQ(func(x), mpfr::Round(x, mpfr_mode)); + ASSERT_FP_EQ(func(x), mpfr::round(x, mpfr_mode)); } } } @@ -121,7 +121,7 @@ class RIntTestTemplate : public __llvm_libc::testing::Test { for (int mode : ROUNDING_MODES) { __llvm_libc::fputil::set_round(mode); mpfr::RoundingMode mpfr_mode = to_mpfr_rounding_mode(mode); - ASSERT_FP_EQ(func(x), mpfr::Round(x, mpfr_mode)); + ASSERT_FP_EQ(func(x), mpfr::round(x, mpfr_mode)); } } } diff --git a/libc/test/src/math/RoundToIntegerTest.h b/libc/test/src/math/RoundToIntegerTest.h index e95f96e92ab03..60647eaadc6b0 100644 --- a/libc/test/src/math/RoundToIntegerTest.h +++ b/libc/test/src/math/RoundToIntegerTest.h @@ -141,7 +141,7 @@ class RoundToIntegerTestTemplate : public __llvm_libc::testing::Test { F x = F(bits); long mpfr_result; - bool erangeflag = mpfr::RoundToLong(x, mpfr_result); + bool erangeflag = mpfr::round_to_long(x, mpfr_result); ASSERT_FALSE(erangeflag); test_one_input(func, x, mpfr_result, false); } @@ -163,10 +163,10 @@ class RoundToIntegerTestTemplate : public __llvm_libc::testing::Test { long mpfr_long_result; bool erangeflag; if (TestModes) - erangeflag = - mpfr::RoundToLong(x, to_mpfr_rounding_mode(mode), mpfr_long_result); + erangeflag = mpfr::round_to_long(x, to_mpfr_rounding_mode(mode), + mpfr_long_result); else - erangeflag = mpfr::RoundToLong(x, mpfr_long_result); + erangeflag = mpfr::round_to_long(x, mpfr_long_result); ASSERT_FALSE(erangeflag); I mpfr_result = mpfr_long_result; test_one_input(func, x, mpfr_result, false); @@ -210,13 +210,13 @@ class RoundToIntegerTestTemplate : public __llvm_libc::testing::Test { __llvm_libc::fputil::set_round(m); long mpfr_long_result; bool erangeflag = - mpfr::RoundToLong(x, to_mpfr_rounding_mode(m), mpfr_long_result); + mpfr::round_to_long(x, to_mpfr_rounding_mode(m), mpfr_long_result); ASSERT_TRUE(erangeflag); test_one_input(func, x, INTEGER_MIN, true); } } else { long mpfr_long_result; - bool erangeflag = mpfr::RoundToLong(x, mpfr_long_result); + bool erangeflag = mpfr::round_to_long(x, mpfr_long_result); ASSERT_TRUE(erangeflag); test_one_input(func, x, INTEGER_MIN, true); } @@ -280,8 +280,8 @@ class RoundToIntegerTestTemplate : public __llvm_libc::testing::Test { if (TestModes) { for (int m : ROUNDING_MODES) { long mpfr_long_result; - bool erangeflag = - mpfr::RoundToLong(x, to_mpfr_rounding_mode(m), mpfr_long_result); + bool erangeflag = mpfr::round_to_long(x, to_mpfr_rounding_mode(m), + mpfr_long_result); I mpfr_result = mpfr_long_result; __llvm_libc::fputil::set_round(m); if (erangeflag) @@ -291,7 +291,7 @@ class RoundToIntegerTestTemplate : public __llvm_libc::testing::Test { } } else { long mpfr_long_result; - bool erangeflag = mpfr::RoundToLong(x, mpfr_long_result); + bool erangeflag = mpfr::round_to_long(x, mpfr_long_result); I mpfr_result = mpfr_long_result; if (erangeflag) test_one_input(func, x, x > 0 ? INTEGER_MAX : INTEGER_MIN, true); diff --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp index 7c4f061b47b50..4e25c874d122f 100644 --- a/libc/utils/MPFRWrapper/MPFRUtils.cpp +++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp @@ -38,73 +38,77 @@ namespace mpfr { template struct Precision; template <> struct Precision { - static constexpr unsigned int value = 24; + static constexpr unsigned int VALUE = 24; }; template <> struct Precision { - static constexpr unsigned int value = 53; + static constexpr unsigned int VALUE = 53; }; #if !(defined(LLVM_LIBC_ARCH_X86)) template <> struct Precision { - static constexpr unsigned int value = 64; + static constexpr unsigned int VALUE = 64; }; #else template <> struct Precision { - static constexpr unsigned int value = 113; + static constexpr unsigned int VALUE = 113; }; #endif class MPFRNumber { // A precision value which allows sufficiently large additional // precision even compared to quad-precision floating point values. - unsigned int mpfrPrecision; + unsigned int mpfr_precision; mpfr_t value; public: - MPFRNumber() : mpfrPrecision(256) { mpfr_init2(value, mpfrPrecision); } + MPFRNumber() : mpfr_precision(256) { mpfr_init2(value, mpfr_precision); } // We use explicit EnableIf specializations to disallow implicit // conversions. Implicit conversions can potentially lead to loss of // precision. template ::Value, int> = 0> - explicit MPFRNumber(XType x, int precision = 128) : mpfrPrecision(precision) { - mpfr_init2(value, mpfrPrecision); + explicit MPFRNumber(XType x, int precision = 128) + : mpfr_precision(precision) { + mpfr_init2(value, mpfr_precision); mpfr_set_flt(value, x, MPFR_RNDN); } template ::Value, int> = 0> - explicit MPFRNumber(XType x, int precision = 128) : mpfrPrecision(precision) { - mpfr_init2(value, mpfrPrecision); + explicit MPFRNumber(XType x, int precision = 128) + : mpfr_precision(precision) { + mpfr_init2(value, mpfr_precision); mpfr_set_d(value, x, MPFR_RNDN); } template ::Value, int> = 0> - explicit MPFRNumber(XType x, int precision = 128) : mpfrPrecision(precision) { - mpfr_init2(value, mpfrPrecision); + explicit MPFRNumber(XType x, int precision = 128) + : mpfr_precision(precision) { + mpfr_init2(value, mpfr_precision); mpfr_set_ld(value, x, MPFR_RNDN); } template ::Value, int> = 0> - explicit MPFRNumber(XType x, int precision = 128) : mpfrPrecision(precision) { - mpfr_init2(value, mpfrPrecision); + explicit MPFRNumber(XType x, int precision = 128) + : mpfr_precision(precision) { + mpfr_init2(value, mpfr_precision); mpfr_set_sj(value, x, MPFR_RNDN); } - MPFRNumber(const MPFRNumber &other) : mpfrPrecision(other.mpfrPrecision) { - mpfr_init2(value, mpfrPrecision); + MPFRNumber(const MPFRNumber &other) : mpfr_precision(other.mpfr_precision) { + mpfr_init2(value, mpfr_precision); mpfr_set(value, other.value, MPFR_RNDN); } ~MPFRNumber() { mpfr_clear(value); } MPFRNumber &operator=(const MPFRNumber &rhs) { - mpfrPrecision = rhs.mpfrPrecision; + mpfr_precision = rhs.mpfr_precision; mpfr_set(value, rhs.value, MPFR_RNDN); return *this; } @@ -185,7 +189,7 @@ class MPFRNumber { return result; } - bool roundToLong(long &result) const { + bool roung_to_long(long &result) const { // We first calculate the rounded value. This way, when converting // to long using mpfr_get_si, the rounding direction of MPFR_RNDN // (or any other rounding mode), does not have an influence. @@ -195,10 +199,10 @@ class MPFRNumber { return mpfr_erangeflag_p(); } - bool roundToLong(mpfr_rnd_t rnd, long &result) const { + bool roung_to_long(mpfr_rnd_t rnd, long &result) const { MPFRNumber rint_result; mpfr_rint(rint_result.value, value, rnd); - return rint_result.roundToLong(result); + return rint_result.roung_to_long(result); } MPFRNumber rint(mpfr_rnd_t rnd) const { @@ -374,7 +378,7 @@ namespace internal { template cpp::EnableIfType::Value, MPFRNumber> -unaryOperation(Operation op, InputType input) { +unary_operation(Operation op, InputType input) { MPFRNumber mpfrInput(input); switch (op) { case Operation::Abs: @@ -416,7 +420,7 @@ unaryOperation(Operation op, InputType input) { template cpp::EnableIfType::Value, MPFRNumber> -unaryOperationTwoOutputs(Operation op, InputType input, int &output) { +unary_operation_two_outputs(Operation op, InputType input, int &output) { MPFRNumber mpfrInput(input); switch (op) { case Operation::Frexp: @@ -428,7 +432,7 @@ unaryOperationTwoOutputs(Operation op, InputType input, int &output) { template cpp::EnableIfType::Value, MPFRNumber> -binaryOperationOneOutput(Operation op, InputType x, InputType y) { +binary_operation_one_output(Operation op, InputType x, InputType y) { MPFRNumber inputX(x), inputY(y); switch (op) { case Operation::Hypot: @@ -440,7 +444,8 @@ binaryOperationOneOutput(Operation op, InputType x, InputType y) { template cpp::EnableIfType::Value, MPFRNumber> -binaryOperationTwoOutputs(Operation op, InputType x, InputType y, int &output) { +binary_operation_two_outputs(Operation op, InputType x, InputType y, + int &output) { MPFRNumber inputX(x), inputY(y); switch (op) { case Operation::RemQuo: @@ -452,11 +457,12 @@ binaryOperationTwoOutputs(Operation op, InputType x, InputType y, int &output) { template cpp::EnableIfType::Value, MPFRNumber> -ternaryOperationOneOutput(Operation op, InputType x, InputType y, InputType z) { +ternary_operation_one_output(Operation op, InputType x, InputType y, + InputType z) { // For FMA function, we just need to compare with the mpfr_fma with the same // precision as InputType. Using higher precision as the intermediate results // to compare might incorrectly fail due to double-rounding errors. - constexpr unsigned int prec = Precision::value; + constexpr unsigned int prec = Precision::VALUE; MPFRNumber inputX(x, prec), inputY(y, prec), inputZ(z, prec); switch (op) { case Operation::Fma: @@ -467,54 +473,55 @@ ternaryOperationOneOutput(Operation op, InputType x, InputType y, InputType z) { } template -void explainUnaryOperationSingleOutputError(Operation op, T input, T matchValue, - testutils::StreamWrapper &OS) { +void explain_unary_operation_single_output_error(Operation op, T input, + T matchValue, + testutils::StreamWrapper &OS) { MPFRNumber mpfrInput(input); - MPFRNumber mpfrResult = unaryOperation(op, input); + MPFRNumber mpfr_result = unary_operation(op, input); MPFRNumber mpfrMatchValue(matchValue); FPBits inputBits(input); FPBits matchBits(matchValue); - FPBits mpfrResultBits(mpfrResult.as()); + FPBits mpfr_resultBits(mpfr_result.as()); OS << "Match value not within tolerance value of MPFR result:\n" << " Input decimal: " << mpfrInput.str() << '\n'; __llvm_libc::fputil::testing::describeValue(" Input bits: ", input, OS); OS << '\n' << " Match decimal: " << mpfrMatchValue.str() << '\n'; __llvm_libc::fputil::testing::describeValue(" Match bits: ", matchValue, OS); - OS << '\n' << " MPFR result: " << mpfrResult.str() << '\n'; + OS << '\n' << " MPFR result: " << mpfr_result.str() << '\n'; __llvm_libc::fputil::testing::describeValue( - " MPFR rounded: ", mpfrResult.as(), OS); + " MPFR rounded: ", mpfr_result.as(), OS); OS << '\n'; - OS << " ULP error: " << std::to_string(mpfrResult.ulp(matchValue)) + OS << " ULP error: " << std::to_string(mpfr_result.ulp(matchValue)) << '\n'; } template void -explainUnaryOperationSingleOutputError(Operation op, float, float, - testutils::StreamWrapper &); -template void -explainUnaryOperationSingleOutputError(Operation op, double, double, - testutils::StreamWrapper &); -template void explainUnaryOperationSingleOutputError( +explain_unary_operation_single_output_error(Operation op, float, float, + testutils::StreamWrapper &); +template void explain_unary_operation_single_output_error( + Operation op, double, double, testutils::StreamWrapper &); +template void explain_unary_operation_single_output_error( Operation op, long double, long double, testutils::StreamWrapper &); template -void explainUnaryOperationTwoOutputsError(Operation op, T input, - const BinaryOutput &libcResult, - testutils::StreamWrapper &OS) { +void explain_unary_operation_two_outputs_error( + Operation op, T input, const BinaryOutput &libc_result, + testutils::StreamWrapper &OS) { MPFRNumber mpfrInput(input); FPBits inputBits(input); int mpfrIntResult; - MPFRNumber mpfrResult = unaryOperationTwoOutputs(op, input, mpfrIntResult); + MPFRNumber mpfr_result = + unary_operation_two_outputs(op, input, mpfrIntResult); - if (mpfrIntResult != libcResult.i) { + if (mpfrIntResult != libc_result.i) { OS << "MPFR integral result: " << mpfrIntResult << '\n' - << "Libc integral result: " << libcResult.i << '\n'; + << "Libc integral result: " << libc_result.i << '\n'; } else { OS << "Integral result from libc matches integral result from MPFR.\n"; } - MPFRNumber mpfrMatchValue(libcResult.f); + MPFRNumber mpfrMatchValue(libc_result.f); OS << "Libc floating point result is not within tolerance value of the MPFR " << "result.\n\n"; @@ -522,74 +529,73 @@ void explainUnaryOperationTwoOutputsError(Operation op, T input, OS << "Libc floating point value: " << mpfrMatchValue.str() << '\n'; __llvm_libc::fputil::testing::describeValue( - " Libc floating point bits: ", libcResult.f, OS); + " Libc floating point bits: ", libc_result.f, OS); OS << "\n\n"; - OS << " MPFR result: " << mpfrResult.str() << '\n'; + OS << " MPFR result: " << mpfr_result.str() << '\n'; __llvm_libc::fputil::testing::describeValue( - " MPFR rounded: ", mpfrResult.as(), OS); + " MPFR rounded: ", mpfr_result.as(), OS); OS << '\n' << " ULP error: " - << std::to_string(mpfrResult.ulp(libcResult.f)) << '\n'; + << std::to_string(mpfr_result.ulp(libc_result.f)) << '\n'; } -template void explainUnaryOperationTwoOutputsError( +template void explain_unary_operation_two_outputs_error( Operation, float, const BinaryOutput &, testutils::StreamWrapper &); template void -explainUnaryOperationTwoOutputsError(Operation, double, - const BinaryOutput &, - testutils::StreamWrapper &); -template void explainUnaryOperationTwoOutputsError( +explain_unary_operation_two_outputs_error(Operation, double, + const BinaryOutput &, + testutils::StreamWrapper &); +template void explain_unary_operation_two_outputs_error( Operation, long double, const BinaryOutput &, testutils::StreamWrapper &); template -void explainBinaryOperationTwoOutputsError(Operation op, - const BinaryInput &input, - const BinaryOutput &libcResult, - testutils::StreamWrapper &OS) { +void explain_binary_operation_two_outputs_error( + Operation op, const BinaryInput &input, + const BinaryOutput &libc_result, testutils::StreamWrapper &OS) { MPFRNumber mpfrX(input.x); MPFRNumber mpfrY(input.y); FPBits xbits(input.x); FPBits ybits(input.y); int mpfrIntResult; - MPFRNumber mpfrResult = - binaryOperationTwoOutputs(op, input.x, input.y, mpfrIntResult); - MPFRNumber mpfrMatchValue(libcResult.f); + MPFRNumber mpfr_result = + binary_operation_two_outputs(op, input.x, input.y, mpfrIntResult); + MPFRNumber mpfrMatchValue(libc_result.f); OS << "Input decimal: x: " << mpfrX.str() << " y: " << mpfrY.str() << '\n' << "MPFR integral result: " << mpfrIntResult << '\n' - << "Libc integral result: " << libcResult.i << '\n' + << "Libc integral result: " << libc_result.i << '\n' << "Libc floating point result: " << mpfrMatchValue.str() << '\n' - << " MPFR result: " << mpfrResult.str() << '\n'; + << " MPFR result: " << mpfr_result.str() << '\n'; __llvm_libc::fputil::testing::describeValue( - "Libc floating point result bits: ", libcResult.f, OS); + "Libc floating point result bits: ", libc_result.f, OS); __llvm_libc::fputil::testing::describeValue( - " MPFR rounded bits: ", mpfrResult.as(), OS); - OS << "ULP error: " << std::to_string(mpfrResult.ulp(libcResult.f)) << '\n'; + " MPFR rounded bits: ", mpfr_result.as(), OS); + OS << "ULP error: " << std::to_string(mpfr_result.ulp(libc_result.f)) << '\n'; } -template void explainBinaryOperationTwoOutputsError( +template void explain_binary_operation_two_outputs_error( Operation, const BinaryInput &, const BinaryOutput &, testutils::StreamWrapper &); -template void explainBinaryOperationTwoOutputsError( +template void explain_binary_operation_two_outputs_error( Operation, const BinaryInput &, const BinaryOutput &, testutils::StreamWrapper &); -template void explainBinaryOperationTwoOutputsError( +template void explain_binary_operation_two_outputs_error( Operation, const BinaryInput &, const BinaryOutput &, testutils::StreamWrapper &); template -void explainBinaryOperationOneOutputError(Operation op, - const BinaryInput &input, - T libcResult, - testutils::StreamWrapper &OS) { +void explain_binary_operation_one_output_error(Operation op, + const BinaryInput &input, + T libc_result, + testutils::StreamWrapper &OS) { MPFRNumber mpfrX(input.x); MPFRNumber mpfrY(input.y); FPBits xbits(input.x); FPBits ybits(input.y); - MPFRNumber mpfrResult = binaryOperationOneOutput(op, input.x, input.y); - MPFRNumber mpfrMatchValue(libcResult); + MPFRNumber mpfr_result = binary_operation_one_output(op, input.x, input.y); + MPFRNumber mpfrMatchValue(libc_result); OS << "Input decimal: x: " << mpfrX.str() << " y: " << mpfrY.str() << '\n'; __llvm_libc::fputil::testing::describeValue("First input bits: ", input.x, @@ -598,36 +604,36 @@ void explainBinaryOperationOneOutputError(Operation op, OS); OS << "Libc result: " << mpfrMatchValue.str() << '\n' - << "MPFR result: " << mpfrResult.str() << '\n'; + << "MPFR result: " << mpfr_result.str() << '\n'; __llvm_libc::fputil::testing::describeValue( - "Libc floating point result bits: ", libcResult, OS); + "Libc floating point result bits: ", libc_result, OS); __llvm_libc::fputil::testing::describeValue( - " MPFR rounded bits: ", mpfrResult.as(), OS); - OS << "ULP error: " << std::to_string(mpfrResult.ulp(libcResult)) << '\n'; + " MPFR rounded bits: ", mpfr_result.as(), OS); + OS << "ULP error: " << std::to_string(mpfr_result.ulp(libc_result)) << '\n'; } -template void explainBinaryOperationOneOutputError( +template void explain_binary_operation_one_output_error( Operation, const BinaryInput &, float, testutils::StreamWrapper &); -template void explainBinaryOperationOneOutputError( +template void explain_binary_operation_one_output_error( Operation, const BinaryInput &, double, testutils::StreamWrapper &); -template void explainBinaryOperationOneOutputError( +template void explain_binary_operation_one_output_error( Operation, const BinaryInput &, long double, testutils::StreamWrapper &); template -void explainTernaryOperationOneOutputError(Operation op, - const TernaryInput &input, - T libcResult, - testutils::StreamWrapper &OS) { - MPFRNumber mpfrX(input.x, Precision::value); - MPFRNumber mpfrY(input.y, Precision::value); - MPFRNumber mpfrZ(input.z, Precision::value); +void explain_ternary_operation_one_output_error(Operation op, + const TernaryInput &input, + T libc_result, + testutils::StreamWrapper &OS) { + MPFRNumber mpfrX(input.x, Precision::VALUE); + MPFRNumber mpfrY(input.y, Precision::VALUE); + MPFRNumber mpfrZ(input.z, Precision::VALUE); FPBits xbits(input.x); FPBits ybits(input.y); FPBits zbits(input.z); - MPFRNumber mpfrResult = - ternaryOperationOneOutput(op, input.x, input.y, input.z); - MPFRNumber mpfrMatchValue(libcResult); + MPFRNumber mpfr_result = + ternary_operation_one_output(op, input.x, input.y, input.z); + MPFRNumber mpfrMatchValue(libc_result); OS << "Input decimal: x: " << mpfrX.str() << " y: " << mpfrY.str() << " z: " << mpfrZ.str() << '\n'; @@ -639,144 +645,142 @@ void explainTernaryOperationOneOutputError(Operation op, OS); OS << "Libc result: " << mpfrMatchValue.str() << '\n' - << "MPFR result: " << mpfrResult.str() << '\n'; + << "MPFR result: " << mpfr_result.str() << '\n'; __llvm_libc::fputil::testing::describeValue( - "Libc floating point result bits: ", libcResult, OS); + "Libc floating point result bits: ", libc_result, OS); __llvm_libc::fputil::testing::describeValue( - " MPFR rounded bits: ", mpfrResult.as(), OS); - OS << "ULP error: " << std::to_string(mpfrResult.ulp(libcResult)) << '\n'; + " MPFR rounded bits: ", mpfr_result.as(), OS); + OS << "ULP error: " << std::to_string(mpfr_result.ulp(libc_result)) << '\n'; } -template void explainTernaryOperationOneOutputError( +template void explain_ternary_operation_one_output_error( Operation, const TernaryInput &, float, testutils::StreamWrapper &); -template void explainTernaryOperationOneOutputError( +template void explain_ternary_operation_one_output_error( Operation, const TernaryInput &, double, testutils::StreamWrapper &); -template void explainTernaryOperationOneOutputError( +template void explain_ternary_operation_one_output_error( Operation, const TernaryInput &, long double, testutils::StreamWrapper &); template -bool compareUnaryOperationSingleOutput(Operation op, T input, T libcResult, - double ulpError) { +bool compare_unary_operation_single_output(Operation op, T input, T libc_result, + double ulp_error) { // If the ulp error is exactly 0.5 (i.e a tie), we would check that the result // is rounded to the nearest even. - MPFRNumber mpfrResult = unaryOperation(op, input); - double ulp = mpfrResult.ulp(libcResult); - bool bitsAreEven = ((FPBits(libcResult).uintval() & 1) == 0); - return (ulp < ulpError) || - ((ulp == ulpError) && ((ulp != 0.5) || bitsAreEven)); + MPFRNumber mpfr_result = unary_operation(op, input); + double ulp = mpfr_result.ulp(libc_result); + bool bits_are_even = ((FPBits(libc_result).uintval() & 1) == 0); + return (ulp < ulp_error) || + ((ulp == ulp_error) && ((ulp != 0.5) || bits_are_even)); } -template bool compareUnaryOperationSingleOutput(Operation, float, float, - double); -template bool compareUnaryOperationSingleOutput(Operation, double, - double, double); -template bool compareUnaryOperationSingleOutput(Operation, - long double, - long double, - double); +template bool compare_unary_operation_single_output(Operation, float, + float, double); +template bool compare_unary_operation_single_output(Operation, double, + double, double); +template bool compare_unary_operation_single_output(Operation, + long double, + long double, + double); template -bool compareUnaryOperationTwoOutputs(Operation op, T input, - const BinaryOutput &libcResult, - double ulpError) { +bool compare_unary_operation_two_outputs(Operation op, T input, + const BinaryOutput &libc_result, + double ulp_error) { int mpfrIntResult; - MPFRNumber mpfrResult = unaryOperationTwoOutputs(op, input, mpfrIntResult); - double ulp = mpfrResult.ulp(libcResult.f); + MPFRNumber mpfr_result = + unary_operation_two_outputs(op, input, mpfrIntResult); + double ulp = mpfr_result.ulp(libc_result.f); - if (mpfrIntResult != libcResult.i) + if (mpfrIntResult != libc_result.i) return false; - bool bitsAreEven = ((FPBits(libcResult.f).uintval() & 1) == 0); - return (ulp < ulpError) || - ((ulp == ulpError) && ((ulp != 0.5) || bitsAreEven)); + bool bits_are_even = ((FPBits(libc_result.f).uintval() & 1) == 0); + return (ulp < ulp_error) || + ((ulp == ulp_error) && ((ulp != 0.5) || bits_are_even)); } template bool -compareUnaryOperationTwoOutputs(Operation, float, - const BinaryOutput &, double); -template bool -compareUnaryOperationTwoOutputs(Operation, double, - const BinaryOutput &, double); -template bool compareUnaryOperationTwoOutputs( +compare_unary_operation_two_outputs(Operation, float, + const BinaryOutput &, double); +template bool compare_unary_operation_two_outputs( + Operation, double, const BinaryOutput &, double); +template bool compare_unary_operation_two_outputs( Operation, long double, const BinaryOutput &, double); template -bool compareBinaryOperationTwoOutputs(Operation op, const BinaryInput &input, - const BinaryOutput &libcResult, - double ulpError) { +bool compare_binary_operation_two_outputs(Operation op, + const BinaryInput &input, + const BinaryOutput &libc_result, + double ulp_error) { int mpfrIntResult; - MPFRNumber mpfrResult = - binaryOperationTwoOutputs(op, input.x, input.y, mpfrIntResult); - double ulp = mpfrResult.ulp(libcResult.f); + MPFRNumber mpfr_result = + binary_operation_two_outputs(op, input.x, input.y, mpfrIntResult); + double ulp = mpfr_result.ulp(libc_result.f); - if (mpfrIntResult != libcResult.i) { + if (mpfrIntResult != libc_result.i) { if (op == Operation::RemQuo) { - if ((0x7 & mpfrIntResult) != (0x7 & libcResult.i)) + if ((0x7 & mpfrIntResult) != (0x7 & libc_result.i)) return false; } else { return false; } } - bool bitsAreEven = ((FPBits(libcResult.f).uintval() & 1) == 0); - return (ulp < ulpError) || - ((ulp == ulpError) && ((ulp != 0.5) || bitsAreEven)); + bool bits_are_even = ((FPBits(libc_result.f).uintval() & 1) == 0); + return (ulp < ulp_error) || + ((ulp == ulp_error) && ((ulp != 0.5) || bits_are_even)); } -template bool -compareBinaryOperationTwoOutputs(Operation, const BinaryInput &, - const BinaryOutput &, double); -template bool -compareBinaryOperationTwoOutputs(Operation, const BinaryInput &, - const BinaryOutput &, double); -template bool compareBinaryOperationTwoOutputs( +template bool compare_binary_operation_two_outputs( + Operation, const BinaryInput &, const BinaryOutput &, double); +template bool compare_binary_operation_two_outputs( + Operation, const BinaryInput &, const BinaryOutput &, + double); +template bool compare_binary_operation_two_outputs( Operation, const BinaryInput &, const BinaryOutput &, double); template -bool compareBinaryOperationOneOutput(Operation op, const BinaryInput &input, - T libcResult, double ulpError) { - MPFRNumber mpfrResult = binaryOperationOneOutput(op, input.x, input.y); - double ulp = mpfrResult.ulp(libcResult); - - bool bitsAreEven = ((FPBits(libcResult).uintval() & 1) == 0); - return (ulp < ulpError) || - ((ulp == ulpError) && ((ulp != 0.5) || bitsAreEven)); +bool compare_binary_operation_one_output(Operation op, + const BinaryInput &input, + T libc_result, double ulp_error) { + MPFRNumber mpfr_result = binary_operation_one_output(op, input.x, input.y); + double ulp = mpfr_result.ulp(libc_result); + + bool bits_are_even = ((FPBits(libc_result).uintval() & 1) == 0); + return (ulp < ulp_error) || + ((ulp == ulp_error) && ((ulp != 0.5) || bits_are_even)); } -template bool compareBinaryOperationOneOutput(Operation, - const BinaryInput &, - float, double); -template bool -compareBinaryOperationOneOutput(Operation, const BinaryInput &, - double, double); -template bool compareBinaryOperationOneOutput( +template bool compare_binary_operation_one_output( + Operation, const BinaryInput &, float, double); +template bool compare_binary_operation_one_output( + Operation, const BinaryInput &, double, double); +template bool compare_binary_operation_one_output( Operation, const BinaryInput &, long double, double); template -bool compareTernaryOperationOneOutput(Operation op, - const TernaryInput &input, - T libcResult, double ulpError) { - MPFRNumber mpfrResult = - ternaryOperationOneOutput(op, input.x, input.y, input.z); - double ulp = mpfrResult.ulp(libcResult); - - bool bitsAreEven = ((FPBits(libcResult).uintval() & 1) == 0); - return (ulp < ulpError) || - ((ulp == ulpError) && ((ulp != 0.5) || bitsAreEven)); +bool compare_ternary_operation_one_output(Operation op, + const TernaryInput &input, + T libc_result, double ulp_error) { + MPFRNumber mpfr_result = + ternary_operation_one_output(op, input.x, input.y, input.z); + double ulp = mpfr_result.ulp(libc_result); + + bool bits_are_even = ((FPBits(libc_result).uintval() & 1) == 0); + return (ulp < ulp_error) || + ((ulp == ulp_error) && ((ulp != 0.5) || bits_are_even)); } -template bool -compareTernaryOperationOneOutput(Operation, const TernaryInput &, - float, double); -template bool compareTernaryOperationOneOutput( +template bool compare_ternary_operation_one_output( + Operation, const TernaryInput &, float, double); +template bool compare_ternary_operation_one_output( Operation, const TernaryInput &, double, double); -template bool compareTernaryOperationOneOutput( +template bool compare_ternary_operation_one_output( Operation, const TernaryInput &, long double, double); -static mpfr_rnd_t getMPFRRoundingMode(RoundingMode mode) { +static mpfr_rnd_t get_mpfr_rounding_mode(RoundingMode mode) { switch (mode) { case RoundingMode::Upward: return MPFR_RNDU; @@ -795,33 +799,33 @@ static mpfr_rnd_t getMPFRRoundingMode(RoundingMode mode) { } // namespace internal -template bool RoundToLong(T x, long &result) { +template bool round_to_long(T x, long &result) { MPFRNumber mpfr(x); - return mpfr.roundToLong(result); + return mpfr.roung_to_long(result); } -template bool RoundToLong(float, long &); -template bool RoundToLong(double, long &); -template bool RoundToLong(long double, long &); +template bool round_to_long(float, long &); +template bool round_to_long(double, long &); +template bool round_to_long(long double, long &); -template bool RoundToLong(T x, RoundingMode mode, long &result) { +template bool round_to_long(T x, RoundingMode mode, long &result) { MPFRNumber mpfr(x); - return mpfr.roundToLong(internal::getMPFRRoundingMode(mode), result); + return mpfr.roung_to_long(internal::get_mpfr_rounding_mode(mode), result); } -template bool RoundToLong(float, RoundingMode, long &); -template bool RoundToLong(double, RoundingMode, long &); -template bool RoundToLong(long double, RoundingMode, long &); +template bool round_to_long(float, RoundingMode, long &); +template bool round_to_long(double, RoundingMode, long &); +template bool round_to_long(long double, RoundingMode, long &); -template T Round(T x, RoundingMode mode) { +template T round(T x, RoundingMode mode) { MPFRNumber mpfr(x); - MPFRNumber result = mpfr.rint(internal::getMPFRRoundingMode(mode)); + MPFRNumber result = mpfr.rint(internal::get_mpfr_rounding_mode(mode)); return result.as(); } -template float Round(float, RoundingMode); -template double Round(double, RoundingMode); -template long double Round(long double, RoundingMode); +template float round(float, RoundingMode); +template double round(double, RoundingMode); +template long double round(long double, RoundingMode); } // namespace mpfr } // namespace testing diff --git a/libc/utils/MPFRWrapper/MPFRUtils.h b/libc/utils/MPFRWrapper/MPFRUtils.h index ff35a56231bb1..c52f2a76d9c6e 100644 --- a/libc/utils/MPFRWrapper/MPFRUtils.h +++ b/libc/utils/MPFRWrapper/MPFRUtils.h @@ -98,132 +98,137 @@ namespace internal { template struct AreMatchingBinaryInputAndBinaryOutput { - static constexpr bool value = false; + static constexpr bool VALUE = false; }; template struct AreMatchingBinaryInputAndBinaryOutput, BinaryOutput> { - static constexpr bool value = cpp::IsFloatingPointType::Value; + static constexpr bool VALUE = cpp::IsFloatingPointType::Value; }; template -bool compareUnaryOperationSingleOutput(Operation op, T input, T libcOutput, - double t); +bool compare_unary_operation_single_output(Operation op, T input, T libc_output, + double t); template -bool compareUnaryOperationTwoOutputs(Operation op, T input, - const BinaryOutput &libcOutput, - double t); +bool compare_unary_operation_two_outputs(Operation op, T input, + const BinaryOutput &libc_output, + double t); template -bool compareBinaryOperationTwoOutputs(Operation op, const BinaryInput &input, - const BinaryOutput &libcOutput, - double t); +bool compare_binary_operation_two_outputs(Operation op, + const BinaryInput &input, + const BinaryOutput &libc_output, + double t); template -bool compareBinaryOperationOneOutput(Operation op, const BinaryInput &input, - T libcOutput, double t); +bool compare_binary_operation_one_output(Operation op, + const BinaryInput &input, + T libc_output, double t); template -bool compareTernaryOperationOneOutput(Operation op, - const TernaryInput &input, - T libcOutput, double t); +bool compare_ternary_operation_one_output(Operation op, + const TernaryInput &input, + T libc_output, double t); template -void explainUnaryOperationSingleOutputError(Operation op, T input, T matchValue, - testutils::StreamWrapper &OS); +void explain_unary_operation_single_output_error(Operation op, T input, + T match_value, + testutils::StreamWrapper &OS); template -void explainUnaryOperationTwoOutputsError(Operation op, T input, - const BinaryOutput &matchValue, - testutils::StreamWrapper &OS); +void explain_unary_operation_two_outputs_error( + Operation op, T input, const BinaryOutput &match_value, + testutils::StreamWrapper &OS); template -void explainBinaryOperationTwoOutputsError(Operation op, - const BinaryInput &input, - const BinaryOutput &matchValue, - testutils::StreamWrapper &OS); +void explain_binary_operation_two_outputs_error( + Operation op, const BinaryInput &input, + const BinaryOutput &match_value, testutils::StreamWrapper &OS); template -void explainBinaryOperationOneOutputError(Operation op, - const BinaryInput &input, - T matchValue, - testutils::StreamWrapper &OS); +void explain_binary_operation_one_output_error(Operation op, + const BinaryInput &input, + T match_value, + testutils::StreamWrapper &OS); template -void explainTernaryOperationOneOutputError(Operation op, - const TernaryInput &input, - T matchValue, - testutils::StreamWrapper &OS); +void explain_ternary_operation_one_output_error(Operation op, + const TernaryInput &input, + T match_value, + testutils::StreamWrapper &OS); template class MPFRMatcher : public testing::Matcher { InputType input; - OutputType matchValue; - double ulpTolerance; + OutputType match_value; + double ulp_tolerance; public: - MPFRMatcher(InputType testInput, double ulpTolerance) - : input(testInput), ulpTolerance(ulpTolerance) {} + MPFRMatcher(InputType testInput, double ulp_tolerance) + : input(testInput), ulp_tolerance(ulp_tolerance) {} bool match(OutputType libcResult) { - matchValue = libcResult; - return match(input, matchValue, ulpTolerance); + match_value = libcResult; + return match(input, match_value, ulp_tolerance); } - void explainError(testutils::StreamWrapper &OS) override { - explainError(input, matchValue, OS); + // This method is marked with NOLINT because it the name `explainError` + // does not confirm to the coding style. + void explainError(testutils::StreamWrapper &OS) override { // NOLINT + explain_error(input, match_value, OS); } private: template static bool match(T in, T out, double tolerance) { - return compareUnaryOperationSingleOutput(op, in, out, tolerance); + return compare_unary_operation_single_output(op, in, out, tolerance); } template static bool match(T in, const BinaryOutput &out, double tolerance) { - return compareUnaryOperationTwoOutputs(op, in, out, tolerance); + return compare_unary_operation_two_outputs(op, in, out, tolerance); } template static bool match(const BinaryInput &in, T out, double tolerance) { - return compareBinaryOperationOneOutput(op, in, out, tolerance); + return compare_binary_operation_one_output(op, in, out, tolerance); } template static bool match(BinaryInput in, const BinaryOutput &out, double tolerance) { - return compareBinaryOperationTwoOutputs(op, in, out, tolerance); + return compare_binary_operation_two_outputs(op, in, out, tolerance); } template static bool match(const TernaryInput &in, T out, double tolerance) { - return compareTernaryOperationOneOutput(op, in, out, tolerance); + return compare_ternary_operation_one_output(op, in, out, tolerance); } template - static void explainError(T in, T out, testutils::StreamWrapper &OS) { - explainUnaryOperationSingleOutputError(op, in, out, OS); + static void explain_error(T in, T out, testutils::StreamWrapper &OS) { + explain_unary_operation_single_output_error(op, in, out, OS); } template - static void explainError(T in, const BinaryOutput &out, - testutils::StreamWrapper &OS) { - explainUnaryOperationTwoOutputsError(op, in, out, OS); + static void explain_error(T in, const BinaryOutput &out, + testutils::StreamWrapper &OS) { + explain_unary_operation_two_outputs_error(op, in, out, OS); } template - static void explainError(const BinaryInput &in, const BinaryOutput &out, - testutils::StreamWrapper &OS) { - explainBinaryOperationTwoOutputsError(op, in, out, OS); + static void explain_error(const BinaryInput &in, + const BinaryOutput &out, + testutils::StreamWrapper &OS) { + explain_binary_operation_two_outputs_error(op, in, out, OS); } template - static void explainError(const BinaryInput &in, T out, - testutils::StreamWrapper &OS) { - explainBinaryOperationOneOutputError(op, in, out, OS); + static void explain_error(const BinaryInput &in, T out, + testutils::StreamWrapper &OS) { + explain_binary_operation_one_output_error(op, in, out, OS); } template - static void explainError(const TernaryInput &in, T out, - testutils::StreamWrapper &OS) { - explainTernaryOperationOneOutputError(op, in, out, OS); + static void explain_error(const TernaryInput &in, T out, + testutils::StreamWrapper &OS) { + explain_ternary_operation_one_output_error(op, in, out, OS); } }; @@ -232,7 +237,7 @@ class MPFRMatcher : public testing::Matcher { // Return true if the input and ouput types for the operation op are valid // types. template -constexpr bool isValidOperation() { +constexpr bool is_valid_operation() { return (Operation::BeginUnaryOperationsSingleOutput < op && op < Operation::EndUnaryOperationsSingleOutput && cpp::IsSame::Value && @@ -248,7 +253,7 @@ constexpr bool isValidOperation() { (Operation::BeginBinaryOperationsTwoOutputs < op && op < Operation::EndBinaryOperationsTwoOutputs && internal::AreMatchingBinaryInputAndBinaryOutput::value) || + OutputType>::VALUE) || (Operation::BeginTernaryOperationsSingleOuput < op && op < Operation::EndTernaryOperationsSingleOutput && cpp::IsFloatingPointType::Value && @@ -257,29 +262,29 @@ constexpr bool isValidOperation() { template __attribute__((no_sanitize("address"))) -cpp::EnableIfType(), +cpp::EnableIfType(), internal::MPFRMatcher> -getMPFRMatcher(InputType input, OutputType outputUnused, double t) { +get_mpfr_matcher(InputType input, OutputType output_unused, double t) { return internal::MPFRMatcher(input, t); } enum class RoundingMode : uint8_t { Upward, Downward, TowardZero, Nearest }; -template T Round(T x, RoundingMode mode); +template T round(T x, RoundingMode mode); -template bool RoundToLong(T x, long &result); -template bool RoundToLong(T x, RoundingMode mode, long &result); +template bool round_to_long(T x, long &result); +template bool round_to_long(T x, RoundingMode mode, long &result); } // namespace mpfr } // namespace testing } // namespace __llvm_libc -#define EXPECT_MPFR_MATCH(op, input, matchValue, tolerance) \ - EXPECT_THAT(matchValue, __llvm_libc::testing::mpfr::getMPFRMatcher( \ - input, matchValue, tolerance)) +#define EXPECT_MPFR_MATCH(op, input, match_value, tolerance) \ + EXPECT_THAT(match_value, __llvm_libc::testing::mpfr::get_mpfr_matcher( \ + input, match_value, tolerance)) -#define ASSERT_MPFR_MATCH(op, input, matchValue, tolerance) \ - ASSERT_THAT(matchValue, __llvm_libc::testing::mpfr::getMPFRMatcher( \ - input, matchValue, tolerance)) +#define ASSERT_MPFR_MATCH(op, input, match_value, tolerance) \ + ASSERT_THAT(match_value, __llvm_libc::testing::mpfr::get_mpfr_matcher( \ + input, match_value, tolerance)) #endif // LLVM_LIBC_UTILS_TESTUTILS_MPFRUTILS_H