[libc-commits] [libc] [libc] add FXBits class (PR #82065)

via libc-commits libc-commits at lists.llvm.org
Fri Feb 16 15:21:56 PST 2024


================
@@ -19,6 +21,74 @@
 
 namespace LIBC_NAMESPACE::fixed_point {
 
+template <typename T> struct FXBits {
+private:
+  using fx_rep = FXRep<T>;
+  using StorageType = typename fx_rep::StorageType;
+
+  StorageType value;
+
+  static_assert(fx_rep::FRACTION_LEN > 0);
+
+  static constexpr size_t FRACTION_OFFSET = 0; // Just for completeness
+  static constexpr size_t INTEGRAL_OFFSET = fx_rep::FRACTION_LEN;
+  static constexpr size_t SIGN_OFFSET =
+      fx_rep::SIGN_LEN == 0
+          ? 0
+          : ((sizeof(StorageType) * CHAR_BIT) - fx_rep::SIGN_LEN);
+
+  static constexpr StorageType FRACTION_MASK =
+      (StorageType(1) << fx_rep::FRACTION_LEN) - 1;
----------------
lntue wrote:

You need to take care of the edge case when `FRACTION_LEN` is the same as `sizeof(StorageType) * CHAR_BIT` separately.

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


More information about the libc-commits mailing list