[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 04:06:09 PST 2026
https://github.com/sriramshastry updated https://github.com/llvm/llvm-project/pull/236472
>From 933935578f0388d6eeeb074733d02f9b697b5600 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/src/__support/math/coshf16.h | 4 +-
libc/src/__support/math/exp10f16.h | 4 +-
libc/src/__support/math/exp10m1f16.h | 4 +-
libc/src/__support/math/exp2bf16.h | 4 +-
libc/src/__support/math/exp2f16.h | 4 +-
libc/src/__support/math/exp2m1f16.h | 4 +-
libc/src/__support/math/expf16.h | 4 +-
libc/src/__support/math/expm1f16.h | 4 +-
libc/src/__support/math/sinhf16.h | 4 +-
libc/test/src/math/smoke/coshf16_test.cpp | 30 ++++++++++++--
libc/test/src/math/smoke/exp10f16_test.cpp | 22 +++++++++++
libc/test/src/math/smoke/exp10m1f16_test.cpp | 26 ++++++++++++-
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 | 26 ++++++++++++-
libc/test/src/math/smoke/expf16_test.cpp | 22 +++++++++++
libc/test/src/math/smoke/expm1f16_test.cpp | 26 ++++++++++++-
libc/test/src/math/smoke/sinhf16_test.cpp | 41 ++++++++++++++++++--
18 files changed, 241 insertions(+), 32 deletions(-)
diff --git a/libc/src/__support/math/coshf16.h b/libc/src/__support/math/coshf16.h
index f5015199b3f936..d8cd53942a87ba 100644
--- a/libc/src/__support/math/coshf16.h
+++ b/libc/src/__support/math/coshf16.h
@@ -90,16 +90,16 @@ LIBC_INLINE constexpr float16 coshf16(float16 x) {
if (x_bits.is_inf())
return FPBits::inf().get_val();
+ fputil::raise_overflow_except_if_required<float>();
+
#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
fputil::set_errno_if_required(ERANGE);
- fputil::raise_except_if_required(FE_OVERFLOW | FE_INEXACT);
return FPBits::inf().get_val();
#else
switch (fputil::quick_get_round()) {
case FE_TONEAREST:
case FE_UPWARD:
fputil::set_errno_if_required(ERANGE);
- fputil::raise_except_if_required(FE_OVERFLOW | FE_INEXACT);
return FPBits::inf().get_val();
default:
return FPBits::max_normal().get_val();
diff --git a/libc/src/__support/math/exp10f16.h b/libc/src/__support/math/exp10f16.h
index 727914f1bb6013..3a525ea4baa09a 100644
--- a/libc/src/__support/math/exp10f16.h
+++ b/libc/src/__support/math/exp10f16.h
@@ -82,16 +82,16 @@ LIBC_INLINE constexpr float16 exp10f16(float16 x) {
if (x_bits.is_inf())
return FPBits::inf().get_val();
+ fputil::raise_overflow_except_if_required<float>();
+
#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
fputil::set_errno_if_required(ERANGE);
- fputil::raise_except_if_required(FE_OVERFLOW);
return FPBits::inf().get_val();
#else
switch (fputil::quick_get_round()) {
case FE_TONEAREST:
case FE_UPWARD:
fputil::set_errno_if_required(ERANGE);
- fputil::raise_except_if_required(FE_OVERFLOW);
return FPBits::inf().get_val();
default:
return FPBits::max_normal().get_val();
diff --git a/libc/src/__support/math/exp10m1f16.h b/libc/src/__support/math/exp10m1f16.h
index cb685298f01c78..aa99797f33b9df 100644
--- a/libc/src/__support/math/exp10m1f16.h
+++ b/libc/src/__support/math/exp10m1f16.h
@@ -93,16 +93,16 @@ LIBC_INLINE constexpr float16 exp10m1f16(float16 x) {
if (x_bits.is_inf())
return FPBits::inf().get_val();
+ fputil::raise_overflow_except_if_required<float>();
+
#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
fputil::set_errno_if_required(ERANGE);
- fputil::raise_except_if_required(FE_OVERFLOW | FE_INEXACT);
return FPBits::inf().get_val();
#else
switch (fputil::quick_get_round()) {
case FE_TONEAREST:
case FE_UPWARD:
fputil::set_errno_if_required(ERANGE);
- fputil::raise_except_if_required(FE_OVERFLOW | FE_INEXACT);
return FPBits::inf().get_val();
default:
return FPBits::max_normal().get_val();
diff --git a/libc/src/__support/math/exp2bf16.h b/libc/src/__support/math/exp2bf16.h
index 44fa5da0ebe477..79b8141c8fcdb1 100644
--- a/libc/src/__support/math/exp2bf16.h
+++ b/libc/src/__support/math/exp2bf16.h
@@ -82,10 +82,11 @@ LIBC_INLINE bfloat16 exp2bf16(bfloat16 x) {
if (x_bits.is_inf())
return x;
+ fputil::raise_overflow_except_if_required<float>();
+
#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
// If we can only RN, then it'll go towards INF
fputil::set_errno_if_required(ERANGE);
- fputil::raise_overflow_except_if_required<float>();
return FPBits::inf().get_val();
#else
// If not, get what other rounding modes are available
@@ -94,7 +95,6 @@ LIBC_INLINE bfloat16 exp2bf16(bfloat16 x) {
case FE_UPWARD:
// Both of these go to Inf
fputil::set_errno_if_required(ERANGE);
- fputil::raise_overflow_except_if_required<float>();
return FPBits::inf().get_val();
default:
// RD and RZ go to the maximum possible value in the
diff --git a/libc/src/__support/math/exp2f16.h b/libc/src/__support/math/exp2f16.h
index fe2c78faeef981..68c50f2e1ce35f 100644
--- a/libc/src/__support/math/exp2f16.h
+++ b/libc/src/__support/math/exp2f16.h
@@ -66,16 +66,16 @@ LIBC_INLINE constexpr float16 exp2f16(float16 x) {
if (x_bits.is_inf())
return FPBits::inf().get_val();
+ fputil::raise_overflow_except_if_required<float>();
+
#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
fputil::set_errno_if_required(ERANGE);
- fputil::raise_except_if_required(FE_OVERFLOW);
return FPBits::inf().get_val();
#else
switch (fputil::quick_get_round()) {
case FE_TONEAREST:
case FE_UPWARD:
fputil::set_errno_if_required(ERANGE);
- fputil::raise_except_if_required(FE_OVERFLOW);
return FPBits::inf().get_val();
default:
return FPBits::max_normal().get_val();
diff --git a/libc/src/__support/math/exp2m1f16.h b/libc/src/__support/math/exp2m1f16.h
index 7ea8898200259f..b60d693ac5796a 100644
--- a/libc/src/__support/math/exp2m1f16.h
+++ b/libc/src/__support/math/exp2m1f16.h
@@ -106,16 +106,16 @@ LIBC_INLINE constexpr float16 exp2m1f16(float16 x) {
if (x_bits.is_inf())
return FPBits::inf().get_val();
+ fputil::raise_overflow_except_if_required<float>();
+
#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
fputil::set_errno_if_required(ERANGE);
- fputil::raise_except_if_required(FE_OVERFLOW | FE_INEXACT);
return FPBits::inf().get_val();
#else
switch (fputil::quick_get_round()) {
case FE_TONEAREST:
case FE_UPWARD:
fputil::set_errno_if_required(ERANGE);
- fputil::raise_except_if_required(FE_OVERFLOW | FE_INEXACT);
return FPBits::inf().get_val();
default:
return FPBits::max_normal().get_val();
diff --git a/libc/src/__support/math/expf16.h b/libc/src/__support/math/expf16.h
index fd0c92b791ecfc..7e6ca831bf2fe6 100644
--- a/libc/src/__support/math/expf16.h
+++ b/libc/src/__support/math/expf16.h
@@ -75,16 +75,16 @@ LIBC_INLINE constexpr float16 expf16(float16 x) {
if (x_bits.is_inf())
return FPBits::inf().get_val();
+ fputil::raise_overflow_except_if_required<float>();
+
#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
fputil::set_errno_if_required(ERANGE);
- fputil::raise_except_if_required(FE_OVERFLOW);
return FPBits::inf().get_val();
#else
switch (fputil::quick_get_round()) {
case FE_TONEAREST:
case FE_UPWARD:
fputil::set_errno_if_required(ERANGE);
- fputil::raise_except_if_required(FE_OVERFLOW);
return FPBits::inf().get_val();
default:
return FPBits::max_normal().get_val();
diff --git a/libc/src/__support/math/expm1f16.h b/libc/src/__support/math/expm1f16.h
index 08234a0ad4ace9..c1f6b9ff306fe7 100644
--- a/libc/src/__support/math/expm1f16.h
+++ b/libc/src/__support/math/expm1f16.h
@@ -86,16 +86,16 @@ LIBC_INLINE constexpr float16 expm1f16(float16 x) {
if (x_bits.is_inf())
return FPBits::inf().get_val();
+ fputil::raise_overflow_except_if_required<float>();
+
#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
fputil::set_errno_if_required(ERANGE);
- fputil::raise_except_if_required(FE_OVERFLOW | FE_INEXACT);
return FPBits::inf().get_val();
#else
switch (fputil::quick_get_round()) {
case FE_TONEAREST:
case FE_UPWARD:
fputil::set_errno_if_required(ERANGE);
- fputil::raise_except_if_required(FE_OVERFLOW | FE_INEXACT);
return FPBits::inf().get_val();
default:
return FPBits::max_normal().get_val();
diff --git a/libc/src/__support/math/sinhf16.h b/libc/src/__support/math/sinhf16.h
index 45d7eddd01fed6..e5fce3e2bc9db3 100644
--- a/libc/src/__support/math/sinhf16.h
+++ b/libc/src/__support/math/sinhf16.h
@@ -129,9 +129,10 @@ LIBC_INLINE constexpr float16 sinhf16(float16 x) {
if (x_bits.is_inf())
return FPBits::inf(x_bits.sign()).get_val();
+ fputil::raise_overflow_except_if_required<float>();
+
#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
fputil::set_errno_if_required(ERANGE);
- fputil::raise_except_if_required(FE_OVERFLOW | FE_INEXACT);
return FPBits::inf(x_bits.sign()).get_val();
#else
int rounding_mode = fputil::quick_get_round();
@@ -139,7 +140,6 @@ LIBC_INLINE constexpr float16 sinhf16(float16 x) {
(x_bits.is_pos() && rounding_mode == FE_UPWARD) ||
(x_bits.is_neg() && rounding_mode == FE_DOWNWARD)) {
fputil::set_errno_if_required(ERANGE);
- fputil::raise_except_if_required(FE_OVERFLOW | FE_INEXACT);
return FPBits::inf(x_bits.sign()).get_val();
}
#endif // LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
diff --git a/libc/test/src/math/smoke/coshf16_test.cpp b/libc/test/src/math/smoke/coshf16_test.cpp
index 6d5824d37dafbd..570cee472225b2 100644
--- a/libc/test/src/math/smoke/coshf16_test.cpp
+++ b/libc/test/src/math/smoke/coshf16_test.cpp
@@ -58,11 +58,11 @@ TEST_F(LlvmLibcCoshf16Test, Overflow) {
EXPECT_MATH_ERRNO(ERANGE);
EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD(
- max_normal, LIBC_NAMESPACE::coshf16(x), FE_INEXACT);
+ max_normal, LIBC_NAMESPACE::coshf16(x), FE_OVERFLOW | FE_INEXACT);
EXPECT_MATH_ERRNO(0);
EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
- max_normal, LIBC_NAMESPACE::coshf16(x), FE_INEXACT);
+ max_normal, LIBC_NAMESPACE::coshf16(x), FE_OVERFLOW | FE_INEXACT);
EXPECT_MATH_ERRNO(0);
// round(-acosh(2^16), HP, RD);
@@ -77,10 +77,32 @@ TEST_F(LlvmLibcCoshf16Test, Overflow) {
EXPECT_MATH_ERRNO(ERANGE);
EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD(
- max_normal, LIBC_NAMESPACE::coshf16(x), FE_INEXACT);
+ max_normal, LIBC_NAMESPACE::coshf16(x), FE_OVERFLOW | FE_INEXACT);
EXPECT_MATH_ERRNO(0);
EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
- max_normal, LIBC_NAMESPACE::coshf16(x), FE_INEXACT);
+ max_normal, LIBC_NAMESPACE::coshf16(x), FE_OVERFLOW | FE_INEXACT);
EXPECT_MATH_ERRNO(0);
}
+
+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(0);
+ EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
+ largest, LIBC_NAMESPACE::coshf16(x), FE_OVERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(0);
+#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..ab57145327ed26 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(0);
+ EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
+ largest, LIBC_NAMESPACE::exp10f16(x), FE_OVERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(0);
+#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 1ba0e4c2effcc8..1a2e4b0f83efeb 100644
--- a/libc/test/src/math/smoke/exp10m1f16_test.cpp
+++ b/libc/test/src/math/smoke/exp10m1f16_test.cpp
@@ -54,11 +54,11 @@ TEST_F(LlvmLibcExp10m1f16Test, Overflow) {
EXPECT_MATH_ERRNO(ERANGE);
EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD(
- max_normal, LIBC_NAMESPACE::exp10m1f16(x), FE_INEXACT);
+ max_normal, LIBC_NAMESPACE::exp10m1f16(x), FE_OVERFLOW | FE_INEXACT);
EXPECT_MATH_ERRNO(0);
EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
- max_normal, LIBC_NAMESPACE::exp10m1f16(x), FE_INEXACT);
+ max_normal, LIBC_NAMESPACE::exp10m1f16(x), FE_OVERFLOW | FE_INEXACT);
EXPECT_MATH_ERRNO(0);
}
@@ -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(0);
+ EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
+ largest, LIBC_NAMESPACE::exp10m1f16(x), FE_OVERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(0);
+#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..eede5782e703f9 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(0);
+ EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
+ largest, LIBC_NAMESPACE::exp2bf16(x), FE_OVERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(0);
+#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..d322c428635466 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(0);
+ EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
+ largest, LIBC_NAMESPACE::exp2f16(x), FE_OVERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(0);
+#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 4917128c7f641d..4d95ed916b7182 100644
--- a/libc/test/src/math/smoke/exp2m1f16_test.cpp
+++ b/libc/test/src/math/smoke/exp2m1f16_test.cpp
@@ -52,11 +52,11 @@ TEST_F(LlvmLibcExp2m1f16Test, Overflow) {
EXPECT_MATH_ERRNO(ERANGE);
EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD(
- max_normal, LIBC_NAMESPACE::exp2m1f16(x), FE_INEXACT);
+ max_normal, LIBC_NAMESPACE::exp2m1f16(x), FE_OVERFLOW | FE_INEXACT);
EXPECT_MATH_ERRNO(0);
EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
- max_normal, LIBC_NAMESPACE::exp2m1f16(x), FE_INEXACT);
+ max_normal, LIBC_NAMESPACE::exp2m1f16(x), FE_OVERFLOW | FE_INEXACT);
EXPECT_MATH_ERRNO(0);
}
@@ -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(0);
+ EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
+ largest, LIBC_NAMESPACE::exp2m1f16(x), FE_OVERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(0);
+#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..8a709462e58c0d 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(0);
+ EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
+ largest, LIBC_NAMESPACE::expf16(x), FE_OVERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(0);
+#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 545304ced63ed8..d01750dd9f01bd 100644
--- a/libc/test/src/math/smoke/expm1f16_test.cpp
+++ b/libc/test/src/math/smoke/expm1f16_test.cpp
@@ -53,11 +53,11 @@ TEST_F(LlvmLibcExpm1f16Test, Overflow) {
EXPECT_MATH_ERRNO(ERANGE);
EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD(
- max_normal, LIBC_NAMESPACE::expm1f16(x), FE_INEXACT);
+ max_normal, LIBC_NAMESPACE::expm1f16(x), FE_OVERFLOW | FE_INEXACT);
EXPECT_MATH_ERRNO(0);
EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
- max_normal, LIBC_NAMESPACE::expm1f16(x), FE_INEXACT);
+ max_normal, LIBC_NAMESPACE::expm1f16(x), FE_OVERFLOW | FE_INEXACT);
EXPECT_MATH_ERRNO(0);
}
@@ -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(0);
+ EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
+ largest, LIBC_NAMESPACE::expm1f16(x), FE_OVERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(0);
+#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 1cbf19c5d3a087..3cef8732e32a9e 100644
--- a/libc/test/src/math/smoke/sinhf16_test.cpp
+++ b/libc/test/src/math/smoke/sinhf16_test.cpp
@@ -56,11 +56,11 @@ TEST_F(LlvmLibcSinhf16Test, Overflow) {
EXPECT_MATH_ERRNO(ERANGE);
EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD(
- max_normal, LIBC_NAMESPACE::sinhf16(x), FE_INEXACT);
+ max_normal, LIBC_NAMESPACE::sinhf16(x), FE_OVERFLOW | FE_INEXACT);
EXPECT_MATH_ERRNO(0);
EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
- max_normal, LIBC_NAMESPACE::sinhf16(x), FE_INEXACT);
+ max_normal, LIBC_NAMESPACE::sinhf16(x), FE_OVERFLOW | FE_INEXACT);
EXPECT_MATH_ERRNO(0);
// round(asinh(-2^16), HP, RD);
@@ -71,7 +71,7 @@ TEST_F(LlvmLibcSinhf16Test, Overflow) {
EXPECT_MATH_ERRNO(ERANGE);
EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_UPWARD(
- neg_max_normal, LIBC_NAMESPACE::sinhf16(x), FE_INEXACT);
+ neg_max_normal, LIBC_NAMESPACE::sinhf16(x), FE_OVERFLOW | FE_INEXACT);
EXPECT_MATH_ERRNO(0);
EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_DOWNWARD(
@@ -79,6 +79,39 @@ TEST_F(LlvmLibcSinhf16Test, Overflow) {
EXPECT_MATH_ERRNO(ERANGE);
EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
- neg_max_normal, LIBC_NAMESPACE::sinhf16(x), FE_INEXACT);
+ neg_max_normal, LIBC_NAMESPACE::sinhf16(x), FE_OVERFLOW | FE_INEXACT);
EXPECT_MATH_ERRNO(0);
}
+
+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(0);
+ EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
+ largest, LIBC_NAMESPACE::sinhf16(x), FE_OVERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(0);
+#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(0);
+ EXPECT_FP_EQ_WITH_EXCEPTION_ROUNDING_TOWARD_ZERO(
+ lowest, LIBC_NAMESPACE::sinhf16(negative), FE_OVERFLOW | FE_INEXACT);
+ EXPECT_MATH_ERRNO(0);
+#endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+}
>From d2deaeb387c8d2a253a1050fc0e2d4cabff4e5db 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 b1bb1a3f3b3434..cae3710f8ecbae 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -5137,6 +5137,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 a87017afd0dfc3f7d52fbe4b08dda9df01f8011f 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 | 9 ++++
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, 114 insertions(+)
diff --git a/libc/src/__support/math/exp2bf16.h b/libc/src/__support/math/exp2bf16.h
index 79b8141c8fcdb1..7bbd39f7756905 100644
--- a/libc/src/__support/math/exp2bf16.h
+++ b/libc/src/__support/math/exp2bf16.h
@@ -140,6 +140,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 5fdde6d6204f74..47e290c69caef0 100644
--- a/libc/src/__support/math/expbf16.h
+++ b/libc/src/__support/math/expbf16.h
@@ -65,6 +65,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)) {
@@ -111,10 +117,13 @@ LIBC_INLINE bfloat16 expbf16(bfloat16 x) {
fputil::raise_except_if_required(FE_UNDERFLOW | FE_INEXACT);
#ifdef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+ if (LIBC_UNLIKELY(x_u == 0xc2b9U))
+ return FPBits::min_subnormal().get_val();
return FPBits::zero().get_val();
#else
switch (fputil::quick_get_round()) {
case FE_UPWARD:
+ return FPBits::min_subnormal().get_val();
case FE_TONEAREST:
if (LIBC_UNLIKELY(x_u == 0xc2b9U))
return FPBits::min_subnormal().get_val();
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index cae3710f8ecbae..670b4136de17c7 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -1483,6 +1483,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
)
@@ -1528,6 +1529,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 eede5782e703f9..59775a5a75250c 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 fcb4e6aa285b8b733b6b2f58afb58465a0460d11 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 e5fce3e2bc9db3..1601bf35db38e2 100644
--- a/libc/src/__support/math/sinhf16.h
+++ b/libc/src/__support/math/sinhf16.h
@@ -105,6 +105,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 3cef8732e32a9e..ac8fd46ce3e17f 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 ab4d20c133b1cbaf1261609e1628febcb36ac838 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 b60d693ac5796a..05555a395b8bde 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)
@@ -155,8 +157,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 4d95ed916b7182..bfb4a9cf505775 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