Skip to content

Commit 7fdc51b

Browse files
committed
[libcxx] Use generic builtins for popcount, clz and ctz
Use __builtin_popcountg instead of __buildin_popcount{l|ll} Use __builtin_clzg instead of __buildin_clz{l|ll} Use __builtin_ctzg instead of __builtin_ctz{l|ll}
1 parent cce18e4 commit 7fdc51b

File tree

6 files changed

+46
-4
lines changed

6 files changed

+46
-4
lines changed

libcxx/include/__bit/countl.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,27 @@ _LIBCPP_PUSH_MACROS
2525
_LIBCPP_BEGIN_NAMESPACE_STD
2626

2727
_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_clz(unsigned __x) _NOEXCEPT {
28+
#if __has_builtin(__builtin_clzg)
29+
return __builtin_clzg(__x);
30+
#else
2831
return __builtin_clz(__x);
32+
#endif
2933
}
3034

3135
_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_clz(unsigned long __x) _NOEXCEPT {
36+
#if __has_builtin(__builtin_clzg)
37+
return __builtin_clzg(__x);
38+
#else
3239
return __builtin_clzl(__x);
40+
#endif
3341
}
3442

3543
_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_clz(unsigned long long __x) _NOEXCEPT {
44+
#if __has_builtin(__builtin_clzg)
45+
return __builtin_clzg(__x);
46+
#else
3647
return __builtin_clzll(__x);
48+
#endif
3749
}
3850

3951
#ifndef _LIBCPP_HAS_NO_INT128
@@ -47,8 +59,13 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_clz(__uint128_t __x)
4759
// - Any bits set:
4860
// - The number of leading zeros of the input is the number of leading
4961
// zeros in the high 64-bits.
62+
# if __has_builtin(__builtin_clzg)
63+
return ((__x >> 64) == 0) ? (64 + __builtin_clzg(static_cast<unsigned long long>(__x)))
64+
: __builtin_clzg(static_cast<unsigned long long>(__x >> 64));
65+
# else
5066
return ((__x >> 64) == 0) ? (64 + __builtin_clzll(static_cast<unsigned long long>(__x)))
5167
: __builtin_clzll(static_cast<unsigned long long>(__x >> 64));
68+
# endif
5269
}
5370
#endif // _LIBCPP_HAS_NO_INT128
5471

libcxx/include/__bit/countr.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,27 @@ _LIBCPP_PUSH_MACROS
2424
_LIBCPP_BEGIN_NAMESPACE_STD
2525

2626
_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_ctz(unsigned __x) _NOEXCEPT {
27+
#if __has_builtin(__builtin_ctzg)
28+
return __builtin_ctzg(__x);
29+
#else
2730
return __builtin_ctz(__x);
31+
#endif
2832
}
2933

3034
_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_ctz(unsigned long __x) _NOEXCEPT {
35+
#if __has_builtin(__builtin_ctzg)
36+
return __builtin_ctzg(__x);
37+
#else
3138
return __builtin_ctzl(__x);
39+
#endif
3240
}
3341

3442
_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_ctz(unsigned long long __x) _NOEXCEPT {
43+
#if __has_builtin(__builtin_ctzg)
44+
return __builtin_ctzg(__x);
45+
#else
3546
return __builtin_ctzll(__x);
47+
#endif
3648
}
3749

3850
template <class _Tp>

libcxx/include/__bit/popcount.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,27 @@ _LIBCPP_PUSH_MACROS
2424
_LIBCPP_BEGIN_NAMESPACE_STD
2525

2626
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_popcount(unsigned __x) _NOEXCEPT {
27+
#if __has_builtin(__builtin_popcountg)
28+
return __builtin_popcountg(__x);
29+
#else
2730
return __builtin_popcount(__x);
31+
#endif
2832
}
2933

3034
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_popcount(unsigned long __x) _NOEXCEPT {
35+
#if __has_builtin(__builtin_popcountg)
36+
return __builtin_popcountg(__x);
37+
#else
3138
return __builtin_popcountl(__x);
39+
#endif
3240
}
3341

3442
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_popcount(unsigned long long __x) _NOEXCEPT {
43+
#if __has_builtin(__builtin_popcountg)
44+
return __builtin_popcountg(__x);
45+
#else
3546
return __builtin_popcountll(__x);
47+
#endif
3648
}
3749

3850
#if _LIBCPP_STD_VER >= 20

libcxx/src/include/ryu/d2s_intrinsics.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
249249
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline bool __multipleOfPowerOf2(const uint64_t __value, const uint32_t __p) {
250250
_LIBCPP_ASSERT_INTERNAL(__value != 0, "");
251251
_LIBCPP_ASSERT_INTERNAL(__p < 64, "");
252-
// __builtin_ctzll doesn't appear to be faster here.
252+
// __builtin_ctzll/__builtin_ctzg doesn't appear to be faster here.
253253
return (__value & ((1ull << __p) - 1)) == 0;
254254
}
255255

libcxx/src/include/ryu/ryu.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
// Avoid formatting to keep the changes with the original code minimal.
4444
// clang-format off
4545

46+
#include <__bit/countr.h>
4647
#include <__charconv/chars_format.h>
4748
#include <__charconv/to_chars_result.h>
4849
#include <__config>
@@ -72,15 +73,15 @@ _LIBCPP_HIDE_FROM_ABI inline unsigned char _BitScanForward64(unsigned long* __in
7273
if (__mask == 0) {
7374
return false;
7475
}
75-
*__index = __builtin_ctzll(__mask);
76+
*__index = __libcpp_ctz(__mask);
7677
return true;
7778
}
7879

7980
_LIBCPP_HIDE_FROM_ABI inline unsigned char _BitScanForward(unsigned long* __index, unsigned int __mask) {
8081
if (__mask == 0) {
8182
return false;
8283
}
83-
*__index = __builtin_ctz(__mask);
84+
*__index = __libcpp_ctz(__mask);
8485
return true;
8586
}
8687
#endif // !_MSC_VER

libcxx/src/ryu/f2s.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ inline constexpr uint64_t __FLOAT_POW5_SPLIT[47] = {
107107
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI inline bool __multipleOfPowerOf2(const uint32_t __value, const uint32_t __p) {
108108
_LIBCPP_ASSERT_INTERNAL(__value != 0, "");
109109
_LIBCPP_ASSERT_INTERNAL(__p < 32, "");
110-
// __builtin_ctz doesn't appear to be faster here.
110+
// __builtin_ctz/__builtin_ctzg doesn't appear to be faster here.
111111
return (__value & ((1u << __p) - 1)) == 0;
112112
}
113113

0 commit comments

Comments
 (0)