Skip to content
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

Merged
merged 2 commits into from Apr 5, 2024

Conversation

nickdesaulniers
Copy link
Member

These are new in clang-19+, gcc-14+.

@nickdesaulniers
Copy link
Member Author

cc @overmighty

@llvmbot llvmbot added the libc label Mar 26, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Mar 26, 2024

@llvm/pr-subscribers-libc

Author: Nick Desaulniers (nickdesaulniers)

Changes

These are new in clang-19+, gcc-14+.


Full diff: https://github.com/llvm/llvm-project/pull/86746.diff

1 Files Affected:

  • (modified) libc/src/__support/CPP/bit.h (+18)
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
 

@nickdesaulniers
Copy link
Member Author

Note: already did this for popcount in #86531.

@nickdesaulniers
Copy link
Member Author

not sure what's going wrong in presubmit; will look more tomorrow.

@gchatelet
Copy link
Contributor

@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.

@nickdesaulniers
Copy link
Member Author

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+.
libc/src/__support/CPP/bit.h Outdated Show resolved Hide resolved
Copy link
Contributor

@gchatelet gchatelet left a 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!)

@nickdesaulniers nickdesaulniers merged commit 2744a24 into llvm:main Apr 5, 2024
4 checks passed
@nickdesaulniers nickdesaulniers deleted the g_builtins branch April 5, 2024 21:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants