Skip to content

Commit fefda16

Browse files
committed
Fix ambiguous call in {ranges, std}::find
1 parent 3c90c90 commit fefda16

File tree

4 files changed

+143
-17
lines changed

4 files changed

+143
-17
lines changed

libcxx/include/__algorithm/find.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ __find_bool(__bit_iterator<_Cp, _IsConst> __first, typename __size_difference_ty
106106
if (__first.__ctz_ != 0) {
107107
__storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
108108
__storage_type __dn = std::min(__clz_f, __n);
109-
__storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
109+
__storage_type __m = std::__middle_mask<__storage_type>(__first.__ctz_, __clz_f - __dn);
110110
__storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_) & __m;
111111
if (__b)
112-
return _It(__first.__seg_, static_cast<unsigned>(std::__libcpp_ctz(__b)));
112+
return _It(__first.__seg_, static_cast<unsigned>(std::__countr_zero(__b)));
113113
if (__n == __dn)
114114
return __first + __n;
115115
__n -= __dn;
@@ -119,14 +119,14 @@ __find_bool(__bit_iterator<_Cp, _IsConst> __first, typename __size_difference_ty
119119
for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word) {
120120
__storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_);
121121
if (__b)
122-
return _It(__first.__seg_, static_cast<unsigned>(std::__libcpp_ctz(__b)));
122+
return _It(__first.__seg_, static_cast<unsigned>(std::__countr_zero(__b)));
123123
}
124124
// do last partial word
125125
if (__n > 0) {
126-
__storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
126+
__storage_type __m = std::__trailing_mask<__storage_type>(__bits_per_word - __n);
127127
__storage_type __b = std::__invert_if<!_ToFind>(*__first.__seg_) & __m;
128128
if (__b)
129-
return _It(__first.__seg_, static_cast<unsigned>(std::__libcpp_ctz(__b)));
129+
return _It(__first.__seg_, static_cast<unsigned>(std::__countr_zero(__b)));
130130
}
131131
return _It(__first.__seg_, static_cast<unsigned>(__n));
132132
}

libcxx/include/__bit/countr.h

Lines changed: 78 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include <__bit/rotate.h>
1616
#include <__concepts/arithmetic.h>
1717
#include <__config>
18+
#include <__type_traits/enable_if.h>
19+
#include <__type_traits/is_unsigned.h>
1820
#include <limits>
1921

2022
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -38,20 +40,19 @@ _LIBCPP_BEGIN_NAMESPACE_STD
3840
return __builtin_ctzll(__x);
3941
}
4042

41-
template <class _Tp>
42-
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int __countr_zero(_Tp __t) _NOEXCEPT {
43-
#if __has_builtin(__builtin_ctzg)
44-
return __builtin_ctzg(__t, numeric_limits<_Tp>::digits);
45-
#else // __has_builtin(__builtin_ctzg)
46-
if (__t == 0)
47-
return numeric_limits<_Tp>::digits;
48-
if (sizeof(_Tp) <= sizeof(unsigned int))
43+
#if _LIBCPP_STD_VER >= 17
44+
// Implementation using constexpr if for C++ standards >= 17
45+
46+
// Precondition: __t != 0 (This is guaranteed by the caller __countr_zero, which handles __t == 0 as a special case)
47+
template <class _Tp, __enable_if_t<is_unsigned<_Tp>::value, int> = 0>
48+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 int __countr_zero_impl(_Tp __t) _NOEXCEPT {
49+
if constexpr (sizeof(_Tp) <= sizeof(unsigned int)) {
4950
return std::__libcpp_ctz(static_cast<unsigned int>(__t));
50-
else if (sizeof(_Tp) <= sizeof(unsigned long))
51+
} else if constexpr (sizeof(_Tp) <= sizeof(unsigned long)) {
5152
return std::__libcpp_ctz(static_cast<unsigned long>(__t));
52-
else if (sizeof(_Tp) <= sizeof(unsigned long long))
53+
} else if constexpr (sizeof(_Tp) <= sizeof(unsigned long long)) {
5354
return std::__libcpp_ctz(static_cast<unsigned long long>(__t));
54-
else {
55+
} else {
5556
int __ret = 0;
5657
const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
5758
while (static_cast<unsigned long long>(__t) == 0uLL) {
@@ -60,7 +61,72 @@ template <class _Tp>
6061
}
6162
return __ret + std::__libcpp_ctz(static_cast<unsigned long long>(__t));
6263
}
63-
#endif // __has_builtin(__builtin_ctzg)
64+
}
65+
66+
#else
67+
// Equivalent SFINAE-based implementation for older C++ standards < 17
68+
69+
// Precondition: __t != 0 (This is guaranteed by the caller __countr_zero, which handles __t == 0 as a special case)
70+
template < class _Tp, __enable_if_t<is_unsigned<_Tp>::value && sizeof(_Tp) <= sizeof(unsigned int), int> = 0>
71+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __countr_zero_impl(_Tp __t) _NOEXCEPT {
72+
return std::__libcpp_ctz(static_cast<unsigned int>(__t));
73+
}
74+
75+
// Precondition: __t != 0 (This is guaranteed by the caller __countr_zero)
76+
template < class _Tp,
77+
__enable_if_t<is_unsigned<_Tp>::value && (sizeof(_Tp) > sizeof(unsigned int)) &&
78+
sizeof(_Tp) <= sizeof(unsigned long),
79+
int> = 0 >
80+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __countr_zero_impl(_Tp __t) _NOEXCEPT {
81+
return std::__libcpp_ctz(static_cast<unsigned long>(__t));
82+
}
83+
84+
// Precondition: __t != 0 (This is guaranteed by the caller __countr_zero)
85+
template < class _Tp,
86+
__enable_if_t<is_unsigned<_Tp>::value && (sizeof(_Tp) > sizeof(unsigned long)) &&
87+
sizeof(_Tp) <= sizeof(unsigned long long),
88+
int> = 0 >
89+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __countr_zero_impl(_Tp __t) _NOEXCEPT {
90+
return std::__libcpp_ctz(static_cast<unsigned long long>(__t));
91+
}
92+
93+
# if _LIBCPP_STD_VER == 11
94+
95+
// Recursive constexpr implementation for C++11 due to limited constexpr support
96+
// Precondition: __t != 0 (This is guaranteed by the caller __countr_zero)
97+
template < class _Tp, __enable_if_t<is_unsigned<_Tp>::value && (sizeof(_Tp) > sizeof(unsigned long long)), int> = 0 >
98+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __countr_zero_impl(_Tp __t) _NOEXCEPT {
99+
unsigned long long __ull = static_cast<unsigned long long>(__t);
100+
const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
101+
return __ull == 0ull ? __ulldigits + std::__countr_zero_impl<_Tp>(__t >> __ulldigits) : std::__libcpp_ctz(__ull);
102+
}
103+
104+
# else
105+
106+
// Loop-based constexpr implementation for C++14 (and non-constexpr for C++03, 98)
107+
// Precondition: __t != 0 (This is guaranteed by the caller __countr_zero)
108+
template < class _Tp, __enable_if_t<is_unsigned<_Tp>::value && (sizeof(_Tp) > sizeof(unsigned long long)), int> = 0 >
109+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int __countr_zero_impl(_Tp __t) _NOEXCEPT {
110+
int __ret = 0;
111+
const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
112+
while (static_cast<unsigned long long>(__t) == 0uLL) {
113+
__ret += __ulldigits;
114+
__t >>= __ulldigits;
115+
}
116+
return __ret + std::__libcpp_ctz(static_cast<unsigned long long>(__t));
117+
}
118+
119+
# endif // _LIBCPP_STD_VER == 11
120+
121+
#endif // _LIBCPP_STD_VER >= 17
122+
123+
template <class _Tp, __enable_if_t<is_unsigned<_Tp>::value, int> = 0>
124+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __countr_zero(_Tp __t) _NOEXCEPT {
125+
#if __has_builtin(__builtin_ctzg)
126+
return __builtin_ctzg(__t, numeric_limits<_Tp>::digits);
127+
#else
128+
return __t != 0 ? __countr_zero_impl(__t) : numeric_limits<_Tp>::digits;
129+
#endif
64130
}
65131

66132
#if _LIBCPP_STD_VER >= 20

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// ADDITIONAL_COMPILE_FLAGS(cl-style-warnings): /wd4245 /wd4305 /wd4310 /wd4389 /wd4805
1717
// ADDITIONAL_COMPILE_FLAGS(has-fconstexpr-steps): -fconstexpr-steps=20000000
1818
// ADDITIONAL_COMPILE_FLAGS(has-fconstexpr-ops-limit): -fconstexpr-ops-limit=80000000
19+
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
1920

2021
// <algorithm>
2122

@@ -31,6 +32,7 @@
3132
#include <vector>
3233
#include <type_traits>
3334

35+
#include "sized_allocator.h"
3436
#include "test_macros.h"
3537
#include "test_iterators.h"
3638
#include "type_algorithms.h"
@@ -209,6 +211,33 @@ struct TestIntegerPromotions {
209211
}
210212
};
211213

214+
TEST_CONSTEXPR_CXX20 void test_bititer_with_custom_sized_types() {
215+
{
216+
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
217+
std::vector<bool, Alloc> in(100, false, Alloc(1));
218+
in[in.size() - 2] = true;
219+
assert(std::find(in.begin(), in.end(), true) == in.end() - 2);
220+
}
221+
{
222+
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
223+
std::vector<bool, Alloc> in(200, false, Alloc(1));
224+
in[in.size() - 2] = true;
225+
assert(std::find(in.begin(), in.end(), true) == in.end() - 2);
226+
}
227+
{
228+
using Alloc = sized_allocator<bool, std::uint32_t, std::int32_t>;
229+
std::vector<bool, Alloc> in(200, false, Alloc(1));
230+
in[in.size() - 2] = true;
231+
assert(std::find(in.begin(), in.end(), true) == in.end() - 2);
232+
}
233+
{
234+
using Alloc = sized_allocator<bool, std::uint64_t, std::int64_t>;
235+
std::vector<bool, Alloc> in(200, false, Alloc(1));
236+
in[in.size() - 2] = true;
237+
assert(std::find(in.begin(), in.end(), true) == in.end() - 2);
238+
}
239+
}
240+
212241
TEST_CONSTEXPR_CXX20 bool test() {
213242
types::for_each(types::integer_types(), TestTypes<char>());
214243
types::for_each(types::integer_types(), TestTypes<int>());
@@ -227,6 +256,7 @@ TEST_CONSTEXPR_CXX20 bool test() {
227256
#endif
228257

229258
types::for_each(types::integral_types(), TestIntegerPromotions());
259+
test_bititer_with_custom_sized_types();
230260

231261
{ // Test vector<bool>::iterator optimization
232262
std::vector<bool> vec(256 + 8);

libcxx/test/std/algorithms/alg.nonmodifying/alg.find/ranges.find.pass.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <vector>
3535

3636
#include "almost_satisfies_types.h"
37+
#include "sized_allocator.h"
3738
#include "test_iterators.h"
3839

3940
struct NotEqualityComparable {};
@@ -123,6 +124,33 @@ class TriviallyComparable {
123124
bool operator==(const TriviallyComparable&) const = default;
124125
};
125126

127+
constexpr void test_bititer_with_custom_sized_types() {
128+
{
129+
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
130+
std::vector<bool, Alloc> in(100, false, Alloc(1));
131+
in[in.size() - 2] = true;
132+
assert(std::ranges::find(in, true) == in.end() - 2);
133+
}
134+
{
135+
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
136+
std::vector<bool, Alloc> in(200, false, Alloc(1));
137+
in[in.size() - 2] = true;
138+
assert(std::ranges::find(in, true) == in.end() - 2);
139+
}
140+
{
141+
using Alloc = sized_allocator<bool, std::uint32_t, std::int32_t>;
142+
std::vector<bool, Alloc> in(200, false, Alloc(1));
143+
in[in.size() - 2] = true;
144+
assert(std::ranges::find(in, true) == in.end() - 2);
145+
}
146+
{
147+
using Alloc = sized_allocator<bool, std::uint64_t, std::int64_t>;
148+
std::vector<bool, Alloc> in(200, false, Alloc(1));
149+
in[in.size() - 2] = true;
150+
assert(std::ranges::find(in, true) == in.end() - 2);
151+
}
152+
}
153+
126154
constexpr bool test() {
127155
types::for_each(types::type_list<char, wchar_t, int, long, TriviallyComparable<char>, TriviallyComparable<wchar_t>>{},
128156
[]<class T> {
@@ -217,6 +245,8 @@ constexpr bool test() {
217245
}
218246
}
219247

248+
test_bititer_with_custom_sized_types();
249+
220250
return true;
221251
}
222252

0 commit comments

Comments
 (0)