[libc-commits] [libc] [libc][math] Return success when canonicalize quiets a signaling NaN (PR #236476)
via libc-commits
libc-commits at lists.llvm.org
Thu Nov 5 05:53:50 PST 2026
https://github.com/sriramshastry updated https://github.com/llvm/llvm-project/pull/236476
>From 7887daf82b6d13a1ed2e73af4702fe0673e1066d Mon Sep 17 00:00:00 2001
From: sriramshastry <sriramshastry at gmail.com>
Date: Wed, 28 Oct 2026 16:21:24 +0000
Subject: [PATCH 1/2] [libc][math] Return success when canonicalize quiets a
signaling NaN
Return zero after storing a canonical quiet NaN from a signaling input.
Preserve invalid reporting and the separate unsupported-encoding paths.
Update the shared smoke fixture to check successful status and explicitly
verify that the stored result is quiet.
Signed-off-by: sriramshastry <sriramshastry at gmail.com>
---
libc/src/__support/FPUtil/BasicOperations.h | 6 +++---
libc/test/src/math/smoke/CanonicalizeTest.h | 3 ++-
2 files changed, 5 insertions(+), 4 deletions(-)
diff --git a/libc/src/__support/FPUtil/BasicOperations.h b/libc/src/__support/FPUtil/BasicOperations.h
index 5b81b921296a04..9eb200b5e2bff4 100644
--- a/libc/src/__support/FPUtil/BasicOperations.h
+++ b/libc/src/__support/FPUtil/BasicOperations.h
@@ -318,7 +318,7 @@ LIBC_INLINE constexpr int canonicalize(T &cx, const T &x) {
cx = FPBits<T>::quiet_nan(sx.sign(), sx.get_explicit_mantissa())
.get_val();
raise_except_if_required(FE_INVALID);
- return 1;
+ return 0;
} else
cx = x;
} else if (exponent == 0 && bit63)
@@ -331,13 +331,13 @@ LIBC_INLINE constexpr int canonicalize(T &cx, const T &x) {
cx =
FPBits<T>::quiet_nan(sx.sign(), sx.get_explicit_mantissa()).get_val();
raise_except_if_required(FE_INVALID);
- return 1;
+ return 0;
} else
cx = x;
} else if (LIBC_UNLIKELY(sx.is_signaling_nan())) {
cx = FPBits<T>::quiet_nan(sx.sign(), sx.get_explicit_mantissa()).get_val();
raise_except_if_required(FE_INVALID);
- return 1;
+ return 0;
} else
cx = x;
return 0;
diff --git a/libc/test/src/math/smoke/CanonicalizeTest.h b/libc/test/src/math/smoke/CanonicalizeTest.h
index fc7f9caf74671b..e2b1ca6bfa503f 100644
--- a/libc/test/src/math/smoke/CanonicalizeTest.h
+++ b/libc/test/src/math/smoke/CanonicalizeTest.h
@@ -51,8 +51,9 @@ class CanonicalizeTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
TEST_SPECIAL(cx, neg_inf, 0, 0);
EXPECT_FP_EQ(cx, neg_inf);
- TEST_SPECIAL(cx, sNaN, 1, FE_INVALID);
+ TEST_SPECIAL(cx, sNaN, 0, FE_INVALID);
EXPECT_FP_EQ(cx, aNaN);
+ EXPECT_FALSE(FPBits(cx).is_signaling_nan());
}
void testX64_80SpecialNumbers(CanonicalizeFunc f) {
>From 07dc259ba44843a20254cc49efccf3045278b441 Mon Sep 17 00:00:00 2001
From: sriramshastry <sriramshastry at gmail.com>
Date: Wed, 28 Oct 2026 16:41:51 +0000
Subject: [PATCH 2/2] [libc][math] Correct iscanonical signaling NaN
expectations
Signed-off-by: sriramshastry <sriramshastry at gmail.com>
---
libc/src/__support/FPUtil/BasicOperations.h | 10 ++++++
libc/src/__support/math/iscanonical.h | 3 +-
libc/src/__support/math/iscanonicalbf16.h | 3 +-
libc/src/__support/math/iscanonicalf.h | 3 +-
libc/src/__support/math/iscanonicalf128.h | 3 +-
libc/src/__support/math/iscanonicalf16.h | 3 +-
libc/src/__support/math/iscanonicalf80.h | 3 +-
libc/src/__support/math/iscanonicall.h | 3 +-
libc/test/src/math/smoke/IsCanonicalTest.h | 40 +++++++++++++++++++--
9 files changed, 54 insertions(+), 17 deletions(-)
diff --git a/libc/src/__support/FPUtil/BasicOperations.h b/libc/src/__support/FPUtil/BasicOperations.h
index 9eb200b5e2bff4..eaf7706965efc4 100644
--- a/libc/src/__support/FPUtil/BasicOperations.h
+++ b/libc/src/__support/FPUtil/BasicOperations.h
@@ -280,6 +280,16 @@ LIBC_INLINE constexpr int issignaling_impl(const T &x) {
return sx.is_signaling_nan();
}
+template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
+LIBC_INLINE constexpr int iscanonical_impl(const T &x) {
+ if constexpr (get_fp_type<T>() == FPType::X86_Binary80) {
+ FPBits<T> sx(x);
+ // The explicit integer bit is set exactly when the exponent is nonzero.
+ return sx.get_implicit_bit() == (sx.get_biased_exponent() != 0);
+ }
+ return 1;
+}
+
template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
LIBC_INLINE constexpr int canonicalize(T &cx, const T &x) {
FPBits<T> sx(x);
diff --git a/libc/src/__support/math/iscanonical.h b/libc/src/__support/math/iscanonical.h
index db0388c150eeb4..16e9f857bd0c64 100644
--- a/libc/src/__support/math/iscanonical.h
+++ b/libc/src/__support/math/iscanonical.h
@@ -17,8 +17,7 @@ namespace math {
#undef iscanonical
LIBC_INLINE constexpr int iscanonical(double x) {
- double temp{};
- return fputil::canonicalize(temp, x) == 0;
+ return fputil::iscanonical_impl(x);
}
} // namespace math
diff --git a/libc/src/__support/math/iscanonicalbf16.h b/libc/src/__support/math/iscanonicalbf16.h
index 8072fad9323acd..86c345c460c598 100644
--- a/libc/src/__support/math/iscanonicalbf16.h
+++ b/libc/src/__support/math/iscanonicalbf16.h
@@ -17,8 +17,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
LIBC_INLINE constexpr int iscanonicalbf16(bfloat16 x) {
- bfloat16 tmp{};
- return fputil::canonicalize(tmp, x) == 0;
+ return fputil::iscanonical_impl(x);
}
} // namespace math
diff --git a/libc/src/__support/math/iscanonicalf.h b/libc/src/__support/math/iscanonicalf.h
index bff6cfaed26a5c..8ac5e249dad4df 100644
--- a/libc/src/__support/math/iscanonicalf.h
+++ b/libc/src/__support/math/iscanonicalf.h
@@ -16,8 +16,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
LIBC_INLINE constexpr int iscanonicalf(float x) {
- float temp{};
- return fputil::canonicalize(temp, x) == 0;
+ return fputil::iscanonical_impl(x);
}
} // namespace math
diff --git a/libc/src/__support/math/iscanonicalf128.h b/libc/src/__support/math/iscanonicalf128.h
index d517038ec6d701..23b9a45cdb21f9 100644
--- a/libc/src/__support/math/iscanonicalf128.h
+++ b/libc/src/__support/math/iscanonicalf128.h
@@ -19,8 +19,7 @@ namespace math {
using LIBC_NAMESPACE::fputil::Float128;
LIBC_INLINE constexpr int iscanonicalf128(Float128 x) {
- Float128 temp{};
- return fputil::canonicalize(temp, x) == 0;
+ return fputil::iscanonical_impl(x);
}
} // namespace math
diff --git a/libc/src/__support/math/iscanonicalf16.h b/libc/src/__support/math/iscanonicalf16.h
index e152e13cbfccb9..f548cdc5358ffa 100644
--- a/libc/src/__support/math/iscanonicalf16.h
+++ b/libc/src/__support/math/iscanonicalf16.h
@@ -20,8 +20,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
LIBC_INLINE constexpr int iscanonicalf16(float16 x) {
- float16 temp{};
- return fputil::canonicalize(temp, x) == 0;
+ return fputil::iscanonical_impl(x);
}
} // namespace math
diff --git a/libc/src/__support/math/iscanonicalf80.h b/libc/src/__support/math/iscanonicalf80.h
index b2b8b51ccf4012..d42f535228c306 100644
--- a/libc/src/__support/math/iscanonicalf80.h
+++ b/libc/src/__support/math/iscanonicalf80.h
@@ -24,8 +24,7 @@ namespace math {
using LIBC_NAMESPACE::fputil::Float80;
LIBC_INLINE constexpr int iscanonicalf80(Float80 x) {
- Float80 temp{};
- return fputil::canonicalize(temp, x) == 0;
+ return fputil::iscanonical_impl(x);
}
} // namespace math
diff --git a/libc/src/__support/math/iscanonicall.h b/libc/src/__support/math/iscanonicall.h
index e989441be26550..c8bd29e15e1e6d 100644
--- a/libc/src/__support/math/iscanonicall.h
+++ b/libc/src/__support/math/iscanonicall.h
@@ -16,8 +16,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace math {
LIBC_INLINE constexpr int iscanonicall(long double x) {
- long double temp{};
- return fputil::canonicalize(temp, x) == 0;
+ return fputil::iscanonical_impl(x);
}
} // namespace math
diff --git a/libc/test/src/math/smoke/IsCanonicalTest.h b/libc/test/src/math/smoke/IsCanonicalTest.h
index 011c3d3cb524e4..fee0591c46bd68 100644
--- a/libc/test/src/math/smoke/IsCanonicalTest.h
+++ b/libc/test/src/math/smoke/IsCanonicalTest.h
@@ -22,10 +22,13 @@ class IsCanonicalTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
typedef int (*IsCanonicalFunc)(T);
void testSpecialNumbers(IsCanonicalFunc func) {
+ LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
EXPECT_EQ(func(aNaN), 1);
EXPECT_EQ(func(neg_aNaN), 1);
- EXPECT_EQ(func(sNaN), 0);
- EXPECT_EQ(func(neg_sNaN), 0);
+ EXPECT_EQ(func(sNaN), 1);
+ EXPECT_FP_EXCEPTION(0);
+ EXPECT_EQ(func(neg_sNaN), 1);
+ EXPECT_FP_EXCEPTION(0);
EXPECT_EQ(func(inf), 1);
EXPECT_EQ(func(neg_inf), 1);
EXPECT_EQ(func(min_normal), 1);
@@ -36,6 +39,7 @@ class IsCanonicalTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
EXPECT_EQ(func(max_denormal), 1);
EXPECT_EQ(func(zero), 1);
EXPECT_EQ(func(neg_zero), 1);
+ EXPECT_FP_EXCEPTION(0);
}
void testRoundedNumbers(IsCanonicalFunc func) {
@@ -46,11 +50,41 @@ class IsCanonicalTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
EXPECT_EQ(func(T(1234.0)), 1);
EXPECT_EQ(func(T(-1234.0)), 1);
}
+
+ void testX86Binary80SpecialNumbers(IsCanonicalFunc func) {
+ if constexpr (LIBC_NAMESPACE::fputil::get_fp_type<T>() ==
+ LIBC_NAMESPACE::fputil::FPType::X86_Binary80) {
+ FPBits pseudo_denormal = FPBits::min_normal();
+ pseudo_denormal.set_biased_exponent(0);
+ FPBits unnormal = FPBits::min_normal();
+ unnormal.set_implicit_bit(false);
+ FPBits pseudo_inf = FPBits::inf();
+ pseudo_inf.set_implicit_bit(false);
+ FPBits pseudo_nan = FPBits::quiet_nan();
+ pseudo_nan.set_implicit_bit(false);
+ FPBits pseudo_snan = FPBits::signaling_nan();
+ pseudo_snan.set_implicit_bit(false);
+ const FPBits inputs[] = {pseudo_denormal, unnormal, pseudo_inf,
+ pseudo_nan, pseudo_snan};
+ for (FPBits bits : inputs) {
+ for (int negative = 0; negative < 2; ++negative) {
+ bits.set_sign(negative ? Sign::NEG : Sign::POS);
+ const T value = bits.get_val();
+ LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
+ EXPECT_EQ(func(value), 0);
+ EXPECT_FP_EXCEPTION(0);
+ }
+ }
+ }
+ }
};
#define LIST_ISCANONICAL_TESTS(Name, T, func) \
using LlvmLibc##Name##Test = IsCanonicalTest<T>; \
TEST_F(LlvmLibc##Name##Test, SpecialNumbers) { testSpecialNumbers(&func); } \
- TEST_F(LlvmLibc##Name##Test, RoundedNumbers) { testRoundedNumbers(&func); }
+ TEST_F(LlvmLibc##Name##Test, RoundedNumbers) { testRoundedNumbers(&func); } \
+ TEST_F(LlvmLibc##Name##Test, X86Binary80SpecialNumbers) { \
+ testX86Binary80SpecialNumbers(&func); \
+ }
#endif // LLVM_LIBC_TEST_SRC_MATH_SMOKE_ISCANONICALTEST_H
More information about the libc-commits
mailing list