Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libc][math] Fix is_quiet_nan function in FPBits #76931

Merged
merged 2 commits into from
Jan 4, 2024

Conversation

nishantwrp
Copy link
Contributor

No description provided.

@nishantwrp
Copy link
Contributor Author

@lntue ptal!

@llvmbot llvmbot added the libc label Jan 4, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Jan 4, 2024

@llvm/pr-subscribers-libc

Author: Nishant Mittal (nishantwrp)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/76931.diff

3 Files Affected:

  • (modified) libc/src/__support/FPUtil/FPBits.h (+4-4)
  • (modified) libc/src/__support/FPUtil/x86_64/LongDoubleBits.h (+4)
  • (modified) libc/test/src/__support/FPUtil/fpbits_test.cpp (+15)
diff --git a/libc/src/__support/FPUtil/FPBits.h b/libc/src/__support/FPUtil/FPBits.h
index 8304b76d3d8ad0..59e0f34fec3f87 100644
--- a/libc/src/__support/FPUtil/FPBits.h
+++ b/libc/src/__support/FPUtil/FPBits.h
@@ -167,9 +167,9 @@ struct FPBaseMasksAndShifts : public internal::FPLayout<fp_type> {
           ? bit_at(SIG_LEN - 1) | bit_at(SIG_LEN - 2) // 0b1100...
           : bit_at(SIG_LEN - 1);                      // 0b1000...
 
-  // If a number x is a NAN, then it is a signalling NAN if:
-  //   SIGNALING_NAN_MASK & bits(x) != 0
-  LIBC_INLINE_VAR static constexpr StorageType SIGNALING_NAN_MASK =
+  // Mask to generate a default signaling NAN. Any NAN that is not
+  // a quiet NAN is considered a signaling NAN.
+  LIBC_INLINE_VAR static constexpr StorageType DEFAULT_SIGNALING_NAN =
       UP::ENCODING == internal::FPEncoding::X86_ExtendedPrecision
           ? bit_at(SIG_LEN - 1) | bit_at(SIG_LEN - 3) // 0b1010...
           : bit_at(SIG_LEN - 2);                      // 0b0100...
@@ -376,7 +376,7 @@ template <typename T> struct FPBits : public internal::FPRep<get_fp_type<T>()> {
   }
 
   LIBC_INLINE constexpr bool is_quiet_nan() const {
-    return (bits & EXP_SIG_MASK) == (EXP_MASK | QUIET_NAN_MASK);
+    return (bits & EXP_SIG_MASK) >= (EXP_MASK | QUIET_NAN_MASK);
   }
 
   LIBC_INLINE constexpr bool is_inf_or_nan() const {
diff --git a/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h b/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
index 4dc5d25e269820..2f0323c7b0d823 100644
--- a/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
+++ b/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
@@ -115,6 +115,10 @@ struct FPBits<long double> : public internal::FPRep<FPType::X86_Binary80> {
            (get_biased_exponent() != 0 && get_implicit_bit() == 0);
   }
 
+  LIBC_INLINE constexpr bool is_quiet_nan() const {
+    return (bits & EXP_SIG_MASK) >= (EXP_MASK | QUIET_NAN_MASK);
+  }
+
   // Methods below this are used by tests.
 
   LIBC_INLINE static constexpr long double zero() { return 0.0l; }
diff --git a/libc/test/src/__support/FPUtil/fpbits_test.cpp b/libc/test/src/__support/FPUtil/fpbits_test.cpp
index fa743855c48619..e2dbe248ef2131 100644
--- a/libc/test/src/__support/FPUtil/fpbits_test.cpp
+++ b/libc/test/src/__support/FPUtil/fpbits_test.cpp
@@ -69,6 +69,9 @@ TEST(LlvmLibcFPBitsTest, FloatType) {
   EXPECT_EQ(negnum.uintval(), static_cast<uint32_t>(0xBF900000));
   EXPECT_STREQ(LIBC_NAMESPACE::str(negnum).c_str(),
                "0xBF900000 = (S: 1, E: 0x007F, M: 0x00100000)");
+
+  FloatBits quiet_nan = FloatBits(FloatBits::build_quiet_nan(1));
+  EXPECT_EQ(quiet_nan.is_quiet_nan(), true);
 }
 
 TEST(LlvmLibcFPBitsTest, DoubleType) {
@@ -129,6 +132,9 @@ TEST(LlvmLibcFPBitsTest, DoubleType) {
   EXPECT_EQ(negnum.uintval(), static_cast<uint64_t>(0xBFF2000000000000));
   EXPECT_STREQ(LIBC_NAMESPACE::str(negnum).c_str(),
                "0xBFF2000000000000 = (S: 1, E: 0x03FF, M: 0x0002000000000000)");
+
+  DoubleBits quiet_nan = DoubleBits(DoubleBits::build_quiet_nan(1));
+  EXPECT_EQ(quiet_nan.is_quiet_nan(), true);
 }
 
 #ifdef LIBC_TARGET_ARCH_IS_X86
@@ -210,6 +216,9 @@ TEST(LlvmLibcFPBitsTest, X86LongDoubleType) {
       LIBC_NAMESPACE::str(negnum).c_str(),
       "0x000000000000BFFF9000000000000000 = "
       "(S: 1, E: 0x3FFF, I: 1, M: 0x00000000000000001000000000000000)");
+
+  LongDoubleBits quiet_nan = LongDoubleBits(LongDoubleBits::build_quiet_nan(1));
+  EXPECT_EQ(quiet_nan.is_quiet_nan(), true);
 }
 #else
 TEST(LlvmLibcFPBitsTest, LongDoubleType) {
@@ -284,6 +293,9 @@ TEST(LlvmLibcFPBitsTest, LongDoubleType) {
   EXPECT_STREQ(LIBC_NAMESPACE::str(negnum).c_str(),
                "0xBFFF2000000000000000000000000000 = "
                "(S: 1, E: 0x3FFF, M: 0x00002000000000000000000000000000)");
+
+  LongDoubleBits quiet_nan = LongDoubleBits(LongDoubleBits::build_quiet_nan(1));
+  EXPECT_EQ(quiet_nan.is_quiet_nan(), true);
 #endif
 }
 #endif
@@ -357,5 +369,8 @@ TEST(LlvmLibcFPBitsTest, Float128Type) {
   EXPECT_STREQ(LIBC_NAMESPACE::str(negnum).c_str(),
                "0xBFFF2000000000000000000000000000 = "
                "(S: 1, E: 0x3FFF, M: 0x00002000000000000000000000000000)");
+
+  Float128Bits quiet_nan = Float128Bits(Float128Bits::build_quiet_nan(1));
+  EXPECT_EQ(quiet_nan.is_quiet_nan(), true);
 }
 #endif // LIBC_COMPILER_HAS_FLOAT128

@lntue lntue self-requested a review January 4, 2024 15:23
Copy link
Contributor

@lntue lntue left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this!

@lntue lntue merged commit 79a2e2b into llvm:main Jan 4, 2024
4 checks passed
@nishantwrp nishantwrp deleted the fix-quiet-nan branch January 4, 2024 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants