[libc-commits] [libc] [libc][math] Report remquo domain errors and quiet signaling NaNs (PR #236474)

via libc-commits libc-commits at lists.llvm.org
Tue Nov 10 08:57:44 PST 2026


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

>From 21385b1766a2aeeecfa456a207186b957bc6065b Mon Sep 17 00:00:00 2001
From: sriramshastry <sriramshastry at gmail.com>
Date: Wed, 28 Oct 2026 16:21:22 +0000
Subject: [PATCH] [libc][math] Report remquo domain errors and quiet signaling
 NaNs

Check both operands for signaling NaNs before quiet-NaN propagation.
Return a quiet NaN and report invalid when either operand signals.
Add conditional EDOM and invalid reporting to the existing domain branch.

Cover both operand orders and NaN precedence in the shared smoke fixture.
Do not constrain the unspecified quotient output for NaN results.

Signed-off-by: sriramshastry <sriramshastry at gmail.com>
---
 libc/src/__support/FPUtil/CMakeLists.txt      |   1 +
 .../FPUtil/DivisionAndRemainderOperations.h   |  39 +++--
 libc/test/src/math/smoke/RemQuoTest.h         | 139 ++++++++++++------
 3 files changed, 120 insertions(+), 59 deletions(-)

diff --git a/libc/src/__support/FPUtil/CMakeLists.txt b/libc/src/__support/FPUtil/CMakeLists.txt
index f2d07ab222bab3..6658c5c26974ce 100644
--- a/libc/src/__support/FPUtil/CMakeLists.txt
+++ b/libc/src/__support/FPUtil/CMakeLists.txt
@@ -106,6 +106,7 @@ add_header_library(
     .normal_float
     libc.src.__support.CPP.type_traits
     libc.src.__support.common
+    libc.src.__support.macros.optimization
 )
 
 add_header_library(
diff --git a/libc/src/__support/FPUtil/DivisionAndRemainderOperations.h b/libc/src/__support/FPUtil/DivisionAndRemainderOperations.h
index bc22a2331f12d7..d167d249264c36 100644
--- a/libc/src/__support/FPUtil/DivisionAndRemainderOperations.h
+++ b/libc/src/__support/FPUtil/DivisionAndRemainderOperations.h
@@ -16,6 +16,7 @@
 #include "src/__support/CPP/type_traits.h"
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
 
 namespace LIBC_NAMESPACE_DECL {
 namespace fputil {
@@ -27,21 +28,31 @@ static constexpr int QUOTIENT_LSB_BITS = 3;
 template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
 LIBC_INLINE constexpr T remquo(T x, T y, int &q) {
   FPBits<T> xbits(x), ybits(y);
-  if (xbits.is_nan())
-    return x;
-  if (ybits.is_nan())
-    return y;
-  if (xbits.is_inf() || ybits.is_zero())
-    return FPBits<T>::quiet_nan().get_val();
-
-  if (xbits.is_zero()) {
-    q = 0;
-    return LIBC_NAMESPACE::fputil::copysign(T(0.0), x);
-  }
+  if (LIBC_UNLIKELY(xbits.is_inf_or_nan() || ybits.is_inf_or_nan() ||
+                    xbits.is_zero() || ybits.is_zero())) {
+    if (xbits.is_signaling_nan() || ybits.is_signaling_nan()) {
+      raise_except_if_required(FE_INVALID);
+      return FPBits<T>::quiet_nan().get_val();
+    }
+    if (xbits.is_nan())
+      return x;
+    if (ybits.is_nan())
+      return y;
+    if (xbits.is_inf() || ybits.is_zero()) {
+      set_errno_if_required(EDOM);
+      raise_except_if_required(FE_INVALID);
+      return FPBits<T>::quiet_nan().get_val();
+    }
 
-  if (ybits.is_inf()) {
-    q = 0;
-    return x;
+    if (xbits.is_zero()) {
+      q = 0;
+      return LIBC_NAMESPACE::fputil::copysign(T(0.0), x);
+    }
+
+    if (ybits.is_inf()) {
+      q = 0;
+      return x;
+    }
   }
 
   const Sign result_sign =
diff --git a/libc/test/src/math/smoke/RemQuoTest.h b/libc/test/src/math/smoke/RemQuoTest.h
index ec0e7636f52599..a7e9a8f9e36fc8 100644
--- a/libc/test/src/math/smoke/RemQuoTest.h
+++ b/libc/test/src/math/smoke/RemQuoTest.h
@@ -9,6 +9,8 @@
 #ifndef LLVM_LIBC_TEST_SRC_MATH_REMQUOTEST_H
 #define LLVM_LIBC_TEST_SRC_MATH_REMQUOTEST_H
 
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
 #include "src/__support/FPUtil/FPBits.h"
 #include "test/UnitTest/FEnvSafeTest.h"
 #include "test/UnitTest/FPMatcher.h"
@@ -18,14 +20,7 @@ using LIBC_NAMESPACE::Sign;
 
 template <typename T>
 class RemQuoTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
-  using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
-  using StorageType = typename FPBits::StorageType;
-
-  const T inf = FPBits::inf(Sign::POS).get_val();
-  const T neg_inf = FPBits::inf(Sign::NEG).get_val();
-  const T zero = FPBits::zero(Sign::POS).get_val();
-  const T neg_zero = FPBits::zero(Sign::NEG).get_val();
-  const T nan = FPBits::quiet_nan().get_val();
+  DECLARE_SPECIAL_CONSTANTS(T)
 
 public:
   typedef T (*RemQuoFunc)(T, T, int *);
@@ -36,60 +31,114 @@ class RemQuoTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
 
     y = T(1.0);
     x = inf;
-    EXPECT_FP_EQ(nan, func(x, y, &quotient));
+    EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(x, y, &quotient), FE_INVALID);
+    EXPECT_MATH_ERRNO(EDOM);
     x = neg_inf;
-    EXPECT_FP_EQ(nan, func(x, y, &quotient));
+    EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(x, y, &quotient), FE_INVALID);
+    EXPECT_MATH_ERRNO(EDOM);
 
     x = T(1.0);
     y = zero;
-    EXPECT_FP_EQ(nan, func(x, y, &quotient));
+    EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(x, y, &quotient), FE_INVALID);
+    EXPECT_MATH_ERRNO(EDOM);
     y = neg_zero;
-    EXPECT_FP_EQ(nan, func(x, y, &quotient));
-
-    y = nan;
-    x = T(1.0);
-    EXPECT_FP_EQ(nan, func(x, y, &quotient));
+    EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(x, y, &quotient), FE_INVALID);
+    EXPECT_MATH_ERRNO(EDOM);
 
-    y = T(1.0);
-    x = nan;
-    EXPECT_FP_EQ(nan, func(x, y, &quotient));
-
-    x = nan;
-    y = nan;
-    EXPECT_FP_EQ(nan, func(x, y, &quotient));
+    EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(zero, zero, &quotient), FE_INVALID);
+    EXPECT_MATH_ERRNO(EDOM);
+    EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(inf, neg_inf, &quotient),
+                                FE_INVALID);
+    EXPECT_MATH_ERRNO(EDOM);
 
-    x = zero;
-    y = T(1.0);
-    EXPECT_FP_EQ(func(x, y, &quotient), zero);
+    y = aNaN;
+    x = T(1.0);
+    EXPECT_FP_EQ(aNaN, func(x, y, &quotient));
 
-    x = neg_zero;
     y = T(1.0);
-    EXPECT_FP_EQ(func(x, y, &quotient), neg_zero);
-
-    x = T(1.125);
-    y = inf;
-    EXPECT_FP_EQ(func(x, y, &quotient), x);
-    EXPECT_EQ(quotient, 0);
+    x = aNaN;
+    EXPECT_FP_EQ(aNaN, func(x, y, &quotient));
+
+    x = aNaN;
+    y = aNaN;
+    EXPECT_FP_EQ(aNaN, func(x, y, &quotient));
+
+    const T finite = T(1.0);
+    const T others[] = {finite, zero, inf, aNaN, sNaN};
+    T result = zero;
+    for (T other : others) {
+      EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, result = func(sNaN, other, &quotient),
+                                  FE_INVALID);
+      EXPECT_MATH_ERRNO(0);
+      EXPECT_FALSE(FPBits(result).is_signaling_nan());
+      EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, result = func(other, sNaN, &quotient),
+                                  FE_INVALID);
+      EXPECT_MATH_ERRNO(0);
+      EXPECT_FALSE(FPBits(result).is_signaling_nan());
+    }
+
+    // A quiet NaN takes precedence over an otherwise invalid domain.
+    EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(aNaN, zero, &quotient), 0);
+    EXPECT_MATH_ERRNO(0);
+    EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(inf, aNaN, &quotient), 0);
+    EXPECT_MATH_ERRNO(0);
+
+    const T zeros[] = {zero, neg_zero};
+    const T denominators[] = {finite, T(-1.0), inf, neg_inf};
+    for (T numerator : zeros) {
+      for (T denominator : denominators) {
+        quotient = 123;
+        EXPECT_FP_EQ_WITH_EXCEPTION(numerator,
+                                    func(numerator, denominator, &quotient), 0);
+        EXPECT_MATH_ERRNO(0);
+        EXPECT_EQ(quotient, 0);
+      }
+    }
+
+    const T numerators[] = {T(1.125), T(-1.125), min_denormal,
+                            neg_min_denormal};
+    const T infinities[] = {inf, neg_inf};
+    for (T numerator : numerators) {
+      for (T denominator : infinities) {
+        testRegular(func, numerator, denominator, numerator, 0);
+      }
+    }
   }
 
   void testEqualNumeratorAndDenominator(RemQuoFunc func) {
-    T x = T(1.125), y = T(1.125);
-    int q;
+    const T values[] = {T(1.125), min_denormal, max_denormal, min_normal};
 
     // When the remainder is zero, the standard requires it to
     // have the same sign as x.
 
-    EXPECT_FP_EQ(func(x, y, &q), zero);
-    EXPECT_EQ(q, 1);
-
-    EXPECT_FP_EQ(func(x, -y, &q), zero);
-    EXPECT_EQ(q, -1);
-
-    EXPECT_FP_EQ(func(-x, y, &q), neg_zero);
-    EXPECT_EQ(q, -1);
+    for (T x : values) {
+      testRegular(func, x, x, zero, 1);
+      testRegular(func, x, -x, zero, -1);
+      testRegular(func, -x, x, neg_zero, -1);
+      testRegular(func, -x, -x, neg_zero, 1);
+    }
+  }
 
-    EXPECT_FP_EQ(func(-x, -y, &q), neg_zero);
-    EXPECT_EQ(q, 1);
+private:
+  void testRegular(RemQuoFunc func, T x, T y, T expected,
+                   int expected_quotient) {
+#ifdef FE_DENORM
+    constexpr int DENORM_EXCEPT = FE_DENORM;
+#elif defined(FE_DENORMAL)
+    constexpr int DENORM_EXCEPT = FE_DENORMAL;
+#elif defined(__FE_DENORM)
+    constexpr int DENORM_EXCEPT = __FE_DENORM;
+#else
+    constexpr int DENORM_EXCEPT = 0;
+#endif
+    int quotient = 123;
+    LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
+    EXPECT_FP_EQ(expected, func(x, y, &quotient));
+    EXPECT_MATH_ERRNO(0);
+    // Denormal operands may raise a nonstandard exception on some targets.
+    LIBC_NAMESPACE::fputil::clear_except(DENORM_EXCEPT);
+    EXPECT_FP_EXCEPTION(0);
+    EXPECT_EQ(quotient, expected_quotient);
   }
 };
 



More information about the libc-commits mailing list