[libc-commits] [libc] [libc] add FXBits class (PR #82065)
via libc-commits
libc-commits at lists.llvm.org
Fri Feb 16 15:23:44 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;
+ static constexpr StorageType INTEGRAL_MASK =
+ ((StorageType(1) << fx_rep::INTEGRAL_LEN) - 1) << INTEGRAL_OFFSET;
----------------
lntue wrote:
This might hit UB when `INTEGRAL_OFFSET == sizeof(StorageType) * CHAR_BIT`.
https://github.com/llvm/llvm-project/pull/82065
More information about the libc-commits
mailing list