-
Notifications
You must be signed in to change notification settings - Fork 1
/
test-align.cpp
124 lines (98 loc) · 3.45 KB
/
test-align.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "simd_helpers/align.hpp"
#include "simd_helpers/simd_debug.hpp"
using namespace std;
using namespace simd_helpers;
// -------------------------------------------------------------------------------------------------
//
// void test_align<T,S>(rng)
// Tests all align() operations for a given (T,S)
//
// simd_t<T,S> simd_align_slow(int A, simd_t<T,S> x, simd_t<T,S> y);
// Slow version of align() in which A is a function argument, not a template parameter.
//
// void simd_align_slow(int A, simd_ntuple<T,S,N> &dst, const simd_ntuple<T,S,N> &x, const simd_ntuple<T,S,N> &src)
// Slow version of align() in which A is a function argument, not a template parameter.
template<typename T, int S, int A1=S+1, typename std::enable_if<(A1==0),int>::type = 0>
static simd_t<T,S> simd_align_slow(int A, simd_t<T,S> x, simd_t<T,S> y)
{
throw runtime_error("simd_align_slow() internal error");
}
template<typename T, int S, int A1=S+1, typename std::enable_if<(A1>0),int>::type = 0>
static simd_t<T,S> simd_align_slow(int A, simd_t<T,S> x, simd_t<T,S> y)
{
return (A == A1-1) ? (simd_align<A1-1>(x,y)) : simd_align_slow<T,S,A1-1>(A,x,y);
}
template<typename T, int S, int N, int A1=S+1, typename std::enable_if<(A1==0),int>::type = 0>
static void simd_align_slow(int A, simd_ntuple<T,S,N> &dst, const simd_ntuple<T,S,N> &x, const simd_ntuple<T,S,N> &y)
{
throw runtime_error("simd_align_slow() internal error");
}
template<typename T, int S, int N, int A1=S+1, typename std::enable_if<(A1>0),int>::type = 0>
static void simd_align_slow(int A, simd_ntuple<T,S,N> &dst, const simd_ntuple<T,S,N> &x, const simd_ntuple<T,S,N> &y)
{
if (A == A1-1)
simd_align<A1-1> (dst, x, y);
else
simd_align_slow<T,S,N,A1-1> (A, dst, x, y);
}
template<typename T, int S, int N>
static void test_align_ntuple(std::mt19937 &rng)
{
for (int A = 0; A <= S; A++) {
simd_ntuple<T,S,N> x = uniform_random_simd_ntuple<T,S,N> (rng, 0, 100);
simd_ntuple<T,S,N> y = uniform_random_simd_ntuple<T,S,N> (rng, 0, 100);
simd_ntuple<T,S,N> t;
simd_align_slow(A, t, x, y);
vector<T> vx = vectorize(x);
vector<T> vy = vectorize(y);
vector<T> vt = vectorize(t);
for (int i = 0; i < N; i++) {
for (int s = 0; s < S; s++) {
T u = vt[i*S+s];
T v = (s+A < S) ? vx[i*S+(s+A)] : vy[i*S+(s+A-S)];
assert(u == v);
}
}
}
}
template<typename T, int S>
static void test_align(std::mt19937 &rng)
{
for (int A = 0; A <= S; A++) {
simd_t<T,S> x = uniform_random_simd_t<T,S> (rng, 0, 100);
simd_t<T,S> y = uniform_random_simd_t<T,S> (rng, 0, 100);
simd_t<T,S> t = simd_align_slow(A, x, y);
vector<T> vx = vectorize(x);
vector<T> vy = vectorize(y);
vector<T> vt = vectorize(t);
for (int s = 0; s < S; s++) {
T u = vt[s];
T v = (s+A < S) ? vx[s+A] : vy[s+A-S];
assert(u == v);
}
}
test_align_ntuple<T,S,1> (rng);
test_align_ntuple<T,S,2> (rng);
test_align_ntuple<T,S,3> (rng);
test_align_ntuple<T,S,4> (rng);
}
// -------------------------------------------------------------------------------------------------
int main(int argc, char **argv)
{
std::random_device rd;
std::mt19937 rng(rd());
for (int iter = 0; iter < 10000; iter++) {
test_align<int,4> (rng);
test_align<float,4> (rng);
test_align<double,2> (rng);
test_align<int64_t,2> (rng);
#ifdef __AVX__
test_align<int,8> (rng);
test_align<float,8> (rng);
test_align<double,4> (rng);
test_align<int64_t,4> (rng);
#endif
}
cout << "test-align: pass\n";
return 0;
}