diff --git a/SingleSource/UnitTests/Vectorizer/index-select.cpp b/SingleSource/UnitTests/Vectorizer/index-select.cpp new file mode 100644 --- /dev/null +++ b/SingleSource/UnitTests/Vectorizer/index-select.cpp @@ -0,0 +1,181 @@ +#include +#include +#include +#include +#include +#include + +#include "common.h" + +template using Fn2Ty = std::function; +template +static void checkVectorFunction(Fn2Ty ScalarFn, Fn2Ty VectorFn, + const char *Name) { + std::cout << "Checking " << Name << "\n"; + + unsigned N = 1000; + std::unique_ptr Src1(new Ty[N]); + std::unique_ptr Src2(new Ty[N]); + + { + init_data(Src1, N); + init_data(Src2, N); + auto Reference = ScalarFn(&Src1[0], &Src2[0], N); + auto ToCheck = VectorFn(&Src1[0], &Src2[0], N); + if (Reference != ToCheck) { + std::cerr << "Miscompare\n"; + exit(1); + } + } + + { + std::sort(&Src1[0], &Src1[N]); + std::sort(&Src2[0], &Src2[N]); + auto Reference = ScalarFn(&Src1[0], &Src2[0], N); + auto ToCheck = VectorFn(&Src1[0], &Src2[0], N); + if (Reference != ToCheck) { + std::cerr << "Miscompare\n"; + exit(1); + } + } + + { + for (unsigned I = 0; I != N; ++I) { + Src1[I] = -1; + Src2[I] = -1; + } + auto Reference = ScalarFn(&Src1[0], &Src2[0], N); + auto ToCheck = VectorFn(&Src1[0], &Src2[0], N); + if (Reference != ToCheck) { + std::cerr << "Miscompare\n"; + exit(1); + } + } + + { + for (unsigned I = 0; I != N; ++I) { + Src1[I] = 0; + Src2[I] = -1; + } + auto Reference = ScalarFn(&Src1[0], &Src2[0], N); + auto ToCheck = VectorFn(&Src1[0], &Src2[0], N); + if (Reference != ToCheck) { + std::cerr << "Miscompare\n"; + exit(1); + } + } + + { + for (unsigned I = 0; I != N; ++I) { + Src1[I] = -1; + Src2[I] = 0; + } + auto Reference = ScalarFn(&Src1[0], &Src2[0], N); + auto ToCheck = VectorFn(&Src1[0], &Src2[0], N); + if (Reference != ToCheck) { + std::cerr << "Miscompare\n"; + exit(1); + } + } + + { + for (unsigned I = 0; I != N; ++I) { + Src1[I] = 0; + Src2[I] = 0; + } + auto Reference = ScalarFn(&Src1[0], &Src2[0], N); + auto ToCheck = VectorFn(&Src1[0], &Src2[0], N); + if (Reference != ToCheck) { + std::cerr << "Miscompare\n"; + exit(1); + } + } +} + +int main(void) { + rng = std::mt19937(15); + + { + DEFINE_SCALAR_AND_VECTOR_FN2( + uint32_t Min = -1; + uint32_t MinIdx = 0;, + for (unsigned I = 0; I < TC; I++) { + uint32_t D = A[I] + B[I]; + if (D < Min) { + Min = D; + MinIdx = I; + } + } + return MinIdx; + ); + checkVectorFunction(ScalarFn, VectorFn, "min_index_select_u32_u32_start_0"); + } + + { + DEFINE_SCALAR_AND_VECTOR_FN2( + uint32_t Min = -1; + uint32_t MinIdx = 0;, + for (unsigned I = 0; I < TC / 2; I += 2) { + uint32_t D = A[I] + B[I]; + if (D < Min) { + Min = D; + MinIdx = I; + } + } + return MinIdx; + ); + checkVectorFunction(ScalarFn, VectorFn, "min_index_select_u32_u32_start_0_inc_2"); + } + + + { + DEFINE_SCALAR_AND_VECTOR_FN2( + uint32_t Min = 0; + uint32_t MinIdx = 2;, + for (unsigned I = 0; I < TC; I++) { + uint32_t D = A[I] + B[I]; + if (D < Min) { + Min = D; + MinIdx = I; + } + } + return MinIdx; + ); + checkVectorFunction(ScalarFn, VectorFn, "min_index_select_u32_u32_start_2"); + } + + { + DEFINE_SCALAR_AND_VECTOR_FN2( + uint32_t Min = -1; + uint32_t MinIdx = 0;, + for (uint64_t I = 0; I < TC; I++) { + uint32_t D = A[I] + B[I]; + if (D < Min) { + Min = D; + MinIdx = I; + } + } + return MinIdx; + ); + checkVectorFunction(ScalarFn, VectorFn, "min_index_select_u32_u32_with_trunc"); + } + + { + DEFINE_SCALAR_AND_VECTOR_FN2( + uint32_t Min = -1; + uint32_t MinIdx = 0;, + for (unsigned I = TC; I > 0; I--) { + uint32_t D = A[I] + B[I]; + if (D < Min) { + Min = D; + MinIdx = I; + } + } + return MinIdx; + ); + checkVectorFunction(ScalarFn, VectorFn, "min_index_select_u32_u32_induction_decrement"); + } + + + return 0; +} diff --git a/SingleSource/UnitTests/Vectorizer/index-select.reference_output b/SingleSource/UnitTests/Vectorizer/index-select.reference_output new file mode 100644 --- /dev/null +++ b/SingleSource/UnitTests/Vectorizer/index-select.reference_output @@ -0,0 +1,6 @@ +Checking min_index_select_u32_u32_start_0 +Checking min_index_select_u32_u32_start_0_inc_2 +Checking min_index_select_u32_u32_start_2 +Checking min_index_select_u32_u32_with_trunc +Checking min_index_select_u32_u32_induction_decrement +exit 0