[libc-commits] [libc] [libc][NFC] Make `QNAN_MASK` an implementation detail (PR #75945)

Guillaume Chatelet via libc-commits libc-commits at lists.llvm.org
Tue Dec 19 08:08:22 PST 2023


https://github.com/gchatelet created https://github.com/llvm/llvm-project/pull/75945

None

>From f8bcc8e0e8ac999a0d53151f20d8e4f382216599 Mon Sep 17 00:00:00 2001
From: Guillaume Chatelet <gchatelet at google.com>
Date: Tue, 19 Dec 2023 16:07:56 +0000
Subject: [PATCH] [libc][NFC] Make `QNAN_MASK` an implementation detail

---
 libc/src/__support/FPUtil/FPBits.h            | 11 +++++---
 libc/src/__support/FPUtil/FloatProperties.h   | 25 +++++++++----------
 .../__support/FPUtil/x86_64/LongDoubleBits.h  |  8 ++++--
 libc/src/__support/str_to_float.h             |  1 -
 4 files changed, 25 insertions(+), 20 deletions(-)

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);



More information about the libc-commits mailing list