[libc-commits] [libc] [libc][stdfix] Implement `countlsfx` functions (PR #114318)
via libc-commits
libc-commits at lists.llvm.org
Sat Nov 2 19:11:36 PDT 2024
================
@@ -142,6 +160,25 @@ template <typename T> LIBC_INLINE constexpr T abs(T x) {
}
}
+template <typename T>
+LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_fixed_point_v<T>, int>
+countls(T x) {
+ using FXRep = FXRep<T>;
+ using BitType = typename FXRep::StorageType;
+ constexpr int CONTAIN_LEN = cpp::numeric_limits<BitType>::digits;
+ constexpr int PADDING_LEN = CONTAIN_LEN - FXRep::VALUE_LEN;
+
+ if constexpr (FXRep::SIGN_LEN != 0) {
+ if (x < 0) {
+ x = -(x + FXRep::EPS());
+ }
+ }
+
+ FXBits<T> fxbits(x);
+ BitType value_bits = fxbits.get_value_bits();
----------------
duncpro wrote:
So rename `get_value_bits` to `get_bits` and
inside `get_bits` we `& TOTAL_MASK` instead of `& VALUE_MASK`.
The idea is...`TOTAL_MASK` is actually `StorageType::MAX` in most cases, and so clang can easily see its useless and optimize away.
Whereas it can't as easily get optimized away if we use `VALUE_MASK` because we discard the sign bit.
https://github.com/llvm/llvm-project/pull/114318
More information about the libc-commits
mailing list