diff --git a/libc/src/math/generic/expm1f.cpp b/libc/src/math/generic/expm1f.cpp --- a/libc/src/math/generic/expm1f.cpp +++ b/libc/src/math/generic/expm1f.cpp @@ -51,7 +51,7 @@ int unbiased_exponent = static_cast(xbits.get_unbiased_exponent()); // |x| < 2^-4 - if (unbiased_exponent < 123) { + if (unbiased_exponent < 122) { // |x| < 2^-25 if (unbiased_exponent < 102) { // x = -0.0f @@ -67,16 +67,21 @@ // fma(x, x, x) ~ x + x^2 instead. return fputil::fma(x, x, x); } - // 2^-25 <= |x| < 2^-4 + // // 2^-25 <= |x| < 2^-4 double xd = static_cast(x); double xsq = xd * xd; // Degree-8 minimax polynomial generated by Sollya with: // > display = hexadecimal; // > P = fpminimax(expm1(x)/x, 7, [|D...|], [-2^-4, 2^-4]); double r = - fputil::polyeval(xd, 0x1p-1, 0x1.55555555559abp-3, 0x1.55555555551a7p-5, - 0x1.111110f70f2a4p-7, 0x1.6c16c17639e82p-10, - 0x1.a02526febbea6p-13, 0x1.a01dc40888fcdp-16); + // fputil::polyeval(xd, 0x1p-1, 0x1.55555555559abp-3, + // 0x1.55555555551a7p-5, + // 0x1.111110f70f2a4p-7, 0x1.6c16c17639e82p-10, + // 0x1.a02526febbea6p-13, 0x1.a01dc40888fcdp-16); + fputil::polyeval(xd, 0x1p-1, 0x1.555555555554ep-3, 0x1.5555555451452p-5, + 0x1.111111118667p-7, 0x1.6c18c98d68ca7p-10, + 0x1.a01ae76480441p-13); + // return static_cast(xd*r); return static_cast(fputil::fma(r, xsq, xd)); } @@ -95,43 +100,40 @@ // generated by Sollya. // Exceptional value - if (xbits.uintval() == 0xbdc1'c6cbU) { + if (x == 0x1.6b7d8ap-3f) { // x = -0x1.838d96p-4f int round_mode = fputil::get_round(); - if (round_mode == FE_TONEAREST || round_mode == FE_DOWNWARD) - return -0x1.71c884p-4f; - return -0x1.71c882p-4f; + if (round_mode == FE_TONEAREST || round_mode == FE_UPWARD) + return 0x1.8dbe64p-3f; + return 0x1.8dbe62p-3f; + // int round_mode = fputil::get_round(); + // if (round_mode == FE_TONEAREST || round_mode == FE_DOWNWARD) + // return -0x1.71c884p-4f; + // return -0x1.71c882p-4f; } // x_hi = hi + mid. - int x_hi = static_cast(x * 0x1.0p7f); + int x_hi = static_cast(x * 0x1.0p7f + (xbits.get_sign() ? -0.5f : 0.5f)); // Subtract (hi + mid) from x to get lo. x -= static_cast(x_hi) * 0x1.0p-7f; double xd = static_cast(x); - // Make sure that -2^(-8) <= lo < 2^-8. - if (x >= 0x1.0p-8f) { - ++x_hi; - xd -= 0x1.0p-7; - } - if (x < -0x1.0p-8f) { - --x_hi; - xd += 0x1.0p-7; - } x_hi += 104 << 7; // hi = x_hi >> 7 double exp_hi = EXP_M1[x_hi >> 7]; // lo = x_hi & 0x0000'007fU; double exp_mid = EXP_M2[x_hi & 0x7f]; double exp_hi_mid = exp_hi * exp_mid; + double expm1_hi_mid = fputil::fma(exp_hi, exp_mid, -1.0); // Degree-7 minimax polynomial generated by Sollya with the following // commands: // > display = hexadecimal; // > Q = fpminimax(expm1(x)/x, 6, [|D...|], [-2^-8, 2^-8]); // > Q; - double exp_lo = fputil::polyeval( - xd, 0x1p0, 0x1p0, 0x1p-1, 0x1.5555555555555p-3, 0x1.55555555553ap-5, - 0x1.1111111204dfcp-7, 0x1.6c16cb2da593ap-10, 0x1.9ff1648996d2ep-13); - return static_cast(fputil::fma(exp_hi_mid, exp_lo, -1.0)); + double expm1_lo = + xd * fputil::polyeval(xd, 0x1.ffffffffff777p-1, 0x1.000000000071cp-1, + 0x1.555566668e5e7p-3, 0x1.55555555ef243p-5); + return static_cast(fputil::fma(exp_hi_mid, expm1_lo, expm1_hi_mid)); + // return static_cast(fputil::fma(exp_hi_mid, expm1_lo, -1.0)); } } // namespace __llvm_libc diff --git a/libc/test/src/math/exhaustive/exhaustive_test.cpp b/libc/test/src/math/exhaustive/exhaustive_test.cpp --- a/libc/test/src/math/exhaustive/exhaustive_test.cpp +++ b/libc/test/src/math/exhaustive/exhaustive_test.cpp @@ -15,6 +15,7 @@ #include #include "exhaustive_test.h" +#include "src/__support/FPUtil/FPBits.h" #include "utils/UnitTest/Test.h" template @@ -28,14 +29,26 @@ thread_list.emplace_back([this, begin, end, rounding]() { std::stringstream msg; msg << "-- Testing from " << begin << " to " << end << " [0x" << std::hex - << begin << ", 0x" << end << ") ..." << std::endl; + << begin << ", 0x" << end << "), [" << std::hexfloat + << float(__llvm_libc::fputil::FPBits( + static_cast(begin))) + << ", " + << float( + __llvm_libc::fputil::FPBits(static_cast(end))) + << ") ..." << std::endl; std::cout << msg.str(); msg.str(""); bool result = check(begin, end, rounding); msg << "** Finished testing from " << std::dec << begin << " to " << end - << " [0x" << std::hex << begin << ", 0x" << end + << " [0x" << std::hex << begin << ", 0x" << end << "), [" + << std::hexfloat + << float(__llvm_libc::fputil::FPBits( + static_cast(begin))) + << ", " + << float( + __llvm_libc::fputil::FPBits(static_cast(end))) << ") : " << (result ? "PASSED" : "FAILED") << std::endl; std::cout << msg.str(); }); diff --git a/libc/test/src/math/exhaustive/expm1f_test.cpp b/libc/test/src/math/exhaustive/expm1f_test.cpp --- a/libc/test/src/math/exhaustive/expm1f_test.cpp +++ b/libc/test/src/math/exhaustive/expm1f_test.cpp @@ -12,6 +12,8 @@ #include "utils/MPFRWrapper/MPFRUtils.h" #include "utils/UnitTest/FPMatcher.h" +#include + using FPBits = __llvm_libc::fputil::FPBits; namespace mpfr = __llvm_libc::testing::mpfr; @@ -20,18 +22,24 @@ bool check(uint32_t start, uint32_t stop, mpfr::RoundingMode rounding) override { mpfr::ForceRoundingMode r(rounding); - uint32_t bits = start; + uint32_t bits = stop; + int tolerance = 2; bool result = true; do { FPBits xbits(bits); float x = float(xbits); result &= EXPECT_MPFR_MATCH(mpfr::Operation::Expm1, x, __llvm_libc::expm1f(x), 0.5, rounding); - } while (bits++ < stop); + if (!result) + --tolerance; + if (tolerance == 0) + break; + } while (bits-- > start); + return result; } }; -static constexpr int NUM_THREADS = 16; +static const int NUM_THREADS = std::thread::hardware_concurrency(); // Range: [0, 89]; static constexpr uint32_t POS_START = 0x0000'0000U;