-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathtest_batch.cpp
More file actions
134 lines (111 loc) · 3.66 KB
/
test_batch.cpp
File metadata and controls
134 lines (111 loc) · 3.66 KB
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
125
126
127
128
129
130
131
132
133
134
/***************************************************************************
* Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and *
* Martin Renou *
* Copyright (c) QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#include <functional>
#include "test_utils.hpp"
template <class B>
class batch : public testing::Test
{
protected:
using batch_type = B;
using value_type = typename B::value_type;
static constexpr size_t size = B::size;
using array_type = std::array<value_type, size>;
array_type lhs;
array_type rhs;
batch()
{
init_operands();
}
void test_load_store() const
{
array_type res;
batch_type b;
b.load_unaligned(lhs.data());
b.store_unaligned(res.data());
EXPECT_EQ(res, lhs) << print_function_name("load_unaligned / store_unaligned");
alignas(XSIMD_DEFAULT_ALIGNMENT) array_type arhs(this->rhs);
alignas(XSIMD_DEFAULT_ALIGNMENT) array_type ares;
b.load_aligned(arhs.data());
b.store_aligned(ares.data());
EXPECT_EQ(ares, rhs) << print_function_name("load_aligned / store_aligned");
}
void test_constructors() const
{
array_type tmp;
std::fill(tmp.begin(), tmp.end(), value_type(2));
batch_type b0(2);
EXPECT_EQ(b0, tmp) << print_function_name("batch(value_type)");
batch_type b1(lhs.data());
EXPECT_EQ(b1, lhs) << print_function_name("batch(value_type*)");
}
void test_arithmetic() const
{
// batch + batch
{
array_type expected;
std::transform(lhs.cbegin(), lhs.cend(), rhs.cbegin(), expected.begin(), std::plus<value_type>());
batch_type res = batch_lhs() + batch_rhs();
EXPECT_BATCH_EQ(res, expected) << print_function_name("batch + batch");
}
}
private:
batch_type batch_lhs() const
{
return batch_type(lhs.data());
}
batch_type batch_rhs() const
{
return batch_type(rhs.data());
}
template <class T = value_type>
xsimd::enable_integral_t<T, void> init_operands()
{
for (size_t i = 0; i < size; ++i)
{
bool negative_lhs = std::is_signed<T>::value && (i % 2 == 1);
lhs[i] = value_type(i) * (negative_lhs ? -10 : 10);
rhs[i] = value_type(i) + value_type(4);
}
}
template <class T = value_type>
xsimd::enable_floating_point_t<T, void> init_operands()
{
for (size_t i = 0; i < size; ++i)
{
lhs[i] = value_type(i) / 4 + value_type(1.2) * std::sqrt(value_type(i + 0.25));
rhs[i] = value_type(10.2) / (i + 2) + value_type(0.25);
}
}
};
TYPED_TEST_SUITE_P(batch);
TYPED_TEST_P(batch, load_store)
{
this->test_load_store();
}
TYPED_TEST_P(batch, constructors)
{
this->test_constructors();
}
TYPED_TEST_P(batch, arithmetic)
{
this->test_arithmetic();
}
REGISTER_TYPED_TEST_SUITE_P(
batch,
load_store,
constructors,
arithmetic
);
#if XSIMD_X86_INSTR_SET >= XSIMD_X86_SSE2_VERSION
INSTANTIATE_TYPED_TEST_SUITE_P(sse,
batch,
sse_types,
simd_test_names);
#endif