-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_reduce.cpp
More file actions
173 lines (135 loc) · 5.86 KB
/
Copy pathtest_reduce.cpp
File metadata and controls
173 lines (135 loc) · 5.86 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/***************************************************************************
* Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and *
* Martin Renou *
* Copyright (c) QuantStack *
* Copyright (c) Serge Guelton *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#include "xsimd_algorithm/stl/reduce.hpp"
#ifndef XSIMD_NO_SUPPORTED_ARCHITECTURE
#include "doctest/doctest.h"
#include <numeric>
#include <vector>
template <class T>
using test_allocator_type = xsimd::aligned_allocator<T>;
#if XSIMD_WITH_NEON && !XSIMD_WITH_NEON64
using test_value_type = float;
#else
using test_value_type = double;
#endif
struct multiply
{
template <class T>
T operator()(const T& a, const T& b) const
{
return a * b;
}
};
TEST_CASE("xsimd_reduce - unaligned_begin_unaligned_end")
{
using aligned_vec_t = std::vector<test_value_type, test_allocator_type<test_value_type>>;
constexpr std::size_t num_elements = 4 * xsimd::batch<test_value_type>::size;
constexpr std::size_t small_num = xsimd::batch<test_value_type>::size - 1;
aligned_vec_t vec(num_elements, 123.);
aligned_vec_t small_vec(small_num, 42.);
test_value_type init = 1337.;
auto const begin = std::next(vec.begin());
auto const end = std::prev(vec.end());
CHECK_EQ(std::accumulate(begin, end, init), xsimd::reduce(begin, end, init));
if (small_vec.size() > 1)
{
auto const sbegin = std::next(small_vec.begin());
auto const send = std::prev(small_vec.end());
CHECK_EQ(std::accumulate(sbegin, send, init), xsimd::reduce(sbegin, send, init));
}
}
TEST_CASE("xsimd_reduce - unaligned_begin_aligned_end")
{
using aligned_vec_t = std::vector<test_value_type, test_allocator_type<test_value_type>>;
constexpr std::size_t num_elements = 4 * xsimd::batch<test_value_type>::size;
constexpr std::size_t small_num = xsimd::batch<test_value_type>::size - 1;
aligned_vec_t vec(num_elements, 123.);
aligned_vec_t small_vec(small_num, 42.);
test_value_type init = 1337.;
auto const begin = std::next(vec.begin());
auto const end = vec.end();
CHECK_EQ(std::accumulate(begin, end, init), xsimd::reduce(begin, end, init));
if (small_vec.size() > 1)
{
auto const sbegin = std::next(small_vec.begin());
auto const send = small_vec.end();
CHECK_EQ(std::accumulate(sbegin, send, init), xsimd::reduce(sbegin, send, init));
}
}
TEST_CASE("xsimd_reduce - aligned_begin_unaligned_end")
{
using aligned_vec_t = std::vector<test_value_type, test_allocator_type<test_value_type>>;
constexpr std::size_t num_elements = 4 * xsimd::batch<test_value_type>::size;
constexpr std::size_t small_num = xsimd::batch<test_value_type>::size - 1;
aligned_vec_t vec(num_elements, 123.);
aligned_vec_t small_vec(small_num, 42.);
test_value_type init = 1337.;
auto const begin = vec.begin();
auto const end = std::prev(vec.end());
CHECK_EQ(std::accumulate(begin, end, init), xsimd::reduce(begin, end, init));
if (small_vec.size() > 1)
{
auto const sbegin = small_vec.begin();
auto const send = std::prev(small_vec.end());
CHECK_EQ(std::accumulate(sbegin, send, init), xsimd::reduce(sbegin, send, init));
}
}
TEST_CASE("xsimd_reduce - aligned_begin_aligned_end")
{
using aligned_vec_t = std::vector<test_value_type, test_allocator_type<test_value_type>>;
constexpr std::size_t num_elements = 4 * xsimd::batch<test_value_type>::size;
constexpr std::size_t small_num = xsimd::batch<test_value_type>::size - 1;
aligned_vec_t vec(num_elements, 123.);
aligned_vec_t small_vec(small_num, 42.);
test_value_type init = 1337.;
auto const begin = vec.begin();
auto const end = vec.end();
CHECK_EQ(std::accumulate(begin, end, init), xsimd::reduce(begin, end, init));
if (small_vec.size() > 1)
{
auto const sbegin = small_vec.begin();
auto const send = small_vec.end();
CHECK_EQ(std::accumulate(sbegin, send, init), xsimd::reduce(sbegin, send, init));
}
}
TEST_CASE("xsimd_reduce - using_custom_binary_function")
{
using aligned_vec_t = std::vector<test_value_type, test_allocator_type<test_value_type>>;
constexpr std::size_t num_elements = 4 * xsimd::batch<test_value_type>::size;
constexpr std::size_t small_num = xsimd::batch<test_value_type>::size - 1;
aligned_vec_t vec(num_elements, 123.);
aligned_vec_t small_vec(small_num, 42.);
test_value_type init = 1337.;
auto const begin = vec.begin();
auto const end = vec.end();
if (std::is_same<aligned_vec_t::value_type, double>::value)
{
CHECK(std::accumulate(begin, end, init, multiply {}) == doctest::Approx(xsimd::reduce(begin, end, init, multiply {})));
}
else
{
CHECK(std::accumulate(begin, end, init, multiply {}) == doctest::Approx(xsimd::reduce(begin, end, init, multiply {})));
}
if (small_vec.size() > 1)
{
auto const sbegin = small_vec.begin();
auto const send = small_vec.end();
if (std::is_same<aligned_vec_t::value_type, double>::value)
{
CHECK(std::accumulate(sbegin, send, init, multiply {}) == doctest::Approx(xsimd::reduce(sbegin, send, init, multiply {})));
}
else
{
CHECK(std::accumulate(sbegin, send, init, multiply {}) == doctest::Approx(xsimd::reduce(sbegin, send, init, multiply {})));
}
}
}
#endif