[libc-commits] [libc] 0651368 - [libc][math] Correct expm1f16 subnormal rounding and range reporting (#236310)
via libc-commits
libc-commits at lists.llvm.org
Tue Nov 10 20:58:24 PST 2026
Author: sriramshastry
Date: 2026-11-11T10:28:20+05:30
New Revision: 0651368ba1ce1f3c3bb72f4a00fcb243cf16b095
URL: https://github.com/llvm/llvm-project/commit/0651368ba1ce1f3c3bb72f4a00fcb243cf16b095
DIFF: https://github.com/llvm/llvm-project/commit/0651368ba1ce1f3c3bb72f4a00fcb243cf16b095.diff
LOG: [libc][math] Correct expm1f16 subnormal rounding and range reporting (#236310)
Correct expm1f16 rounding for nonzero half subnormals and the negative
minimum-normal input. Select adjacent results directly, preserve signed
zero,
and use tininess after rounding for underflow/ERANGE.
Local tests on x86-64 Linux with GCC 13 and Clang 18: 5/5 focused smoke
tests
per compiler. MPFR 4.2.1 value checks cover all 63,488 finite half
encodings
in four rounding modes, with no mismatches. Reporting checks cover
nonzero
subnormals and four adjacent boundary encodings.
MPFR's own underflow flag differs at 0x03ff under upward rounding; the
PR uses
the after-rounding policy. The separate overflow fix is not included.
Assisted-by: GitHub Copilot
Signed-off-by: sriramshastry <sriramshastry at gmail.com>
Added:
Modified:
libc/src/__support/math/expm1f16.h
libc/test/src/math/smoke/expm1f16_test.cpp
Removed:
################################################################################
diff --git a/libc/src/__support/math/expm1f16.h b/libc/src/__support/math/expm1f16.h
index ccc73c88b7f9e..5e0cbb44a6a5b 100644
--- a/libc/src/__support/math/expm1f16.h
+++ b/libc/src/__support/math/expm1f16.h
@@ -105,6 +105,26 @@ LIBC_INLINE constexpr float16 expm1f16(float16 x) {
// When 0 < |x| <= 2^(-3).
if (x_abs <= 0x3000U && !x_bits.is_zero()) {
+ // 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 < 0x0400U || x_u == 0x8400U)) {
+ uint16_t out = x_u;
+#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+ int mode = fputil::quick_get_round();
+ bool increase =
+ (x_bits.is_pos() && mode == FE_UPWARD) ||
+ (x_bits.is_neg() && (mode == FE_UPWARD || mode == FE_TOWARDZERO));
+ if (increase)
+ out = static_cast<uint16_t>(x_u + (x_bits.is_neg() ? -1 : 1));
+#endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+ 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();
+ }
#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
if (auto r = EXPM1F16_EXCEPTS_LO.lookup(x_u);
diff --git a/libc/test/src/math/smoke/expm1f16_test.cpp b/libc/test/src/math/smoke/expm1f16_test.cpp
index 976c7202a99de..fa892cb24787b 100644
--- a/libc/test/src/math/smoke/expm1f16_test.cpp
+++ b/libc/test/src/math/smoke/expm1f16_test.cpp
@@ -15,6 +15,67 @@
using LlvmLibcExpm1f16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
+TEST_F(LlvmLibcExpm1f16Test, 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 // LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+ 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::Upward ||
+ mode == RoundingMode::TowardZero)))
+ expected = static_cast<uint16_t>(expected + (negative ? -1 : 1));
+ bool tiny = (expected & 0x7fff) < 0x0400;
+ EXPECT_FP_EQ_WITH_EXCEPTION(
+ Bits(expected).get_val(),
+ LIBC_NAMESPACE::expm1f16(
+ Bits(uint16_t(magnitude | sign)).get_val()),
+ FE_INEXACT | (tiny ? FE_UNDERFLOW : 0));
+ EXPECT_MATH_ERRNO(tiny ? ERANGE : 0);
+ }
+ }
+ }
+}
+
+TEST_F(LlvmLibcExpm1f16Test, NegativeMinNormalTininessAfterRounding) {
+ 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 // LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
+ for (RoundingMode mode : MODES) {
+ ForceRoundingMode rounding(mode);
+ if (!rounding.success)
+ continue;
+ uint16_t expected = 0x8400;
+ if (mode == RoundingMode::Upward || mode == RoundingMode::TowardZero)
+ expected = 0x83ff;
+ bool tiny = (expected & 0x7fff) < 0x0400;
+ EXPECT_FP_EQ_WITH_EXCEPTION(
+ Bits(expected).get_val(),
+ LIBC_NAMESPACE::expm1f16(Bits(uint16_t(0x8400)).get_val()),
+ FE_INEXACT | (tiny ? FE_UNDERFLOW : 0));
+ EXPECT_MATH_ERRNO(tiny ? ERANGE : 0);
+ }
+}
+
TEST_F(LlvmLibcExpm1f16Test, SpecialNumbers) {
EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::expm1f16(aNaN));
EXPECT_MATH_ERRNO(0);
More information about the libc-commits
mailing list