[libc-commits] [libc] [libc][stdfix] fix overflow in EXP_HI table for exphk (PR #240253)

Jaeyong Lee via libc-commits libc-commits at lists.llvm.org
Sun Nov 8 20:13:19 PST 2026


https://github.com/npmania created https://github.com/llvm/llvm-project/pull/240253

In `exphk`, `x_rounded = 5.5` maps to `hi = 6` and `mid = -0.5`, where `exp(6)` overflows `short accum` and clamps `EXP_HI[11]` to `SACCUM_MAX`.

Shift `hi` to `[-6, 5]` and `mid` to `[0, 0.875]`, and tighten the overflow bound from `0x1.64p2hk` to `0x1.63p2hk` so `x = 0x1.638p2hk` does not overflow `short accum`.

>From c135d38ebaf5081a697934336411c0bffe02cbdb Mon Sep 17 00:00:00 2001
From: Jaeyong Lee <jay at glendalab.com>
Date: Mon, 9 Nov 2026 12:35:00 +0900
Subject: [PATCH] [libc][stdfix] fix overflow in EXP_HI table for exphk

In `exphk`, `x_rounded = 5.5` maps to `hi = 6` and `mid = -0.5`, where
`exp(6)` overflows `short accum` and clamps `EXP_HI[11]` to
`SACCUM_MAX`.

Shift `hi` to `[-6, 5]` and `mid` to `[0, 0.875]`, and tighten the
overflow bound from `0x1.64p2hk` to `0x1.63p2hk` so `x = 0x1.638p2hk`
does not overflow `short accum`.
---
 libc/src/stdfix/exphk.cpp      | 41 +++++++++++++++++++---------------
 libc/test/src/stdfix/ExpTest.h |  8 ++++---
 2 files changed, 28 insertions(+), 21 deletions(-)

diff --git a/libc/src/stdfix/exphk.cpp b/libc/src/stdfix/exphk.cpp
index 0b17a4075e8f33..cec0dcf988c20c 100644
--- a/libc/src/stdfix/exphk.cpp
+++ b/libc/src/stdfix/exphk.cpp
@@ -18,22 +18,24 @@ namespace {
 
 // Look up tables for exp(hi) and exp(mid).
 // Generated with Sollya:
-// > for i from 0 to 89 do {
-//     hi = floor(i/8) - 5;
-//     m = i/8 - floor(i/8) - 0.5;
+// > for i from 0 to 11 do {
+//     hi = i - 6;
 //     e_hi = nearestint(exp(hi) * 2^7) * 2^-7;
+//     print(hi, e_hi);
+//   };
+// > for i from 0 to 7 do {
+//     m = i/8;
 //     e_mid = nearestint(exp(m) * 2^7) * 2^-7;
-//     print(hi, e_hi, m, e_mid);
+//     print(m, e_mid);
 //   };
-// Notice that when i = 88 and 89, e_hi will overflow short accum range.
 static constexpr short accum EXP_HI[12] = {
-    0x1.0p-7hk, 0x1.0p-6hk, 0x1.8p-5hk,  0x1.1p-3hk,  0x1.78p-2hk,  0x1.0p0hk,
-    0x1.5cp1hk, 0x1.d9p2hk, 0x1.416p4hk, 0x1.b4dp5hk, 0x1.28d4p7hk, SACCUM_MAX,
+    0x0.0p0hk, 0x1.0p-7hk, 0x1.0p-6hk, 0x1.8p-5hk,  0x1.1p-3hk,  0x1.78p-2hk,
+    0x1.0p0hk, 0x1.5cp1hk, 0x1.d9p2hk, 0x1.416p4hk, 0x1.b4dp5hk, 0x1.28d4p7hk,
 };
 
 static constexpr short accum EXP_MID[8] = {
-    0x1.38p-1hk, 0x1.6p-1hk, 0x1.9p-1hk, 0x1.c4p-1hk,
-    0x1.0p0hk,   0x1.22p0hk, 0x1.48p0hk, 0x1.74p0hk,
+    0x1.0p0hk,  0x1.22p0hk, 0x1.48p0hk, 0x1.74p0hk,
+    0x1.a6p0hk, 0x1.dep0hk, 0x1.0fp1hk, 0x1.33p1hk,
 };
 
 } // anonymous namespace
@@ -42,7 +44,8 @@ LLVM_LIBC_FUNCTION(short accum, exphk, (short accum x)) {
   using FXRep = fixed_point::FXRep<short accum>;
   using StorageType = typename FXRep::StorageType;
   // Output overflow
-  if (LIBC_UNLIKELY(x >= 0x1.64p2hk))
+  //   ceil(log(2^8) * 2^7) * 2^-7
+  if (LIBC_UNLIKELY(x >= 0x1.63p2hk))
     return FXRep::MAX();
   // Lower bound where exp(x) -> 0:
   //   floor(log(2^-8) * 2^7) * 2^-7
@@ -50,7 +53,7 @@ LLVM_LIBC_FUNCTION(short accum, exphk, (short accum x)) {
     return FXRep::ZERO();
 
   // Current range of x:
-  //   -0x1.628p2 <= x <= 0x1.638p2
+  //   -0x1.628p2 <= x <= 0x1.628p2
   // Range reduction:
   //   x = hi + mid + lo,
   // where:
@@ -72,18 +75,20 @@ LLVM_LIBC_FUNCTION(short accum, exphk, (short accum x)) {
   // Range of x_rounded:
   //   x_rounded >= floor((-0x1.628p2 + 0x1.0p-4) * 2^3) * 2^-3
   //              = -0x1.6p2 = -5.5
-  // To get the indices, we shift the values so that it start with 0.
-  // Range of indices:  0 <= indices <= 89
-  StorageType indices = cpp::bit_cast<StorageType>((x_rounded + 0x1.6p2hk) >>
+  //   x_rounded <= floor((0x1.628p2 + 0x1.0p-4) * 2^3) * 2^-3
+  //              = 0x1.6p2 = 5.5
+  // As hi = floor(x_rounded) >= -6, we add 6 (0x1.8p2hk) so that hi + 6 >= 0.
+  // Range of indices:  4 <= indices <= 92
+  StorageType indices = cpp::bit_cast<StorageType>((x_rounded + 0x1.8p2hk) >>
                                                    (FXRep::FRACTION_LEN - 3));
   // So we have the following relation:
-  //   indices = (hi + mid + 44/8) * 8
+  //   indices = (hi + mid + 6) * 8
   // That implies:
-  //   hi + mid = indices/8 - 5.5
+  //   hi + mid = indices/8 - 6
   // So for lookup tables, we can use the upper 4 bits to get:
-  //   exp( floor(indices / 8) - 5 )
+  //   exp( floor(indices / 8) - 6 )
   // and lower 3 bits for:
-  //   exp( (indices - floor(indices)) - 0.5 )
+  //   exp( indices/8 - floor(indices / 8) )
   short accum exp_hi = EXP_HI[indices >> 3];
   short accum exp_mid = EXP_MID[indices & 0x7];
   // exp(x) ~ exp(hi) * exp(mid) * (1 + lo);
diff --git a/libc/test/src/stdfix/ExpTest.h b/libc/test/src/stdfix/ExpTest.h
index 3513c8583caf7a..67a25fbb2902b9 100644
--- a/libc/test/src/stdfix/ExpTest.h
+++ b/libc/test/src/stdfix/ExpTest.h
@@ -41,7 +41,7 @@ template <typename T> class ExpTest : public LIBC_NAMESPACE::testing::Test {
   }
 
   void test_range_with_step(ExpFunc func, T step, bool rel_error) {
-    constexpr int COUNT = 255;
+    constexpr int COUNT = 2047;
     constexpr double ERR = 3.0 * static_cast<double>(eps);
     double x_d = 0.0;
     T x = step;
@@ -50,6 +50,8 @@ template <typename T> class ExpTest : public LIBC_NAMESPACE::testing::Test {
       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());
       double errors = rel_error
                           ? LIBC_NAMESPACE::fputil::abs((y_d / result) - 1.0)
                           : LIBC_NAMESPACE::fputil::abs(y_d - result);
@@ -63,11 +65,11 @@ template <typename T> class ExpTest : public LIBC_NAMESPACE::testing::Test {
   }
 
   void test_positive_range(ExpFunc func) {
-    test_range_with_step(func, T(0x1.0p-6), /*rel_error*/ true);
+    test_range_with_step(func, T(0x1.0p-7), /*rel_error*/ true);
   }
 
   void test_negative_range(ExpFunc func) {
-    test_range_with_step(func, T(-0x1.0p-6), /*rel_error*/ false);
+    test_range_with_step(func, T(-0x1.0p-7), /*rel_error*/ false);
   }
 };
 



More information about the libc-commits mailing list