Skip to content

Commit 2c220d8

Browse files
committed
Revert "[libc++] Replace __libcpp_popcount by __builtin_popcountg (llvm#133937)"
This reverts commit 703cfe7.
1 parent 92a6898 commit 2c220d8

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

libcxx/include/__bit/popcount.h

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
// TODO: __builtin_popcountg is available since Clang 19 and GCC 14. When support for older versions is dropped, we can
10+
// refactor this code to exclusively use __builtin_popcountg.
11+
912
#ifndef _LIBCPP___BIT_POPCOUNT_H
1013
#define _LIBCPP___BIT_POPCOUNT_H
1114

@@ -21,10 +24,50 @@ _LIBCPP_PUSH_MACROS
2124

2225
_LIBCPP_BEGIN_NAMESPACE_STD
2326

27+
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_popcount(unsigned __x) _NOEXCEPT {
28+
return __builtin_popcount(__x);
29+
}
30+
31+
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_popcount(unsigned long __x) _NOEXCEPT {
32+
return __builtin_popcountl(__x);
33+
}
34+
35+
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_popcount(unsigned long long __x) _NOEXCEPT {
36+
return __builtin_popcountll(__x);
37+
}
38+
39+
template <class _Tp>
40+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __popcount_impl(_Tp __t) _NOEXCEPT {
41+
if _LIBCPP_CONSTEXPR (sizeof(_Tp) <= sizeof(unsigned int)) {
42+
return std::__libcpp_popcount(static_cast<unsigned int>(__t));
43+
} else if _LIBCPP_CONSTEXPR (sizeof(_Tp) <= sizeof(unsigned long)) {
44+
return std::__libcpp_popcount(static_cast<unsigned long>(__t));
45+
} else if _LIBCPP_CONSTEXPR (sizeof(_Tp) <= sizeof(unsigned long long)) {
46+
return std::__libcpp_popcount(static_cast<unsigned long long>(__t));
47+
} else {
48+
#if _LIBCPP_STD_VER == 11
49+
return __t != 0 ? std::__libcpp_popcount(static_cast<unsigned long long>(__t)) +
50+
std::__popcount_impl<_Tp>(__t >> numeric_limits<unsigned long long>::digits)
51+
: 0;
52+
#else
53+
int __ret = 0;
54+
while (__t != 0) {
55+
__ret += std::__libcpp_popcount(static_cast<unsigned long long>(__t));
56+
__t >>= std::numeric_limits<unsigned long long>::digits;
57+
}
58+
return __ret;
59+
#endif
60+
}
61+
}
62+
2463
template <class _Tp>
2564
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __popcount(_Tp __t) _NOEXCEPT {
26-
static_assert(__is_unsigned_integer_v<_Tp>, "__popcount only works with unsigned types");
65+
static_assert(is_unsigned<_Tp>::value, "__popcount only works with unsigned types");
66+
#if __has_builtin(__builtin_popcountg) // TODO (LLVM 21): This can be dropped once we only support Clang >= 19.
2767
return __builtin_popcountg(__t);
68+
#else
69+
return std::__popcount_impl(__t);
70+
#endif
2871
}
2972

3073
#if _LIBCPP_STD_VER >= 20

libcxx/include/__stop_token/atomic_unique_lock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2828
// and LockedBit is the value of State when the lock bit is set, e.g 1 << 2
2929
template <class _State, _State _LockedBit>
3030
class _LIBCPP_AVAILABILITY_SYNC __atomic_unique_lock {
31-
static_assert(std::__popcount(static_cast<unsigned long long>(_LockedBit)) == 1,
31+
static_assert(std::__libcpp_popcount(static_cast<unsigned long long>(_LockedBit)) == 1,
3232
"LockedBit must be an integer where only one bit is set");
3333

3434
std::atomic<_State>& __state_;

0 commit comments

Comments
 (0)