[libc-commits] [libc] a5466f0 - [libc] Improve the performance of expm1f.

Tue Ly via libc-commits libc-commits at lists.llvm.org
Wed Mar 30 16:23:41 PDT 2022


Author: Tue Ly
Date: 2022-03-30T19:23:25-04:00
New Revision: a5466f0436d2c287a20882cd1beae3e6cf56fea4

URL: https://github.com/llvm/llvm-project/commit/a5466f0436d2c287a20882cd1beae3e6cf56fea4
DIFF: https://github.com/llvm/llvm-project/commit/a5466f0436d2c287a20882cd1beae3e6cf56fea4.diff

LOG: [libc] Improve the performance of expm1f.

Improve the performance of expm1f:
- Rearrange the selection logic for different cases to improve the overall
throughput.
- Use the same degree-4 polynomial for large inputs as `expf`
(https://reviews.llvm.org/D122418), reduced from a degree-7 polynomial.

Performance benchmark using perf tool from CORE-MATH project
(https://gitlab.inria.fr/core-math/core-math/-/tree/master):
Before this patch:
```
$ ./perf.sh expm1f

CORE-MATH reciprocal throughput   : 15.362
System LIBC reciprocal throughput : 53.288
LIBC reciprocal throughput        : 54.572

$ ./perf.sh expm1f --latency

CORE-MATH latency   : 57.759
System LIBC latency : 147.146
LIBC latency        : 118.057
```

After this patch:
```
$ ./perf.sh expm1f

CORE-MATH reciprocal throughput   : 15.359
System LIBC reciprocal throughput : 53.188
LIBC reciprocal throughput        : 14.600

$ ./perf.sh expm1f --latency

CORE-MATH latency   : 57.774
System LIBC latency : 147.119
LIBC latency        : 60.280

```

Reviewed By: michaelrj, santoshn, zimmermann6

Differential Revision: https://reviews.llvm.org/D122538

Added: 
    

Modified: 
    libc/src/math/generic/expm1f.cpp
    libc/test/src/math/exhaustive/exhaustive_test.cpp
    libc/test/src/math/exhaustive/expm1f_test.cpp
    libc/test/src/math/expm1f_test.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/math/generic/expm1f.cpp b/libc/src/math/generic/expm1f.cpp
index 7256c4fd75f56..b0544b76e09ac 100644
--- a/libc/src/math/generic/expm1f.cpp
+++ b/libc/src/math/generic/expm1f.cpp
@@ -24,41 +24,59 @@ LLVM_LIBC_FUNCTION(float, expm1f, (float x)) {
   using FPBits = typename fputil::FPBits<float>;
   FPBits xbits(x);
 
-  // When x < log(2^-25) or nan
-  if (unlikely(xbits.uintval() >= 0xc18a'a123U)) {
-    // exp(-Inf) = 0
-    if (xbits.is_inf())
-      return -1.0f;
-    // exp(nan) = nan
-    if (xbits.is_nan())
-      return x;
+  uint32_t x_u = xbits.uintval();
+  uint32_t x_abs = x_u & 0x7fff'ffffU;
+
+  // Exceptional value
+  if (unlikely(x_u == 0x3e35'bec5U)) { // x = 0x1.6b7d8ap-3f
     int round_mode = fputil::get_round();
-    if (round_mode == FE_UPWARD || round_mode == FE_TOWARDZERO)
-      return -0x1.ffff'fep-1f; // -1.0f + 0x1.0p-24f
-    return -1.0f;
+    if (round_mode == FE_TONEAREST || round_mode == FE_UPWARD)
+      return 0x1.8dbe64p-3f;
+    return 0x1.8dbe62p-3f;
   }
-  // x >= 89 or nan
-  if (unlikely(!xbits.get_sign() && (xbits.uintval() >= 0x42b2'0000))) {
-    if (xbits.uintval() < 0x7f80'0000U) {
-      int rounding = fputil::get_round();
-      if (rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO)
-        return static_cast<float>(FPBits(FPBits::MAX_NORMAL));
 
-      errno = ERANGE;
+  // When |x| > 25*log(2), or nan
+  if (unlikely(x_abs >= 0x418a'a123U)) {
+    // x < log(2^-25)
+    if (xbits.get_sign()) {
+      // exp(-Inf) = 0
+      if (xbits.is_inf())
+        return -1.0f;
+      // exp(nan) = nan
+      if (xbits.is_nan())
+        return x;
+      int round_mode = fputil::get_round();
+      if (round_mode == FE_UPWARD || round_mode == FE_TOWARDZERO)
+        return -0x1.ffff'fep-1f; // -1.0f + 0x1.0p-24f
+      return -1.0f;
+    } else {
+      // x >= 89 or nan
+      if (xbits.uintval() >= 0x42b2'0000) {
+        if (xbits.uintval() < 0x7f80'0000U) {
+          int rounding = fputil::get_round();
+          if (rounding == FE_DOWNWARD || rounding == FE_TOWARDZERO)
+            return static_cast<float>(FPBits(FPBits::MAX_NORMAL));
+
+          errno = ERANGE;
+        }
+        return x + static_cast<float>(FPBits::inf());
+      }
     }
-    return x + static_cast<float>(FPBits::inf());
   }
 
-  int unbiased_exponent = static_cast<int>(xbits.get_unbiased_exponent());
   // |x| < 2^-4
-  if (unbiased_exponent < 123) {
+  if (x_abs < 0x3d80'0000U) {
     // |x| < 2^-25
-    if (unbiased_exponent < 102) {
+    if (x_abs < 0x3300'0000U) {
       // x = -0.0f
       if (unlikely(xbits.uintval() == 0x8000'0000U))
         return x;
-      // When |x| < 2^-25, the relative error:
-      //   |(e^x - 1) - x| / |x| < |x^2| / |x| = |x| < 2^-25 < epsilon(1)/2.
+      // When |x| < 2^-25, the relative error of the approximation e^x - 1 ~ x
+      // is:
+      //   |(e^x - 1) - x| / |e^x - 1| < |x^2| / |x|
+      //                               = |x|
+      //                               < 2^-25
+      //                               < epsilon(1)/2.
       // So the correctly rounded values of expm1(x) are:
       //   = x + eps(x) if rounding mode = FE_UPWARD,
       //                   or (rounding mode = FE_TOWARDZERO and x is negative),
@@ -67,20 +85,21 @@ LLVM_LIBC_FUNCTION(float, expm1f, (float x)) {
       //   fma(x, x, x) ~ x + x^2 instead.
       return fputil::fma(x, x, x);
     }
+
     // 2^-25 <= |x| < 2^-4
     double xd = static_cast<double>(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]);
+    // > P = fpminimax((expm1(x) - x)/x^2, 6, [|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.55555555557ddp-3, 0x1.55555555552fap-5,
+                         0x1.111110fcd58b7p-7, 0x1.6c16c1717660bp-10,
+                         0x1.a0241f0006d62p-13, 0x1.a01e3f8d3c06p-16);
     return static_cast<float>(fputil::fma(r, xsq, xd));
   }
 
-  // For -18 < x < 89, to compute exp(x), we perform the following range
+  // For -18 < x < 89, to compute expm1(x), we perform the following range
   // reduction: find hi, mid, lo such that:
   //   x = hi + mid + lo, in which
   //     hi is an integer,
@@ -89,48 +108,30 @@ LLVM_LIBC_FUNCTION(float, expm1f, (float x)) {
   // In particular,
   //   hi + mid = round(x * 2^7) * 2^(-7).
   // Then,
-  //   exp(x) = exp(hi + mid + lo) = exp(hi) * exp(mid) * exp(lo).
+  //   expm1(x) = exp(hi + mid + lo) - 1 = exp(hi) * exp(mid) * exp(lo) - 1.
   // We store exp(hi) and exp(mid) in the lookup tables EXP_M1 and EXP_M2
-  // respectively.  exp(lo) is computed using a degree-7 minimax polynomial
+  // respectively.  exp(lo) is computed using a degree-4 minimax polynomial
   // generated by Sollya.
 
-  // Exceptional value
-  if (xbits.uintval() == 0xbdc1'c6cbU) {
-    // 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;
-  }
-
   // x_hi = hi + mid.
-  int x_hi = static_cast<int>(x * 0x1.0p7f);
+  int x_hi = static_cast<int>(x * 0x1.0p7f + (xbits.get_sign() ? -0.5f : 0.5f));
   // Subtract (hi + mid) from x to get lo.
   x -= static_cast<float>(x_hi) * 0x1.0p-7f;
   double xd = static_cast<double>(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;
-  // Degree-7 minimax polynomial generated by Sollya with the following
+  // Degree-4 minimax polynomial generated by Sollya with the following
   // commands:
   //   > display = hexadecimal;
-  //   > Q = fpminimax(expm1(x)/x, 6, [|D...|], [-2^-8, 2^-8]);
+  //   > Q = fpminimax(expm1(x)/x, 3, [|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);
+  double exp_lo =
+      fputil::polyeval(xd, 0x1.0p0, 0x1.ffffffffff777p-1, 0x1.000000000071cp-1,
+                       0x1.555566668e5e7p-3, 0x1.55555555ef243p-5);
   return static_cast<float>(fputil::fma(exp_hi_mid, exp_lo, -1.0));
 }
 

diff  --git a/libc/test/src/math/exhaustive/exhaustive_test.cpp b/libc/test/src/math/exhaustive/exhaustive_test.cpp
index 227a9a113ba84..1b713eb92ed98 100644
--- a/libc/test/src/math/exhaustive/exhaustive_test.cpp
+++ b/libc/test/src/math/exhaustive/exhaustive_test.cpp
@@ -15,6 +15,7 @@
 #include <vector>
 
 #include "exhaustive_test.h"
+#include "src/__support/FPUtil/FPBits.h"
 #include "utils/UnitTest/Test.h"
 
 template <typename T>
@@ -28,14 +29,26 @@ void LlvmLibcExhaustiveTest<T>::test_full_range(T start, T stop, int nthreads,
     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<float>(
+                 static_cast<uint32_t>(begin)))
+          << ", "
+          << float(
+                 __llvm_libc::fputil::FPBits<float>(static_cast<uint32_t>(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<float>(
+                 static_cast<uint32_t>(begin)))
+          << ", "
+          << float(
+                 __llvm_libc::fputil::FPBits<float>(static_cast<uint32_t>(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
index 8a4cf762b2695..5131f1de6025e 100644
--- 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 <thread>
+
 using FPBits = __llvm_libc::fputil::FPBits<float>;
 
 namespace mpfr = __llvm_libc::testing::mpfr;
@@ -20,18 +22,19 @@ struct LlvmLibcExpfExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
   bool check(uint32_t start, uint32_t stop,
              mpfr::RoundingMode rounding) override {
     mpfr::ForceRoundingMode r(rounding);
-    uint32_t bits = start;
+    uint32_t bits = stop;
     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);
+    } 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;

diff  --git a/libc/test/src/math/expm1f_test.cpp b/libc/test/src/math/expm1f_test.cpp
index 0971b708b6d86..78be76ec57f73 100644
--- a/libc/test/src/math/expm1f_test.cpp
+++ b/libc/test/src/math/expm1f_test.cpp
@@ -92,6 +92,11 @@ TEST(LlvmLibcExpm1fTest, Borderline) {
   ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Expm1, x,
                                  __llvm_libc::expm1f(x), 0.5);
   EXPECT_MATH_ERRNO(0);
+
+  x = float(FPBits(0x3e35bec5U));
+  ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Expm1, x,
+                                 __llvm_libc::expm1f(x), 0.5);
+  EXPECT_MATH_ERRNO(0);
 }
 
 TEST(LlvmLibcExpm1fTest, InFloatRange) {


        


More information about the libc-commits mailing list