diff --git a/libc/src/math/generic/logf.cpp b/libc/src/math/generic/logf.cpp --- a/libc/src/math/generic/logf.cpp +++ b/libc/src/math/generic/logf.cpp @@ -9,13 +9,15 @@ #include "src/math/logf.h" #include "common_constants.h" // Lookup table for (1/f) #include "src/__support/FPUtil/BasicOperations.h" +#include "src/__support/FPUtil/FEnvImpl.h" #include "src/__support/FPUtil/FMA.h" #include "src/__support/FPUtil/FPBits.h" #include "src/__support/FPUtil/PolyEval.h" #include "src/__support/common.h" -// This is a correctly-rounded algorithm for log(x) in single precision with -// round-to-nearest, tie-to-even mode from the RLIBM project at: +// This is an algorithm for log(x) in single precision which is correctly +// rounded for all rounding modes, based on the implementation of log(x) from +// the RLIBM project at: // https://people.cs.rutgers.edu/~sn349/rlibm // Step 1 - Range reduction: @@ -101,6 +103,42 @@ constexpr double LOG_2 = 0x1.62e42fefa39efp-1; using FPBits = typename fputil::FPBits; FPBits xbits(x); + + switch (FPBits(x).uintval()) { + case 0x41178febU: + if (fputil::get_round() == FE_TONEAREST) + return 0x1.1fcbcep+1f; + break; + case 0x4c5d65a5U: + if (fputil::get_round() == FE_TONEAREST) + return 0x1.1e0696p+4f; + break; + case 0x65d890d3U: + if (fputil::get_round() == FE_TONEAREST) + return 0x1.a9a3f2p+5f; + break; + case 0x6f31a8ecU: + if (fputil::get_round() == FE_TONEAREST) + return 0x1.08b512p+6f; + break; + case 0x3f800001U: + if (fputil::get_round() == FE_UPWARD) + return 0x1p-23f; + return 0x1.fffffep-24f; + case 0x500ffb03U: + if (fputil::get_round() != FE_UPWARD) + return 0x1.6fdd34p+4f; + break; + case 0x7a17f30aU: + if (fputil::get_round() != FE_UPWARD) + return 0x1.451436p+6f; + break; + case 0x5cd69e88U: + if (fputil::get_round() != FE_UPWARD) + return 0x1.45c146p+5f; + break; + } + int m = 0; if (xbits.uintval() < FPBits::MIN_NORMAL || @@ -130,26 +168,14 @@ double d = static_cast(xbits) - static_cast(f); d *= ONE_OVER_F[f_index]; + double extra_factor = + fputil::fma(static_cast(m), LOG_2, LOG_F[f_index]); + double r = __llvm_libc::fputil::polyeval( - d, 0x1.0000000008169p+0, -0x1.0000004f78405p-1, 0x1.555654d2bc769p-2, - -0x1.00a570d090322p-2, 0x1.e158d823f89cap-3); + d, extra_factor, 0x1.fffffffffffacp-1, -0x1.fffffffef9cb2p-2, + 0x1.5555513bc679ap-2, -0x1.fff4805ea441p-3, 0x1.930180dbde91ap-3); - double extra_factor = - __llvm_libc::fputil::fma(static_cast(m), LOG_2, LOG_F[f_index]); - switch (FPBits(x).uintval()) { - case 0x3f80d19f: - return 0x1.a1e82cp-8f; - case 0x41178feb: - return 0x1.1fcbcep+1f; - case 0x4c5d65a5: - return 0x1.1e0696p+4f; - case 0x65d890d3: - return 0x1.a9a3f2p+5f; - case 0x6f31a8ec: - return 0x1.08b512p+6f; - default: - return static_cast(__llvm_libc::fputil::fma(d, r, extra_factor)); - } + return static_cast(r); } #pragma clang diagnostic pop diff --git a/libc/test/src/math/logf_test.cpp b/libc/test/src/math/logf_test.cpp --- a/libc/test/src/math/logf_test.cpp +++ b/libc/test/src/math/logf_test.cpp @@ -31,13 +31,14 @@ } TEST(LlvmLibcLogfTest, TrickyInputs) { - constexpr int N = 24; + constexpr int N = 28; constexpr uint32_t INPUTS[N] = { 0x3509dcf6U, 0x3bf86ef0U, 0x3ca1c99fU, 0x3d13e105U, 0x3f7ff1f2U, - 0x3f7fffffU, 0x3f800006U, 0x3f800014U, 0x3f80001cU, 0x3f80c777U, - 0x3f80ce72U, 0x3f80d19fU, 0x3f80f7bfU, 0x3f80fcfeU, 0x3f81feb4U, - 0x3f83d731U, 0x3f90cb1dU, 0x3fc55379U, 0x3fd364d7U, 0x41178febU, - 0x4c5d65a5U, 0x4e85f412U, 0x65d890d3U, 0x6f31a8ecU}; + 0x3f7fffffU, 0x3f800001U, 0x3f800006U, 0x3f800014U, 0x3f80001cU, + 0x3f80c777U, 0x3f80ce72U, 0x3f80d19fU, 0x3f80f7bfU, 0x3f80fcfeU, + 0x3f81feb4U, 0x3f83d731U, 0x3f90cb1dU, 0x3fc55379U, 0x3fd364d7U, + 0x41178febU, 0x4c5d65a5U, 0x4e85f412U, 0x500ffb03U, 0x5cd69e88U, + 0x65d890d3U, 0x6f31a8ecU, 0x7a17f30aU}; for (int i = 0; i < N; ++i) { float x = float(FPBits(INPUTS[i])); EXPECT_MPFR_MATCH(mpfr::Operation::Log, x, __llvm_libc::logf(x), 0.5);