[libc-commits] [libc] [libc][stdfix] Implement fixed point `countlsfx` functions in llvm-libc (PR #125356)

via libc-commits libc-commits at lists.llvm.org
Tue Feb 4 12:14:12 PST 2025


================
@@ -163,6 +176,26 @@ template <typename T> LIBC_INLINE constexpr T round(T x, int n) {
   return bit_and((x + round_bit), rounding_mask);
 }
 
+// count leading zeros
+template <typename T>
+LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_fixed_point_v<T>, int>
+countls(T f) {
+  using FXRep = FXRep<T>;
+  using BitType = typename FXRep::StorageType;
+  using FXBits = FXBits<T>;
+
+  constexpr int CONTAIN_LEN = cpp::numeric_limits<BitType>::digits;
+  constexpr int PADDING_LEN = CONTAIN_LEN - (FXRep::INTEGRAL_LEN + FXRep::FRACTION_LEN);
----------------
PiJoules wrote:

Note this should also account for the SIGN_LEN. Right now, this would treat the sign bit as a padding bit, which just so happens to be subtracted from the final calculation so the result of `countls` is still correct.

I think you could instead use `FXRep::TOTAL_LEN` here which should account for all the value bits.

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


More information about the libc-commits mailing list