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

[SingleSource/Vectorizer] Add unit tests for conditional scalar assignment pattern #155

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
52 changes: 48 additions & 4 deletions SingleSource/UnitTests/Vectorizer/common.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <memory>
#include <random>
#include <type_traits>

#define DEFINE_SCALAR_AND_VECTOR_FN2(Init, Loop) \
auto ScalarFn = [](auto *A, auto *B, unsigned TC) { \
Expand All @@ -17,6 +18,16 @@
_Pragma("clang loop vectorize(enable)") Loop \
};

#define DEFINE_SCALAR_AND_VECTOR_FN4(Init, Loop) \
auto ScalarFn = [](auto *cond0, auto *cond1, auto *data0, auto *data1, \
unsigned N, int x) { \
Init _Pragma("clang loop vectorize(disable)") Loop \
}; \
auto VectorFn = [](auto *cond0, auto *cond1, auto *data0, auto *data1, \
unsigned N, int x) { \
Init _Pragma("clang loop vectorize(enable)") Loop \
};

#define DEFINE_NESTED_SCALAR_AND_VECTOR_FN4(InnerLoopCode) \
auto ScalarFn = [](auto *A, auto *B, unsigned OuterTC, unsigned InnerTC) { \
for (unsigned long i = 0; i < OuterTC; i++) { \
Expand Down Expand Up @@ -55,15 +66,40 @@

static std::mt19937 rng;

// Initialize arrays A with random numbers.
template <typename Ty>
static void init_data(const std::unique_ptr<Ty[]> &A, unsigned N) {
// Initialize arrays A with random integers.
template <typename Int,
std::enable_if_t<std::is_integral<Int>::value, bool> = true>
static void init_data(const std::unique_ptr<Int[]> &A, unsigned N) {
std::uniform_int_distribution<uint64_t> distrib(
std::numeric_limits<Ty>::min(), std::numeric_limits<Ty>::max());
std::numeric_limits<Int>::min(), std::numeric_limits<Int>::max());
for (unsigned i = 0; i < N; i++)
A[i] = distrib(rng);
}

// Initialize arrays A with random floating points.
template <typename Float,
std::enable_if_t<std::is_floating_point<Float>::value, bool> = true>
static void init_data(const std::unique_ptr<Float[]> &A, unsigned N) {
std::uniform_real_distribution<float> distrib(
std::numeric_limits<Float>::min(), std::numeric_limits<Float>::max());
for (unsigned i = 0; i < N; i++)
A[i] = distrib(rng);
}

template <typename Ptr,
std::enable_if_t<std::is_pointer<Ptr>::value, bool> = true>
static void init_data(const std::unique_ptr<Ptr[]> &A, unsigned N) {
for (unsigned i = 0; i < N; i++)
A[i] = nullptr;
}

// Initialize arrays A with random booleans.
static void init_cond(const std::unique_ptr<bool[]> &A, unsigned N) {
std::uniform_int_distribution<uint64_t> distrib(0, 1);
for (unsigned i = 0; i < N; i++)
A[i] = !!distrib(rng);
}

template <typename Ty>
static void check(const std::unique_ptr<Ty[]> &Reference,
const std::unique_ptr<Ty[]> &Tmp, unsigned NumElements) {
Expand All @@ -72,3 +108,11 @@ static void check(const std::unique_ptr<Ty[]> &Reference,
exit(1);
}
}

template <typename Ty>
static void check(const Ty Reference, const Ty ToCheck, unsigned NumElements) {
if (Reference != ToCheck) {
std::cerr << "Miscompare\n";
exit(1);
}
}
Loading