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

Guillaume Chatelet via libc-commits libc-commits at lists.llvm.org
Tue Jan 16 02:27:43 PST 2024


================
@@ -132,6 +132,70 @@ 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");
 
+protected:
+  enum class Exp : int32_t {
+    ZERO = 0,
+    // The exponent value for denormal numbers.
+    SUBNORMAL = -EXP_BIAS,
+    // The minimum exponent value for normal numbers.
+    MIN = SUBNORMAL + 1,
+    // The maximum exponent value for normal numbers.
+    MAX = EXP_BIAS,
+    // Special value all ones.
+    INF = MAX + 1,
+    // Aliases
+    BITS_ALL_ZEROES = SUBNORMAL,
+    BITS_ALL_ONES = INF,
+  };
+
+  enum class Sig : StorageType {
+    ZERO = 0,
+    ONE = 1,
+    MSB = StorageType(1) << (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 Sig operator|(const Sig a, const Sig b) {
+    return Sig{storage_cast(storage_cast(a) | storage_cast(b))};
+  }
+  LIBC_INLINE friend constexpr Sig operator^(const Sig a, const Sig b) {
+    return Sig{storage_cast(storage_cast(a) ^ storage_cast(b))};
+  }
+  LIBC_INLINE friend constexpr Sig operator>>(const Sig a, int shift) {
+    return Sig{storage_cast(storage_cast(a) >> shift)};
+  }
+
+  LIBC_INLINE static constexpr StorageType encode(Exp exp) {
+    return storage_cast(static_cast<int32_t>(exp) + EXP_BIAS) << SIG_LEN;
----------------
gchatelet wrote:

We don't have `assert` in libc, only `static_assert`s. The behavior is to truncate the value if it exceeds its maximum value. I've made it clearer in the documentation of the enums.

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


More information about the libc-commits mailing list