[libc-commits] [libc] [libc][stdfix] Fix overflow bound for expk (PR #240301)

Jaeyong Lee via libc-commits libc-commits at lists.llvm.org
Mon Nov 9 09:49:26 PST 2026


https://github.com/npmania updated https://github.com/llvm/llvm-project/pull/240301

>From ef0d8c1b0d9664e3c25644470a790a7da3fa2bec Mon Sep 17 00:00:00 2001
From: Jaeyong Lee <jay at glendalab.com>
Date: Mon, 9 Nov 2026 17:45:46 +0900
Subject: [PATCH 1/4] [libc][stdfix] Fix overflow bound for expk

In `expk`, the overflow bound `0x1.62e4p3k` uses `floor` instead of
`ceil`, clamping `x = 0x1.62e4p3k` to `ACCUM_MAX`.

Update the overflow bound from `0x1.62e4p3k` to `0x1.62e44p3k`, and fix
comment nits as in `exphk`.
---
 libc/src/stdfix/expk.cpp       | 16 ++++++++--------
 libc/test/src/stdfix/ExpTest.h | 16 ++++++++++++----
 2 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/libc/src/stdfix/expk.cpp b/libc/src/stdfix/expk.cpp
index 0851c7fef48d93..7d5331006a7de2 100644
--- a/libc/src/stdfix/expk.cpp
+++ b/libc/src/stdfix/expk.cpp
@@ -51,8 +51,8 @@ LLVM_LIBC_FUNCTION(accum, expk, (accum x)) {
   using FXRep = fixed_point::FXRep<accum>;
   using StorageType = typename FXRep::StorageType;
   // Output overflow
-  // > floor(log(2^16) * 2^15) * 2^-15
-  if (LIBC_UNLIKELY(x >= 0x1.62e4p3k))
+  // > ceil(log(2^16) * 2^15) * 2^-15
+  if (LIBC_UNLIKELY(x >= 0x1.62e44p3k))
     return FXRep::MAX();
   // Lower bound where exp(x) -> 0:
   //   floor(log(2^-16) * 2^15) * 2^-15
@@ -60,7 +60,7 @@ LLVM_LIBC_FUNCTION(accum, expk, (accum x)) {
     return FXRep::ZERO();
 
   // Current range of x:
-  //   -0x1.62e4p3 <= x <= 0x1.62e3cp3
+  //   -0x1.62e4p3 <= x <= 0x1.62e4p3
   // Range reduction:
   //   x = hi + mid + lo,
   // where:
@@ -72,9 +72,9 @@ LLVM_LIBC_FUNCTION(accum, expk, (accum x)) {
   // with relative errors < |lo|^3/2 <= 2^-16.
   //   exp(hi) and exp(mid) are extracted from small lookup tables.
 
-  // Round-to-nearest 1/16, tie-to-(+Int):
+  // Round-to-nearest 1/16, tie-to-(+Inf):
   constexpr accum ONE_THIRTY_SECOND = 0x1.0p-5k;
-  // x_rounded = floor(x + 1/16).
+  // x_rounded = floor((x + 1/32) * 2^4) * 2^-4.
   accum x_rounded = ((x + ONE_THIRTY_SECOND) >> (FXRep::FRACTION_LEN - 4))
                     << (FXRep::FRACTION_LEN - 4);
   accum lo = x - x_rounded;
@@ -90,16 +90,16 @@ LLVM_LIBC_FUNCTION(accum, expk, (accum x)) {
   //   indices = (hi + mid + 177/16) * 16
   // That implies:
   //   hi + mid = indices/16 - 11.0625
-  // So for lookup tables, we can use the upper 4 bits to get:
+  // So for lookup tables, we can use the upper 5 bits to get:
   //   exp( floor(indices / 16) - 11 )
   // and lower 4 bits for:
-  //   exp( (indices - floor(indices)) - 0.0625 )
+  //   exp( indices/16 - floor(indices / 16) - 0.0625 )
   accum exp_hi = EXP_HI[indices >> 4];
   accum exp_mid = EXP_MID[indices & 0xf];
   // exp(x) ~ exp(hi) * exp(mid) * (1 + lo);
   accum l1 = 0x1.0p0k + (lo >> 1); // = 1 + lo / 2
   accum l2 = 0x1.0p0k + lo * l1;   // = 1 + lo * (1 + lo / 2) = 1 + lo + lo^2/2
-  return (exp_hi * (exp_mid * l2));
+  return exp_hi * (exp_mid * l2);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/stdfix/ExpTest.h b/libc/test/src/stdfix/ExpTest.h
index 67a25fbb2902b9..33db9f109ebef8 100644
--- a/libc/test/src/stdfix/ExpTest.h
+++ b/libc/test/src/stdfix/ExpTest.h
@@ -40,18 +40,22 @@ template <typename T> class ExpTest : public LIBC_NAMESPACE::testing::Test {
     EXPECT_EQ(zero, func(T(-30)));
   }
 
-  void test_range_with_step(ExpFunc func, T step, bool rel_error) {
+  void test_range_with_step(ExpFunc func, T step, bool rel_error,
+                            T start = zero) {
     constexpr int COUNT = 2047;
     constexpr double ERR = 3.0 * static_cast<double>(eps);
     double x_d = 0.0;
-    T x = step;
+    T x = start;
     for (int i = 0; i < COUNT; ++i) {
       x += step;
       x_d = static_cast<double>(x);
       double y_d = static_cast<double>(func(x));
       double result = LIBC_NAMESPACE::exp(x_d);
-      if (result > static_cast<double>(FXRep::MAX()))
-        result = static_cast<double>(FXRep::MAX());
+      if (result > static_cast<double>(FXRep::MAX())) {
+        ASSERT_EQ(func(x), FXRep::MAX());
+        continue;
+      }
+      ASSERT_LT(func(x), FXRep::MAX());
       double errors = rel_error
                           ? LIBC_NAMESPACE::fputil::abs((y_d / result) - 1.0)
                           : LIBC_NAMESPACE::fputil::abs(y_d - result);
@@ -66,10 +70,14 @@ template <typename T> class ExpTest : public LIBC_NAMESPACE::testing::Test {
 
   void test_positive_range(ExpFunc func) {
     test_range_with_step(func, T(0x1.0p-7), /*rel_error*/ true);
+    test_range_with_step(func, eps, /*rel_error*/ true);
+    test_range_with_step(func, eps, /*rel_error*/ true,
+                         T(FXRep::INTEGRAL_LEN == 8 ? 0x1.6p2 : 0x1.62p3));
   }
 
   void test_negative_range(ExpFunc func) {
     test_range_with_step(func, T(-0x1.0p-7), /*rel_error*/ false);
+    test_range_with_step(func, -eps, /*rel_error*/ false);
   }
 };
 

>From 523b0cfb432f0b9da417390ca820d7cd9d5b9ea9 Mon Sep 17 00:00:00 2001
From: Jaeyong Lee <jay at glendalab.com>
Date: Tue, 10 Nov 2026 02:29:20 +0900
Subject: [PATCH 2/4] Fix remaining comment nits in expk

---
 libc/src/stdfix/expk.cpp | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/libc/src/stdfix/expk.cpp b/libc/src/stdfix/expk.cpp
index 7d5331006a7de2..3a39dcfeee6307 100644
--- a/libc/src/stdfix/expk.cpp
+++ b/libc/src/stdfix/expk.cpp
@@ -51,7 +51,7 @@ LLVM_LIBC_FUNCTION(accum, expk, (accum x)) {
   using FXRep = fixed_point::FXRep<accum>;
   using StorageType = typename FXRep::StorageType;
   // Output overflow
-  // > ceil(log(2^16) * 2^15) * 2^-15
+  //   ceil(log(2^16) * 2^15) * 2^-15
   if (LIBC_UNLIKELY(x >= 0x1.62e44p3k))
     return FXRep::MAX();
   // Lower bound where exp(x) -> 0:
@@ -82,8 +82,10 @@ LLVM_LIBC_FUNCTION(accum, expk, (accum x)) {
   // Range of x_rounded:
   //   x_rounded >= floor((-0x1.62e4p3 + 0x1.0p-5) * 2^4) * 2^-4
   //              = -0x1.62p3 = -11.0625
+  //   x_rounded <= floor((0x1.62e4p3 + 0x1.0p-5) * 2^4) * 2^-4
+  //              = 0x1.62p3 = 11.0625
   // To get the indices, we shift the values so that it start with 0.
-  // Range of indices: 0 <= indices <= 355.
+  // Range of indices: 0 <= indices <= 354.
   StorageType indices = cpp::bit_cast<StorageType>((x_rounded + 0x1.62p3k) >>
                                                    (FXRep::FRACTION_LEN - 4));
   // So we have the following relation:

>From b3c538c77660d704a94205addbf2cc3c15f5ef05 Mon Sep 17 00:00:00 2001
From: Jaeyong Lee <jay at glendalab.com>
Date: Tue, 10 Nov 2026 02:30:26 +0900
Subject: [PATCH 3/4] Add negative boundary test in ExpTest

---
 libc/test/src/stdfix/ExpTest.h | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libc/test/src/stdfix/ExpTest.h b/libc/test/src/stdfix/ExpTest.h
index 33db9f109ebef8..6b130370850575 100644
--- a/libc/test/src/stdfix/ExpTest.h
+++ b/libc/test/src/stdfix/ExpTest.h
@@ -78,6 +78,8 @@ template <typename T> class ExpTest : public LIBC_NAMESPACE::testing::Test {
   void test_negative_range(ExpFunc func) {
     test_range_with_step(func, T(-0x1.0p-7), /*rel_error*/ false);
     test_range_with_step(func, -eps, /*rel_error*/ false);
+    test_range_with_step(func, -eps, /*rel_error*/ false,
+                         T(FXRep::INTEGRAL_LEN == 8 ? -0x1.6p2 : -0x1.62p3));
   }
 };
 

>From 3befb82f8834c4ad3b5a365660389378c7839126 Mon Sep 17 00:00:00 2001
From: Jaeyong Lee <jay at glendalab.com>
Date: Tue, 10 Nov 2026 02:48:58 +0900
Subject: [PATCH 4/4] Fix another comment nit

---
 libc/src/stdfix/expk.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/stdfix/expk.cpp b/libc/src/stdfix/expk.cpp
index 3a39dcfeee6307..9ce19b3c2bbd07 100644
--- a/libc/src/stdfix/expk.cpp
+++ b/libc/src/stdfix/expk.cpp
@@ -98,7 +98,7 @@ LLVM_LIBC_FUNCTION(accum, expk, (accum x)) {
   //   exp( indices/16 - floor(indices / 16) - 0.0625 )
   accum exp_hi = EXP_HI[indices >> 4];
   accum exp_mid = EXP_MID[indices & 0xf];
-  // exp(x) ~ exp(hi) * exp(mid) * (1 + lo);
+  // exp(x) ~ exp(hi) * exp(mid) * (1 + lo + lo^2 / 2);
   accum l1 = 0x1.0p0k + (lo >> 1); // = 1 + lo / 2
   accum l2 = 0x1.0p0k + lo * l1;   // = 1 + lo * (1 + lo / 2) = 1 + lo + lo^2/2
   return exp_hi * (exp_mid * l2);



More information about the libc-commits mailing list