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][NFC] Make QNAN_MASK an implementation detail of FPBits #75945

Merged

Conversation

gchatelet
Copy link
Contributor

@gchatelet gchatelet commented Dec 19, 2023

No description provided.

@llvmbot
Copy link

llvmbot commented Dec 19, 2023

@llvm/pr-subscribers-libc

Author: Guillaume Chatelet (gchatelet)

Changes

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

4 Files Affected:

  • (modified) libc/src/__support/FPUtil/FPBits.h (+7-4)
  • (modified) libc/src/__support/FPUtil/FloatProperties.h (+12-13)
  • (modified) libc/src/__support/FPUtil/x86_64/LongDoubleBits.h (+6-2)
  • (modified) libc/src/__support/str_to_float.h (-1)
diff --git a/libc/src/__support/FPUtil/FPBits.h b/libc/src/__support/FPUtil/FPBits.h
index 7f5dd0fca58d4f..fd67c6e0ea5ff0 100644
--- a/libc/src/__support/FPUtil/FPBits.h
+++ b/libc/src/__support/FPUtil/FPBits.h
@@ -39,7 +39,11 @@ template <typename T> struct FPBits : private FloatProperties<T> {
   using FloatProperties<T>::EXP_LEN;
   using FloatProperties<T>::FRACTION_MASK;
   using FloatProperties<T>::FRACTION_LEN;
-  using FloatProperties<T>::QUIET_NAN_MASK;
+
+private:
+  using FloatProperties<T>::QNAN_MASK;
+
+public:
   using FloatProperties<T>::SIGN_MASK;
 
   // Reinterpreting bits as an integer value and interpreting the bits of an
@@ -90,7 +94,6 @@ template <typename T> struct FPBits : private FloatProperties<T> {
                 "Data type and integral representation have different sizes.");
 
   static constexpr int MAX_EXPONENT = (1 << EXP_LEN) - 1;
-
   static constexpr StorageType MIN_SUBNORMAL = StorageType(1);
   static constexpr StorageType MAX_SUBNORMAL = FRACTION_MASK;
   static constexpr StorageType MIN_NORMAL = (StorageType(1) << FRACTION_LEN);
@@ -154,7 +157,7 @@ template <typename T> struct FPBits : private FloatProperties<T> {
   }
 
   LIBC_INLINE constexpr bool is_quiet_nan() const {
-    return (bits & EXP_MANT_MASK) == (EXP_MASK | QUIET_NAN_MASK);
+    return (bits & EXP_MANT_MASK) == (EXP_MASK | QNAN_MASK);
   }
 
   LIBC_INLINE constexpr bool is_inf_or_nan() const {
@@ -196,7 +199,7 @@ template <typename T> struct FPBits : private FloatProperties<T> {
   }
 
   LIBC_INLINE static constexpr T build_quiet_nan(StorageType v) {
-    return build_nan(QUIET_NAN_MASK | v);
+    return build_nan(QNAN_MASK | v);
   }
 
   // The function convert integer number and unbiased exponent to proper float
diff --git a/libc/src/__support/FPUtil/FloatProperties.h b/libc/src/__support/FPUtil/FloatProperties.h
index 896c29919e2f77..dd714fde017874 100644
--- a/libc/src/__support/FPUtil/FloatProperties.h
+++ b/libc/src/__support/FPUtil/FloatProperties.h
@@ -143,16 +143,6 @@ struct FPProperties : public internal::FPBaseProperties<fp_type> {
     return StorageType(1) << position;
   }
 
-  LIBC_INLINE_VAR static constexpr StorageType QNAN_MASK =
-      UP::ENCODING == internal::FPEncoding::X86_ExtendedPrecision
-          ? bit_at(SIG_LEN - 1) | bit_at(SIG_LEN - 2) // 0b1100...
-          : bit_at(SIG_LEN - 1);                      // 0b1000...
-
-  LIBC_INLINE_VAR static constexpr StorageType SNAN_MASK =
-      UP::ENCODING == internal::FPEncoding::X86_ExtendedPrecision
-          ? bit_at(SIG_LEN - 1) | bit_at(SIG_LEN - 3) // 0b1010...
-          : bit_at(SIG_LEN - 2);                      // 0b0100...
-
 public:
   // The number of bits after the decimal dot when the number is in normal form.
   LIBC_INLINE_VAR static constexpr int FRACTION_LEN =
@@ -166,9 +156,18 @@ struct FPProperties : public internal::FPBaseProperties<fp_type> {
       EXP_MASK | SIG_MASK;
 
   // If a number x is a NAN, then it is a quiet NAN if:
-  //   QuietNaNMask & bits(x) != 0
-  // Else, it is a signalling NAN.
-  static constexpr StorageType QUIET_NAN_MASK = QNAN_MASK;
+  //   QNAN_MASK & bits(x) != 0
+  LIBC_INLINE_VAR static constexpr StorageType QNAN_MASK =
+      UP::ENCODING == internal::FPEncoding::X86_ExtendedPrecision
+          ? 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:
+  //   SNAN_MASK & bits(x) != 0
+  LIBC_INLINE_VAR static constexpr StorageType SNAN_MASK =
+      UP::ENCODING == internal::FPEncoding::X86_ExtendedPrecision
+          ? bit_at(SIG_LEN - 1) | bit_at(SIG_LEN - 3) // 0b1010...
+          : bit_at(SIG_LEN - 2);                      // 0b0100...
 };
 
 //-----------------------------------------------------------------------------
diff --git a/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h b/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
index 89c47063ebac4d..27435abdd16d4b 100644
--- a/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
+++ b/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
@@ -35,7 +35,11 @@ template <> struct FPBits<long double> : private FloatProperties<long double> {
   using FloatProperties<long double>::EXP_LEN;
   using FloatProperties<long double>::FRACTION_MASK;
   using FloatProperties<long double>::FRACTION_LEN;
-  using FloatProperties<long double>::QUIET_NAN_MASK;
+
+private:
+  using FloatProperties<long double>::QNAN_MASK;
+
+public:
   using FloatProperties<long double>::SIGN_MASK;
 
   static constexpr int MAX_EXPONENT = 0x7FFF;
@@ -196,7 +200,7 @@ template <> struct FPBits<long double> : private FloatProperties<long double> {
   }
 
   LIBC_INLINE static constexpr long double build_quiet_nan(StorageType v) {
-    return build_nan(QUIET_NAN_MASK | v);
+    return build_nan(QNAN_MASK | v);
   }
 
   LIBC_INLINE static constexpr long double min_normal() {
diff --git a/libc/src/__support/str_to_float.h b/libc/src/__support/str_to_float.h
index 9984bcd7064d75..ec2b8b062b9b78 100644
--- a/libc/src/__support/str_to_float.h
+++ b/libc/src/__support/str_to_float.h
@@ -1167,7 +1167,6 @@ LIBC_INLINE StrToNumResult<T> strtofloatingpoint(const char *__restrict src) {
           index = left_paren;
         }
       }
-      nan_mantissa |= fputil::FloatProperties<T>::QUIET_NAN_MASK;
       if (result.get_sign()) {
         result = FPBits(result.build_quiet_nan(nan_mantissa));
         result.set_sign(true);

@gchatelet gchatelet changed the title [libc][NFC] Make QNAN_MASK an implementation detail [libc][NFC] Make QNAN_MASK an implementation detail of FPBits Dec 19, 2023
libc/src/__support/FPUtil/FPBits.h Outdated Show resolved Hide resolved
@gchatelet gchatelet merged commit 747061f into llvm:main Dec 19, 2023
4 checks passed
@gchatelet gchatelet deleted the make_qnan_mask_an_implementation_detail branch December 19, 2023 16:16
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.

3 participants