Skip to content

Commit 28df28b

Browse files
committed
[libc++] Use _BitInt and __builtin_popcountg in bitset::count()
1 parent c5ded52 commit 28df28b

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

libcxx/include/bitset

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,14 @@ bitset<_Size>::to_string(char __zero, char __one) const {
867867

868868
template <size_t _Size>
869869
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 size_t bitset<_Size>::count() const _NOEXCEPT {
870-
return static_cast<size_t>(std::count(__base::__make_iter(0), __base::__make_iter(_Size), true));
870+
#ifdef _LIBCPP_COMPILER_CLANG_BASED
871+
if constexpr (_Size <= __base::__bits_per_word) {
872+
return __builtin_popcountg(static_cast<unsigned _BitInt(_Size)>(__base::__first_));
873+
} else
874+
#endif
875+
{
876+
return static_cast<size_t>(std::count(__base::__make_iter(0), __base::__make_iter(_Size), true));
877+
}
871878
}
872879

873880
template <size_t _Size>

0 commit comments

Comments
 (0)