[libc-commits] [libc] bfa24f2 - [libc][math] Correct logb exceptions and nextup/down boundaries (#236473)

via libc-commits libc-commits at lists.llvm.org
Mon Nov 9 20:01:06 PST 2026


Author: sriramshastry
Date: 2026-11-10T09:31:01+05:30
New Revision: bfa24f2805f402c264bc68f117eabb4c6411b1e0

URL: https://github.com/llvm/llvm-project/commit/bfa24f2805f402c264bc68f117eabb4c6411b1e0
DIFF: https://github.com/llvm/llvm-project/commit/bfa24f2805f402c264bc68f117eabb4c6411b1e0.diff

LOG: [libc][math] Correct logb exceptions and nextup/down boundaries (#236473)

Fix logb so infinity does not raise FE_DIVBYZERO and signaling NaNs
raise
FE_INVALID and return a quiet NaN. Correct nextup/nextdown's
finite-to-infinity
and binary80 normal/subnormal steps.

Use bit-based zero checks to avoid FE_DENORM on subnormal inputs. Extend
the
shared tests to check result bits, flags and logb errno.

Tests on x86-64 Linux: 18 focused Clang 18 fullbuild targets and 36
overlay
targets per compiler with GCC 13 and Clang 18.

Assisted-by: GitHub Copilot

---------

Signed-off-by: sriramshastry <sriramshastry at gmail.com>

Added: 
    

Modified: 
    libc/src/__support/FPUtil/ManipulationFunctions.h
    libc/src/__support/FPUtil/x86_64/NextUpDownLongDouble.h
    libc/test/src/math/smoke/LogbTest.h
    libc/test/src/math/smoke/NextDownTest.h
    libc/test/src/math/smoke/NextUpTest.h

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/FPUtil/ManipulationFunctions.h b/libc/src/__support/FPUtil/ManipulationFunctions.h
index 1919b239d2fb9f..51cbf7dde904d7 100644
--- a/libc/src/__support/FPUtil/ManipulationFunctions.h
+++ b/libc/src/__support/FPUtil/ManipulationFunctions.h
@@ -137,13 +137,17 @@ template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
 LIBC_INLINE constexpr T logb(T x) {
   FPBits<T> bits(x);
   if (LIBC_UNLIKELY(bits.is_zero() || bits.is_inf_or_nan())) {
-    if (bits.is_nan())
+    if (bits.is_nan()) {
+      if (bits.is_signaling_nan()) {
+        raise_except_if_required(FE_INVALID);
+        return FPBits<T>::quiet_nan().get_val();
+      }
       return x;
-
-    raise_except_if_required(FE_DIVBYZERO);
+    }
 
     if (bits.is_zero()) {
       set_errno_if_required(ERANGE);
+      raise_except_if_required(FE_DIVBYZERO);
       return FPBits<T>::inf(Sign::NEG).get_val();
     }
     // bits is inf.
@@ -232,12 +236,11 @@ LIBC_INLINE constexpr T nextupdown(T x) {
   constexpr Sign sign = IsDown ? Sign::NEG : Sign::POS;
 
   FPBits<T> xbits(x);
-  if (xbits.is_nan() || xbits == FPBits<T>::max_normal(sign) ||
-      xbits == FPBits<T>::inf(sign))
+  if (xbits.is_nan() || xbits == FPBits<T>::inf(sign))
     return x;
 
   using StorageType = typename FPBits<T>::StorageType;
-  if (x != T(0)) {
+  if (!xbits.is_zero()) {
     if (xbits.sign() == sign) {
       xbits = FPBits<T>(StorageType(xbits.uintval() + 1));
     } else {

diff  --git a/libc/src/__support/FPUtil/x86_64/NextUpDownLongDouble.h b/libc/src/__support/FPUtil/x86_64/NextUpDownLongDouble.h
index 589328ae2a425a..61a7cea8c5fbbb 100644
--- a/libc/src/__support/FPUtil/x86_64/NextUpDownLongDouble.h
+++ b/libc/src/__support/FPUtil/x86_64/NextUpDownLongDouble.h
@@ -27,11 +27,17 @@ LIBC_INLINE LIBC_BIT_CAST_CONSTEXPR long double nextupdown(long double x) {
 
   using FPBits_t = FPBits<long double>;
   FPBits_t xbits(x);
-  if (xbits.is_nan() || xbits == FPBits_t::max_normal(sign) ||
-      xbits == FPBits_t::inf(sign))
+  if (xbits.is_nan() || xbits == FPBits_t::inf(sign))
     return x;
 
-  if (x == 0.0l)
+  // Crossing the normal/subnormal boundary also changes the explicit
+  // integer bit in the x86 extended precision encoding.
+  if (xbits == FPBits_t::max_subnormal(sign))
+    return FPBits_t::min_normal(sign).get_val();
+  if (xbits == FPBits_t::min_normal(sign.negate()))
+    return FPBits_t::max_subnormal(sign.negate()).get_val();
+
+  if (xbits.is_zero())
     return FPBits_t::min_subnormal(sign).get_val();
 
   using StorageType = typename FPBits_t::StorageType;

diff  --git a/libc/test/src/math/smoke/LogbTest.h b/libc/test/src/math/smoke/LogbTest.h
index 3162af735c3747..e148f8888300cb 100644
--- a/libc/test/src/math/smoke/LogbTest.h
+++ b/libc/test/src/math/smoke/LogbTest.h
@@ -24,11 +24,20 @@ class LogbTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
   typedef T (*LogbFunc)(T);
 
   void testSpecialNumbers(LogbFunc func) {
-    ASSERT_FP_EQ(aNaN, func(aNaN));
-    ASSERT_FP_EQ(inf, func(inf));
-    ASSERT_FP_EQ(inf, func(neg_inf));
-    ASSERT_FP_EQ(neg_inf, func(zero));
-    ASSERT_FP_EQ(neg_inf, func(neg_zero));
+    EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(aNaN), 0);
+    EXPECT_MATH_ERRNO(0);
+    T snan_result = zero;
+    EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, snan_result = func(sNaN), FE_INVALID);
+    EXPECT_FALSE(FPBits(snan_result).is_signaling_nan());
+    EXPECT_MATH_ERRNO(0);
+    EXPECT_FP_EQ_WITH_EXCEPTION(inf, func(inf), 0);
+    EXPECT_MATH_ERRNO(0);
+    EXPECT_FP_EQ_WITH_EXCEPTION(inf, func(neg_inf), 0);
+    EXPECT_MATH_ERRNO(0);
+    EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, func(zero), FE_DIVBYZERO);
+    EXPECT_MATH_ERRNO(ERANGE);
+    EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, func(neg_zero), FE_DIVBYZERO);
+    EXPECT_MATH_ERRNO(ERANGE);
   }
 
   void testPowersOfTwo(LogbFunc func) {

diff  --git a/libc/test/src/math/smoke/NextDownTest.h b/libc/test/src/math/smoke/NextDownTest.h
index 0b0ab83bcb6b6a..039d246a2621ca 100644
--- a/libc/test/src/math/smoke/NextDownTest.h
+++ b/libc/test/src/math/smoke/NextDownTest.h
@@ -24,15 +24,21 @@ class NextDownTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
   void testNaN(NextDownFunc func) { ASSERT_FP_EQ(func(aNaN), aNaN); }
 
   void testBoundaries(NextDownFunc func) {
-    ASSERT_FP_EQ(zero, func(min_denormal));
+    EXPECT_FP_EQ_WITH_EXCEPTION(zero, func(min_denormal), 0);
 
     ASSERT_FP_EQ(neg_min_denormal, func(zero));
     ASSERT_FP_EQ(neg_min_denormal, func(neg_zero));
 
-    ASSERT_FP_EQ(neg_max_normal, func(neg_max_normal));
+    EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, func(neg_max_normal), 0);
     ASSERT_FP_EQ(neg_inf, func(neg_inf));
 
     ASSERT_FP_EQ(max_normal, func(inf));
+
+    EXPECT_FP_EQ_WITH_EXCEPTION(
+        FPBits::min_normal(Sign::NEG).get_val(),
+        func(FPBits::max_subnormal(Sign::NEG).get_val()), 0);
+    EXPECT_FP_EQ_WITH_EXCEPTION(FPBits::max_subnormal().get_val(),
+                                func(FPBits::min_normal().get_val()), 0);
   }
 };
 

diff  --git a/libc/test/src/math/smoke/NextUpTest.h b/libc/test/src/math/smoke/NextUpTest.h
index 886be60e595726..46eb69c808e020 100644
--- a/libc/test/src/math/smoke/NextUpTest.h
+++ b/libc/test/src/math/smoke/NextUpTest.h
@@ -24,15 +24,21 @@ class NextUpTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
   void testNaN(NextUpFunc func) { ASSERT_FP_EQ(func(aNaN), aNaN); }
 
   void testBoundaries(NextUpFunc func) {
-    ASSERT_FP_EQ(neg_zero, func(neg_min_denormal));
+    EXPECT_FP_EQ_WITH_EXCEPTION(neg_zero, func(neg_min_denormal), 0);
 
     ASSERT_FP_EQ(min_denormal, func(zero));
     ASSERT_FP_EQ(min_denormal, func(neg_zero));
 
-    ASSERT_FP_EQ(max_normal, func(max_normal));
+    EXPECT_FP_EQ_WITH_EXCEPTION(inf, func(max_normal), 0);
     ASSERT_FP_EQ(inf, func(inf));
 
     ASSERT_FP_EQ(neg_max_normal, func(neg_inf));
+
+    EXPECT_FP_EQ_WITH_EXCEPTION(FPBits::min_normal().get_val(),
+                                func(FPBits::max_subnormal().get_val()), 0);
+    EXPECT_FP_EQ_WITH_EXCEPTION(FPBits::max_subnormal(Sign::NEG).get_val(),
+                                func(FPBits::min_normal(Sign::NEG).get_val()),
+                                0);
   }
 };
 


        


More information about the libc-commits mailing list