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 min/max/minmax_element for 64-bit types on x86 #2821

Merged
merged 5 commits into from
Jun 25, 2022
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
5 changes: 3 additions & 2 deletions stl/src/vector_algorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -789,8 +789,9 @@ namespace {

static _Signed_t _Get_any(const __m128i _Cur) noexcept {
#ifdef _M_IX86
return static_cast<_Signed_t>((static_cast<_Unsigned_t>(_mm_extract_epi32(_Cur, 1)) << 32)
| static_cast<_Unsigned_t>(_mm_cvtsi128_si32(_Cur)));
return static_cast<_Signed_t>(
(static_cast<_Unsigned_t>(static_cast<uint32_t>(_mm_extract_epi32(_Cur, 1))) << 32)
| static_cast<_Unsigned_t>(static_cast<uint32_t>(_mm_cvtsi128_si32(_Cur))));
#else // ^^^ x86 ^^^ / vvv x64 vvv
return static_cast<_Signed_t>(_mm_cvtsi128_si64(_Cur));
#endif // ^^^ x64 ^^^
Expand Down
66 changes: 52 additions & 14 deletions tests/std/tests/VSO_0000000_vector_algorithms/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,51 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <algorithm>
#include <assert.h>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <isa_availability.h>
#include <limits>
#include <list>
#include <random>
#include <type_traits>
#include <vector>

using namespace std;

#pragma warning(disable : 4984) // 'if constexpr' is a C++17 language extension
#ifdef __clang__
#pragma clang diagnostic ignored "-Wc++17-extensions" // constexpr if is a C++17 extension
#endif // __clang__

void initialize_randomness(mt19937_64& gen) {
constexpr size_t n = mt19937_64::state_size;
constexpr size_t w = mt19937_64::word_size;
static_assert(w % 32 == 0, "w should be evenly divisible by 32");
constexpr size_t k = w / 32;
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved

vector<uint32_t> vec(n * k);

random_device rd;
generate(vec.begin(), vec.end(), ref(rd));

printf("This is a randomized test.\n");
printf("DO NOT IGNORE/RERUN ANY FAILURES.\n");
printf("You must report them to the STL maintainers.\n\n");

printf("Seed vector: ");
for (const auto& e : vec) {
printf("%u,", e);
}
printf("\n");

seed_seq seq(vec.cbegin(), vec.cend());
gen.seed(seq);
}

#if (defined(_M_IX86) || defined(_M_X64)) && !defined(_M_CEE_PURE)
extern "C" long __isa_enabled;

Expand Down Expand Up @@ -164,10 +198,9 @@ void test_case_min_max_element(const vector<T>& input) {

template <class T>
void test_min_max_element(mt19937_64& gen) {
using Distribution = conditional_t<is_floating_point_v<T>, uniform_real_distribution<T>,
conditional_t<(sizeof(T) > 1), uniform_int_distribution<T>, uniform_int_distribution<int>>>;
using Limits = numeric_limits<T>;

Distribution dis(1, 20);
uniform_int_distribution<conditional_t<sizeof(T) == 1, int, T>> dis(Limits::min(), Limits::max());

vector<T> input;
input.reserve(dataCount);
Expand Down Expand Up @@ -324,9 +357,7 @@ void test_swap_ranges(mt19937_64& gen) {
}
}

void test_vector_algorithms() {
mt19937_64 gen(1729);

void test_vector_algorithms(mt19937_64& gen) {
test_count<char>(gen);
test_count<signed char>(gen);
test_count<unsigned char>(gen);
Expand Down Expand Up @@ -356,16 +387,20 @@ void test_vector_algorithms() {
test_min_max_element<unsigned int>(gen);
test_min_max_element<long long>(gen);
test_min_max_element<unsigned long long>(gen);
test_min_max_element<float>(gen);
test_min_max_element<double>(gen);
test_min_max_element<long double>(gen);

test_min_max_element_pointers(gen);

test_min_max_element_special_cases<int8_t, 16>(); // SSE2 vectors
test_min_max_element_special_cases<int8_t, 32>(); // AVX2 vectors
test_min_max_element_special_cases<int8_t, 64>(); // AVX512 vectors

// Test VSO-1558536, a regression caused by GH-2447 that was specific to 64-bit types on x86.
test_case_min_max_element(vector<uint64_t>{10, 0x8000'0000ULL, 20, 30});
test_case_min_max_element(vector<uint64_t>{10, 20, 0xD000'0000'B000'0000ULL, 30, 0xC000'0000'A000'0000ULL});
test_case_min_max_element(vector<int64_t>{10, 0x8000'0000LL, 20, 30});
test_case_min_max_element(
vector<int64_t>{-6604286336755016904, -4365366089374418225, 6104371530830675888, -8582621853879131834});

test_reverse<char>(gen);
test_reverse<signed char>(gen);
test_reverse<unsigned char>(gen);
Expand Down Expand Up @@ -438,18 +473,21 @@ void test_various_containers() {
}

int main() {
test_vector_algorithms();
mt19937_64 gen;
initialize_randomness(gen);

test_vector_algorithms(gen);
test_various_containers();
#ifndef _M_CEE_PURE
#if defined(_M_IX86) || defined(_M_X64)
disable_instructions(__ISA_AVAILABLE_AVX2);
test_vector_algorithms();
test_vector_algorithms(gen);
disable_instructions(__ISA_AVAILABLE_SSE42);
test_vector_algorithms();
test_vector_algorithms(gen);
#endif // defined(_M_IX86) || defined(_M_X64)
#if defined(_M_IX86)
disable_instructions(__ISA_AVAILABLE_SSE2);
test_vector_algorithms();
test_vector_algorithms(gen);
#endif // defined(_M_IX86)
#endif // _M_CEE_PURE
}