Skip to content

Commit eeb8232

Browse files
committed
Test refactoring: complex exponential
1 parent cef367b commit eeb8232

File tree

3 files changed

+217
-2
lines changed

3 files changed

+217
-2
lines changed

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ set(XSIMD_TESTS
148148
test_batch_complex.cpp
149149
test_batch_float.cpp
150150
test_batch_int.cpp
151+
test_complex_exponential.cpp
151152
test_error_gamma.cpp
152153
test_exponential.cpp
153154
test_fp_manipulation.cpp

test/test_complex_exponential.cpp

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
/***************************************************************************
2+
* Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and *
3+
* Martin Renou *
4+
* Copyright (c) QuantStack *
5+
* *
6+
* Distributed under the terms of the BSD 3-Clause License. *
7+
* *
8+
* The full license is in the file LICENSE, distributed with this software. *
9+
****************************************************************************/
10+
11+
#include "xsimd/math/xsimd_math_complex.hpp"
12+
#include "test_utils.hpp"
13+
14+
template <class B>
15+
class complex_exponential_test : public testing::Test
16+
{
17+
protected:
18+
19+
using batch_type = B;
20+
using real_batch_type = typename B::real_batch;
21+
using value_type = typename B::value_type;
22+
using real_value_type = typename value_type::value_type;
23+
static constexpr size_t size = B::size;
24+
using vector_type = std::vector<value_type>;
25+
26+
size_t nb_input;
27+
vector_type exp_input;
28+
vector_type huge_exp_input;
29+
vector_type log_input;
30+
vector_type expected;
31+
vector_type res;
32+
33+
complex_exponential_test()
34+
{
35+
nb_input = 10000 * size;
36+
exp_input.resize(nb_input);
37+
huge_exp_input.resize(nb_input);
38+
log_input.resize(nb_input);
39+
for (size_t i = 0; i < nb_input; ++i)
40+
{
41+
exp_input[i] = value_type(real_value_type(-1.5) + i * real_value_type(3) / nb_input,
42+
real_value_type(-1.3) + i * real_value_type(2) / nb_input);
43+
huge_exp_input[i] = value_type(real_value_type(0), real_value_type(102.12) + i * real_value_type(100.) / nb_input);
44+
log_input[i] = value_type(real_value_type(0.001 + i * 100 / nb_input),
45+
real_value_type(0.002 + i * 110 / nb_input));
46+
}
47+
expected.resize(nb_input);
48+
res.resize(nb_input);
49+
}
50+
51+
void test_exp()
52+
{
53+
std::transform(exp_input.cbegin(), exp_input.cend(), expected.begin(),
54+
[](const value_type& v) { using std::exp; return exp(v); });
55+
batch_type in, out;
56+
for (size_t i = 0; i < nb_input; i += size)
57+
{
58+
detail::load_batch(in, exp_input, i);
59+
out = exp(in);
60+
detail::store_batch(out, res, i);
61+
}
62+
size_t diff = detail::get_nb_diff(res, expected);
63+
EXPECT_EQ(diff, 0) << print_function_name("exp");
64+
}
65+
66+
void test_expm1()
67+
{
68+
std::transform(exp_input.cbegin(), exp_input.cend(), expected.begin(),
69+
[](const value_type& v) { using xsimd::expm1; return expm1(v); });
70+
71+
batch_type in, out;
72+
for (size_t i = 0; i < nb_input; i += size)
73+
{
74+
detail::load_batch(in, exp_input, i);
75+
out = expm1(in);
76+
detail::store_batch(out, res, i);
77+
}
78+
size_t diff = detail::get_nb_diff(res, expected);
79+
EXPECT_EQ(diff, 0) << print_function_name("expm1");
80+
}
81+
82+
void test_huge_exp()
83+
{
84+
std::transform(huge_exp_input.cbegin(), huge_exp_input.cend(), expected.begin(),
85+
[](const value_type& v) { using std::exp; return exp(v); });
86+
batch_type in, out;
87+
for (size_t i = 0; i < nb_input; i += size)
88+
{
89+
detail::load_batch(in, huge_exp_input, i);
90+
out = exp(in);
91+
detail::store_batch(out, res, i);
92+
}
93+
size_t diff = detail::get_nb_diff(res, expected);
94+
EXPECT_EQ(diff, 0) << print_function_name("huge exp");
95+
}
96+
97+
void test_log()
98+
{
99+
std::transform(log_input.cbegin(), log_input.cend(), expected.begin(),
100+
[](const value_type& v) { using std::log; return log(v); });
101+
batch_type in, out;
102+
for (size_t i = 0; i < nb_input; i += size)
103+
{
104+
detail::load_batch(in, log_input, i);
105+
out = log(in);
106+
detail::store_batch(out, res, i);
107+
}
108+
size_t diff = detail::get_nb_diff(res, expected);
109+
EXPECT_EQ(diff, 0) << print_function_name("log");
110+
}
111+
112+
void test_log2()
113+
{
114+
std::transform(log_input.cbegin(), log_input.cend(), expected.begin(),
115+
[](const value_type& v) { using xsimd::log2; return log2(v); });
116+
batch_type in, out;
117+
for (size_t i = 0; i < nb_input; i += size)
118+
{
119+
detail::load_batch(in, log_input, i);
120+
out = log2(in);
121+
detail::store_batch(out, res, i);
122+
}
123+
size_t diff = detail::get_nb_diff(res, expected);
124+
EXPECT_EQ(diff, 0) << print_function_name("log2");
125+
}
126+
127+
void test_log10()
128+
{
129+
std::transform(log_input.cbegin(), log_input.cend(), expected.begin(),
130+
[](const value_type& v) { using std::log10; return log10(v); });
131+
batch_type in, out;
132+
for (size_t i = 0; i < nb_input; i += size)
133+
{
134+
detail::load_batch(in, log_input, i);
135+
out = log10(in);
136+
detail::store_batch(out, res, i);
137+
}
138+
size_t diff = detail::get_nb_diff(res, expected);
139+
EXPECT_EQ(diff, 0) << print_function_name("log10");
140+
}
141+
142+
void test_log1p()
143+
{
144+
std::transform(log_input.cbegin(), log_input.cend(), expected.begin(),
145+
[](const value_type& v) { using xsimd::log1p; return log1p(v); });
146+
batch_type in, out;
147+
for (size_t i = 0; i < nb_input; i += size)
148+
{
149+
detail::load_batch(in, log_input, i);
150+
out = log1p(in);
151+
detail::store_batch(out, res, i);
152+
}
153+
size_t diff = detail::get_nb_diff(res, expected);
154+
EXPECT_EQ(diff, 0) << print_function_name("log1p");
155+
}
156+
157+
void test_sign()
158+
{
159+
std::transform(log_input.cbegin(), log_input.cend(), expected.begin(),
160+
[](const value_type& v) { using xsimd::sign; return sign(v); });
161+
batch_type in, out;
162+
for (size_t i = 0; i < nb_input; i += size)
163+
{
164+
detail::load_batch(in, log_input, i);
165+
out = sign(in);
166+
detail::store_batch(out, res, i);
167+
}
168+
size_t diff = detail::get_nb_diff(res, expected);
169+
EXPECT_EQ(diff, 0) << print_function_name("sign");
170+
}
171+
};
172+
173+
TYPED_TEST_SUITE(complex_exponential_test, batch_complex_types, simd_test_names);
174+
175+
TYPED_TEST(complex_exponential_test, exp)
176+
{
177+
this->test_exp();
178+
}
179+
180+
TYPED_TEST(complex_exponential_test, expm1)
181+
{
182+
this->test_expm1();
183+
}
184+
185+
TYPED_TEST(complex_exponential_test, huge_exp)
186+
{
187+
this->test_huge_exp();
188+
}
189+
190+
TYPED_TEST(complex_exponential_test, log)
191+
{
192+
this->test_log();
193+
}
194+
195+
TYPED_TEST(complex_exponential_test, log2)
196+
{
197+
this->test_log2();
198+
}
199+
200+
TYPED_TEST(complex_exponential_test, log10)
201+
{
202+
this->test_log10();
203+
}
204+
205+
TYPED_TEST(complex_exponential_test, log1p)
206+
{
207+
this->test_log1p();
208+
}
209+
210+
TYPED_TEST(complex_exponential_test, sign)
211+
{
212+
this->test_sign();
213+
}
214+

test/test_utils.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,13 +435,13 @@ namespace detail
435435
template <class B, class S>
436436
void load_batch(B& b, const S& src, size_t i = 0)
437437
{
438-
b.load_unaligned(&src[i]);
438+
b.load_unaligned(src.data() + i);
439439
}
440440

441441
template <class B, class D>
442442
void store_batch(const B& b, D& dst, size_t i = 0)
443443
{
444-
b.store_unaligned(&dst[i]);
444+
b.store_unaligned(dst.data() + i);
445445
}
446446
}
447447

0 commit comments

Comments
 (0)