Skip to content

Commit f70dcb4

Browse files
committed
Put implementation details into __popcount_impl
1 parent 4b26eef commit f70dcb4

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

libcxx/include/__algorithm/count.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ __count_bool(__bit_iterator<_Cp, _IsConst> __first, typename __size_difference_t
6262
}
6363
// do middle whole words
6464
for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word)
65-
__r += std::__popcount(__storage_type(std::__invert_if<!_ToCount>(*__first.__seg_)));
65+
__r += std::__popcount(std::__invert_if<!_ToCount>(*__first.__seg_));
6666
// do last partial word
6767
if (__n > 0) {
6868
__storage_type __m = std::__trailing_mask<__storage_type>(__bits_per_word - __n);

libcxx/include/__bit/popcount.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_popcount(unsigned lo
4040
}
4141

4242
template <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

6978
template <__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

Comments
 (0)