-
Notifications
You must be signed in to change notification settings - Fork 264
/
Copy pathtest_basic_math.cpp
177 lines (156 loc) · 5.62 KB
/
test_basic_math.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
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
174
175
176
177
/***************************************************************************
* 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/xsimd.hpp"
#ifndef XSIMD_NO_SUPPORTED_ARCHITECTURE
#include "test_utils.hpp"
namespace detail
{
template <class T, bool is_int = std::is_integral<typename T::value_type>::value>
struct infinity_tester
{
static void test_isfinite()
{
T input(1);
INFO("input: ", input);
CHECK_UNARY(xsimd::all(xsimd::isfinite(input)));
}
static void test_isinf()
{
T input(1);
INFO("input: ", input);
CHECK_FALSE(xsimd::any(xsimd::isinf(input)));
}
};
template <class T>
struct infinity_tester<T, false>
{
static void test_isfinite()
{
T input = xsimd::infinity<T>();
CHECK_FALSE(xsimd::any(xsimd::isfinite(input)));
}
static void test_isinf()
{
T input = xsimd::infinity<T>();
CHECK_UNARY(xsimd::all(xsimd::isinf(input)));
}
};
}
template <class B>
struct basic_math_test
{
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;
array_type clip_input;
array_type from_input;
basic_math_test()
{
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)) + value_type(1.);
rhs[i] = value_type(10.2) / (i + 2) + value_type(0.25) + value_type(1.);
clip_input[i] = static_cast<value_type>(i) * value_type(0.25);
from_input[i] = rhs[i] - value_type(1);
}
}
void test_fmod() const
{
array_type expected;
std::transform(
lhs.cbegin(), lhs.cend(), rhs.cbegin(), expected.begin(),
[](const value_type& l, const value_type& r)
{ return std::fmod(l, r); });
batch_type res = xsimd::fmod(batch_lhs(), batch_rhs());
CHECK_BATCH_EQ(res, expected);
}
void test_remainder() const
{
array_type expected;
std::transform(lhs.cbegin(), lhs.cend(), rhs.cbegin(), expected.begin(),
[](const value_type& l, const value_type& r)
{ return std::remainder(l, r); });
batch_type res = xsimd::remainder(batch_lhs(), batch_rhs());
CHECK_BATCH_EQ(res, expected);
}
void test_fdim() const
{
array_type expected;
std::transform(lhs.cbegin(), lhs.cend(), rhs.cbegin(), expected.begin(),
[](const value_type& l, const value_type& r)
{ return std::fdim(l, r); });
batch_type res = xsimd::fdim(batch_lhs(), batch_rhs());
CHECK_BATCH_EQ(res, expected);
}
void test_clip()
{
value_type clip_lo = static_cast<value_type>(0.5);
value_type clip_hi = static_cast<value_type>(1.);
array_type expected;
std::transform(clip_input.cbegin(), clip_input.cend(), expected.begin(),
[clip_lo, clip_hi](const value_type& l)
{
return l < clip_lo ? clip_lo : clip_hi < l ? clip_hi
: l;
});
batch_type res = xsimd::clip(batch_clip_input(), batch_type(clip_lo), batch_type(clip_hi));
CHECK_BATCH_EQ(res, expected);
}
void test_isfinite()
{
detail::infinity_tester<batch_type>::test_isfinite();
}
void test_isinf()
{
detail::infinity_tester<batch_type>::test_isinf();
}
void test_nextafter()
{
array_type expected;
std::transform(from_input.cbegin(), from_input.cend(), rhs.cbegin(), expected.begin(),
[](const value_type& l, const value_type& r)
{ return std::nextafter(l, r); });
batch_type res = xsimd::nextafter(batch_from_input(), batch_rhs());
CHECK_BATCH_EQ(res, expected);
}
private:
batch_type batch_lhs() const
{
return batch_type::load_unaligned(lhs.data());
}
batch_type batch_rhs() const
{
return batch_type::load_unaligned(rhs.data());
}
batch_type batch_clip_input() const
{
return batch_type::load_unaligned(clip_input.data());
}
batch_type batch_from_input() const
{
return batch_type::load_unaligned(from_input.data());
}
};
TEST_CASE_TEMPLATE("[basic math tests]", B, BATCH_MATH_TYPES)
{
basic_math_test<B> Test;
SUBCASE("fmod") { Test.test_fmod(); }
SUBCASE("remainder") { Test.test_remainder(); }
SUBCASE("fdim") { Test.test_fdim(); }
SUBCASE("clip") { Test.test_clip(); }
SUBCASE("isfinite") { Test.test_isfinite(); }
SUBCASE("isinf") { Test.test_isinf(); }
SUBCASE("nextafter") { Test.test_nextafter(); }
}
#endif