[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 00:56:00 PST 2026
https://github.com/npmania created https://github.com/llvm/llvm-project/pull/240301
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 minor header and comment nits as in `exphk`.
Stacked on #240253.
>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 1/3] [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);
}
};
>From c34f7eb7b2c2a0dc85733a5371886ce077894d74 Mon Sep 17 00:00:00 2001
From: Jaeyong Lee <jay at glendalab.com>
Date: Mon, 9 Nov 2026 17:04:08 +0900
Subject: [PATCH 2/3] Fix nits found while reading expk
---
libc/src/stdfix/exphk.cpp | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/libc/src/stdfix/exphk.cpp b/libc/src/stdfix/exphk.cpp
index cec0dcf988c20c..f5e192911aa466 100644
--- a/libc/src/stdfix/exphk.cpp
+++ b/libc/src/stdfix/exphk.cpp
@@ -9,8 +9,9 @@
#include "exphk.h"
#include "src/__support/CPP/bit.h"
#include "src/__support/common.h"
-#include "src/__support/fixed_point/fx_bits.h"
+#include "src/__support/fixed_point/fx_rep.h"
#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
namespace LIBC_NAMESPACE_DECL {
@@ -65,9 +66,9 @@ LLVM_LIBC_FUNCTION(short accum, exphk, (short accum x)) {
// with relative errors < |lo|^2 <= 2^-8.
// exp(hi) and exp(mid) are extracted from small lookup tables.
- // Round-to-nearest 1/8, tie-to-(+Int):
+ // Round-to-nearest 1/8, tie-to-(+Inf):
constexpr short accum ONE_SIXTEENTH = 0x1.0p-4hk;
- // x_rounded = floor(x + 1/16).
+ // x_rounded = floor((x + 1/16) * 2^3) * 2^-3.
short accum x_rounded = ((x + ONE_SIXTEENTH) >> (FXRep::FRACTION_LEN - 3))
<< (FXRep::FRACTION_LEN - 3);
short accum lo = x - x_rounded;
@@ -92,7 +93,7 @@ LLVM_LIBC_FUNCTION(short accum, exphk, (short accum x)) {
short accum exp_hi = EXP_HI[indices >> 3];
short accum exp_mid = EXP_MID[indices & 0x7];
// exp(x) ~ exp(hi) * exp(mid) * (1 + lo);
- return (exp_hi * (exp_mid * (0x1.0p0hk + lo)));
+ return exp_hi * (exp_mid * (0x1.0p0hk + lo));
}
} // namespace LIBC_NAMESPACE_DECL
>From d775379aba527f457f8da9d66a472a3c897734fc 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 3/3] [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
minor header and comment nits as in `exphk`.
---
libc/src/stdfix/expk.cpp | 19 ++++++++++---------
libc/test/src/stdfix/ExpTest.h | 16 ++++++++++++----
2 files changed, 22 insertions(+), 13 deletions(-)
diff --git a/libc/src/stdfix/expk.cpp b/libc/src/stdfix/expk.cpp
index 0851c7fef48d93..9a97ed8c36ec5e 100644
--- a/libc/src/stdfix/expk.cpp
+++ b/libc/src/stdfix/expk.cpp
@@ -9,8 +9,9 @@
#include "expk.h"
#include "src/__support/CPP/bit.h"
#include "src/__support/common.h"
-#include "src/__support/fixed_point/fx_bits.h"
+#include "src/__support/fixed_point/fx_rep.h"
#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
namespace LIBC_NAMESPACE_DECL {
@@ -51,8 +52,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 +61,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 +73,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 +91,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);
}
};
More information about the libc-commits
mailing list