-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-special-functions.cpp
More file actions
134 lines (104 loc) · 3.15 KB
/
test-special-functions.cpp
File metadata and controls
134 lines (104 loc) · 3.15 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
#include "simd_helpers/simd_debug.hpp"
using namespace std;
using namespace simd_helpers;
template<typename T, int S>
void test_exp2(std::mt19937 &rng)
{
const T xmax = (sizeof(T)==8) ? 990.0 : 89.0;
const T thresh = (sizeof(T)==8) ? 3.0e-13 : 3.0e-6;
simd_t<T,S> x, y;
T x_arr[S];
T y_arr[S];
for (int iter = 0; iter < 100000; iter++) {
for (int i = 0; i < S; i++)
x_arr[i] = uniform_rand(rng, -xmax, xmax);
x.loadu(x_arr);
y = simd_exp2_unsafe(x);
y.storeu(y_arr);
for (int i = 0; i < S; i++) {
T dlog = log2(y_arr[i]) - x_arr[i];
if (abs(dlog) > thresh) {
cout << "test_exp2<" << type_name<T>() << "," << S << "> failed:"
<< " input=" << x_arr[i] << ", output=" << y_arr[i] << ", expected=" << exp2(x_arr[i])
<< ", delta_log=" << dlog << ", thresh=" << thresh << endl;
exit(1);
}
}
}
}
template<typename T, int S>
void test_log2p(std::mt19937 &rng)
{
const T thresh = (sizeof(T)==8) ? 3.0e-13 : 3.0e-6;
simd_t<T,S> x, y;
T x_arr[S];
T y_arr[S];
for (int iter = 0; iter < 100000; iter++) {
for (int i = 0; i < S; i++)
x_arr[i] = uniform_rand(rng, 0.0, 1.0);
x.loadu(x_arr);
y = _simd_log2p_restricted(x);
y.storeu(y_arr);
for (int i = 0; i < S; i++) {
T z = log1p(x_arr[i]) / log(2.0);
T dlog = log(y_arr[i]) - log(z);
if (abs(dlog) > thresh) {
cout << "test_log2p<" << type_name<T>() << "," << S << "> failed:"
<< " input=" << x_arr[i] << ", output=" << y_arr[i] << ", expected=" << z
<< ", delta_log=" << dlog << ", thresh=" << thresh << endl;
exit(1);
}
}
}
}
template<typename T, int S>
void test_log_add(std::mt19937 &rng)
{
const T xmax = (sizeof(T)==8) ? 1000.0 : 100.0;
const T thresh = (sizeof(T)==8) ? 3.0e-13 : 3.0e-5;
simd_t<T,S> x, y, z;
T x_arr[S];
T y_arr[S];
T z_arr[S];
// Use huge number of iterations to ensure that overflow/underflow is exhaustively tested.
for (int iter = 0; iter < 100000; iter++) {
for (int i = 0; i < S; i++) {
x_arr[i] = uniform_rand(rng, -xmax, xmax);
y_arr[i] = uniform_rand(rng, -xmax, xmax);
}
x.loadu(x_arr);
y.loadu(y_arr);
z = simd_ln_add(x, y);
z.storeu(z_arr);
for (int i = 0; i < S; i++) {
T m = max(x_arr[i], y_arr[i]);
T w = m + log(exp(x_arr[i]-m) + exp(y_arr[i]-m));
if (abs(z_arr[i]-w) > thresh) {
cout << "test_log_add<" << type_name<T>() << "," << S << "> failed:"
<< " input=(" << x_arr[i] << "," << y_arr[i] << "), output=" << z_arr[i]
<< ", expected=" << w << ", delta=" << abs(z_arr[i]-w) << ", thresh=" << thresh << endl;
exit(1);
}
}
}
}
template<typename T, int S>
void test_special_functions(std::mt19937 &rng)
{
test_exp2<T,S> (rng);
test_log2p<T,S> (rng);
test_log_add<T,S> (rng);
cout << "test_special_functions<" << type_name<T>() << "," << S << ">: pass" << endl;
}
int main(int argc, char **argv)
{
std::random_device rd;
std::mt19937 rng(rd());
test_special_functions<float,4> (rng);
test_special_functions<double,2> (rng);
#ifdef __AVX__
test_special_functions<float,8> (rng);
test_special_functions<double,4> (rng);
#endif
return 0;
}