[libc-commits] [libc] [libc][math] Fix low-precision range reporting and rounding (PR #236472)

via libc-commits libc-commits at lists.llvm.org
Thu Nov 5 05:54:01 PST 2026


https://github.com/sriramshastry updated https://github.com/llvm/llvm-project/pull/236472

>From 16077992037a5c5d5f3be6af20888fe7d02a8d93 Mon Sep 17 00:00:00 2001
From: sriramshastry <sriramshastry at gmail.com>
Date: Wed, 28 Oct 2026 16:21:14 +0000
Subject: [PATCH 1/5] [libc][math] Raise overflow for directed finite
 exponential results

Report overflow and inexact before selecting infinity or finite saturation
in the existing large-input exponential and hyperbolic paths.

Preserve current errno choices and infinity-input handling. Extend the
smoke tests to all supported rounding modes, including negative sinh
overflow, and strengthen the existing finite-result flag assertions.

Signed-off-by: sriramshastry <sriramshastry at gmail.com>
---
 libc/test/src/math/smoke/coshf16_test.cpp    | 22 +++++++++++++
 libc/test/src/math/smoke/exp10f16_test.cpp   | 22 +++++++++++++
 libc/test/src/math/smoke/exp10m1f16_test.cpp | 22 +++++++++++++
 libc/test/src/math/smoke/exp2bf16_test.cpp   | 22 +++++++++++++
 libc/test/src/math/smoke/exp2f16_test.cpp    | 22 +++++++++++++
 libc/test/src/math/smoke/exp2m1f16_test.cpp  | 22 +++++++++++++
 libc/test/src/math/smoke/expf16_test.cpp     | 22 +++++++++++++
 libc/test/src/math/smoke/expm1f16_test.cpp   | 22 +++++++++++++
 libc/test/src/math/smoke/sinhf16_test.cpp    | 33 ++++++++++++++++++++
 9 files changed, 209 insertions(+)

diff --git a/libc/test/src/math/smoke/coshf16_test.cpp b/libc/test/src/math/smoke/coshf16_test.cpp
index afe7a67816bb3a..472eff2c7ae9f2 100644
--- a/libc/test/src/math/smoke/coshf16_test.cpp
+++ b/libc/test/src/math/smoke/coshf16_test.cpp
@@ -84,3 +84,25 @@ TEST_F(LlvmLibcCoshf16Test, Overflow) {
       max_normal, LIBC_NAMESPACE::coshf16(x), FE_OVERFLOW | FE_INEXACT);
   EXPECT_MATH_ERRNO(ERANGE);
 }
+
+TEST_F(LlvmLibcCoshf16Test, OverflowStatusAllRoundingModes) {
+  using Bits = LIBC_NAMESPACE::fputil::FPBits<float16>;
+  const float16 x = Bits(uint16_t(0x5000)).get_val();
+  const float16 largest = Bits(uint16_t(0x7bff)).get_val();
+  const float16 infinity = Bits::inf().get_val();
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_NEAREST(
+      infinity, LIBC_NAMESPACE::coshf16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+
+#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_UPWARD(
+      infinity, LIBC_NAMESPACE::coshf16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD(
+      largest, LIBC_NAMESPACE::coshf16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
+      largest, LIBC_NAMESPACE::coshf16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+#endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+}
diff --git a/libc/test/src/math/smoke/exp10f16_test.cpp b/libc/test/src/math/smoke/exp10f16_test.cpp
index c2dffd1be06762..b51bad0110e059 100644
--- a/libc/test/src/math/smoke/exp10f16_test.cpp
+++ b/libc/test/src/math/smoke/exp10f16_test.cpp
@@ -59,3 +59,25 @@ TEST_F(LlvmLibcExp10f16Test, Underflow) {
       FE_UNDERFLOW | FE_INEXACT);
   EXPECT_MATH_ERRNO(ERANGE);
 }
+
+TEST_F(LlvmLibcExp10f16Test, OverflowStatusAllRoundingModes) {
+  using Bits = LIBC_NAMESPACE::fputil::FPBits<float16>;
+  const float16 x = Bits(uint16_t(0x5000)).get_val();
+  const float16 largest = Bits(uint16_t(0x7bff)).get_val();
+  const float16 infinity = Bits::inf().get_val();
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_NEAREST(
+      infinity, LIBC_NAMESPACE::exp10f16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+
+#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_UPWARD(
+      infinity, LIBC_NAMESPACE::exp10f16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD(
+      largest, LIBC_NAMESPACE::exp10f16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
+      largest, LIBC_NAMESPACE::exp10f16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+#endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+}
diff --git a/libc/test/src/math/smoke/exp10m1f16_test.cpp b/libc/test/src/math/smoke/exp10m1f16_test.cpp
index a2f6c2c15a6ee3..1fbd8c257fbcd3 100644
--- a/libc/test/src/math/smoke/exp10m1f16_test.cpp
+++ b/libc/test/src/math/smoke/exp10m1f16_test.cpp
@@ -105,3 +105,25 @@ TEST_F(LlvmLibcExp10m1f16Test, ResultNearNegOne) {
       LIBC_NAMESPACE::fputil::cast<float16>(-0x1.ffcp-1),
       LIBC_NAMESPACE::exp10m1f16(x), FE_INEXACT);
 }
+
+TEST_F(LlvmLibcExp10m1f16Test, OverflowStatusAllRoundingModes) {
+  using Bits = LIBC_NAMESPACE::fputil::FPBits<float16>;
+  const float16 x = Bits(uint16_t(0x5000)).get_val();
+  const float16 largest = Bits(uint16_t(0x7bff)).get_val();
+  const float16 infinity = Bits::inf().get_val();
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_NEAREST(
+      infinity, LIBC_NAMESPACE::exp10m1f16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+
+#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_UPWARD(
+      infinity, LIBC_NAMESPACE::exp10m1f16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD(
+      largest, LIBC_NAMESPACE::exp10m1f16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
+      largest, LIBC_NAMESPACE::exp10m1f16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+#endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+}
diff --git a/libc/test/src/math/smoke/exp2bf16_test.cpp b/libc/test/src/math/smoke/exp2bf16_test.cpp
index c9930fa218b4ce..b429a7fbac6bc3 100644
--- a/libc/test/src/math/smoke/exp2bf16_test.cpp
+++ b/libc/test/src/math/smoke/exp2bf16_test.cpp
@@ -64,3 +64,25 @@ class LlvmLibcExp2bf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest {
 };
 
 TEST_F(LlvmLibcExp2bf16Test, SpecialNumbers) { test_special_numbers(); }
+
+TEST_F(LlvmLibcExp2bf16Test, OverflowStatusAllRoundingModes) {
+  using Bits = LIBC_NAMESPACE::fputil::FPBits<bfloat16>;
+  const bfloat16 x = Bits(uint16_t(0x4380)).get_val();
+  const bfloat16 largest = Bits(uint16_t(0x7f7f)).get_val();
+  const bfloat16 infinity = Bits::inf().get_val();
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_NEAREST(
+      infinity, LIBC_NAMESPACE::exp2bf16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+
+#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_UPWARD(
+      infinity, LIBC_NAMESPACE::exp2bf16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD(
+      largest, LIBC_NAMESPACE::exp2bf16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
+      largest, LIBC_NAMESPACE::exp2bf16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+#endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+}
diff --git a/libc/test/src/math/smoke/exp2f16_test.cpp b/libc/test/src/math/smoke/exp2f16_test.cpp
index 31226292b880a5..07e2e23dcff056 100644
--- a/libc/test/src/math/smoke/exp2f16_test.cpp
+++ b/libc/test/src/math/smoke/exp2f16_test.cpp
@@ -59,3 +59,25 @@ TEST_F(LlvmLibcExp2f16Test, Underflow) {
       FE_UNDERFLOW | FE_INEXACT);
   EXPECT_MATH_ERRNO(ERANGE);
 }
+
+TEST_F(LlvmLibcExp2f16Test, OverflowStatusAllRoundingModes) {
+  using Bits = LIBC_NAMESPACE::fputil::FPBits<float16>;
+  const float16 x = Bits(uint16_t(0x5000)).get_val();
+  const float16 largest = Bits(uint16_t(0x7bff)).get_val();
+  const float16 infinity = Bits::inf().get_val();
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_NEAREST(
+      infinity, LIBC_NAMESPACE::exp2f16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+
+#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_UPWARD(
+      infinity, LIBC_NAMESPACE::exp2f16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD(
+      largest, LIBC_NAMESPACE::exp2f16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
+      largest, LIBC_NAMESPACE::exp2f16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+#endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+}
diff --git a/libc/test/src/math/smoke/exp2m1f16_test.cpp b/libc/test/src/math/smoke/exp2m1f16_test.cpp
index 1e5a5dfc6ced97..01d30bac6b540e 100644
--- a/libc/test/src/math/smoke/exp2m1f16_test.cpp
+++ b/libc/test/src/math/smoke/exp2m1f16_test.cpp
@@ -82,3 +82,25 @@ TEST_F(LlvmLibcExp2m1f16Test, ResultNearNegOne) {
   EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
       -0x1.ffcp-1, LIBC_NAMESPACE::exp2m1f16(x), FE_INEXACT);
 }
+
+TEST_F(LlvmLibcExp2m1f16Test, OverflowStatusAllRoundingModes) {
+  using Bits = LIBC_NAMESPACE::fputil::FPBits<float16>;
+  const float16 x = Bits(uint16_t(0x5000)).get_val();
+  const float16 largest = Bits(uint16_t(0x7bff)).get_val();
+  const float16 infinity = Bits::inf().get_val();
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_NEAREST(
+      infinity, LIBC_NAMESPACE::exp2m1f16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+
+#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_UPWARD(
+      infinity, LIBC_NAMESPACE::exp2m1f16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD(
+      largest, LIBC_NAMESPACE::exp2m1f16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
+      largest, LIBC_NAMESPACE::exp2m1f16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+#endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+}
diff --git a/libc/test/src/math/smoke/expf16_test.cpp b/libc/test/src/math/smoke/expf16_test.cpp
index 6334a31e6c3976..aa0d4314a3dda7 100644
--- a/libc/test/src/math/smoke/expf16_test.cpp
+++ b/libc/test/src/math/smoke/expf16_test.cpp
@@ -59,3 +59,25 @@ TEST_F(LlvmLibcExpf16Test, Underflow) {
       FE_UNDERFLOW | FE_INEXACT);
   EXPECT_MATH_ERRNO(ERANGE);
 }
+
+TEST_F(LlvmLibcExpf16Test, OverflowStatusAllRoundingModes) {
+  using Bits = LIBC_NAMESPACE::fputil::FPBits<float16>;
+  const float16 x = Bits(uint16_t(0x5000)).get_val();
+  const float16 largest = Bits(uint16_t(0x7bff)).get_val();
+  const float16 infinity = Bits::inf().get_val();
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_NEAREST(
+      infinity, LIBC_NAMESPACE::expf16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+
+#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_UPWARD(
+      infinity, LIBC_NAMESPACE::expf16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD(
+      largest, LIBC_NAMESPACE::expf16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
+      largest, LIBC_NAMESPACE::expf16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+#endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+}
diff --git a/libc/test/src/math/smoke/expm1f16_test.cpp b/libc/test/src/math/smoke/expm1f16_test.cpp
index 6cb6def4ed243c..976c7202a99dea 100644
--- a/libc/test/src/math/smoke/expm1f16_test.cpp
+++ b/libc/test/src/math/smoke/expm1f16_test.cpp
@@ -103,3 +103,25 @@ TEST_F(LlvmLibcExpm1f16Test, ResultNearNegOne) {
       LIBC_NAMESPACE::fputil::cast<float16>(-0x1.ffcp-1),
       LIBC_NAMESPACE::expm1f16(x), FE_INEXACT);
 }
+
+TEST_F(LlvmLibcExpm1f16Test, OverflowStatusAllRoundingModes) {
+  using Bits = LIBC_NAMESPACE::fputil::FPBits<float16>;
+  const float16 x = Bits(uint16_t(0x5000)).get_val();
+  const float16 largest = Bits(uint16_t(0x7bff)).get_val();
+  const float16 infinity = Bits::inf().get_val();
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_NEAREST(
+      infinity, LIBC_NAMESPACE::expm1f16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+
+#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_UPWARD(
+      infinity, LIBC_NAMESPACE::expm1f16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD(
+      largest, LIBC_NAMESPACE::expm1f16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
+      largest, LIBC_NAMESPACE::expm1f16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+#endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+}
diff --git a/libc/test/src/math/smoke/sinhf16_test.cpp b/libc/test/src/math/smoke/sinhf16_test.cpp
index 45b14068c48127..7de2bf355f6c59 100644
--- a/libc/test/src/math/smoke/sinhf16_test.cpp
+++ b/libc/test/src/math/smoke/sinhf16_test.cpp
@@ -82,3 +82,36 @@ TEST_F(LlvmLibcSinhf16Test, Overflow) {
       neg_max_normal, LIBC_NAMESPACE::sinhf16(x), FE_OVERFLOW | FE_INEXACT);
   EXPECT_MATH_ERRNO(ERANGE);
 }
+
+TEST_F(LlvmLibcSinhf16Test, OverflowStatusAllRoundingModes) {
+  using Bits = LIBC_NAMESPACE::fputil::FPBits<float16>;
+  const float16 x = Bits(uint16_t(0x5000)).get_val();
+  const float16 largest = Bits(uint16_t(0x7bff)).get_val();
+  const float16 infinity = Bits::inf().get_val();
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_NEAREST(
+      infinity, LIBC_NAMESPACE::sinhf16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+
+#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_UPWARD(
+      infinity, LIBC_NAMESPACE::sinhf16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD(
+      largest, LIBC_NAMESPACE::sinhf16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
+      largest, LIBC_NAMESPACE::sinhf16(x), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+#endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+
+#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+  const float16 negative = Bits(uint16_t(0xd000)).get_val();
+  const float16 lowest = Bits(uint16_t(0xfbff)).get_val();
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_UPWARD(
+      lowest, LIBC_NAMESPACE::sinhf16(negative), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
+      lowest, LIBC_NAMESPACE::sinhf16(negative), FE_OVERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+#endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+}

>From 1189b88d67fab7188160b75b5c64437089b684eb Mon Sep 17 00:00:00 2001
From: sriramshastry <sriramshastry at gmail.com>
Date: Wed, 28 Oct 2026 16:21:14 +0000
Subject: [PATCH 2/5] [libc][math] Avoid spurious atanbf16 range exceptions

Delay squaring until a polynomial path needs it. Evaluate the small
correction in double and use the signed pi/2 rounding region for input
magnitudes at least 4096.

Add result and range-exception regressions with the required fenv
dependency. No performance change is claimed.

Signed-off-by: sriramshastry <sriramshastry at gmail.com>
---
 libc/src/__support/math/atanbf16.h         | 17 ++++++--
 libc/test/src/math/smoke/CMakeLists.txt    |  1 +
 libc/test/src/math/smoke/atanbf16_test.cpp | 46 ++++++++++++++++++++++
 3 files changed, 61 insertions(+), 3 deletions(-)

diff --git a/libc/src/__support/math/atanbf16.h b/libc/src/__support/math/atanbf16.h
index 563b8325b84285..796420a6cf4b62 100644
--- a/libc/src/__support/math/atanbf16.h
+++ b/libc/src/__support/math/atanbf16.h
@@ -58,7 +58,6 @@ LIBC_INLINE bfloat16 atanbf16(bfloat16 x) {
   };
 
   float xf = x;
-  float x_sq = xf * xf;
 
   // |x| <= 1
   if (x_abs <= 0x3f80) {
@@ -67,9 +66,13 @@ LIBC_INLINE bfloat16 atanbf16(bfloat16 x) {
       return x;
 
     // For smaller x
-    if (LIBC_UNLIKELY(x_abs <= 0x3db8))
-      return fputil::cast<bfloat16>(fputil::multiply_add(xf, -0x1p-9f, xf));
+    if (LIBC_UNLIKELY(x_abs <= 0x3db8)) {
+      // Keep the small correction normal even for the smallest bfloat16 input.
+      double xd = xf;
+      return fputil::cast<bfloat16>(fputil::multiply_add(xd, -0x1p-9, xd));
+    }
 
+    float x_sq = xf * xf;
     float result = atan_eval(x_sq);
     return fputil::cast<bfloat16>(fputil::multiply_add(xf * x_sq, result, xf));
   }
@@ -88,6 +91,14 @@ LIBC_INLINE bfloat16 atanbf16(bfloat16 x) {
     return fputil::cast<bfloat16>(sign * PI_2);
   }
 
+  // For |x| >= 2^12, atan(|x|) lies strictly between 0x1.92p0 and pi/2.
+  // This interval has the same bfloat16 rounding as pi/2 in every mode.
+  // Avoid squaring large inputs or forming tiny reciprocal intermediates.
+  if (LIBC_UNLIKELY(x_abs >= 0x4580))
+    return fputil::cast<bfloat16>(sign * PI_2);
+
+  float x_sq = xf * xf;
+
   // If |x| > 1:
   // atan(x) = sign(x) * (pi/2 - atan(1/|x|))
   // Since 1/|x| < 1, we can use the same polynomial.
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index b00f01371bedc0..a54c85d6477baf 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -5378,6 +5378,7 @@ add_fp_unittest(
     libc.src.math.atanbf16
     libc.hdr.errno_macros
     libc.hdr.fenv_macros
+    libc.src.__support.FPUtil.fenv_impl
     libc.src.__support.FPUtil.bfloat16
 )
 
diff --git a/libc/test/src/math/smoke/atanbf16_test.cpp b/libc/test/src/math/smoke/atanbf16_test.cpp
index 36b0fd69566b6d..4df1931b43fc3d 100644
--- a/libc/test/src/math/smoke/atanbf16_test.cpp
+++ b/libc/test/src/math/smoke/atanbf16_test.cpp
@@ -6,6 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "src/__support/FPUtil/FEnvImpl.h"
 #include "src/__support/FPUtil/bfloat16.h"
 #include "src/math/atanbf16.h"
 #include "test/UnitTest/FEnvSafeTest.h"
@@ -47,3 +48,48 @@ class LlvmLibcAtanBf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest {
   }
 };
 TEST_F(LlvmLibcAtanBf16Test, SpecialNumbers) { test_special_numbers(); }
+
+TEST_F(LlvmLibcAtanBf16Test, NoSpuriousRangeExceptions) {
+  using Bits = LIBC_NAMESPACE::fputil::FPBits<bfloat16>;
+  using namespace LIBC_NAMESPACE::fputil::testing;
+#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+  constexpr RoundingMode MODES[] = {RoundingMode::Nearest};
+#else
+  constexpr RoundingMode MODES[] = {RoundingMode::Nearest, RoundingMode::Upward,
+                                    RoundingMode::Downward,
+                                    RoundingMode::TowardZero};
+#endif // LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+  // Each result is normal, including the tiny inputs that used to underflow
+  // while squaring x or computing the small correction in float.
+  constexpr uint16_t INPUTS[] = {0x0100, 0x2000, 0x3c00,
+                                 0x4580, 0x5f80, 0x7f7f};
+  for (RoundingMode mode : MODES) {
+    ForceRoundingMode rounding(mode);
+    if (!rounding.success)
+      continue;
+    for (uint16_t magnitude : INPUTS) {
+      constexpr uint16_t SIGNS[] = {0, 0x8000};
+      for (uint16_t sign : SIGNS) {
+        bool negative = sign != 0;
+        uint16_t expected = magnitude < 0x4580 ? magnitude : uint16_t(0x3fc9);
+        if (magnitude < 0x4580) {
+          if (mode == RoundingMode::TowardZero ||
+              (negative && mode == RoundingMode::Upward) ||
+              (!negative && mode == RoundingMode::Downward))
+            --expected;
+        } else if ((negative && mode == RoundingMode::Downward) ||
+                   (!negative && mode == RoundingMode::Upward)) {
+          ++expected;
+        }
+        LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
+        bfloat16 result = LIBC_NAMESPACE::atanbf16(
+            Bits(uint16_t(magnitude | sign)).get_val());
+        EXPECT_FP_EQ(Bits(uint16_t(expected | sign)).get_val(), result);
+        EXPECT_EQ(
+            LIBC_NAMESPACE::fputil::test_except(FE_OVERFLOW | FE_UNDERFLOW), 0);
+        EXPECT_MATH_ERRNO(0);
+        LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
+      }
+    }
+  }
+}

>From 8e29e9f6174a2d4a095de843a620b9f2da6a2511 Mon Sep 17 00:00:00 2001
From: sriramshastry <sriramshastry at gmail.com>
Date: Wed, 28 Oct 2026 16:21:15 +0000
Subject: [PATCH 3/5] [libc][math] Fix bfloat16 exponential underflow and tiny
 input paths

Return minimum subnormal for finite far-negative expbf16 inputs under
upward rounding. Preserve the -92.5 nearest result in nearest-only builds.

Use double intermediates on the tiny-input expbf16 and exp2bf16 paths
to avoid spurious float-intermediate range exceptions. Add value and
range-flag regressions with explicit fenv dependencies.

Signed-off-by: sriramshastry <sriramshastry at gmail.com>
---
 libc/src/__support/math/exp2bf16.h         |  8 ++++
 libc/src/__support/math/expbf16.h          |  6 +++
 libc/test/src/math/smoke/CMakeLists.txt    |  2 +
 libc/test/src/math/smoke/exp2bf16_test.cpp | 40 ++++++++++++++++
 libc/test/src/math/smoke/expbf16_test.cpp  | 55 ++++++++++++++++++++++
 5 files changed, 111 insertions(+)

diff --git a/libc/src/__support/math/exp2bf16.h b/libc/src/__support/math/exp2bf16.h
index ee46e63cffe8de..1dcd05b82f3e1d 100644
--- a/libc/src/__support/math/exp2bf16.h
+++ b/libc/src/__support/math/exp2bf16.h
@@ -105,6 +105,14 @@ LIBC_INLINE bfloat16 exp2bf16(bfloat16 x) {
     }
   }
 
+  // This interval rounds like 1 + x * log(2) in every bfloat16 mode.
+  // Double intermediates avoid underflow in the float polynomial for tiny x.
+  if (LIBC_UNLIKELY(x_bits.abs().uintval() <= 0x3980U)) {
+    double xd = static_cast<float>(x);
+    return fputil::cast<bfloat16>(
+        fputil::multiply_add(xd, 0x1.62e42fefa39efp-1, 1.0));
+  }
+
   // For inputs not handled above in -134.0 < x < 128.0,
   // we perform the following range reduction:
   // x = hi + mid + lo
diff --git a/libc/src/__support/math/expbf16.h b/libc/src/__support/math/expbf16.h
index 4d7751eb975017..c909e31ec8be69 100644
--- a/libc/src/__support/math/expbf16.h
+++ b/libc/src/__support/math/expbf16.h
@@ -64,6 +64,12 @@ LIBC_INLINE bfloat16 expbf16(bfloat16 x) {
   uint16_t x_u = x_bits.uintval();
   uint16_t x_abs = x_u & 0x7fffU;
 
+  // For |x| <= 2^-12, exp(x) rounds like 1 + x in bfloat16.
+  // Use double so tiny products in the float polynomial cannot underflow.
+  if (LIBC_UNLIKELY(x_abs <= 0x3980U))
+    return fputil::cast<bfloat16>(1.0 +
+                                  static_cast<double>(static_cast<float>(x)));
+
   // 0 <= |x| <= 2^(-3), or |x| >= 89, or x is NaN
   if (LIBC_UNLIKELY(x_abs <= 0x3e00U || x_abs >= 0x42b2U)) {
 
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index a54c85d6477baf..6d67a0191091d3 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -1502,6 +1502,7 @@ add_fp_unittest(
     libc.hdr.errno_macros
     libc.hdr.fenv_macros
     libc.src.math.expbf16
+    libc.src.__support.FPUtil.fenv_impl
     libc.src.__support.FPUtil.bfloat16
     libc.src.__support.FPUtil.cast
 )
@@ -1547,6 +1548,7 @@ add_fp_unittest(
     libc.hdr.errno_macros
     libc.hdr.fenv_macros
     libc.src.math.exp2bf16
+    libc.src.__support.FPUtil.fenv_impl
     libc.src.__support.FPUtil.bfloat16
 )
 
diff --git a/libc/test/src/math/smoke/exp2bf16_test.cpp b/libc/test/src/math/smoke/exp2bf16_test.cpp
index b429a7fbac6bc3..e6bb78bba6560c 100644
--- a/libc/test/src/math/smoke/exp2bf16_test.cpp
+++ b/libc/test/src/math/smoke/exp2bf16_test.cpp
@@ -13,12 +13,52 @@
 
 #include "hdr/errno_macros.h"
 #include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
 #include "src/__support/FPUtil/bfloat16.h"
 #include "src/math/exp2bf16.h"
 #include "test/UnitTest/FEnvSafeTest.h"
 #include "test/UnitTest/FPMatcher.h"
 #include "test/UnitTest/Test.h"
 
+using LlvmLibcExp2bf16RangeTest = LIBC_NAMESPACE::testing::FEnvSafeTest;
+
+TEST_F(LlvmLibcExp2bf16RangeTest, BfloatRangeEdges) {
+  using Bits = LIBC_NAMESPACE::fputil::FPBits<bfloat16>;
+  using namespace LIBC_NAMESPACE::fputil::testing;
+  constexpr uint16_t INPUTS[] = {0x0001, 0x0080, 0x0100, 0x3980};
+#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+  constexpr RoundingMode MODES[] = {RoundingMode::Nearest};
+#else
+  constexpr RoundingMode MODES[] = {RoundingMode::Nearest, RoundingMode::Upward,
+                                    RoundingMode::Downward,
+                                    RoundingMode::TowardZero};
+#endif
+  for (RoundingMode mode : MODES) {
+    ForceRoundingMode rounding(mode);
+    if (!rounding.success)
+      continue;
+    for (uint16_t magnitude : INPUTS) {
+      constexpr uint16_t SIGNS[] = {0, 0x8000};
+      for (uint16_t sign : SIGNS) {
+        uint16_t expected = 0x3f80;
+        if (sign == 0 && mode == RoundingMode::Upward)
+          ++expected;
+        if (sign != 0 && (mode == RoundingMode::Downward ||
+                          mode == RoundingMode::TowardZero))
+          --expected;
+        LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
+        bfloat16 result = LIBC_NAMESPACE::exp2bf16(
+            Bits(uint16_t(magnitude | sign)).get_val());
+        EXPECT_FP_EQ(Bits(expected).get_val(), result);
+        EXPECT_EQ(
+            LIBC_NAMESPACE::fputil::test_except(FE_OVERFLOW | FE_UNDERFLOW), 0);
+        EXPECT_MATH_ERRNO(0);
+        LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
+      }
+    }
+  }
+}
+
 class LlvmLibcExp2bf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest {
   DECLARE_SPECIAL_CONSTANTS(bfloat16)
 
diff --git a/libc/test/src/math/smoke/expbf16_test.cpp b/libc/test/src/math/smoke/expbf16_test.cpp
index 67e04916d1710e..81787afb0d8a5e 100644
--- a/libc/test/src/math/smoke/expbf16_test.cpp
+++ b/libc/test/src/math/smoke/expbf16_test.cpp
@@ -8,12 +8,67 @@
 
 #include "hdr/errno_macros.h"
 #include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
 #include "src/__support/FPUtil/bfloat16.h"
 #include "src/__support/FPUtil/cast.h"
 #include "src/math/expbf16.h"
 #include "test/UnitTest/FEnvSafeTest.h"
 #include "test/UnitTest/FPMatcher.h"
 
+using LlvmLibcExpbf16RangeTest = LIBC_NAMESPACE::testing::FEnvSafeTest;
+
+TEST_F(LlvmLibcExpbf16RangeTest, BfloatRangeEdges) {
+  using Bits = LIBC_NAMESPACE::fputil::FPBits<bfloat16>;
+  using namespace LIBC_NAMESPACE::fputil::testing;
+  constexpr uint16_t INPUTS[] = {0x0001, 0x0080, 0x0100, 0x3980};
+#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+  constexpr RoundingMode MODES[] = {RoundingMode::Nearest};
+#else
+  constexpr RoundingMode MODES[] = {RoundingMode::Nearest, RoundingMode::Upward,
+                                    RoundingMode::Downward,
+                                    RoundingMode::TowardZero};
+#endif
+  for (RoundingMode mode : MODES) {
+    ForceRoundingMode rounding(mode);
+    if (!rounding.success)
+      continue;
+    for (uint16_t magnitude : INPUTS) {
+      constexpr uint16_t SIGNS[] = {0, 0x8000};
+      for (uint16_t sign : SIGNS) {
+        uint16_t expected = 0x3f80;
+        if (sign == 0 && mode == RoundingMode::Upward)
+          ++expected;
+        if (sign != 0 && (mode == RoundingMode::Downward ||
+                          mode == RoundingMode::TowardZero))
+          --expected;
+        LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
+        bfloat16 result =
+            LIBC_NAMESPACE::expbf16(Bits(uint16_t(magnitude | sign)).get_val());
+        EXPECT_FP_EQ(Bits(expected).get_val(), result);
+        EXPECT_EQ(
+            LIBC_NAMESPACE::fputil::test_except(FE_OVERFLOW | FE_UNDERFLOW), 0);
+        EXPECT_MATH_ERRNO(0);
+        LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
+      }
+    }
+  }
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_NEAREST(
+      Bits::min_subnormal().get_val(),
+      LIBC_NAMESPACE::expbf16(Bits(uint16_t(0xc2b9)).get_val()),
+      FE_UNDERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+  constexpr uint16_t FAR_INPUTS[] = {0xc2ba, 0xc300, 0xff7f};
+  for (uint16_t bits : FAR_INPUTS) {
+    EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_UPWARD(
+        Bits::min_subnormal().get_val(),
+        LIBC_NAMESPACE::expbf16(Bits(bits).get_val()),
+        FE_UNDERFLOW | FE_INEXACT);
+    EXPECT_MATH_ERRNO(ERANGE);
+  }
+#endif
+}
+
 class LlvmLibcExpbf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest {
   DECLARE_SPECIAL_CONSTANTS(bfloat16)
 public:

>From bb856c726f888ed27ab02c87cefd25b2dcb22419 Mon Sep 17 00:00:00 2001
From: sriramshastry <sriramshastry at gmail.com>
Date: Wed, 28 Oct 2026 16:21:16 +0000
Subject: [PATCH 4/5] [libc][math] Correct sinhf16 subnormal rounding and range
 reporting

Select the rounded neighbor from the nonzero, outward cubic correction
for signed half subnormals. Fix negative downward rounding and report
inexact with underflow and ERANGE only when the rounded result is tiny.

Add exhaustive signed-subnormal regressions in all supported modes,
including outward transitions to minimum normal.

Signed-off-by: sriramshastry <sriramshastry at gmail.com>
---
 libc/src/__support/math/sinhf16.h         | 19 +++++++++++++
 libc/test/src/math/smoke/sinhf16_test.cpp | 33 +++++++++++++++++++++++
 2 files changed, 52 insertions(+)

diff --git a/libc/src/__support/math/sinhf16.h b/libc/src/__support/math/sinhf16.h
index 5ce957b813e777..3bb71519805a83 100644
--- a/libc/src/__support/math/sinhf16.h
+++ b/libc/src/__support/math/sinhf16.h
@@ -104,6 +104,25 @@ LIBC_INLINE constexpr float16 sinhf16(float16 x) {
   uint16_t x_u = x_bits.uintval();
   uint16_t x_abs = x_u & 0x7fffU;
 
+  // The correction to x is nonzero but less than half a subnormal ulp.
+  // Select the adjacent half value directly, preserving signed zero.
+  if (LIBC_UNLIKELY(x_abs != 0 && x_abs < 0x0400U)) {
+    uint16_t out = x_u;
+#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+    int mode = fputil::quick_get_round();
+    if ((x_bits.is_pos() && mode == FE_UPWARD) ||
+        (x_bits.is_neg() && mode == FE_DOWNWARD))
+      out = static_cast<uint16_t>(x_u + 1);
+#endif
+    int excepts = FE_INEXACT;
+    if ((out & 0x7fffU) < 0x0400U) {
+      fputil::set_errno_if_required(ERANGE);
+      excepts |= FE_UNDERFLOW;
+    }
+    fputil::raise_except_if_required(excepts);
+    return FPBits(out).get_val();
+  }
+
   // When |x| = 0, or -2^(-14) <= x <= -2^(-9), or |x| >= asinh(2^16), or x is
   // NaN.
   if (LIBC_UNLIKELY(x_abs == 0U || (x_u >= 0x8400U && x_u <= 0xa400U) ||
diff --git a/libc/test/src/math/smoke/sinhf16_test.cpp b/libc/test/src/math/smoke/sinhf16_test.cpp
index 7de2bf355f6c59..9f3642bb9df5b5 100644
--- a/libc/test/src/math/smoke/sinhf16_test.cpp
+++ b/libc/test/src/math/smoke/sinhf16_test.cpp
@@ -15,6 +15,39 @@
 
 using LlvmLibcSinhf16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
 
+TEST_F(LlvmLibcSinhf16Test, AllSubnormalRounding) {
+  using Bits = LIBC_NAMESPACE::fputil::FPBits<float16>;
+  using namespace LIBC_NAMESPACE::fputil::testing;
+#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+  constexpr RoundingMode MODES[] = {RoundingMode::Nearest};
+#else
+  constexpr RoundingMode MODES[] = {RoundingMode::Nearest, RoundingMode::Upward,
+                                    RoundingMode::Downward,
+                                    RoundingMode::TowardZero};
+#endif
+  for (RoundingMode mode : MODES) {
+    ForceRoundingMode rounding(mode);
+    if (!rounding.success)
+      continue;
+    for (uint16_t magnitude = 1; magnitude < 0x0400; ++magnitude) {
+      constexpr uint16_t SIGNS[] = {0, 0x8000};
+      for (uint16_t sign : SIGNS) {
+        bool negative = sign != 0;
+        uint16_t expected = static_cast<uint16_t>(magnitude | sign);
+        if ((!negative && mode == RoundingMode::Upward) ||
+            (negative && mode == RoundingMode::Downward))
+          ++expected;
+        bool tiny = (expected & 0x7fff) < 0x0400;
+        EXPECT_FP_EQ_WITH_EXCEPTION(
+            Bits(expected).get_val(),
+            LIBC_NAMESPACE::sinhf16(Bits(uint16_t(magnitude | sign)).get_val()),
+            FE_INEXACT | (tiny ? FE_UNDERFLOW : 0));
+        EXPECT_MATH_ERRNO(tiny ? ERANGE : 0);
+      }
+    }
+  }
+}
+
 TEST_F(LlvmLibcSinhf16Test, SpecialNumbers) {
   EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::sinhf16(aNaN));
   EXPECT_MATH_ERRNO(0);

>From d2a029f5c40c987baee1e0fec75b705522607ba5 Mon Sep 17 00:00:00 2001
From: sriramshastry <sriramshastry at gmail.com>
Date: Wed, 28 Oct 2026 16:21:16 +0000
Subject: [PATCH 5/5] [libc][math] Correct a subnormal exp2m1f16 rounding
 boundary

Add input 0x8461 to the low-range accurate-pass exception table with
independently checked rounding offsets. Report underflow and ERANGE
for its subnormal table result; lookup already reports inexact.

Add a guarded four-mode value and reporting regression. Keep the
separate normal-boundary tininess behavior outside this change.

Signed-off-by: sriramshastry <sriramshastry at gmail.com>
---
 libc/src/__support/math/exp2m1f16.h         | 11 +++++++--
 libc/test/src/math/smoke/exp2m1f16_test.cpp | 25 +++++++++++++++++++++
 2 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/libc/src/__support/math/exp2m1f16.h b/libc/src/__support/math/exp2m1f16.h
index 8cc9dbed46d4a5..b7b7ba304ef877 100644
--- a/libc/src/__support/math/exp2m1f16.h
+++ b/libc/src/__support/math/exp2m1f16.h
@@ -31,8 +31,10 @@ namespace math {
 
 LIBC_INLINE constexpr float16 exp2m1f16(float16 x) {
 #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-  constexpr fputil::ExceptValues<float16, 6> EXP2M1F16_EXCEPTS_LO = {{
+  constexpr fputil::ExceptValues<float16, 7> EXP2M1F16_EXCEPTS_LO = {{
       // (input, RZ output, RU offset, RD offset, RN offset)
+      // Subnormal output straddling the upward rounding boundary.
+      {0x8461U, 0x8308U, 0U, 1U, 1U},
       // x = 0x1.cf4p-13, exp2m1f16(x) = 0x1.41p-13 (RZ)
       {0x0b3dU, 0x0904U, 1U, 0U, 1U},
       // x = 0x1.4fcp-12, exp2m1f16(x) = 0x1.d14p-13 (RZ)
@@ -141,8 +143,13 @@ LIBC_INLINE constexpr float16 exp2m1f16(float16 x) {
     if (x_abs <= 0x3000U) {
 #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
       if (auto r = EXP2M1F16_EXCEPTS_LO.lookup(x_u);
-          LIBC_UNLIKELY(r.has_value()))
+          LIBC_UNLIKELY(r.has_value())) {
+        if (FPBits(r.value()).is_subnormal()) {
+          fputil::set_errno_if_required(ERANGE);
+          fputil::raise_except_if_required(FE_UNDERFLOW);
+        }
         return r.value();
+      }
 #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
 
       float xf = x;
diff --git a/libc/test/src/math/smoke/exp2m1f16_test.cpp b/libc/test/src/math/smoke/exp2m1f16_test.cpp
index 01d30bac6b540e..e096024def61b0 100644
--- a/libc/test/src/math/smoke/exp2m1f16_test.cpp
+++ b/libc/test/src/math/smoke/exp2m1f16_test.cpp
@@ -15,6 +15,31 @@
 
 using LlvmLibcExp2m1f16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
 
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+TEST_F(LlvmLibcExp2m1f16Test, SubnormalBoundary) {
+  using Bits = LIBC_NAMESPACE::fputil::FPBits<float16>;
+  const float16 x = Bits(uint16_t(0x8461)).get_val();
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_NEAREST(Bits(uint16_t(0x8309)).get_val(),
+                                               LIBC_NAMESPACE::exp2m1f16(x),
+                                               FE_UNDERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_UPWARD(Bits(uint16_t(0x8308)).get_val(),
+                                              LIBC_NAMESPACE::exp2m1f16(x),
+                                              FE_UNDERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD(
+      Bits(uint16_t(0x8309)).get_val(), LIBC_NAMESPACE::exp2m1f16(x),
+      FE_UNDERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+  EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
+      Bits(uint16_t(0x8308)).get_val(), LIBC_NAMESPACE::exp2m1f16(x),
+      FE_UNDERFLOW | FE_INEXACT);
+  EXPECT_MATH_ERRNO(ERANGE);
+#endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+}
+#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+
 TEST_F(LlvmLibcExp2m1f16Test, SpecialNumbers) {
   EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::exp2m1f16(aNaN));
   EXPECT_MATH_ERRNO(0);



More information about the libc-commits mailing list