[libc-commits] [libc] [libc][__support][bit] simplify FLZ (PR #81678)
via libc-commits
libc-commits at lists.llvm.org
Tue Feb 13 14:19:32 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Nick Desaulniers (nickdesaulniers)
<details>
<summary>Changes</summary>
`countl_zero(~x)` *is* `countl_one(x)`
---
Full diff: https://github.com/llvm/llvm-project/pull/81678.diff
1 Files Affected:
- (modified) libc/src/__support/CPP/bit.h (+1-26)
``````````diff
diff --git a/libc/src/__support/CPP/bit.h b/libc/src/__support/CPP/bit.h
index 392fbe248138ae..3439a52e98ebf4 100644
--- a/libc/src/__support/CPP/bit.h
+++ b/libc/src/__support/CPP/bit.h
@@ -238,36 +238,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
-#if LIBC_HAS_BUILTIN(__builtin_clz)
-SPECIALIZE_FLZ(first_leading_zero, unsigned int, __builtin_clz)
-#endif
-#if LIBC_HAS_BUILTIN(__builtin_clzl)
-SPECIALIZE_FLZ(first_leading_zero, unsigned long, __builtin_clzl)
-#endif
-#if LIBC_HAS_BUILTIN(__builtin_clzll)
-SPECIALIZE_FLZ(first_leading_zero, unsigned long long, __builtin_clzll)
-#endif
-
-#undef SPECIALIZE_FLZ
-
} // namespace LIBC_NAMESPACE::cpp
#endif // LLVM_LIBC_SRC___SUPPORT_CPP_BIT_H
``````````
</details>
https://github.com/llvm/llvm-project/pull/81678
More information about the libc-commits
mailing list