[libc-commits] [libc] [libc][stdfix] Implement `countlsfx` functions (PR #114318)

via libc-commits libc-commits at lists.llvm.org
Sat Nov 2 19:32:54 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());
----------------
lntue wrote:

That's an odd one, something maybe we should revise when we try to add fixed point types to C standard.  I guess they were worried about integer promotion rules.  Anyway, you can try:
```
  x = FXBits(static_cast<StorageType>(~value)).get_val();
```
to see if it works properly.

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


More information about the libc-commits mailing list