[libc-commits] [libc] 0f6f5bf - [libc][__support][bit] simplify FLZ (#81678)

via libc-commits libc-commits at lists.llvm.org
Wed Feb 14 08:52:41 PST 2024


Author: Nick Desaulniers
Date: 2024-02-14T08:52:38-08:00
New Revision: 0f6f5bfe5322f08a96fda149ff70888dc45a2e35

URL: https://github.com/llvm/llvm-project/commit/0f6f5bfe5322f08a96fda149ff70888dc45a2e35
DIFF: https://github.com/llvm/llvm-project/commit/0f6f5bfe5322f08a96fda149ff70888dc45a2e35.diff

LOG: [libc][__support][bit] simplify FLZ (#81678)

`countl_zero(~x)` *is* `countl_one(x)`

Added: 
    

Modified: 
    libc/src/__support/CPP/bit.h

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/CPP/bit.h b/libc/src/__support/CPP/bit.h
index a8bf75a9a2efac..4115d67c7705c0 100644
--- a/libc/src/__support/CPP/bit.h
+++ b/libc/src/__support/CPP/bit.h
@@ -226,30 +226,11 @@ LIBC_INLINE constexpr To bit_or_static_cast(const From &from) {
   }
 }
 
-#define SPECIALIZE_FLZ(NAME, TYPE, BUILTIN)                                    \
-  template <> [[nodiscard]] LIBC_INLINE constexpr int NAME<TYPE>(TYPE value) { \
-    static_assert(cpp::is_unsigned_v<TYPE>);                                   \
-    return value == cpp::numeric_limits<TYPE>::max()                           \
-               ? 0                                                             \
-               : BUILTIN(static_cast<TYPE>(~value)) + 1;                       \
-  }
-
 template <typename T, typename = cpp::enable_if_t<cpp::is_unsigned_v<T>>>
 [[nodiscard]] LIBC_INLINE constexpr int first_leading_zero(T value) {
-  return value == cpp::numeric_limits<T>::max()
-             ? 0
-             : countl_zero(static_cast<T>(~value)) + 1;
+  return value == cpp::numeric_limits<T>::max() ? 0 : countl_one(value) + 1;
 }
 
-#if LIBC_HAS_BUILTIN(__builtin_clzs)
-SPECIALIZE_FLZ(first_leading_zero, unsigned short, __builtin_clzs)
-#endif
-SPECIALIZE_FLZ(first_leading_zero, unsigned int, __builtin_clz)
-SPECIALIZE_FLZ(first_leading_zero, unsigned long, __builtin_clzl)
-SPECIALIZE_FLZ(first_leading_zero, unsigned long long, __builtin_clzll)
-
-#undef SPECIALIZE_FLZ
-
 } // namespace LIBC_NAMESPACE::cpp
 
 #endif // LLVM_LIBC_SRC___SUPPORT_CPP_BIT_H


        


More information about the libc-commits mailing list