[libc-commits] [libc] [llvm] [libc][NFC] Refactor FPBits and remove LongDoubleBits specialization (PR #78192)

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Tue Jan 16 10:34:59 PST 2024


================
@@ -132,11 +166,94 @@ struct FPRepBase : public internal::FPLayout<fp_type> {
   static_assert((SIG_MASK & EXP_MASK & SIGN_MASK) == 0, "masks disjoint");
   static_assert((SIG_MASK | EXP_MASK | SIGN_MASK) == FP_MASK, "masks cover");
 
-private:
+protected:
   LIBC_INLINE static constexpr StorageType bit_at(int position) {
     return StorageType(1) << position;
   }
 
+  // An opaque type to store a floating point exponent.
+  // We define special values but it is valid to create arbitrary values as long
+  // as they are in the range [MIN, MAX].
+  enum class Exponent : int32_t {
+    MIN = 1 - EXP_BIAS,
+    ZERO = 0,
+    MAX = EXP_BIAS,
+  };
+
+  // An opaque type to store a floating point biased exponent.
+  // We define special values but it is valid to create arbitrary values as long
+  // as they are in the range [BITS_ALL_ZEROES, BITS_ALL_ONES].
+  // Values greater than BITS_ALL_ONES are truncated.
+  enum class BiasedExponent : uint32_t {
+    // The exponent value for denormal numbers.
+    BITS_ALL_ZEROES = 0,
+    // The exponent value for infinity.
+    BITS_ALL_ONES = 2 * EXP_BIAS + 1,
+  };
+
+  LIBC_INLINE static constexpr BiasedExponent biased(Exponent value) {
+    return static_cast<BiasedExponent>(static_cast<int32_t>(value) + EXP_BIAS);
+  }
+
+  // An opaque type to store a floating point significand.
+  // We define special values but it is valid to create arbitrary values as long
+  // as they are in the range [BITS_ALL_ZEROES, BITS_ALL_ONES].
+  // Note that the semantics of the Significand are implementation dependent.
+  // Values greater than BITS_ALL_ONES are truncated.
+  enum class Significand : StorageType {
+    ZERO = 0,
+    LSB = 1,
+    MSB = bit_at(SIG_LEN - 1),
+    // Aliases
+    BITS_ALL_ZEROES = ZERO,
+    BITS_ALL_ONES = SIG_MASK,
+  };
+
+  template <typename T>
+  LIBC_INLINE static constexpr auto storage_cast(T value) {
+    return static_cast<StorageType>(value);
+  }
+
+  LIBC_INLINE friend constexpr Significand operator|(const Significand a,
+                                                     const Significand b) {
+    return Significand{storage_cast(storage_cast(a) | storage_cast(b))};
+  }
+  LIBC_INLINE friend constexpr Significand operator^(const Significand a,
+                                                     const Significand b) {
+    return Significand{storage_cast(storage_cast(a) ^ storage_cast(b))};
+  }
+  LIBC_INLINE friend constexpr Significand operator>>(const Significand a,
+                                                      int shift) {
+    return Significand{storage_cast(storage_cast(a) >> shift)};
+  }
+
+  LIBC_INLINE static constexpr StorageType encode(BiasedExponent exp) {
+    return (storage_cast(exp) << SIG_LEN) & EXP_MASK;
+  }
+
+  LIBC_INLINE static constexpr StorageType encode(Significand value) {
+    return storage_cast(value) & SIG_MASK;
+  }
+
+  LIBC_INLINE static constexpr StorageType encode(BiasedExponent exp,
+                                                  Significand sig) {
+    return encode(exp) | encode(sig);
----------------
nickdesaulniers wrote:

32b arm build bot failure here:
```
/llvm/ndesaulniers/llvm-project/libc/src/__support/FPUtil/FPBits.h:240:12: error: no matching function for call to 'encode'
    return encode(exp) | encode(sig);
           ^~~~~~
/llvm/ndesaulniers/llvm-project/libc/src/__support/FPUtil/FPBits.h:479:30: note: in instantiation of member function '__llvm_libc_18_0_0_git::fputil::FPRepBase<__llvm_libc_18_0_0_git::fputil::FPType::X86_Binary80>::encode' requested here
    return exp_sig_bits() >= encode(BiasedExponent::BITS_ALL_ONES,
                             ^
/llvm/ndesaulniers/llvm-project/libc/src/__support/FPUtil/FPBits.h:238:44: note: candidate function not viable: requires 2 arguments, but 1 was provided
  LIBC_INLINE static constexpr StorageType encode(BiasedExponent exp,
                                           ^
/llvm/ndesaulniers/llvm-project/libc/src/__support/FPUtil/FPBits.h:243:44: note: candidate function not viable: requires 3 arguments, but 1 was provided
  LIBC_INLINE static constexpr StorageType encode(bool sign, BiasedExponent exp,
                                           ^
```

https://github.com/llvm/llvm-project/pull/78192


More information about the libc-commits mailing list