Skip to content

Commit 8b67548

Browse files
committed
Shortcuts that benefit from generic builtins
1 parent 0b67dcd commit 8b67548

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

libcxx/include/__bit/countl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int __countl_zero(_Tp __t) _
7474
if (__t == 0)
7575
return numeric_limits<_Tp>::digits;
7676

77+
#if __has_builtin(__builtin_clzg)
78+
return __builtin_clzg(__t) - (numeric_limits<unsigned>::digits - numeric_limits<_Tp>::digits);
79+
#else
7780
if (sizeof(_Tp) <= sizeof(unsigned int))
7881
return std::__libcpp_clz(static_cast<unsigned int>(__t)) -
7982
(numeric_limits<unsigned int>::digits - numeric_limits<_Tp>::digits);
@@ -95,6 +98,7 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int __countl_zero(_Tp __t) _
9598
}
9699
return __ret + __iter;
97100
}
101+
#endif // __has_builtin(__builtin_clzg)
98102
}
99103

100104
#if _LIBCPP_STD_VER >= 20

libcxx/include/__bit/countr.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int __coun
5252
if (__t == 0)
5353
return numeric_limits<_Tp>::digits;
5454

55+
#if __has_builtin(__builtin_ctz)
56+
return __builtin_ctz(__t);
57+
#else
5558
if (sizeof(_Tp) <= sizeof(unsigned int))
5659
return std::__libcpp_ctz(static_cast<unsigned int>(__t));
5760
else if (sizeof(_Tp) <= sizeof(unsigned long))
@@ -67,6 +70,7 @@ _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int __coun
6770
}
6871
return __ret + std::__libcpp_ctz(static_cast<unsigned long long>(__t));
6972
}
73+
#endif // __has_builtin(__builtin_ctz)
7074
}
7175

7276
#if _LIBCPP_STD_VER >= 20

libcxx/include/__bit/popcount.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_popcount(unsigned lo
5151

5252
template <__libcpp_unsigned_integer _Tp>
5353
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr int popcount(_Tp __t) noexcept {
54+
# if __has_builtin(__builtin_popcount)
55+
return __builtin_popcount(__t);
56+
# else
5457
if (sizeof(_Tp) <= sizeof(unsigned int))
5558
return std::__libcpp_popcount(static_cast<unsigned int>(__t));
5659
else if (sizeof(_Tp) <= sizeof(unsigned long))
@@ -65,6 +68,7 @@ _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr int popcount(_Tp __t) noex
6568
}
6669
return __ret;
6770
}
71+
# endif // __has_builtin(__builtin_popcount)
6872
}
6973

7074
#endif // _LIBCPP_STD_VER >= 20

0 commit comments

Comments
 (0)