Skip to content
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

Fix vectorized ranges::find with unreachable_sentinel to properly mask the beginning and handle unaligned pointers #4450

Merged
merged 4 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 6 additions & 17 deletions stl/src/vector_algorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1862,23 +1862,18 @@ namespace {
unsigned int _Bingo = static_cast<unsigned int>(_mm256_movemask_epi8(_Traits::_Cmp_avx(_Data, _Comparand)));

_Bingo &= _Mask;
if (_Bingo != 0) {
unsigned long _Offset = _tzcnt_u32(_Bingo);
_Advance_bytes(_First, _Offset);
return _First;
}

for (;;) {
_Data = _mm256_load_si256(static_cast<const __m256i*>(_First));
_Bingo = static_cast<unsigned int>(_mm256_movemask_epi8(_Traits::_Cmp_avx(_Data, _Comparand)));

if (_Bingo != 0) {
unsigned long _Offset = _tzcnt_u32(_Bingo);
_Advance_bytes(_First, _Offset);
return _First;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I observe that since we have only one return from AVX2 code path, we can remove _Zeroupper_on_exit _Guard; and put explicit _mm256_zeroupper(); here. Sure even if it is worth doing, can be done as a follow-up

}

_Advance_bytes(_First, 32);

_Data = _mm256_load_si256(static_cast<const __m256i*>(_First));
_Bingo = static_cast<unsigned int>(_mm256_movemask_epi8(_Traits::_Cmp_avx(_Data, _Comparand)));
}
}

Expand All @@ -1898,17 +1893,8 @@ namespace {
unsigned int _Bingo = static_cast<unsigned int>(_mm_movemask_epi8(_Traits::_Cmp_sse(_Data, _Comparand)));

_Bingo &= _Mask;
if (_Bingo != 0) {
unsigned long _Offset;
_BitScanForward(&_Offset, _Bingo); // lgtm [cpp/conditionallyuninitializedvariable]
_Advance_bytes(_First, _Offset);
return _First;
}

for (;;) {
_Data = _mm_load_si128(static_cast<const __m128i*>(_First));
_Bingo = static_cast<unsigned int>(_mm_movemask_epi8(_Traits::_Cmp_sse(_Data, _Comparand)));

if (_Bingo != 0) {
unsigned long _Offset;
_BitScanForward(&_Offset, _Bingo); // lgtm [cpp/conditionallyuninitializedvariable]
Expand All @@ -1917,6 +1903,9 @@ namespace {
}

_Advance_bytes(_First, 16);

_Data = _mm_load_si128(static_cast<const __m128i*>(_First));
_Bingo = static_cast<unsigned int>(_mm_movemask_epi8(_Traits::_Cmp_sse(_Data, _Comparand)));
}
}
#endif // !_M_ARM64EC
Expand Down
33 changes: 33 additions & 0 deletions tests/std/tests/VSO_0000000_vector_algorithms/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,32 @@ void test_find(mt19937_64& gen) {
}
}

#if _HAS_CXX20
// GH-4449 <xutility>: ranges::find with unreachable_sentinel / __std_find_trivial_unsized_1 gives wrong result
template <class T>
void test_gh_4449() {
constexpr T desired_val{11};
constexpr T unwanted_val{22};

T arr[256];

constexpr int mid1 = 64;
constexpr int mid2 = 192;

ranges::fill(arr, arr + mid1, desired_val);
ranges::fill(arr + mid1, arr + mid2, unwanted_val);
ranges::fill(arr + mid2, end(arr), desired_val);

for (int idx = mid1; idx <= mid2; ++idx) { // when idx == mid2, the value is immediately found
const auto where = ranges::find(arr + idx, unreachable_sentinel, desired_val);

assert(where == arr + mid2);

arr[idx] = desired_val; // get ready for the next iteration
}
}
#endif // _HAS_CXX20

#if _HAS_CXX23
template <class T>
void test_case_find_last(const vector<T>& input, T v) {
Expand Down Expand Up @@ -371,6 +397,13 @@ void test_vector_algorithms(mt19937_64& gen) {
test_find<long long>(gen);
test_find<unsigned long long>(gen);

#if _HAS_CXX20
test_gh_4449<uint8_t>();
test_gh_4449<uint16_t>();
test_gh_4449<uint32_t>();
test_gh_4449<uint64_t>();
#endif // _HAS_CXX20

#if _HAS_CXX23
test_find_last<char>(gen);
test_find_last<signed char>(gen);
Expand Down