-
Notifications
You must be signed in to change notification settings - Fork 264
/
Copy pathtest_batch_float.cpp
141 lines (125 loc) · 4.3 KB
/
test_batch_float.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
/***************************************************************************
* 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"
template <class B>
struct batch_float_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>;
using bool_array_type = std::array<bool, size>;
array_type lhs;
array_type rhs;
batch_float_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));
if (lhs[i] == value_type(0))
{
lhs[i] += value_type(0.1);
}
rhs[i] = value_type(10.2) / (i + 2) + value_type(0.25);
}
}
void test_reciprocal() const
{
// reciprocal
{
array_type res, expected;
std::transform(lhs.cbegin(), lhs.cend(), expected.begin(),
[](const value_type& l)
{ return value_type(1) / l; });
batch_type res1 = reciprocal(batch_lhs());
res1.store_unaligned(res.data());
size_t diff = detail::get_nb_diff_near(res, expected, 1e-12f);
INFO("reciprocal");
CHECK_EQ(diff, 0);
}
}
void test_rsqrt() const
{
// rsqrt
{
array_type res, expected;
std::transform(lhs.cbegin(), lhs.cend(), expected.begin(),
[](const value_type& l)
{ return std::ceil((value_type(1) / std::sqrt(l)) * value_type(100)); });
batch_type res1 = ceil(rsqrt(batch_lhs()) * value_type(100));
res1.store_unaligned(res.data());
size_t diff = detail::get_nb_diff_near(res, expected, 1.5f * std::pow(2, 12));
INFO("rsqrt");
CHECK_EQ(diff, 0);
}
}
void test_sqrt() const
{
// sqrt
{
array_type expected;
std::transform(lhs.cbegin(), lhs.cend(), expected.begin(),
[](const value_type& l)
{ return std::sqrt(l); });
batch_type res = sqrt(batch_lhs());
INFO("sqrt");
CHECK_BATCH_EQ(res, expected);
}
}
void test_haddp() const
{
batch_type haddp_input[size];
for (size_t i = 0; i < size; i += 2)
{
haddp_input[i] = batch_lhs();
if (i + 1 < size)
{
haddp_input[i + 1] = batch_rhs();
}
}
array_type expected;
std::fill(expected.begin(), expected.end(), value_type(0));
for (size_t i = 0; i < size; ++i)
{
for (size_t j = 0; j < size; j += 2)
{
expected[j] += lhs[i];
if (j + 1 < size)
{
expected[j + 1] += rhs[i];
}
}
}
auto res = haddp(haddp_input);
INFO("haddp");
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());
}
};
TEST_CASE_TEMPLATE("[xsimd batch float]", B, BATCH_FLOAT_TYPES)
{
batch_float_test<B> Test;
SUBCASE("reciprocal") { Test.test_reciprocal(); }
SUBCASE("sqrt") { Test.test_sqrt(); }
SUBCASE("rsqrt") { Test.test_rsqrt(); }
SUBCASE("haddp") { Test.test_haddp(); }
}
#endif