From 590452a9f9f85349cdb73b7fa0d2b48d9a8f167e Mon Sep 17 00:00:00 2001 From: Peng Liu Date: Tue, 1 Apr 2025 11:16:24 -0400 Subject: [PATCH] Replace __libcpp_popcount by __popcount --- libcxx/include/__bit/popcount.h | 43 ------------------- .../include/__stop_token/atomic_unique_lock.h | 2 +- 2 files changed, 1 insertion(+), 44 deletions(-) diff --git a/libcxx/include/__bit/popcount.h b/libcxx/include/__bit/popcount.h index b1d93caab0081..622c8efd7938a 100644 --- a/libcxx/include/__bit/popcount.h +++ b/libcxx/include/__bit/popcount.h @@ -6,9 +6,6 @@ // //===----------------------------------------------------------------------===// -// TODO: __builtin_popcountg is available since Clang 19 and GCC 14. When support for older versions is dropped, we can -// refactor this code to exclusively use __builtin_popcountg. - #ifndef _LIBCPP___BIT_POPCOUNT_H #define _LIBCPP___BIT_POPCOUNT_H @@ -27,50 +24,10 @@ _LIBCPP_PUSH_MACROS _LIBCPP_BEGIN_NAMESPACE_STD -inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_popcount(unsigned __x) _NOEXCEPT { - return __builtin_popcount(__x); -} - -inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_popcount(unsigned long __x) _NOEXCEPT { - return __builtin_popcountl(__x); -} - -inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_popcount(unsigned long long __x) _NOEXCEPT { - return __builtin_popcountll(__x); -} - -template -[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __popcount_impl(_Tp __t) _NOEXCEPT { - if _LIBCPP_CONSTEXPR (sizeof(_Tp) <= sizeof(unsigned int)) { - return std::__libcpp_popcount(static_cast(__t)); - } else if _LIBCPP_CONSTEXPR (sizeof(_Tp) <= sizeof(unsigned long)) { - return std::__libcpp_popcount(static_cast(__t)); - } else if _LIBCPP_CONSTEXPR (sizeof(_Tp) <= sizeof(unsigned long long)) { - return std::__libcpp_popcount(static_cast(__t)); - } else { -#if _LIBCPP_STD_VER == 11 - return __t != 0 ? std::__libcpp_popcount(static_cast(__t)) + - std::__popcount_impl<_Tp>(__t >> numeric_limits::digits) - : 0; -#else - int __ret = 0; - while (__t != 0) { - __ret += std::__libcpp_popcount(static_cast(__t)); - __t >>= std::numeric_limits::digits; - } - return __ret; -#endif - } -} - template [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __popcount(_Tp __t) _NOEXCEPT { static_assert(is_unsigned<_Tp>::value, "__popcount only works with unsigned types"); -#if __has_builtin(__builtin_popcountg) // TODO (LLVM 21): This can be dropped once we only support Clang >= 19. return __builtin_popcountg(__t); -#else - return std::__popcount_impl(__t); -#endif } #if _LIBCPP_STD_VER >= 20 diff --git a/libcxx/include/__stop_token/atomic_unique_lock.h b/libcxx/include/__stop_token/atomic_unique_lock.h index a698260ac7bbd..05e8f223167f1 100644 --- a/libcxx/include/__stop_token/atomic_unique_lock.h +++ b/libcxx/include/__stop_token/atomic_unique_lock.h @@ -28,7 +28,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD // and LockedBit is the value of State when the lock bit is set, e.g 1 << 2 template class _LIBCPP_AVAILABILITY_SYNC __atomic_unique_lock { - static_assert(std::__libcpp_popcount(static_cast(_LockedBit)) == 1, + static_assert(std::__popcount(static_cast(_LockedBit)) == 1, "LockedBit must be an integer where only one bit is set"); std::atomic<_State>& __state_;