[libc-commits] [libc] [libc][math] Optimize exp2f and exp10f hot paths (PR #224178)

via libc-commits libc-commits at lists.llvm.org
Fri Sep 25 07:30:59 PDT 2026


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff origin/main HEAD --extensions cpp,h -- libc/test/src/math/performance_testing/exp10f_perf.cpp libc/src/__support/math/exp10f.h libc/src/__support/math/exp2f.h libc/test/src/math/exp10f_test.cpp libc/test/src/math/exp2f_test.cpp libc/test/src/math/performance_testing/exp2f_perf.cpp --diff_from_common_commit
``````````

:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/libc/src/__support/math/exp10f.h b/libc/src/__support/math/exp10f.h
index cbc11a2cf..c34d1836d 100644
--- a/libc/src/__support/math/exp10f.h
+++ b/libc/src/__support/math/exp10f.h
@@ -28,10 +28,8 @@ LIBC_INLINE static float exp10f_mid(float x) {
   using fputil::multiply_add;
   double lo2 = rr.lo * rr.lo;
   double c0 = multiply_add(rr.lo, Exp10Base::COEFFS[0], 1.0);
-  double c1 =
-      multiply_add(rr.lo, Exp10Base::COEFFS[2], Exp10Base::COEFFS[1]);
-  double c2 =
-      multiply_add(rr.lo, Exp10Base::COEFFS[4], Exp10Base::COEFFS[3]);
+  double c1 = multiply_add(rr.lo, Exp10Base::COEFFS[2], Exp10Base::COEFFS[1]);
+  double c2 = multiply_add(rr.lo, Exp10Base::COEFFS[4], Exp10Base::COEFFS[3]);
   double p = multiply_add(lo2, c2, c1);
   // 10^x = 2^(mid + hi) * 10^lo
   //      ~ mh * (1 + COEFFS[0] * lo + ... + COEFFS[4] * lo^5)
@@ -146,11 +144,10 @@ LIBC_INLINE float exp10f(float x) {
     return fputil::multiply_add(x, 0x1.26bb1cp+1f, 1.0f);
   }
 
-  if (LIBC_LIKELY(x_abs > 0x3b9a'209bU &&
-                  (x_u < 0x421a'209bU ||
-                   (xbits.is_neg() && x_u <= 0xc234'9e35U)) &&
-                  x_u != 0x3d14'd956U &&
-                  (x_u & 0x800f'ffffU) != 0))
+  if (LIBC_LIKELY(
+          x_abs > 0x3b9a'209bU &&
+          (x_u < 0x421a'209bU || (xbits.is_neg() && x_u <= 0xc234'9e35U)) &&
+          x_u != 0x3d14'd956U && (x_u & 0x800f'ffffU) != 0))
     return exp10f_mid(x);
 
   return exp10f_slow(x);
diff --git a/libc/src/__support/math/exp2f.h b/libc/src/__support/math/exp2f.h
index b67816500..08bcd5d59 100644
--- a/libc/src/__support/math/exp2f.h
+++ b/libc/src/__support/math/exp2f.h
@@ -64,11 +64,9 @@ LIBC_INLINE float exp2f(float x) {
     double mh = fputil::FPBits<double>(uint64_t(mh_bits)).get_val();
 
     // Degree-5 polynomial approximating (2^x - 1)/x generated by Sollya.
-    constexpr double COEFFS[5] = {0x1.62e42fefa39efp-1,
-                    0x1.ebfbdff8131c4p-3,
-                    0x1.c6b08d7061695p-5,
-                    0x1.3b2b1bee74b2ap-7,
-                    0x1.5d88091198529p-10};
+    constexpr double COEFFS[5] = {0x1.62e42fefa39efp-1, 0x1.ebfbdff8131c4p-3,
+                                  0x1.c6b08d7061695p-5, 0x1.3b2b1bee74b2ap-7,
+                                  0x1.5d88091198529p-10};
     double dx_sq = dx * dx;
     double c1 = fputil::multiply_add(dx, COEFFS[0], 1.0);
     double c2 = fputil::multiply_add(dx, COEFFS[2], COEFFS[1]);

``````````

</details>


https://github.com/llvm/llvm-project/pull/224178


More information about the libc-commits mailing list