[libc-commits] [libc] [libc][math] Add shared functions to check exceptions for exp* functions. (PR #202503)
via libc-commits
libc-commits at lists.llvm.org
Mon Jun 8 22:01:55 PDT 2026
https://github.com/lntue updated https://github.com/llvm/llvm-project/pull/202503
>From 20e8e51dd59bb22b6be3832793884f305640d0d7 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Tue, 9 Jun 2026 04:09:57 +0000
Subject: [PATCH 1/3] [libc][math] Add shared functions to check exceptions for
exp* functions.
To be used inside LLVM and other projects.
---
libc/shared/math/check/exp.h | 25 ++++++
libc/shared/math_check_exceptions.h | 16 ++++
.../src/__support/math/check/exp_exceptions.h | 79 +++++++++++++++++++
libc/test/shared/CMakeLists.txt | 12 +++
.../shared/shared_math_check_exp_test.cpp | 57 +++++++++++++
5 files changed, 189 insertions(+)
create mode 100644 libc/shared/math/check/exp.h
create mode 100644 libc/shared/math_check_exceptions.h
create mode 100644 libc/src/__support/math/check/exp_exceptions.h
create mode 100644 libc/test/shared/shared_math_check_exp_test.cpp
diff --git a/libc/shared/math/check/exp.h b/libc/shared/math/check/exp.h
new file mode 100644
index 0000000000000..d805e0bb9613f
--- /dev/null
+++ b/libc/shared/math/check/exp.h
@@ -0,0 +1,25 @@
+//===-- Check exceptions for exp functions ----------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_MATH_CHECK_EXP_H
+#define LLVM_LIBC_SHARED_MATH_CHECK_EXP_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/check/exp_exceptions.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+namespace check {
+
+using math::check::exp_exceptions;
+
+} // namespace check
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_CHECK_EXP_H
diff --git a/libc/shared/math_check_exceptions.h b/libc/shared/math_check_exceptions.h
new file mode 100644
index 0000000000000..2037861ce516d
--- /dev/null
+++ b/libc/shared/math_check_exceptions.h
@@ -0,0 +1,16 @@
+//===-- Check exceptions for floating point math functions ------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SHARED_MATH_CHECK_EXCEPTIONS_H
+#define LLVM_LIBC_SHARED_MATH_CHECK_EXCEPTIONS_H
+
+#include "libc_common.h"
+
+#include "math/check/exp.h"
+
+#endif // LLVM_LIBC_SHARED_MATH_CHECK_EXCEPTIONS_H
diff --git a/libc/src/__support/math/check/exp_exceptions.h b/libc/src/__support/math/check/exp_exceptions.h
new file mode 100644
index 0000000000000..3e05498974d00
--- /dev/null
+++ b/libc/src/__support/math/check/exp_exceptions.h
@@ -0,0 +1,79 @@
+//===-- Check exceptions for exp functions ----------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_CHECK_EXP_EXCEPTIONS_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_CHECK_EXP_EXCEPTIONS_H
+
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+namespace check {
+
+namespace exp_internal {
+
+template <typename T> struct Bounds;
+
+template <> struct Bounds<float> {
+ // Smallest value that will cause overflow, generated from Sollya:
+ // > float_max = 2^127 * (2 - 2^-23);
+ // > upper = round(log(float_max), SG, RU);
+ // > printfloat(upper);
+ static constexpr uint32_t UPPER = 0x42b1'7218;
+ // Largest value that will cause underflow, generated from Sollya:
+ // > float_min = 2^-126;
+ // > lower = round(log(float_min), SG, RD);
+ // > printfloat(lower);
+ static constexpr uint32_t LOWER = 0xc2ae'ac50;
+};
+
+template <> struct Bounds<double> {
+ // Smallest value that will cause overflow, generated from Sollya:
+ // > double_max = 2^1023 * (2 - 2^-52);
+ // > upper = round(log(double_max), D, RU);
+ // > printdouble(upper);
+ static constexpr uint64_t UPPER = 0x4086'2e42'fefa'39f0;
+ // Largest value that will cause underflow, generated from Sollya:
+ // > double_min = 2^-1023;
+ // > lower = round(log(double_min), D, RD);
+ // > printfloat(lower);
+ static constexpr uint64_t LOWER = 0xc086'232b'dd7a'bcd3;
+};
+
+} // namespace exp_internal
+
+template <typename T>
+LIBC_INLINE unsigned exp_exceptions(T x, unsigned rounding_mode) {
+ using FPBits = typename fputil::FPBits<T>;
+ FPBits x_bits(x);
+ if (x_bits.is_signaling_nan())
+ return FE_INVALID;
+ if (x_bits.is_inf_or_nan() || x_bits.is_zero())
+ return 0;
+ FPbits::StorageType x_u = x_bits.uintval();
+ if (x_u >= exp_internal::Bounds<T>::UPPER && x_bits.is_pos())
+ return (rounding_mode == FE_TONEAREST || rounding_mode == FE_UPWARD)
+ ? (FE_OVERFLOW | FE_INEXACT)
+ : FE_INEXACT;
+ if (x_u >= exp_internal::Bounds<T>::LOWER)
+ return FE_UNDERFLOW | FE_INEXACT;
+ return FE_INEXACT;
+}
+
+} // namespace check
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_CHECK_EXP_EXCEPTIONS_H
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 4b34361f0ba43..929bfe1338285 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -842,3 +842,15 @@ add_libc_test(
libc.src.__support.arg_list
)
endif()
+
+add_fp_unittest(
+ shared_math_check_exp_test
+ SUITE
+ libc-shared-tests
+ SRCS
+ shared_math_check_exp_test.cpp
+ DEPENDS
+ libc.hdr.fenv_macros
+ libc.src.__support.CPP.array
+ libc.src.__support.FPUtil.fp_bits
+)
diff --git a/libc/test/shared/shared_math_check_exp_test.cpp b/libc/test/shared/shared_math_check_exp_test.cpp
new file mode 100644
index 0000000000000..9ba41bc271abe
--- /dev/null
+++ b/libc/test/shared/shared_math_check_exp_test.cpp
@@ -0,0 +1,57 @@
+//===-- Unittests for shared math check exception functions ---------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "shared/math_check_exceptions.h"
+#include "src/__support/CPP/array.h"
+#include "src/__support/math/check/exp_exceptions.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+template <typename T>
+class CheckExpTest : public LIBC_NAMESPACE::testing::Test {
+
+ DECLARE_SPECIAL_CONSTANTS(T)
+
+public:
+ void test() {
+ using exp_exceptions = LIBC_NAMESPACE::shared::check::exp_exceptions<T>;
+ using array = LIBC_NAMESPACE::cpp::array;
+ using Bounds = LIBC_NAMESPACE::math::check::exp_internal::Bounds<T>;
+
+ constexpr array<int, 4> ROUNDING_MODES[] = {FE_TONEAREST, FE_UPWARD,
+ FE_DOWNWARD, FE_TOWARDZERO};
+ for (auto rm : ROUNDING_MODES) {
+ EXPECT_EQ(FE_INVALID, exp_exceptions(sNaN, rm));
+ EXPECT_EQ(FE_INVALID, exp_exceptions(neg_sNaN, rm));
+ EXPECT_EQ(0, exp_exceptions(aNaN, rm));
+ EXPECT_EQ(0, exp_exceptions(neg_aNaN, rm));
+ EXPECT_EQ(0, exp_exceptions(inf, rm));
+ EXPECT_EQ(0, exp_exceptions(neg_inf, rm));
+ EXPECT_EQ(0, exp_exceptions(zero, rm));
+ EXPECT_EQ(0, exp_exceptions(neg_zero, rm));
+ EXPECT_EQ(FE_OVERFLOW, exp_exceptions(max_normal, rm) & FE_OVERFLOW);
+ EXPECT_EQ(FE_UNDERFLOW,
+ exp_exceptions(neg_max_normal, rm) & FE_UNDERFLOW);
+ EXPECT_EQ(FE_UNDERFLOW, exp_exceptions(Bounds::LOWER, rm) & FE_UNDERFLOW);
+ EXPECT_EQ(0, exp_exceptions(T(1), rm) & (~FE_INEXACT));
+ }
+
+ EXPECT_EQ(FE_OVERFLOW,
+ exp_exceptions(Bounds::UPPER, FE_TONEAREST) & FE_OVERFLOW);
+ EXPECT_EQ(FE_OVERFLOW,
+ exp_exceptions(Bounds::UPPER, FE_UPWARD) & FE_OVERFLOW);
+ EXPECT_EQ(0, exp_exceptions(Bounds::UPPER, FE_DOWNWARD) & FE_OVERFLOW);
+ EXPECT_EQ(0, exp_exceptions(Bounds::UPPER, FE_TOWARDZERO) & FE_OVERFLOW);
+ }
+};
+
+using LlvmLibcCheckExpTestFloat = CheckExpTest<float>;
+using LlvmLibcCheckExpTestDouble = CheckExpTest<double>;
+
+TEST_F(LlvmLibcCheckExpTestFloat, CheckExpfExceptions) { test(); }
+TEST_F(LlvmLibcCheckExpTestDouble, CheckExpExceptions) { test(); }
>From 24e2e824eac46edae329f7358bb2ced2cfff47d5 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Tue, 9 Jun 2026 04:21:58 +0000
Subject: [PATCH 2/3] Fix typo.
---
libc/src/__support/math/check/exp_exceptions.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/src/__support/math/check/exp_exceptions.h b/libc/src/__support/math/check/exp_exceptions.h
index 3e05498974d00..d22dbfc01b554 100644
--- a/libc/src/__support/math/check/exp_exceptions.h
+++ b/libc/src/__support/math/check/exp_exceptions.h
@@ -60,7 +60,7 @@ LIBC_INLINE unsigned exp_exceptions(T x, unsigned rounding_mode) {
return FE_INVALID;
if (x_bits.is_inf_or_nan() || x_bits.is_zero())
return 0;
- FPbits::StorageType x_u = x_bits.uintval();
+ FPBits::StorageType x_u = x_bits.uintval();
if (x_u >= exp_internal::Bounds<T>::UPPER && x_bits.is_pos())
return (rounding_mode == FE_TONEAREST || rounding_mode == FE_UPWARD)
? (FE_OVERFLOW | FE_INEXACT)
>From 3ce5f381d45e1db320c567a844c778a56e461172 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Tue, 9 Jun 2026 05:01:37 +0000
Subject: [PATCH 3/3] Fix tests.
---
.../src/__support/math/check/exp_exceptions.h | 25 +++++++++++--------
.../shared/shared_math_check_exp_test.cpp | 21 ++++++++++------
2 files changed, 28 insertions(+), 18 deletions(-)
diff --git a/libc/src/__support/math/check/exp_exceptions.h b/libc/src/__support/math/check/exp_exceptions.h
index d22dbfc01b554..16b92cf8110ff 100644
--- a/libc/src/__support/math/check/exp_exceptions.h
+++ b/libc/src/__support/math/check/exp_exceptions.h
@@ -29,12 +29,14 @@ template <> struct Bounds<float> {
// > float_max = 2^127 * (2 - 2^-23);
// > upper = round(log(float_max), SG, RU);
// > printfloat(upper);
- static constexpr uint32_t UPPER = 0x42b1'7218;
+ static constexpr float UPPER = 0x1.62e43p6f;
+ static constexpr uint32_t UPPER_BITS = 0x42b1'7218;
// Largest value that will cause underflow, generated from Sollya:
// > float_min = 2^-126;
// > lower = round(log(float_min), SG, RD);
// > printfloat(lower);
- static constexpr uint32_t LOWER = 0xc2ae'ac50;
+ static constexpr float LOWER = -0x1.5d58ap6f;
+ static constexpr uint32_t LOWER_BITS = 0xc2ae'ac50;
};
template <> struct Bounds<double> {
@@ -42,30 +44,33 @@ template <> struct Bounds<double> {
// > double_max = 2^1023 * (2 - 2^-52);
// > upper = round(log(double_max), D, RU);
// > printdouble(upper);
- static constexpr uint64_t UPPER = 0x4086'2e42'fefa'39f0;
+ static constexpr double UPPER = 0x1.62e42fefa39fp9;
+ static constexpr uint64_t UPPER_BITS = 0x4086'2e42'fefa'39f0;
// Largest value that will cause underflow, generated from Sollya:
- // > double_min = 2^-1023;
+ // > double_min = 2^-1022;
// > lower = round(log(double_min), D, RD);
// > printfloat(lower);
- static constexpr uint64_t LOWER = 0xc086'232b'dd7a'bcd3;
+ static constexpr double LOWER = -0x1.6232bdd7abcd3p9;
+ static constexpr uint64_t LOWER_BITS = 0xc086'232b'dd7a'bcd3;
};
} // namespace exp_internal
-template <typename T>
-LIBC_INLINE unsigned exp_exceptions(T x, unsigned rounding_mode) {
+template <typename T> LIBC_INLINE int exp_exceptions(T x, int rounding_mode) {
using FPBits = typename fputil::FPBits<T>;
+ using StorageType = typename FPBits::StorageType;
+
FPBits x_bits(x);
if (x_bits.is_signaling_nan())
return FE_INVALID;
if (x_bits.is_inf_or_nan() || x_bits.is_zero())
return 0;
- FPBits::StorageType x_u = x_bits.uintval();
- if (x_u >= exp_internal::Bounds<T>::UPPER && x_bits.is_pos())
+ StorageType x_u = x_bits.uintval();
+ if (x_u >= exp_internal::Bounds<T>::UPPER_BITS && x_bits.is_pos())
return (rounding_mode == FE_TONEAREST || rounding_mode == FE_UPWARD)
? (FE_OVERFLOW | FE_INEXACT)
: FE_INEXACT;
- if (x_u >= exp_internal::Bounds<T>::LOWER)
+ if (x_u >= exp_internal::Bounds<T>::LOWER_BITS)
return FE_UNDERFLOW | FE_INEXACT;
return FE_INEXACT;
}
diff --git a/libc/test/shared/shared_math_check_exp_test.cpp b/libc/test/shared/shared_math_check_exp_test.cpp
index 9ba41bc271abe..0fd1ab087f7f5 100644
--- a/libc/test/shared/shared_math_check_exp_test.cpp
+++ b/libc/test/shared/shared_math_check_exp_test.cpp
@@ -18,13 +18,13 @@ class CheckExpTest : public LIBC_NAMESPACE::testing::Test {
DECLARE_SPECIAL_CONSTANTS(T)
public:
- void test() {
- using exp_exceptions = LIBC_NAMESPACE::shared::check::exp_exceptions<T>;
- using array = LIBC_NAMESPACE::cpp::array;
+ void run_test() {
+ using LIBC_NAMESPACE::shared::check::exp_exceptions;
+ using IntArray = LIBC_NAMESPACE::cpp::array<int, 4>;
using Bounds = LIBC_NAMESPACE::math::check::exp_internal::Bounds<T>;
- constexpr array<int, 4> ROUNDING_MODES[] = {FE_TONEAREST, FE_UPWARD,
- FE_DOWNWARD, FE_TOWARDZERO};
+ constexpr IntArray ROUNDING_MODES = {FE_TONEAREST, FE_UPWARD, FE_DOWNWARD,
+ FE_TOWARDZERO};
for (auto rm : ROUNDING_MODES) {
EXPECT_EQ(FE_INVALID, exp_exceptions(sNaN, rm));
EXPECT_EQ(FE_INVALID, exp_exceptions(neg_sNaN, rm));
@@ -34,13 +34,18 @@ class CheckExpTest : public LIBC_NAMESPACE::testing::Test {
EXPECT_EQ(0, exp_exceptions(neg_inf, rm));
EXPECT_EQ(0, exp_exceptions(zero, rm));
EXPECT_EQ(0, exp_exceptions(neg_zero, rm));
- EXPECT_EQ(FE_OVERFLOW, exp_exceptions(max_normal, rm) & FE_OVERFLOW);
EXPECT_EQ(FE_UNDERFLOW,
exp_exceptions(neg_max_normal, rm) & FE_UNDERFLOW);
EXPECT_EQ(FE_UNDERFLOW, exp_exceptions(Bounds::LOWER, rm) & FE_UNDERFLOW);
EXPECT_EQ(0, exp_exceptions(T(1), rm) & (~FE_INEXACT));
}
+ EXPECT_EQ(FE_OVERFLOW,
+ exp_exceptions(max_normal, FE_TONEAREST) & FE_OVERFLOW);
+ EXPECT_EQ(FE_OVERFLOW, exp_exceptions(max_normal, FE_UPWARD) & FE_OVERFLOW);
+ EXPECT_EQ(0, exp_exceptions(max_normal, FE_DOWNWARD) & FE_OVERFLOW);
+ EXPECT_EQ(0, exp_exceptions(max_normal, FE_TOWARDZERO) & FE_OVERFLOW);
+
EXPECT_EQ(FE_OVERFLOW,
exp_exceptions(Bounds::UPPER, FE_TONEAREST) & FE_OVERFLOW);
EXPECT_EQ(FE_OVERFLOW,
@@ -53,5 +58,5 @@ class CheckExpTest : public LIBC_NAMESPACE::testing::Test {
using LlvmLibcCheckExpTestFloat = CheckExpTest<float>;
using LlvmLibcCheckExpTestDouble = CheckExpTest<double>;
-TEST_F(LlvmLibcCheckExpTestFloat, CheckExpfExceptions) { test(); }
-TEST_F(LlvmLibcCheckExpTestDouble, CheckExpExceptions) { test(); }
+TEST_F(LlvmLibcCheckExpTestFloat, CheckExpfExceptions) { run_test(); }
+TEST_F(LlvmLibcCheckExpTestDouble, CheckExpExceptions) { run_test(); }
More information about the libc-commits
mailing list