Skip to content

Commit 9cbcb0a

Browse files
committed
Fix ranges::equal for vector<bool> with small storage types
1 parent 942fb03 commit 9cbcb0a

File tree

6 files changed

+257
-40
lines changed

6 files changed

+257
-40
lines changed

libcxx/include/__algorithm/equal.h

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,27 @@ __equal_unaligned(__bit_iterator<_Cp, _IsConst1> __first1,
5454
unsigned __clz_f = __bits_per_word - __first1.__ctz_;
5555
difference_type __dn = std::min(static_cast<difference_type>(__clz_f), __n);
5656
__n -= __dn;
57-
__storage_type __m = (~__storage_type(0) << __first1.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
57+
__storage_type __m = std::__middle_mask<__storage_type>(__clz_f - __dn, __first1.__ctz_);
5858
__storage_type __b = *__first1.__seg_ & __m;
5959
unsigned __clz_r = __bits_per_word - __first2.__ctz_;
6060
__storage_type __ddn = std::min<__storage_type>(__dn, __clz_r);
61-
__m = (~__storage_type(0) << __first2.__ctz_) & (~__storage_type(0) >> (__clz_r - __ddn));
61+
__m = std::__middle_mask<__storage_type>(__clz_r - __ddn, __first2.__ctz_);
6262
if (__first2.__ctz_ > __first1.__ctz_) {
63-
if ((*__first2.__seg_ & __m) != (__b << (__first2.__ctz_ - __first1.__ctz_)))
63+
if (static_cast<__storage_type>(*__first2.__seg_ & __m) !=
64+
static_cast<__storage_type>(__b << (__first2.__ctz_ - __first1.__ctz_)))
6465
return false;
6566
} else {
66-
if ((*__first2.__seg_ & __m) != (__b >> (__first1.__ctz_ - __first2.__ctz_)))
67+
if (static_cast<__storage_type>(*__first2.__seg_ & __m) !=
68+
static_cast<__storage_type>(__b >> (__first1.__ctz_ - __first2.__ctz_)))
6769
return false;
6870
}
6971
__first2.__seg_ += (__ddn + __first2.__ctz_) / __bits_per_word;
7072
__first2.__ctz_ = static_cast<unsigned>((__ddn + __first2.__ctz_) % __bits_per_word);
7173
__dn -= __ddn;
7274
if (__dn > 0) {
73-
__m = ~__storage_type(0) >> (__bits_per_word - __dn);
74-
if ((*__first2.__seg_ & __m) != (__b >> (__first1.__ctz_ + __ddn)))
75+
__m = std::__trailing_mask<__storage_type>(__bits_per_word - __n);
76+
if (static_cast<__storage_type>(*__first2.__seg_ & __m) !=
77+
static_cast<__storage_type>(__b >> (__first1.__ctz_ + __ddn)))
7578
return false;
7679
__first2.__ctz_ = static_cast<unsigned>(__dn);
7780
}
@@ -81,29 +84,30 @@ __equal_unaligned(__bit_iterator<_Cp, _IsConst1> __first1,
8184
// __first1.__ctz_ == 0;
8285
// do middle words
8386
unsigned __clz_r = __bits_per_word - __first2.__ctz_;
84-
__storage_type __m = ~__storage_type(0) << __first2.__ctz_;
87+
__storage_type __m = std::__leading_mask<__storage_type>(__first2.__ctz_);
8588
for (; __n >= __bits_per_word; __n -= __bits_per_word, ++__first1.__seg_) {
8689
__storage_type __b = *__first1.__seg_;
87-
if ((*__first2.__seg_ & __m) != (__b << __first2.__ctz_))
90+
if (static_cast<__storage_type>(*__first2.__seg_ & __m) != static_cast<__storage_type>(__b << __first2.__ctz_))
8891
return false;
8992
++__first2.__seg_;
90-
if ((*__first2.__seg_ & ~__m) != (__b >> __clz_r))
93+
if (static_cast<__storage_type>(*__first2.__seg_ & static_cast<__storage_type>(~__m)) !=
94+
static_cast<__storage_type>(__b >> __clz_r))
9195
return false;
9296
}
9397
// do last word
9498
if (__n > 0) {
95-
__m = ~__storage_type(0) >> (__bits_per_word - __n);
99+
__m = std::__trailing_mask<__storage_type>(__bits_per_word - __n);
96100
__storage_type __b = *__first1.__seg_ & __m;
97101
__storage_type __dn = std::min(__n, static_cast<difference_type>(__clz_r));
98-
__m = (~__storage_type(0) << __first2.__ctz_) & (~__storage_type(0) >> (__clz_r - __dn));
99-
if ((*__first2.__seg_ & __m) != (__b << __first2.__ctz_))
102+
__m = std::__middle_mask<__storage_type>(__clz_r - __dn, __first2.__ctz_);
103+
if (static_cast<__storage_type>(*__first2.__seg_ & __m) != static_cast<__storage_type>(__b << __first2.__ctz_))
100104
return false;
101105
__first2.__seg_ += (__dn + __first2.__ctz_) / __bits_per_word;
102106
__first2.__ctz_ = static_cast<unsigned>((__dn + __first2.__ctz_) % __bits_per_word);
103107
__n -= __dn;
104108
if (__n > 0) {
105-
__m = ~__storage_type(0) >> (__bits_per_word - __n);
106-
if ((*__first2.__seg_ & __m) != (__b >> __dn))
109+
__m = std::__trailing_mask<__storage_type>(__bits_per_word - __n);
110+
if (static_cast<__storage_type>(*__first2.__seg_ & __m) != static_cast<__storage_type>(__b >> __dn))
107111
return false;
108112
}
109113
}
@@ -128,7 +132,7 @@ __equal_aligned(__bit_iterator<_Cp, _IsConst1> __first1,
128132
unsigned __clz = __bits_per_word - __first1.__ctz_;
129133
difference_type __dn = std::min(static_cast<difference_type>(__clz), __n);
130134
__n -= __dn;
131-
__storage_type __m = (~__storage_type(0) << __first1.__ctz_) & (~__storage_type(0) >> (__clz - __dn));
135+
__storage_type __m = std::__middle_mask<__storage_type>(__clz - __dn, __first1.__ctz_);
132136
if ((*__first2.__seg_ & __m) != (*__first1.__seg_ & __m))
133137
return false;
134138
++__first2.__seg_;
@@ -144,7 +148,7 @@ __equal_aligned(__bit_iterator<_Cp, _IsConst1> __first1,
144148
return false;
145149
// do last word
146150
if (__n > 0) {
147-
__storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
151+
__storage_type __m = std::__trailing_mask<__storage_type>(__bits_per_word - __n);
148152
if ((*__first2.__seg_ & __m) != (*__first1.__seg_ & __m))
149153
return false;
150154
}

libcxx/include/__bit_reference

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,30 @@ struct __size_difference_type_traits<_Cp, __void_t<typename _Cp::difference_type
6868
using size_type = typename _Cp::size_type;
6969
};
7070

71+
// Creates a mask of type `_StorageType` with a specified number of leading zeros (__clz) and sets all remaining
72+
// bits to one.
73+
template <class _StorageType>
74+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _StorageType __trailing_mask(unsigned __clz) {
75+
static_assert(is_unsigned<_StorageType>::value, "__trailing_mask only works with unsigned types");
76+
return static_cast<_StorageType>(~static_cast<_StorageType>(0)) >> __clz;
77+
}
78+
79+
// Creates a mask of type `_StorageType` with a specified number of trailing zeros (__ctz) and sets all remaining
80+
// bits to one.
81+
template <class _StorageType>
82+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _StorageType __leading_mask(unsigned __ctz) {
83+
static_assert(is_unsigned<_StorageType>::value, "__leading_mask only works with unsigned types");
84+
return static_cast<_StorageType>(~static_cast<_StorageType>(0)) << __ctz;
85+
}
86+
87+
// Creates a mask of type `_StorageType` with a specified number of leading zeros (__clz), a specified number of
88+
// trailing zeros (__ctz), and sets all bits in between to one.
89+
template <class _StorageType>
90+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _StorageType __middle_mask(unsigned __clz, unsigned __ctz) {
91+
static_assert(is_unsigned<_StorageType>::value, "__middle_mask only works with unsigned types");
92+
return std::__leading_mask<_StorageType>(__ctz) & std::__trailing_mask<_StorageType>(__clz);
93+
}
94+
7195
// This function is designed to operate correctly even for smaller integral types like `uint8_t`, `uint16_t`,
7296
// or `unsigned short`. Casting back to _StorageType is crucial to prevent undefined behavior that can arise
7397
// from integral promotions.
@@ -80,8 +104,7 @@ __fill_masked_range(_StoragePointer __word, unsigned __clz, unsigned __ctz, bool
80104
using _StorageType = typename pointer_traits<_StoragePointer>::element_type;
81105
_LIBCPP_ASSERT_VALID_INPUT_RANGE(
82106
__ctz + __clz < sizeof(_StorageType) * CHAR_BIT, "__fill_masked_range called with invalid range");
83-
_StorageType __m = static_cast<_StorageType>(static_cast<_StorageType>(~static_cast<_StorageType>(0)) >> __clz) &
84-
static_cast<_StorageType>(static_cast<_StorageType>(~static_cast<_StorageType>(0)) << __ctz);
107+
_StorageType __m = std::__middle_mask<_StorageType>(__clz, __ctz);
85108
if (__fill_val)
86109
*__word |= __m;
87110
else

libcxx/include/__fwd/bit_reference.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ 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 __leading_mask(unsigned __ctz);
38+
39+
template <class _StorageType>
40+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _StorageType __middle_mask(unsigned __clz, unsigned __ctz);
41+
3342
_LIBCPP_END_NAMESPACE_STD
3443

3544
#endif // _LIBCPP___FWD_BIT_REFERENCE_H

libcxx/include/__vector/comparison.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,6 @@ operator==(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __
2929
return __sz == __y.size() && std::equal(__x.begin(), __x.end(), __y.begin());
3030
}
3131

32-
// FIXME: Remove this `vector<bool>` overload once #126369 is resolved, reverting to the generic `operator==`
33-
// with `std::equal` for better performance.
34-
template <class _Allocator>
35-
_LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI bool
36-
operator==(const vector<bool, _Allocator>& __x, const vector<bool, _Allocator>& __y) {
37-
const typename vector<bool, _Allocator>::size_type __sz = __x.size();
38-
if (__sz != __y.size())
39-
return false;
40-
for (typename vector<bool, _Allocator>::size_type __i = 0; __i < __sz; ++__i)
41-
if (__x[__i] != __y[__i])
42-
return false;
43-
return true;
44-
}
45-
4632
#if _LIBCPP_STD_VER <= 17
4733

4834
template <class _Tp, class _Allocator>

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

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424
// MSVC warning C4244: 'argument': conversion from 'wchar_t' to 'const _Ty', possible loss of data
2525
// MSVC warning C4389: '==': signed/unsigned mismatch
2626
// ADDITIONAL_COMPILE_FLAGS(cl-style-warnings): /wd4242 /wd4244 /wd4389
27+
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
2728

2829
#include <algorithm>
2930
#include <cassert>
3031
#include <functional>
3132
#include <vector>
3233

34+
#include "sized_allocator.h"
3335
#include "test_iterators.h"
3436
#include "test_macros.h"
3537
#include "type_algorithms.h"
@@ -173,6 +175,90 @@ TEST_CONSTEXPR_CXX20 bool test() {
173175
test_vector_bool<256>();
174176
}
175177

178+
// Make sure std::equal behaves properly with std::vector<bool> iterators with custom size types.
179+
// See issue: https://github.com/llvm/llvm-project/issues/126369.
180+
{
181+
//// Tests for std::equal with aligned bits
182+
183+
{ // Test the first (partial) word for uint8_t
184+
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
185+
std::vector<bool, Alloc> in(6, true, Alloc(1));
186+
std::vector<bool, Alloc> expected(8, true, Alloc(1));
187+
assert(std::equal(in.begin() + 4, in.end(), expected.begin() + 4));
188+
}
189+
{ // Test the last word for uint8_t
190+
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
191+
std::vector<bool, Alloc> in(12, true, Alloc(1));
192+
std::vector<bool, Alloc> expected(16, true, Alloc(1));
193+
assert(std::equal(in.begin(), in.end(), expected.begin()));
194+
}
195+
{ // Test middle words for uint8_t
196+
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
197+
std::vector<bool, Alloc> in(24, true, Alloc(1));
198+
std::vector<bool, Alloc> expected(29, true, Alloc(1));
199+
assert(std::equal(in.begin(), in.end(), expected.begin()));
200+
}
201+
202+
{ // Test the first (partial) word for uint16_t
203+
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
204+
std::vector<bool, Alloc> in(12, true, Alloc(1));
205+
std::vector<bool, Alloc> expected(16, true, Alloc(1));
206+
assert(std::equal(in.begin() + 4, in.end(), expected.begin() + 4));
207+
}
208+
{ // Test the last word for uint16_t
209+
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
210+
std::vector<bool, Alloc> in(24, true, Alloc(1));
211+
std::vector<bool, Alloc> expected(32, true, Alloc(1));
212+
assert(std::equal(in.begin(), in.end(), expected.begin()));
213+
}
214+
{ // Test middle words for uint16_t
215+
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
216+
std::vector<bool, Alloc> in(48, true, Alloc(1));
217+
std::vector<bool, Alloc> expected(55, true, Alloc(1));
218+
assert(std::equal(in.begin(), in.end(), expected.begin()));
219+
}
220+
221+
//// Tests for std::equal with unaligned bits
222+
223+
{ // Test the first (partial) word for uint8_t
224+
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
225+
std::vector<bool, Alloc> in(6, true, Alloc(1));
226+
std::vector<bool, Alloc> expected(8, true, Alloc(1));
227+
assert(std::equal(in.begin() + 4, in.end(), expected.begin()));
228+
}
229+
{ // Test the last word for uint8_t
230+
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
231+
std::vector<bool, Alloc> in(4, true, Alloc(1));
232+
std::vector<bool, Alloc> expected(8, true, Alloc(1));
233+
assert(std::equal(in.begin(), in.end(), expected.begin() + 3));
234+
}
235+
{ // Test middle words for uint8_t
236+
using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
237+
std::vector<bool, Alloc> in(16, true, Alloc(1));
238+
std::vector<bool, Alloc> expected(24, true, Alloc(1));
239+
assert(std::equal(in.begin(), in.end(), expected.begin() + 4));
240+
}
241+
242+
{ // Test the first (partial) word for uint16_t
243+
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
244+
std::vector<bool, Alloc> in(12, true, Alloc(1));
245+
std::vector<bool, Alloc> expected(16, true, Alloc(1));
246+
assert(std::equal(in.begin() + 4, in.end(), expected.begin()));
247+
}
248+
{ // Test the last word for uint16_t
249+
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
250+
std::vector<bool, Alloc> in(12, true, Alloc(1));
251+
std::vector<bool, Alloc> expected(16, true, Alloc(1));
252+
assert(std::equal(in.begin(), in.end(), expected.begin() + 3));
253+
}
254+
{ // Test the middle words for uint16_t
255+
using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
256+
std::vector<bool, Alloc> in(32, true, Alloc(1));
257+
std::vector<bool, Alloc> expected(64, true, Alloc(1));
258+
assert(std::equal(in.begin(), in.end(), expected.begin() + 4));
259+
}
260+
}
261+
176262
return true;
177263
}
178264

0 commit comments

Comments
 (0)