Skip to content

Commit fc7a2b6

Browse files
committed
Fix ambiguous call in ranges::count & std::count for vector<bool>::iterator
1 parent 3c90c90 commit fc7a2b6

File tree

7 files changed

+164
-66
lines changed

7 files changed

+164
-66
lines changed

libcxx/include/__algorithm/count.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,18 @@ __count_bool(__bit_iterator<_Cp, _IsConst> __first, typename __size_difference_t
5555
if (__first.__ctz_ != 0) {
5656
__storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
5757
__storage_type __dn = std::min(__clz_f, __n);
58-
__storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
59-
__r = std::__libcpp_popcount(std::__invert_if<!_ToCount>(*__first.__seg_) & __m);
58+
__storage_type __m = std::__middle_mask<__storage_type>(__clz_f - __dn, __first.__ctz_);
59+
__r = std::__popcount(__storage_type(std::__invert_if<!_ToCount>(*__first.__seg_) & __m));
6060
__n -= __dn;
6161
++__first.__seg_;
6262
}
6363
// do middle whole words
6464
for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word)
65-
__r += std::__libcpp_popcount(std::__invert_if<!_ToCount>(*__first.__seg_));
65+
__r += std::__popcount(__storage_type(std::__invert_if<!_ToCount>(*__first.__seg_)));
6666
// do last partial word
6767
if (__n > 0) {
68-
__storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
69-
__r += std::__libcpp_popcount(std::__invert_if<!_ToCount>(*__first.__seg_) & __m);
68+
__storage_type __m = std::__trailing_mask<__storage_type>(__bits_per_word - __n);
69+
__r += std::__popcount(__storage_type(std::__invert_if<!_ToCount>(*__first.__seg_) & __m));
7070
}
7171
return __r;
7272
}

libcxx/include/__bit/popcount.h

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <__bit/rotate.h>
1616
#include <__concepts/arithmetic.h>
1717
#include <__config>
18+
#include <__type_traits/is_unsigned.h>
1819
#include <limits>
1920

2021
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -38,31 +39,44 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_popcount(unsigned lo
3839
return __builtin_popcountll(__x);
3940
}
4041

41-
#if _LIBCPP_STD_VER >= 20
42-
43-
template <__libcpp_unsigned_integer _Tp>
44-
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr int popcount(_Tp __t) noexcept {
45-
# if __has_builtin(__builtin_popcountg)
46-
return __builtin_popcountg(__t);
47-
# else // __has_builtin(__builtin_popcountg)
48-
if (sizeof(_Tp) <= sizeof(unsigned int))
42+
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");
45+
if _LIBCPP_CONSTEXPR (sizeof(_Tp) <= sizeof(unsigned int)) {
4946
return std::__libcpp_popcount(static_cast<unsigned int>(__t));
50-
else if (sizeof(_Tp) <= sizeof(unsigned long))
47+
} else if _LIBCPP_CONSTEXPR (sizeof(_Tp) <= sizeof(unsigned long)) {
5148
return std::__libcpp_popcount(static_cast<unsigned long>(__t));
52-
else if (sizeof(_Tp) <= sizeof(unsigned long long))
49+
} else if _LIBCPP_CONSTEXPR (sizeof(_Tp) <= sizeof(unsigned long long)) {
5350
return std::__libcpp_popcount(static_cast<unsigned long long>(__t));
54-
else {
51+
} else {
52+
#if _LIBCPP_STD_VER == 11
53+
// A recursive constexpr implementation for C++11
54+
return __t != 0 ? std::__libcpp_popcount(static_cast<unsigned long long>(__t)) +
55+
std::__popcount<_Tp>(__t >> numeric_limits<unsigned long long>::digits)
56+
: 0;
57+
#else
5558
int __ret = 0;
5659
while (__t != 0) {
5760
__ret += std::__libcpp_popcount(static_cast<unsigned long long>(__t));
58-
__t >>= numeric_limits<unsigned long long>::digits;
61+
__t >>= std::numeric_limits<unsigned long long>::digits;
5962
}
6063
return __ret;
64+
#endif
6165
}
62-
# endif // __has_builtin(__builtin_popcountg)
6366
}
6467

65-
#endif // _LIBCPP_STD_VER >= 20
68+
#if _LIBCPP_STD_VER >= 20
69+
70+
template <__libcpp_unsigned_integer _Tp>
71+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr int popcount(_Tp __t) noexcept {
72+
# if __has_builtin(__builtin_popcountg)
73+
return __builtin_popcountg(__t);
74+
# else
75+
return std::__popcount(__t);
76+
# endif
77+
}
78+
79+
#endif
6680

6781
_LIBCPP_END_NAMESPACE_STD
6882

libcxx/include/__bit_reference

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ struct __size_difference_type_traits<_Cp, __void_t<typename _Cp::difference_type
6767
using size_type = typename _Cp::size_type;
6868
};
6969

70+
template <class _StorageType>
71+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _StorageType __trailing_mask(unsigned __clz) {
72+
static_assert(is_unsigned<_StorageType>::value, "__trailing_mask only works with unsigned types");
73+
return static_cast<_StorageType>(static_cast<_StorageType>(~static_cast<_StorageType>(0)) >> __clz);
74+
}
75+
76+
template <class _StorageType>
77+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _StorageType __middle_mask(unsigned __clz, unsigned __ctz) {
78+
static_assert(is_unsigned<_StorageType>::value, "__middle_mask only works with unsigned types");
79+
return static_cast<_StorageType>(static_cast<_StorageType>(~static_cast<_StorageType>(0)) << __ctz) &
80+
std::__trailing_mask<_StorageType>(__clz);
81+
}
82+
7083
// This function is designed to operate correctly even for smaller integral types like `uint8_t`, `uint16_t`,
7184
// or `unsigned short`. Casting back to _StorageType is crucial to prevent undefined behavior that can arise
7285
// from integral promotions.
@@ -79,8 +92,7 @@ __fill_masked_range(_StoragePointer __word, unsigned __clz, unsigned __ctz, bool
7992
using _StorageType = typename pointer_traits<_StoragePointer>::element_type;
8093
_LIBCPP_ASSERT_VALID_INPUT_RANGE(
8194
__ctz + __clz < sizeof(_StorageType) * CHAR_BIT, "__fill_masked_range called with invalid range");
82-
_StorageType __m = static_cast<_StorageType>(static_cast<_StorageType>(~static_cast<_StorageType>(0)) >> __clz) &
83-
static_cast<_StorageType>(static_cast<_StorageType>(~static_cast<_StorageType>(0)) << __ctz);
95+
_StorageType __m = std::__middle_mask<_StorageType>(__clz, __ctz);
8496
if (__fill_val)
8597
*__word |= __m;
8698
else

libcxx/include/__fwd/bit_reference.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ template <class _StoragePointer>
3030
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void
3131
__fill_masked_range(_StoragePointer __word, unsigned __ctz, unsigned __clz, bool __fill_val);
3232

33+
template <class _StorageType>
34+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _StorageType __trailing_mask(unsigned __clz);
35+
36+
template <class _StorageType>
37+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _StorageType __middle_mask(unsigned __clz, unsigned __ctz);
38+
3339
_LIBCPP_END_NAMESPACE_STD
3440

3541
#endif // _LIBCPP___FWD_BIT_REFERENCE_H

libcxx/test/std/algorithms/alg.nonmodifying/alg.count/count.pass.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515

1616
// ADDITIONAL_COMPILE_FLAGS(has-fconstexpr-steps): -fconstexpr-steps=20000000
1717
// ADDITIONAL_COMPILE_FLAGS(has-fconstexpr-ops-limit): -fconstexpr-ops-limit=80000000
18+
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
1819

1920
#include <algorithm>
2021
#include <cassert>
2122
#include <cstddef>
2223
#include <vector>
2324

25+
#include "sized_allocator.h"
2426
#include "test_macros.h"
2527
#include "test_iterators.h"
2628
#include "type_algorithms.h"
@@ -36,6 +38,29 @@ struct Test {
3638
}
3739
};
3840

41+
TEST_CONSTEXPR_CXX20 void test_bit_iterator_with_custom_sized_types() {
42+
{
43+
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
44+
std::vector<bool, Alloc> in(100, true, Alloc(1));
45+
assert(std::count(in.begin(), in.end(), true) == 100);
46+
}
47+
{
48+
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
49+
std::vector<bool, Alloc> in(199, true, Alloc(1));
50+
assert(std::count(in.begin(), in.end(), true) == 199);
51+
}
52+
{
53+
using Alloc = sized_allocator<bool, std::uint32_t, std::int32_t>;
54+
std::vector<bool, Alloc> in(200, true, Alloc(1));
55+
assert(std::count(in.begin(), in.end(), true) == 200);
56+
}
57+
{
58+
using Alloc = sized_allocator<bool, std::uint64_t, std::int64_t>;
59+
std::vector<bool, Alloc> in(257, true, Alloc(1));
60+
assert(std::count(in.begin(), in.end(), true) == 257);
61+
}
62+
}
63+
3964
TEST_CONSTEXPR_CXX20 bool test() {
4065
types::for_each(types::cpp17_input_iterator_list<const int*>(), Test());
4166

@@ -51,6 +76,8 @@ TEST_CONSTEXPR_CXX20 bool test() {
5176
}
5277
}
5378

79+
test_bit_iterator_with_custom_sized_types();
80+
5481
return true;
5582
}
5683

0 commit comments

Comments
 (0)