Skip to content

[libc++] Fix ambiguous call in {ranges, std}::count #122529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions libcxx/include/__algorithm/count.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@ __count_bool(__bit_iterator<_Cp, _IsConst> __first, typename __size_difference_t
if (__first.__ctz_ != 0) {
__storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
__storage_type __dn = std::min(__clz_f, __n);
__storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
__r = std::__libcpp_popcount(std::__invert_if<!_ToCount>(*__first.__seg_) & __m);
__storage_type __m = std::__middle_mask<__storage_type>(__clz_f - __dn, __first.__ctz_);
__r = std::__popcount(__storage_type(std::__invert_if<!_ToCount>(*__first.__seg_) & __m));
__n -= __dn;
++__first.__seg_;
}
// do middle whole words
for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word)
__r += std::__libcpp_popcount(std::__invert_if<!_ToCount>(*__first.__seg_));
__r += std::__popcount(std::__invert_if<!_ToCount>(*__first.__seg_));
// do last partial word
if (__n > 0) {
__storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
__r += std::__libcpp_popcount(std::__invert_if<!_ToCount>(*__first.__seg_) & __m);
__storage_type __m = std::__trailing_mask<__storage_type>(__bits_per_word - __n);
__r += std::__popcount(__storage_type(std::__invert_if<!_ToCount>(*__first.__seg_) & __m));
}
return __r;
}
Expand Down
46 changes: 32 additions & 14 deletions libcxx/include/__bit/popcount.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <__bit/rotate.h>
#include <__concepts/arithmetic.h>
#include <__config>
#include <__type_traits/is_unsigned.h>
#include <limits>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Expand All @@ -38,31 +39,48 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_popcount(unsigned lo
return __builtin_popcountll(__x);
}

#if _LIBCPP_STD_VER >= 20

template <__libcpp_unsigned_integer _Tp>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr int popcount(_Tp __t) noexcept {
# if __has_builtin(__builtin_popcountg)
return __builtin_popcountg(__t);
# else // __has_builtin(__builtin_popcountg)
if (sizeof(_Tp) <= sizeof(unsigned int))
template <class _Tp>
[[__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<unsigned int>(__t));
else if (sizeof(_Tp) <= sizeof(unsigned long))
} else if _LIBCPP_CONSTEXPR (sizeof(_Tp) <= sizeof(unsigned long)) {
return std::__libcpp_popcount(static_cast<unsigned long>(__t));
else if (sizeof(_Tp) <= sizeof(unsigned long long))
} else if _LIBCPP_CONSTEXPR (sizeof(_Tp) <= sizeof(unsigned long long)) {
return std::__libcpp_popcount(static_cast<unsigned long long>(__t));
else {
} else {
#if _LIBCPP_STD_VER == 11
return __t != 0 ? std::__libcpp_popcount(static_cast<unsigned long long>(__t)) +
std::__popcount_impl<_Tp>(__t >> numeric_limits<unsigned long long>::digits)
: 0;
#else
int __ret = 0;
while (__t != 0) {
__ret += std::__libcpp_popcount(static_cast<unsigned long long>(__t));
__t >>= numeric_limits<unsigned long long>::digits;
__t >>= std::numeric_limits<unsigned long long>::digits;
}
return __ret;
#endif
}
# endif // __has_builtin(__builtin_popcountg)
}

#endif // _LIBCPP_STD_VER >= 20
template <class _Tp>
[[__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

template <__libcpp_unsigned_integer _Tp>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr int popcount(_Tp __t) noexcept {
return std::__popcount(__t);
}

#endif

_LIBCPP_END_NAMESPACE_STD

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@

// ADDITIONAL_COMPILE_FLAGS(has-fconstexpr-steps): -fconstexpr-steps=20000000
// ADDITIONAL_COMPILE_FLAGS(has-fconstexpr-ops-limit): -fconstexpr-ops-limit=80000000
// XFAIL: FROZEN-CXX03-HEADERS-FIXME

#include <algorithm>
#include <cassert>
#include <cstddef>
#include <vector>

#include "sized_allocator.h"
#include "test_macros.h"
#include "test_iterators.h"
#include "type_algorithms.h"
Expand All @@ -39,16 +41,47 @@ struct Test {
TEST_CONSTEXPR_CXX20 bool test() {
types::for_each(types::cpp17_input_iterator_list<const int*>(), Test());

if (TEST_STD_AT_LEAST_20_OR_RUNTIME_EVALUATED) {
std::vector<bool> vec(256 + 64);
for (ptrdiff_t i = 0; i != 256; ++i) {
for (size_t offset = 0; offset != 64; ++offset) {
std::fill(vec.begin(), vec.end(), false);
std::fill(vec.begin() + offset, vec.begin() + i + offset, true);
assert(std::count(vec.begin() + offset, vec.begin() + offset + 256, true) == i);
assert(std::count(vec.begin() + offset, vec.begin() + offset + 256, false) == 256 - i);
// Tests for std::count with std::vector<bool>::iterator optimizations.
{
{ // check that vector<bool>::iterator optimization works as expected
std::vector<bool> vec(256 + 64);
for (ptrdiff_t i = 0; i != 256; ++i) {
for (size_t offset = 0; offset != 64; ++offset) {
std::fill(vec.begin(), vec.end(), false);
std::fill(vec.begin() + offset, vec.begin() + i + offset, true);
assert(std::count(vec.begin() + offset, vec.begin() + offset + 256, true) == i);
assert(std::count(vec.begin() + offset, vec.begin() + offset + 256, false) == 256 - i);
}
}
}

// Fix std::count for std::vector<bool> with small storage types, e.g., std::uint16_t, unsigned short.
// See https://github.com/llvm/llvm-project/issues/122528
{
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
std::vector<bool, Alloc> in(100, true, Alloc(1));
assert(std::count(in.begin(), in.end(), true) == 100);
}
{
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
std::vector<bool, Alloc> in(199, true, Alloc(1));
assert(std::count(in.begin(), in.end(), true) == 199);
}
{
using Alloc = sized_allocator<bool, unsigned short, short>;
std::vector<bool, Alloc> in(200, true, Alloc(1));
assert(std::count(in.begin(), in.end(), true) == 200);
}
{
using Alloc = sized_allocator<bool, std::uint32_t, std::int32_t>;
std::vector<bool, Alloc> in(205, true, Alloc(1));
assert(std::count(in.begin(), in.end(), true) == 205);
}
{
using Alloc = sized_allocator<bool, std::uint64_t, std::int64_t>;
std::vector<bool, Alloc> in(257, true, Alloc(1));
assert(std::count(in.begin(), in.end(), true) == 257);
}
}

return true;
Expand Down
Loading