New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[libc][support][bit] use new type generic builtins #86746
Conversation
cc @overmighty |
@llvm/pr-subscribers-libc Author: Nick Desaulniers (nickdesaulniers) ChangesThese are new in clang-19+, gcc-14+. Full diff: https://github.com/llvm/llvm-project/pull/86746.diff 1 Files Affected:
diff --git a/libc/src/__support/CPP/bit.h b/libc/src/__support/CPP/bit.h
index 3f2fbec944054c..a578e256e8364e 100644
--- a/libc/src/__support/CPP/bit.h
+++ b/libc/src/__support/CPP/bit.h
@@ -73,6 +73,14 @@ has_single_bit(T value) {
/// Only unsigned integral types are allowed.
///
/// Returns cpp::numeric_limits<T>::digits on an input of 0.
+// clang-19+, gcc-14+
+#if __has_builtin(__builtin_ctzg)
+template <typename T>
+[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
+countr_zero(T value) {
+ return __builtin_ctzg(value, cpp::numeric_limits<T>::digits);
+}
+#else
template <typename T>
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
countr_zero(T value) {
@@ -100,6 +108,7 @@ ADD_SPECIALIZATION(countr_zero, unsigned short, __builtin_ctzs)
ADD_SPECIALIZATION(countr_zero, unsigned int, __builtin_ctz)
ADD_SPECIALIZATION(countr_zero, unsigned long, __builtin_ctzl)
ADD_SPECIALIZATION(countr_zero, unsigned long long, __builtin_ctzll)
+#endif // __has_builtin(__builtin_ctzg)
/// Count number of 0's from the most significant bit to the least
/// stopping at the first 1.
@@ -107,9 +116,17 @@ ADD_SPECIALIZATION(countr_zero, unsigned long long, __builtin_ctzll)
/// Only unsigned integral types are allowed.
///
/// Returns cpp::numeric_limits<T>::digits on an input of 0.
+// clang-19+, gcc-14+
+#if __has_builtin(__builtin_clzg)
template <typename T>
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
countl_zero(T value) {
+ return __builtin_clzg(value, cpp::numeric_limits<T>::digits);
+}
+#else
+template <typename T [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<
+ cpp::is_unsigned_v<T>, int>
+ countl_zero(T value) {
if (!value)
return cpp::numeric_limits<T>::digits;
// Bisection method.
@@ -130,6 +147,7 @@ ADD_SPECIALIZATION(countl_zero, unsigned short, __builtin_clzs)
ADD_SPECIALIZATION(countl_zero, unsigned int, __builtin_clz)
ADD_SPECIALIZATION(countl_zero, unsigned long, __builtin_clzl)
ADD_SPECIALIZATION(countl_zero, unsigned long long, __builtin_clzll)
+#endif // __has_builtin(__builtin_clzg)
#undef ADD_SPECIALIZATION
|
Note: already did this for popcount in #86531. |
not sure what's going wrong in presubmit; will look more tomorrow. |
@nickdesaulniers for my information, are you adding these generic builtins for documentation purposes ? As-is they don't provide anything that we don't already have with the specialization (and we have to keep them until clang-19+ / gcc-14+ becomes our minimum compiler version). I'm not opposing this patch, I'm just trying to get the rationale behind it. |
I'd like to simplify the implementations such that one day (when our minimum supported compiler versions advance past when these builtins were added), we can clean up/delete a bunch of code. We might even get there sooner if we start reusing code from libc++. #86563 is where we're trying to utilize these builtins there, too. |
These are new in clang-19+, gcc-14+.
7124d8e
to
a23d6d4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM (Sorry for dropping the ball!)
These are new in clang-19+, gcc-14+.