[libc-commits] [libc] [libc][math] Extend exp_exceptions to float16, bfloat16, float80 and float128 (PR #246998)

Quang Ha via libc-commits libc-commits at lists.llvm.org
Sun Nov 29 09:31:25 PST 2026


https://github.com/quang-ha created https://github.com/llvm/llvm-project/pull/246998

Follow-up to #202503.

## Summary

- The exp overflow/underflow thresholds in `math::check::exp_internal` are now keyed by floating-point format (`fputil::FPType`), not by C++ type. Every type that shares a format uses the same constants, so `exp_exceptions` now works for `float16`, `bfloat16`, `long double` (any layout), native `float128`, and the emulated `fputil::Float80` and `fputil::Float128`.
- New thresholds are added for binary16, bfloat16, x86 80-bit extended and binary128. `Bounds<float>::UPPER_BITS` and `LOWER_BITS`, which `expf` uses, are unchanged.
- The `UPPER`/`LOWER` floating-point value members are dropped from the float/double bounds. Only the old test used them, and their values are kept in comments.
- The test now covers all of these types, plus:
  - the neighbors of UPPER/LOWER (no overflow/underflow);
  - exact flag results;
  - subnormal inputs;
  - non-canonical x86 80-bit encodings (unnormal and pseudo-infinity give `FE_INVALID`).

## How the constants were derived

UPPER = round(log(max_normal), p, RU) and LOWER = round(log(min_normal), p, RD), computed with exact rational arithmetic (Python `decimal`/`fractions` at 250 digits). The code comments give the equivalent Sollya commands. The same script reproduces the existing Sollya-generated float and double constants bit-for-bit.

For every format, the round-to-nearest overflow threshold (exp(x) >= max_normal + ulp/2) and the after-rounding tininess threshold land on the same UPPER/LOWER. So one pair per format is enough, as it is for float/double.

Further checks:
- For bfloat16, libc's `expbf16` was run on all 65,536 inputs in all 4 rounding modes. It starts overflowing exactly at UPPER (`0x42b2`), and its underflow and invalid flags match `exp_exceptions` in every case.
- The binary80 UPPER (`0x400c'b172'17f7'd1cf'79ac`) matches the overflow threshold commonly used for `expl`.

## Question for reviewers

`exp_exceptions` reports no `FE_OVERFLOW` under `FE_DOWNWARD`/`FE_TOWARDZERO`, and libc's `expf` behaves the same way. IEEE 754 does signal overflow in directed rounding modes once RD(exp(x)) > max_normal, i.e. exp(x) >= 2^(emax+1), and `expbf16` raises it there. This PR keeps the existing behavior. Should that be changed in a follow-up?

## Testing

- Windows x86-64 (clang-cl 23.1.6, overlay mode, same configuration as the `windows2022-x86_64` CI job): `libc`, `check-libc-build` and `check-libc` all pass (618/618). `shared_math_check_exp_test` passes 7/7 (float, double, long double, bfloat16, Float80, emulated Float128, x86 encodings).
- `float16`, native `float128` and native x86 80-bit `long double` aren't available on that target. Those paths rely on the Linux x86-64/aarch64 CI jobs.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

>From 00d6706e0628ddc8b0e19937ec61fce383940a34 Mon Sep 17 00:00:00 2001
From: Quang Ha <quangha at mathworks.com>
Date: Sun, 29 Nov 2026 12:24:10 -0500
Subject: [PATCH] [libc][math] Extend exp_exceptions to float16, bfloat16,
 float80 and float128

Key the exp overflow/underflow thresholds by floating-point format
(fputil::FPType) instead of by C++ type, and add thresholds for
binary16, bfloat16, x86 80-bit extended and binary128. All types that
share a format (long double, float128, fputil::Float128,
fputil::Float80) now use the same constants, so exp_exceptions works for
float16, bfloat16, long double, float128 and the emulated Float80 and
Float128 types. Bounds<float>::UPPER_BITS/LOWER_BITS, used by expf, are
unchanged.

The new thresholds are UPPER = round(log(max_normal), p, RU) and
LOWER = round(log(min_normal), p, RD). They were computed with exact
rational arithmetic, which reproduces the existing Sollya-generated
float and double constants bit-for-bit. For every format, the
round-to-nearest overflow threshold and the after-rounding tininess
threshold land on the same values.

The test now covers all of these types, plus the neighbors of
UPPER/LOWER, exact flag results, subnormal inputs, and non-canonical
x86 80-bit encodings (FE_INVALID).

Co-Authored-By: Claude Opus 5.5 <noreply at anthropic.com>
---
 libc/src/__support/math/check/CMakeLists.txt  |  1 +
 .../src/__support/math/check/exp_exceptions.h | 94 +++++++++++++++++--
 libc/test/shared/CMakeLists.txt               |  5 +
 .../shared/shared_math_check_exp_test.cpp     | 74 +++++++++++++--
 4 files changed, 157 insertions(+), 17 deletions(-)

diff --git a/libc/src/__support/math/check/CMakeLists.txt b/libc/src/__support/math/check/CMakeLists.txt
index 31d901f900a8a5..4f8f8f6a364a2d 100644
--- a/libc/src/__support/math/check/CMakeLists.txt
+++ b/libc/src/__support/math/check/CMakeLists.txt
@@ -4,6 +4,7 @@ add_header_library(
     exp_exceptions.h
   DEPENDS
     libc.hdr.fenv_macros
+    libc.hdr.stdint_proxy
     libc.src.__support.FPUtil.fp_bits
     libc.src.__support.common
     libc.src.__support.macros.config
diff --git a/libc/src/__support/math/check/exp_exceptions.h b/libc/src/__support/math/check/exp_exceptions.h
index 16b92cf8110ff6..0f1edc8224c8d7 100644
--- a/libc/src/__support/math/check/exp_exceptions.h
+++ b/libc/src/__support/math/check/exp_exceptions.h
@@ -10,6 +10,7 @@
 #define LLVM_LIBC_SRC___SUPPORT_MATH_CHECK_EXP_EXCEPTIONS_H
 
 #include "hdr/fenv_macros.h"
+#include "hdr/stdint_proxy.h"
 #include "src/__support/FPUtil/FPBits.h"
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
@@ -22,43 +23,116 @@ namespace check {
 
 namespace exp_internal {
 
-template <typename T> struct Bounds;
+// Builds a wide bit pattern from its high and low 64-bit halves.
+template <typename StorageType>
+LIBC_INLINE constexpr StorageType make_bits(uint64_t hi, uint64_t lo) {
+  return (StorageType(hi) << 64) | StorageType(lo);
+}
+
+// Thresholds for exp(x), keyed by the floating-point format, so that all types
+// sharing a format (e.g. long double, float128 and fputil::Float128) use the
+// same constants:
+// - UPPER_BITS: bit pattern of the smallest value that will cause overflow,
+//     UPPER = round(log(max_normal), precision, RU).
+// - LOWER_BITS: bit pattern of the largest value that will cause underflow,
+//     LOWER = round(log(min_normal), precision, RD).
+// For each format, round-to-nearest overflow (exp(x) >= max_normal + ulp/2)
+// and tininess detected after rounding start at the same UPPER and LOWER.
+template <fputil::FPType> struct FormatBounds;
+
+template <> struct FormatBounds<fputil::FPType::IEEE754_Binary16> {
+  // Computed as (Sollya syntax):
+  // > f16_max = 2^15 * (2 - 2^-10);
+  // > upper = round(log(f16_max), 11, RU);
+  // upper = 0x1.63p3
+  static constexpr uint16_t UPPER_BITS = 0x498c;
+  // > f16_min = 2^-14;
+  // > lower = round(log(f16_min), 11, RD);
+  // lower = -0x1.36cp3
+  static constexpr uint16_t LOWER_BITS = 0xc8db;
+};
 
-template <> struct Bounds<float> {
+template <> struct FormatBounds<fputil::FPType::BFloat16> {
+  // Computed as (Sollya syntax):
+  // > bf16_max = 2^127 * (2 - 2^-7);
+  // > upper = round(log(bf16_max), 8, RU);
+  // upper = 0x1.64p6
+  static constexpr uint16_t UPPER_BITS = 0x42b2;
+  // > bf16_min = 2^-126;
+  // > lower = round(log(bf16_min), 8, RD);
+  // lower = -0x1.5ep6
+  static constexpr uint16_t LOWER_BITS = 0xc2af;
+};
+
+template <> struct FormatBounds<fputil::FPType::IEEE754_Binary32> {
   // 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 float UPPER = 0x1.62e43p6f;
+  // upper = 0x1.62e43p6
   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 float LOWER = -0x1.5d58ap6f;
+  // lower = -0x1.5d58ap6
   static constexpr uint32_t LOWER_BITS = 0xc2ae'ac50;
 };
 
-template <> struct Bounds<double> {
+template <> struct FormatBounds<fputil::FPType::IEEE754_Binary64> {
   // 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 double UPPER = 0x1.62e42fefa39fp9;
+  // 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^-1022;
   // > lower = round(log(double_min), D, RD);
-  // > printfloat(lower);
-  static constexpr double LOWER = -0x1.6232bdd7abcd3p9;
+  // > printdouble(lower);
+  // lower = -0x1.6232bdd7abcd3p9
   static constexpr uint64_t LOWER_BITS = 0xc086'232b'dd7a'bcd3;
 };
 
+template <> struct FormatBounds<fputil::FPType::X86_Binary80> {
+  using StorageType = fputil::FPRep<fputil::FPType::X86_Binary80>::StorageType;
+  // Computed as (Sollya syntax):
+  // > f80_max = 2^16383 * (2 - 2^-63);
+  // > upper = round(log(f80_max), 64, RU);
+  // upper = 0x1.62e42fefa39ef358p13
+  static constexpr StorageType UPPER_BITS =
+      make_bits<StorageType>(0x400c, 0xb172'17f7'd1cf'79ac);
+  // > f80_min = 2^-16382;
+  // > lower = round(log(f80_min), 64, RD);
+  // lower = -0x1.62d918ce2421d66p13
+  static constexpr StorageType LOWER_BITS =
+      make_bits<StorageType>(0xc00c, 0xb16c'8c67'1210'eb30);
+};
+
+template <> struct FormatBounds<fputil::FPType::IEEE754_Binary128> {
+  using StorageType =
+      fputil::FPRep<fputil::FPType::IEEE754_Binary128>::StorageType;
+  // Computed as (Sollya syntax):
+  // > f128_max = 2^16383 * (2 - 2^-112);
+  // > upper = round(log(f128_max), 113, RU);
+  // upper = 0x1.62e42fefa39ef35793c7673007e6p13
+  static constexpr StorageType UPPER_BITS =
+      make_bits<StorageType>(0x400c'62e4'2fef'a39e, 0xf357'93c7'6730'07e6);
+  // > f128_min = 2^-16382;
+  // > lower = round(log(f128_min), 113, RD);
+  // lower = -0x1.62d918ce2421d65ff90ac8f4ce66p13
+  static constexpr StorageType LOWER_BITS =
+      make_bits<StorageType>(0xc00c'62d9'18ce'2421, 0xd65f'f90a'c8f4'ce66);
+};
+
+template <typename T> using Bounds = FormatBounds<fputil::get_fp_type<T>()>;
+
 } // namespace exp_internal
 
 template <typename T> LIBC_INLINE int exp_exceptions(T x, int rounding_mode) {
   using FPBits = typename fputil::FPBits<T>;
   using StorageType = typename FPBits::StorageType;
+  using Bounds = exp_internal::Bounds<T>;
 
   FPBits x_bits(x);
   if (x_bits.is_signaling_nan())
@@ -66,11 +140,11 @@ template <typename T> LIBC_INLINE int exp_exceptions(T x, int rounding_mode) {
   if (x_bits.is_inf_or_nan() || x_bits.is_zero())
     return 0;
   StorageType x_u = x_bits.uintval();
-  if (x_u >= exp_internal::Bounds<T>::UPPER_BITS && x_bits.is_pos())
+  if (x_u >= Bounds::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_BITS)
+  if (x_u >= Bounds::LOWER_BITS)
     return FE_UNDERFLOW | FE_INEXACT;
   return FE_INEXACT;
 }
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index be3df9fa6e6ab6..a2598d37ed74a9 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -999,5 +999,10 @@ add_fp_unittest(
   DEPENDS
     libc.hdr.fenv_macros
     libc.src.__support.CPP.array
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.float128
+    libc.src.__support.FPUtil.float80
     libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.macros.properties.types
+    libc.src.__support.math.check.exp_exceptions
 )
diff --git a/libc/test/shared/shared_math_check_exp_test.cpp b/libc/test/shared/shared_math_check_exp_test.cpp
index 0fd1ab087f7f5d..f951bd39743a13 100644
--- a/libc/test/shared/shared_math_check_exp_test.cpp
+++ b/libc/test/shared/shared_math_check_exp_test.cpp
@@ -8,10 +8,17 @@
 
 #include "shared/math_check_exceptions.h"
 #include "src/__support/CPP/array.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/FPUtil/float128.h"
+#include "src/__support/FPUtil/float80.h"
+#include "src/__support/macros/properties/types.h"
 #include "src/__support/math/check/exp_exceptions.h"
 #include "test/UnitTest/FPMatcher.h"
 #include "test/UnitTest/Test.h"
 
+using LIBC_NAMESPACE::fputil::Float128;
+using LIBC_NAMESPACE::fputil::Float80;
+
 template <typename T>
 class CheckExpTest : public LIBC_NAMESPACE::testing::Test {
 
@@ -23,6 +30,15 @@ class CheckExpTest : public LIBC_NAMESPACE::testing::Test {
     using IntArray = LIBC_NAMESPACE::cpp::array<int, 4>;
     using Bounds = LIBC_NAMESPACE::math::check::exp_internal::Bounds<T>;
 
+    const T upper = FPBits(Bounds::UPPER_BITS).get_val();
+    const T lower = FPBits(Bounds::LOWER_BITS).get_val();
+    // The neighbors of UPPER and LOWER towards 0: the largest value that does
+    // not overflow and the smallest value that does not underflow.
+    const T below_upper =
+        FPBits(StorageType(Bounds::UPPER_BITS - StorageType(1))).get_val();
+    const T above_lower =
+        FPBits(StorageType(Bounds::LOWER_BITS - StorageType(1))).get_val();
+
     constexpr IntArray ROUNDING_MODES = {FE_TONEAREST, FE_UPWARD, FE_DOWNWARD,
                                          FE_TOWARDZERO};
     for (auto rm : ROUNDING_MODES) {
@@ -36,8 +52,14 @@ class CheckExpTest : public LIBC_NAMESPACE::testing::Test {
       EXPECT_EQ(0, exp_exceptions(neg_zero, rm));
       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(FE_UNDERFLOW, exp_exceptions(lower, rm) & FE_UNDERFLOW);
       EXPECT_EQ(0, exp_exceptions(T(1), rm) & (~FE_INEXACT));
+
+      EXPECT_EQ(FE_UNDERFLOW | FE_INEXACT, exp_exceptions(lower, rm));
+      EXPECT_EQ(FE_INEXACT, exp_exceptions(above_lower, rm));
+      EXPECT_EQ(FE_INEXACT, exp_exceptions(below_upper, rm));
+      EXPECT_EQ(FE_INEXACT, exp_exceptions(min_denormal, rm));
+      EXPECT_EQ(FE_INEXACT, exp_exceptions(neg_min_denormal, rm));
     }
 
     EXPECT_EQ(FE_OVERFLOW,
@@ -46,17 +68,55 @@ class CheckExpTest : public LIBC_NAMESPACE::testing::Test {
     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,
-              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);
+    EXPECT_EQ(FE_OVERFLOW, exp_exceptions(upper, FE_TONEAREST) & FE_OVERFLOW);
+    EXPECT_EQ(FE_OVERFLOW, exp_exceptions(upper, FE_UPWARD) & FE_OVERFLOW);
+    EXPECT_EQ(0, exp_exceptions(upper, FE_DOWNWARD) & FE_OVERFLOW);
+    EXPECT_EQ(0, exp_exceptions(upper, FE_TOWARDZERO) & FE_OVERFLOW);
+
+    EXPECT_EQ(FE_OVERFLOW | FE_INEXACT, exp_exceptions(upper, FE_TONEAREST));
+    EXPECT_EQ(FE_OVERFLOW | FE_INEXACT, exp_exceptions(upper, FE_UPWARD));
+    EXPECT_EQ(FE_INEXACT, exp_exceptions(upper, FE_DOWNWARD));
+    EXPECT_EQ(FE_INEXACT, exp_exceptions(upper, FE_TOWARDZERO));
   }
 };
 
 using LlvmLibcCheckExpTestFloat = CheckExpTest<float>;
 using LlvmLibcCheckExpTestDouble = CheckExpTest<double>;
+using LlvmLibcCheckExpTestLongDouble = CheckExpTest<long double>;
+using LlvmLibcCheckExpTestBFloat16 = CheckExpTest<bfloat16>;
+using LlvmLibcCheckExpTestFloat80 = CheckExpTest<Float80>;
+using LlvmLibcCheckExpTestEmuFloat128 = CheckExpTest<Float128>;
 
 TEST_F(LlvmLibcCheckExpTestFloat, CheckExpfExceptions) { run_test(); }
 TEST_F(LlvmLibcCheckExpTestDouble, CheckExpExceptions) { run_test(); }
+TEST_F(LlvmLibcCheckExpTestLongDouble, CheckExplExceptions) { run_test(); }
+TEST_F(LlvmLibcCheckExpTestBFloat16, CheckExpbf16Exceptions) { run_test(); }
+TEST_F(LlvmLibcCheckExpTestFloat80, CheckExpf80Exceptions) { run_test(); }
+TEST_F(LlvmLibcCheckExpTestEmuFloat128, CheckExpf128Exceptions) { run_test(); }
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+using LlvmLibcCheckExpTestFloat16 = CheckExpTest<float16>;
+TEST_F(LlvmLibcCheckExpTestFloat16, CheckExpf16Exceptions) { run_test(); }
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#ifdef LIBC_TYPES_FLOAT128_IS_NOT_LONG_DOUBLE
+using LlvmLibcCheckExpTestFloat128 = CheckExpTest<float128>;
+TEST_F(LlvmLibcCheckExpTestFloat128, CheckExpf128Exceptions) { run_test(); }
+#endif // LIBC_TYPES_FLOAT128_IS_NOT_LONG_DOUBLE
+
+// Non-canonical x86 80-bit encodings are invalid operands.
+TEST(LlvmLibcCheckExpTestFloat80Encodings, InvalidEncodings) {
+  using LIBC_NAMESPACE::shared::check::exp_exceptions;
+  using FPBits = LIBC_NAMESPACE::fputil::FPBits<Float80>;
+  using StorageType = FPBits::StorageType;
+  using LIBC_NAMESPACE::math::check::exp_internal::make_bits;
+
+  // Unnormal: non-zero exponent with the explicit bit cleared.
+  const Float80 unnormal =
+      FPBits(make_bits<StorageType>(0x3fff, 0x4000'0000'0000'0000)).get_val();
+  // Pseudo-infinity: maximum exponent with the explicit bit cleared.
+  const Float80 pseudo_inf =
+      FPBits(make_bits<StorageType>(0x7fff, 0)).get_val();
+  EXPECT_EQ(FE_INVALID, exp_exceptions(unnormal, FE_TONEAREST));
+  EXPECT_EQ(FE_INVALID, exp_exceptions(pseudo_inf, FE_TONEAREST));
+}



More information about the libc-commits mailing list