Skip to content

Commit c6437ab

Browse files
committed
Put implementation details into __popcount_impl
1 parent 8487428 commit c6437ab

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

libcxx/include/__bit/popcount.h

Lines changed: 11 additions & 6 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)) {
@@ -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)