@@ -40,8 +40,7 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_popcount(unsigned lo
4040}
4141
4242template <class _Tp >
43- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __popcount (_Tp __t ) _NOEXCEPT {
44- static_assert (is_unsigned<_Tp>::value, " __popcount only works with unsigned types" );
43+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __popcount_impl (_Tp __t ) _NOEXCEPT {
4544 if _LIBCPP_CONSTEXPR (sizeof (_Tp) <= sizeof (unsigned int )) {
4645 return std::__libcpp_popcount (static_cast <unsigned int >(__t ));
4746 } else if _LIBCPP_CONSTEXPR (sizeof (_Tp) <= sizeof (unsigned long )) {
@@ -51,7 +50,7 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __popcount(_Tp __t) _NOEXCEPT {
5150 } else {
5251#if _LIBCPP_STD_VER == 11
5352 return __t != 0 ? std::__libcpp_popcount (static_cast <unsigned long long >(__t )) +
54- std::__popcount <_Tp>(__t >> numeric_limits<unsigned long long >::digits)
53+ std::__popcount_impl <_Tp>(__t >> numeric_limits<unsigned long long >::digits)
5554 : 0 ;
5655#else
5756 int __ret = 0 ;
@@ -64,15 +63,21 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __popcount(_Tp __t) _NOEXCEPT {
6463 }
6564}
6665
66+ template <class _Tp >
67+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __popcount (_Tp __t ) _NOEXCEPT {
68+ static_assert (is_unsigned<_Tp>::value, " __popcount only works with unsigned types" );
69+ #if __has_builtin(__builtin_popcountg) // TODO (LLVM 21): This can be dropped once we only support Clang >= 19.
70+ return __builtin_popcountg (__t );
71+ #else
72+ return std::__popcount_impl (__t );
73+ #endif
74+ }
75+
6776#if _LIBCPP_STD_VER >= 20
6877
6978template <__libcpp_unsigned_integer _Tp>
7079[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr int popcount (_Tp __t ) noexcept {
71- # if __has_builtin(__builtin_popcountg) // TODO (LLVM 21): This can be dropped once we only support Clang >= 19.
72- return __builtin_popcountg (__t );
73- # else
7480 return std::__popcount (__t );
75- # endif
7681}
7782
7883#endif
0 commit comments