[libcxx-commits] [libcxx] [libcxx] Use generic builtins for popcount, clz and ctz (PR #86563)

Nick Desaulniers via libcxx-commits libcxx-commits at lists.llvm.org
Fri Mar 29 10:21:08 PDT 2024


================
@@ -58,6 +65,9 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int __countl_zero(_Tp __t) _
   if (__t == 0)
     return numeric_limits<_Tp>::digits;
 
+#if __has_builtin(__builtin_clzg)
+  return __builtin_clzg(__t);
+#else  // __has_builtin(__builtin_clzg)
----------------
nickdesaulniers wrote:

This builtin may have an optional second argument which is what to evaluate to if the first argument is zero.
https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005fclzg

```suggestion
#if __has_builtin(__builtin_clzg)
  return __builtin_clzg(__t, numeric_limits<_Tp>::digits);
#else  // __has_builtin(__builtin_clzg)
  if (__t == 0)
    return numeric_limits<_Tp>::digits;

```

This way even more code can get deleted in the future.

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


More information about the libcxx-commits mailing list