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] Implement FPBits in terms of FloatProperties to reduce clutter #75196

Merged

Conversation

gchatelet
Copy link
Contributor

Also make type naming consistent:

  • UIntType instead of intU_t
  • FPBits instead of FPBits_t, FPB

…utter

Also make type naming consistent:
 - `UIntType` instead of `intU_t`
 - `FPBits` instead of `FPBits_t`, `FPB`
@llvmbot llvmbot added the libc label Dec 12, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Dec 12, 2023

@llvm/pr-subscribers-libc

Author: Guillaume Chatelet (gchatelet)

Changes

Also make type naming consistent:

  • UIntType instead of intU_t
  • FPBits instead of FPBits_t, FPB

Patch is 39.88 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/75196.diff

17 Files Affected:

  • (modified) libc/src/__support/FPUtil/FPBits.h (+34-34)
  • (modified) libc/src/__support/FPUtil/Hypot.h (+10-10)
  • (modified) libc/src/__support/FPUtil/generic/FMod.h (+37-37)
  • (modified) libc/src/__support/FPUtil/x86_64/LongDoubleBits.h (+29-27)
  • (modified) libc/src/__support/str_to_float.h (+5-5)
  • (modified) libc/src/math/generic/acoshf.cpp (+4-4)
  • (modified) libc/src/math/generic/asinhf.cpp (+3-3)
  • (modified) libc/src/math/generic/atanhf.cpp (+1-1)
  • (modified) libc/src/math/generic/erff.cpp (+1-1)
  • (modified) libc/src/math/generic/explogxf.h (+11-13)
  • (modified) libc/src/math/generic/inv_trigf_utils.h (+10-10)
  • (modified) libc/src/math/generic/log.cpp (+9-9)
  • (modified) libc/src/math/generic/log10.cpp (+9-9)
  • (modified) libc/src/math/generic/log1p.cpp (+15-15)
  • (modified) libc/src/math/generic/log2.cpp (+9-9)
  • (modified) libc/src/math/generic/sinhf.cpp (+2-3)
  • (modified) libc/src/math/generic/tanhf.cpp (+1-1)
diff --git a/libc/src/__support/FPUtil/FPBits.h b/libc/src/__support/FPUtil/FPBits.h
index 65c53921181a7..bef166e14d72b 100644
--- a/libc/src/__support/FPUtil/FPBits.h
+++ b/libc/src/__support/FPUtil/FPBits.h
@@ -36,71 +36,74 @@ template <typename T> struct ExponentWidth {
 // floating numbers. On x86 platforms however, the 'long double' type maps to
 // an x87 floating point format. This format is an IEEE 754 extension format.
 // It is handled as an explicit specialization of this class.
-template <typename T> struct FPBits {
+template <typename T> struct FPBits : private FloatProperties<T> {
   static_assert(cpp::is_floating_point_v<T>,
                 "FPBits instantiated with invalid type.");
+  using typename FloatProperties<T>::UIntType;
+  using FloatProperties<T>::BIT_WIDTH;
+  using FloatProperties<T>::EXP_MANT_MASK;
+  using FloatProperties<T>::EXPONENT_MASK;
+  using FloatProperties<T>::EXPONENT_BIAS;
+  using FloatProperties<T>::EXPONENT_WIDTH;
+  using FloatProperties<T>::MANTISSA_MASK;
+  using FloatProperties<T>::MANTISSA_WIDTH;
+  using FloatProperties<T>::QUIET_NAN_MASK;
+  using FloatProperties<T>::SIGN_MASK;
 
   // Reinterpreting bits as an integer value and interpreting the bits of an
   // integer value as a floating point value is used in tests. So, a convenient
   // type is provided for such reinterpretations.
-  using FloatProp = FloatProperties<T>;
-  using UIntType = typename FloatProp::UIntType;
-
   UIntType bits;
 
   LIBC_INLINE constexpr void set_mantissa(UIntType mantVal) {
-    mantVal &= (FloatProp::MANTISSA_MASK);
-    bits &= ~(FloatProp::MANTISSA_MASK);
+    mantVal &= MANTISSA_MASK;
+    bits &= ~MANTISSA_MASK;
     bits |= mantVal;
   }
 
   LIBC_INLINE constexpr UIntType get_mantissa() const {
-    return bits & FloatProp::MANTISSA_MASK;
+    return bits & MANTISSA_MASK;
   }
 
   LIBC_INLINE constexpr void set_biased_exponent(UIntType expVal) {
-    expVal = (expVal << (FloatProp::MANTISSA_WIDTH)) & FloatProp::EXPONENT_MASK;
-    bits &= ~(FloatProp::EXPONENT_MASK);
+    expVal = (expVal << MANTISSA_WIDTH) & EXPONENT_MASK;
+    bits &= ~EXPONENT_MASK;
     bits |= expVal;
   }
 
   LIBC_INLINE constexpr uint16_t get_biased_exponent() const {
-    return uint16_t((bits & FloatProp::EXPONENT_MASK) >>
-                    (FloatProp::MANTISSA_WIDTH));
+    return uint16_t((bits & EXPONENT_MASK) >> MANTISSA_WIDTH);
   }
 
   // The function return mantissa with the implicit bit set iff the current
   // value is a valid normal number.
   LIBC_INLINE constexpr UIntType get_explicit_mantissa() {
     return ((get_biased_exponent() > 0 && !is_inf_or_nan())
-                ? (FloatProp::MANTISSA_MASK + 1)
+                ? (MANTISSA_MASK + 1)
                 : 0) |
-           (FloatProp::MANTISSA_MASK & bits);
+           (MANTISSA_MASK & bits);
   }
 
   LIBC_INLINE constexpr void set_sign(bool signVal) {
-    bits |= FloatProp::SIGN_MASK;
+    bits |= SIGN_MASK;
     if (!signVal)
-      bits -= FloatProp::SIGN_MASK;
+      bits -= SIGN_MASK;
   }
 
   LIBC_INLINE constexpr bool get_sign() const {
-    return (bits & FloatProp::SIGN_MASK) != 0;
+    return (bits & SIGN_MASK) != 0;
   }
 
   static_assert(sizeof(T) == sizeof(UIntType),
                 "Data type and integral representation have different sizes.");
 
-  static constexpr int EXPONENT_BIAS = (1 << (ExponentWidth<T>::VALUE - 1)) - 1;
-  static constexpr int MAX_EXPONENT = (1 << ExponentWidth<T>::VALUE) - 1;
+  static constexpr int MAX_EXPONENT = (1 << EXPONENT_WIDTH) - 1;
 
   static constexpr UIntType MIN_SUBNORMAL = UIntType(1);
-  static constexpr UIntType MAX_SUBNORMAL =
-      (UIntType(1) << MantissaWidth<T>::VALUE) - 1;
-  static constexpr UIntType MIN_NORMAL =
-      (UIntType(1) << MantissaWidth<T>::VALUE);
+  static constexpr UIntType MAX_SUBNORMAL = (UIntType(1) << MANTISSA_WIDTH) - 1;
+  static constexpr UIntType MIN_NORMAL = (UIntType(1) << MANTISSA_WIDTH);
   static constexpr UIntType MAX_NORMAL =
-      ((UIntType(MAX_EXPONENT) - 1) << MantissaWidth<T>::VALUE) | MAX_SUBNORMAL;
+      ((UIntType(MAX_EXPONENT) - 1) << MANTISSA_WIDTH) | MAX_SUBNORMAL;
 
   // We don't want accidental type promotions/conversions, so we require exact
   // type match.
@@ -151,32 +154,29 @@ template <typename T> struct FPBits {
   }
 
   LIBC_INLINE constexpr bool is_inf() const {
-    return (bits & FloatProp::EXP_MANT_MASK) == FloatProp::EXPONENT_MASK;
+    return (bits & EXP_MANT_MASK) == EXPONENT_MASK;
   }
 
   LIBC_INLINE constexpr bool is_nan() const {
-    return (bits & FloatProp::EXP_MANT_MASK) > FloatProp::EXPONENT_MASK;
+    return (bits & EXP_MANT_MASK) > EXPONENT_MASK;
   }
 
   LIBC_INLINE constexpr bool is_quiet_nan() const {
-    return (bits & FloatProp::EXP_MANT_MASK) ==
-           (FloatProp::EXPONENT_MASK | FloatProp::QUIET_NAN_MASK);
+    return (bits & EXP_MANT_MASK) == (EXPONENT_MASK | QUIET_NAN_MASK);
   }
 
   LIBC_INLINE constexpr bool is_inf_or_nan() const {
-    return (bits & FloatProp::EXPONENT_MASK) == FloatProp::EXPONENT_MASK;
+    return (bits & EXPONENT_MASK) == EXPONENT_MASK;
   }
 
   LIBC_INLINE static constexpr T zero(bool sign = false) {
-    return FPBits(sign ? FloatProp::SIGN_MASK : UIntType(0)).get_val();
+    return FPBits(sign ? SIGN_MASK : UIntType(0)).get_val();
   }
 
   LIBC_INLINE static constexpr T neg_zero() { return zero(true); }
 
   LIBC_INLINE static constexpr T inf(bool sign = false) {
-    return FPBits((sign ? FloatProp::SIGN_MASK : UIntType(0)) |
-                  FloatProp::EXPONENT_MASK)
-        .get_val();
+    return FPBits((sign ? SIGN_MASK : UIntType(0)) | EXPONENT_MASK).get_val();
   }
 
   LIBC_INLINE static constexpr T neg_inf() { return inf(true); }
@@ -204,7 +204,7 @@ template <typename T> struct FPBits {
   }
 
   LIBC_INLINE static constexpr T build_quiet_nan(UIntType v) {
-    return build_nan(FloatProp::QUIET_NAN_MASK | v);
+    return build_nan(QUIET_NAN_MASK | v);
   }
 
   // The function convert integer number and unbiased exponent to proper float
@@ -220,7 +220,7 @@ template <typename T> struct FPBits {
   LIBC_INLINE static constexpr FPBits<T> make_value(UIntType number, int ep) {
     FPBits<T> result;
     // offset: +1 for sign, but -1 for implicit first bit
-    int lz = cpp::countl_zero(number) - FloatProp::EXPONENT_WIDTH;
+    int lz = cpp::countl_zero(number) - EXPONENT_WIDTH;
     number <<= lz;
     ep -= lz;
 
diff --git a/libc/src/__support/FPUtil/Hypot.h b/libc/src/__support/FPUtil/Hypot.h
index 42d9e1b3f8cec..ecf992c0d964c 100644
--- a/libc/src/__support/FPUtil/Hypot.h
+++ b/libc/src/__support/FPUtil/Hypot.h
@@ -104,14 +104,14 @@ template <> struct DoubleLength<uint64_t> {
 //
 template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
 LIBC_INLINE T hypot(T x, T y) {
-  using FPBits_t = FPBits<T>;
-  using UIntType = typename FPBits<T>::UIntType;
+  using FPBits = FPBits<T>;
+  using UIntType = typename FPBits::UIntType;
   using DUIntType = typename DoubleLength<UIntType>::Type;
 
-  FPBits_t x_bits(x), y_bits(y);
+  FPBits x_bits(x), y_bits(y);
 
   if (x_bits.is_inf() || y_bits.is_inf()) {
-    return T(FPBits_t::inf());
+    return T(FPBits::inf());
   }
   if (x_bits.is_nan()) {
     return x;
@@ -193,11 +193,11 @@ LIBC_INLINE T hypot(T x, T y) {
       sticky_bits = sticky_bits || ((sum & 0x3U) != 0);
       sum >>= 2;
       ++out_exp;
-      if (out_exp >= FPBits_t::MAX_EXPONENT) {
+      if (out_exp >= FPBits::MAX_EXPONENT) {
         if (int round_mode = quick_get_round();
             round_mode == FE_TONEAREST || round_mode == FE_UPWARD)
-          return T(FPBits_t::inf());
-        return T(FPBits_t(FPBits_t::MAX_NORMAL));
+          return T(FPBits::inf());
+        return T(FPBits(FPBits::MAX_NORMAL));
       }
     } else {
       // For denormal result, we simply move the leading bit of the result to
@@ -251,10 +251,10 @@ LIBC_INLINE T hypot(T x, T y) {
   if (y_new >= (ONE >> 1)) {
     y_new -= ONE >> 1;
     ++out_exp;
-    if (out_exp >= FPBits_t::MAX_EXPONENT) {
+    if (out_exp >= FPBits::MAX_EXPONENT) {
       if (round_mode == FE_TONEAREST || round_mode == FE_UPWARD)
-        return T(FPBits_t::inf());
-      return T(FPBits_t(FPBits_t::MAX_NORMAL));
+        return T(FPBits::inf());
+      return T(FPBits(FPBits::MAX_NORMAL));
     }
   }
 
diff --git a/libc/src/__support/FPUtil/generic/FMod.h b/libc/src/__support/FPUtil/generic/FMod.h
index 7502660c88a13..5cf982150956e 100644
--- a/libc/src/__support/FPUtil/generic/FMod.h
+++ b/libc/src/__support/FPUtil/generic/FMod.h
@@ -123,9 +123,9 @@ template <typename T> struct FModExceptionalInputHandler {
                 "FModCStandardWrapper instantiated with invalid type.");
 
   LIBC_INLINE static bool pre_check(T x, T y, T &out) {
-    using FPB = fputil::FPBits<T>;
-    const T quiet_nan = FPB::build_quiet_nan(0);
-    FPB sx(x), sy(y);
+    using FPBits = fputil::FPBits<T>;
+    const T quiet_nan = FPBits::build_quiet_nan(0);
+    FPBits sx(x), sy(y);
     if (LIBC_LIKELY(!sy.is_zero() && !sy.is_inf_or_nan() &&
                     !sx.is_inf_or_nan())) {
       return false;
@@ -167,11 +167,11 @@ template <typename T> struct FModFastMathWrapper {
 
 template <typename T> class FModDivisionSimpleHelper {
 private:
-  using intU_t = typename FPBits<T>::UIntType;
+  using UIntType = typename FPBits<T>::UIntType;
 
 public:
-  LIBC_INLINE constexpr static intU_t
-  execute(int exp_diff, int sides_zeroes_count, intU_t m_x, intU_t m_y) {
+  LIBC_INLINE constexpr static UIntType
+  execute(int exp_diff, int sides_zeroes_count, UIntType m_x, UIntType m_y) {
     while (exp_diff > sides_zeroes_count) {
       exp_diff -= sides_zeroes_count;
       m_x <<= sides_zeroes_count;
@@ -185,24 +185,24 @@ template <typename T> class FModDivisionSimpleHelper {
 
 template <typename T> class FModDivisionInvMultHelper {
 private:
-  using FPB = FPBits<T>;
-  using intU_t = typename FPB::UIntType;
+  using FPBits = FPBits<T>;
+  using UIntType = typename FPBits::UIntType;
 
 public:
-  LIBC_INLINE constexpr static intU_t
-  execute(int exp_diff, int sides_zeroes_count, intU_t m_x, intU_t m_y) {
+  LIBC_INLINE constexpr static UIntType
+  execute(int exp_diff, int sides_zeroes_count, UIntType m_x, UIntType m_y) {
     if (exp_diff > sides_zeroes_count) {
-      intU_t inv_hy = (cpp::numeric_limits<intU_t>::max() / m_y);
+      UIntType inv_hy = (cpp::numeric_limits<UIntType>::max() / m_y);
       while (exp_diff > sides_zeroes_count) {
         exp_diff -= sides_zeroes_count;
-        intU_t hd =
-            (m_x * inv_hy) >> (FPB::FloatProp::BIT_WIDTH - sides_zeroes_count);
+        UIntType hd =
+            (m_x * inv_hy) >> (FPBits::BIT_WIDTH - sides_zeroes_count);
         m_x <<= sides_zeroes_count;
         m_x -= hd * m_y;
         while (LIBC_UNLIKELY(m_x > m_y))
           m_x -= m_y;
       }
-      intU_t hd = (m_x * inv_hy) >> (FPB::FloatProp::BIT_WIDTH - exp_diff);
+      UIntType hd = (m_x * inv_hy) >> (FPBits::BIT_WIDTH - exp_diff);
       m_x <<= exp_diff;
       m_x -= hd * m_y;
       while (LIBC_UNLIKELY(m_x > m_y))
@@ -222,44 +222,44 @@ class FMod {
                 "FMod instantiated with invalid type.");
 
 private:
-  using FPB = FPBits<T>;
-  using intU_t = typename FPB::UIntType;
+  using FPBits = FPBits<T>;
+  using UIntType = typename FPBits::UIntType;
 
-  LIBC_INLINE static constexpr FPB eval_internal(FPB sx, FPB sy) {
+  LIBC_INLINE static constexpr FPBits eval_internal(FPBits sx, FPBits sy) {
 
     if (LIBC_LIKELY(sx.uintval() <= sy.uintval())) {
       if (sx.uintval() < sy.uintval())
-        return sx;             // |x|<|y| return x
-      return FPB(FPB::zero()); // |x|=|y| return 0.0
+        return sx;                   // |x|<|y| return x
+      return FPBits(FPBits::zero()); // |x|=|y| return 0.0
     }
 
     int e_x = sx.get_biased_exponent();
     int e_y = sy.get_biased_exponent();
 
     // Most common case where |y| is "very normal" and |x/y| < 2^EXPONENT_WIDTH
-    if (LIBC_LIKELY(e_y > int(FPB::FloatProp::MANTISSA_WIDTH) &&
-                    e_x - e_y <= int(FPB::FloatProp::EXPONENT_WIDTH))) {
-      intU_t m_x = sx.get_explicit_mantissa();
-      intU_t m_y = sy.get_explicit_mantissa();
-      intU_t d = (e_x == e_y) ? (m_x - m_y) : (m_x << (e_x - e_y)) % m_y;
+    if (LIBC_LIKELY(e_y > int(FPBits::MANTISSA_WIDTH) &&
+                    e_x - e_y <= int(FPBits::EXPONENT_WIDTH))) {
+      UIntType m_x = sx.get_explicit_mantissa();
+      UIntType m_y = sy.get_explicit_mantissa();
+      UIntType d = (e_x == e_y) ? (m_x - m_y) : (m_x << (e_x - e_y)) % m_y;
       if (d == 0)
-        return FPB(FPB::zero());
+        return FPBits(FPBits::zero());
       // iy - 1 because of "zero power" for number with power 1
-      return FPB::make_value(d, e_y - 1);
+      return FPBits::make_value(d, e_y - 1);
     }
     /* Both subnormal special case. */
     if (LIBC_UNLIKELY(e_x == 0 && e_y == 0)) {
-      FPB d;
+      FPBits d;
       d.set_mantissa(sx.uintval() % sy.uintval());
       return d;
     }
 
     // Note that hx is not subnormal by conditions above.
-    intU_t m_x = sx.get_explicit_mantissa();
+    UIntType m_x = sx.get_explicit_mantissa();
     e_x--;
 
-    intU_t m_y = sy.get_explicit_mantissa();
-    int lead_zeros_m_y = FPB::FloatProp::EXPONENT_WIDTH;
+    UIntType m_y = sy.get_explicit_mantissa();
+    int lead_zeros_m_y = FPBits::EXPONENT_WIDTH;
     if (LIBC_LIKELY(e_y > 0)) {
       e_y--;
     } else {
@@ -282,34 +282,34 @@ class FMod {
 
     {
       // Shift hx left until the end or n = 0
-      int left_shift = exp_diff < int(FPB::FloatProp::EXPONENT_WIDTH)
+      int left_shift = exp_diff < int(FPBits::EXPONENT_WIDTH)
                            ? exp_diff
-                           : FPB::FloatProp::EXPONENT_WIDTH;
+                           : FPBits::EXPONENT_WIDTH;
       m_x <<= left_shift;
       exp_diff -= left_shift;
     }
 
     m_x %= m_y;
     if (LIBC_UNLIKELY(m_x == 0))
-      return FPB(FPB::zero());
+      return FPBits(FPBits::zero());
 
     if (exp_diff == 0)
-      return FPB::make_value(m_x, e_y);
+      return FPBits::make_value(m_x, e_y);
 
     /* hx next can't be 0, because hx < hy, hy % 2 == 1 hx * 2^i % hy != 0 */
     m_x = DivisionHelper::execute(exp_diff, sides_zeroes_count, m_x, m_y);
-    return FPB::make_value(m_x, e_y);
+    return FPBits::make_value(m_x, e_y);
   }
 
 public:
   LIBC_INLINE static T eval(T x, T y) {
     if (T out; Wrapper::pre_check(x, y, out))
       return out;
-    FPB sx(x), sy(y);
+    FPBits sx(x), sy(y);
     bool sign = sx.get_sign();
     sx.set_sign(false);
     sy.set_sign(false);
-    FPB result = eval_internal(sx, sy);
+    FPBits result = eval_internal(sx, sy);
     result.set_sign(sign);
     return result.get_val();
   }
diff --git a/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h b/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
index f1ef928f23081..a707029138949 100644
--- a/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
+++ b/libc/src/__support/FPUtil/x86_64/LongDoubleBits.h
@@ -26,10 +26,19 @@
 namespace LIBC_NAMESPACE {
 namespace fputil {
 
-template <> struct FPBits<long double> {
-  using UIntType = UInt128;
-
-  static constexpr int EXPONENT_BIAS = 0x3FFF;
+template <> struct FPBits<long double> : private FloatProperties<long double> {
+  using typename FloatProperties<long double>::UIntType;
+  using FloatProperties<long double>::BIT_WIDTH;
+  using FloatProperties<long double>::EXP_MANT_MASK;
+  using FloatProperties<long double>::EXPONENT_MASK;
+  using FloatProperties<long double>::EXPONENT_BIAS;
+  using FloatProperties<long double>::EXPONENT_WIDTH;
+  using FloatProperties<long double>::MANTISSA_MASK;
+  using FloatProperties<long double>::MANTISSA_WIDTH;
+  using FloatProperties<long double>::QUIET_NAN_MASK;
+  using FloatProperties<long double>::SIGN_MASK;
+
+  // static constexpr int EXPONENT_BIAS = 0x3FFF;
   static constexpr int MAX_EXPONENT = 0x7FFF;
   static constexpr UIntType MIN_SUBNORMAL = UIntType(1);
   // Subnormal numbers include the implicit bit in x86 long double formats.
@@ -41,59 +50,52 @@ template <> struct FPBits<long double> {
       (UIntType(MAX_EXPONENT - 1) << (MantissaWidth<long double>::VALUE + 1)) |
       (UIntType(1) << MantissaWidth<long double>::VALUE) | MAX_SUBNORMAL;
 
-  using FloatProp = FloatProperties<long double>;
-
   UIntType bits;
 
   LIBC_INLINE constexpr void set_mantissa(UIntType mantVal) {
-    mantVal &= (FloatProp::MANTISSA_MASK);
-    bits &= ~(FloatProp::MANTISSA_MASK);
+    mantVal &= MANTISSA_MASK;
+    bits &= ~MANTISSA_MASK;
     bits |= mantVal;
   }
 
   LIBC_INLINE constexpr UIntType get_mantissa() const {
-    return bits & FloatProp::MANTISSA_MASK;
+    return bits & MANTISSA_MASK;
   }
 
   LIBC_INLINE constexpr UIntType get_explicit_mantissa() const {
     // The x86 80 bit float represents the leading digit of the mantissa
     // explicitly. This is the mask for that bit.
-    constexpr UIntType EXPLICIT_BIT_MASK =
-        (UIntType(1) << FloatProp::MANTISSA_WIDTH);
-    return bits & (FloatProp::MANTISSA_MASK | EXPLICIT_BIT_MASK);
+    constexpr UIntType EXPLICIT_BIT_MASK = UIntType(1) << MANTISSA_WIDTH;
+    return bits & (MANTISSA_MASK | EXPLICIT_BIT_MASK);
   }
 
   LIBC_INLINE constexpr void set_biased_exponent(UIntType expVal) {
-    expVal =
-        (expVal << (FloatProp::BIT_WIDTH - 1 - FloatProp::EXPONENT_WIDTH)) &
-        FloatProp::EXPONENT_MASK;
-    bits &= ~(FloatProp::EXPONENT_MASK);
+    expVal = (expVal << (BIT_WIDTH - 1 - EXPONENT_WIDTH)) & EXPONENT_MASK;
+    bits &= ~EXPONENT_MASK;
     bits |= expVal;
   }
 
   LIBC_INLINE constexpr uint16_t get_biased_exponent() const {
-    return uint16_t((bits & FloatProp::EXPONENT_MASK) >>
-                    (FloatProp::BIT_WIDTH - 1 - FloatProp::EXPONENT_WIDTH));
+    return uint16_t((bits & EXPONENT_MASK) >> (BIT_WIDTH - 1 - EXPONENT_WIDTH));
   }
 
   LIBC_INLINE constexpr void set_implicit_bit(bool implicitVal) {
-    bits &= ~(UIntType(1) << FloatProp::MANTISSA_WIDTH);
-    bits |= (UIntType(implicitVal) << FloatProp::MANTISSA_WIDTH);
+    bits &= ~(UIntType(1) << MANTISSA_WIDTH);
+    bits |= (UIntType(implicitVal) << MANTISSA_WIDTH);
   }
 
   LIBC_INLINE constexpr bool get_implicit_bit() const {
-    return bool((bits & (UIntType(1) << FloatProp::MANTISSA_WIDTH)) >>
-                FloatProp::MANTISSA_WIDTH);
+    return bool((bits & (UIntType(1) << MANTISSA_WIDTH)) >> MANTISSA_WIDTH);
   }
 
   LIBC_INLINE constexpr void set_sign(bool signVal) {
-    bits &= ~(FloatProp::SIGN_MASK);
-    UIntType sign1 = UIntType(signVal) << (FloatProp::BIT_WIDTH - 1);
+    bits &= ~SIGN_MASK;
+    UIntType sign1 = UIntType(signVal) << (BIT_WIDTH - 1);
     bits |= sign1;
   }
 
   LIBC_INLINE constexpr bool get_sign() const {
-    return bool((bits & FloatProp::SIGN_MASK) >> (FloatProp::BIT_WIDTH - 1));
+    return bool((bits & SIGN_MASK) >> (BIT_WIDTH - 1));
   }
 
   LIBC_INLINE constexpr FPBits() : bits(0) {}
@@ -117,7 +119,7 @@ template <> struct FPBits<long double> {
 
   LIBC_INLINE constexpr UIntType uintval() {
     // We zero the padding bits as they can contain garbage.
-    return bits & FloatProp::FP_MASK;
+    return bits & FP_MASK;
   }
 
   LIBC_INLINE constexpr long double get_val() const {
@@ -196,7 +198,7 @@ template <> struct FPBits<long double> {
   }
 
   LIBC_INLINE static constexpr long double build_quiet_nan(UIntType v) {
-    return build_nan(FloatProp::QUIET_NAN_MASK | v);
+    return build_nan(QUIET_NAN_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 2a6f15c018f1e..3807d3ff57216 100644
--- a/libc/src/__support/str_to_float.h
+++ b/libc/src/__support/str_to_float.h
@@ -71,7 +71,7 @@ LIBC_INLINE cpp::optional<ExpandedFloat<T>>
 eisel_lemire(ExpandedFloat<T> init_num,
              RoundDirection round = RoundDirection::Nearest) {
   using FPBits = typename fputil::FPBits<T>;
-  using FloatProp = typename FPBits::FloatProp;
+  using FloatProp = typename fputil::FloatProperties<T>;
   using UIntType = typename FPBits::UIntType;
 
   UIntType mantissa = init_num.mantissa;
@@ -184,7 +184,7 @@ LIBC_INLINE cpp::optional<ExpandedFloat<long double>>
 eisel_lemire<long double>(ExpandedFloat<long double> init_num,
                           RoundDirection round) {
   using FPBits = typename fputil::...
[truncated]

@gchatelet gchatelet changed the title [libc][NFC] Implement FPBits in terms of FloatProperties to reduce clutter [libc][NFC] Implement FPBits in terms of FloatProperties to reduce clutter Dec 12, 2023
@gchatelet gchatelet merged commit f64a057 into llvm:main Dec 13, 2023
5 checks passed
@gchatelet gchatelet deleted the implement_fp_bits_in_terms_of_float_properties branch December 13, 2023 08:23
gchatelet added a commit that referenced this pull request Dec 13, 2023
gchatelet added a commit that referenced this pull request Dec 13, 2023
…to reduce clutter" (#75304)

Reverts #75196

GCC complains about change of meaning for `FPBits`
```
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-gcc-fullbuild-dbg/llvm-project/libc/src/__support/FPUtil/generic/FMod.h:188:9: error: declaration of ‘using FPBits = struct __llvm_libc_18_0_0_git::fputil::FPBits<T>’ changes meaning of ‘FPBits’ [-fpermissive]
  188 |   using FPBits = FPBits<T>;
      |         ^~~~~~
```

I'll reland with a different name.
gchatelet added a commit to gchatelet/llvm-project that referenced this pull request Dec 13, 2023
…to reduce clutter (llvm#75196)

Also make type naming consistent by using `UIntType` instead of `intU_t`.
This patch  does not include the "use `FPBits` instead of `FPBits_t`, `FPB`" part. This needs more work.
gchatelet added a commit that referenced this pull request Dec 13, 2023
…to reduce clutter (#75196) (#75318)

Also make type naming consistent by using `UIntType` instead of
`intU_t`.
This patch is a reland of #75196 but does not include the "use `FPBits`
instead of `FPBits_t`, `FPB`" part. This needs more work.
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