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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
// -*-C++-*-
#include "vecmathlib.h"
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <typeinfo>
using namespace std;
int num_errors = 0;
template<typename realvec_t>
struct vecmathlib_test {
typedef typename realvec_t::boolvec_t boolvec_t;
typedef typename realvec_t::intvec_t intvec_t;
typedef typename realvec_t::int_t int_t;
typedef typename realvec_t::uint_t uint_t;
typedef typename realvec_t::real_t real_t;
// Short names for type casts
typedef real_t R;
typedef int_t I;
typedef uint_t U;
typedef realvec_t RV;
typedef intvec_t IV;
typedef boolvec_t BV;
typedef vecmathlib::floatprops<real_t> FP;
// Test each function with this many random values
static int const imax = 1000;
// Require that 3/4 of the digits are correct
static real_t constexpr accuracy = pow(realvec_t::epsilon(), R(0.75));
static realvec_t random(real_t const xmin, real_t const xmax)
{
realvec_t x;
for (int i=0; i<realvec_t::size; ++i) {
real_t r =
(xmax - xmin) *FP::convert_float(rand()) / FP::convert_float(RAND_MAX);
x.set_elt(i, xmin + r);
}
return x;
}
static intvec_t random(int_t const nmin, int_t const nmax)
{
intvec_t n;
for (int i=0; i<intvec_t::size; ++i) {
real_t r =
R(nmax - nmin + 1) *
R(rand()) / (real_t(RAND_MAX) + R(1.0));
n.set_elt(i, nmin + FP::convert_int(std::floor(r)));
}
return n;
}
template<typename A>
static void check(char const* const func,
real_t fstd(typename A::scalar_t), realvec_t fvml(A),
A const x,
real_t const accuracy)
{
realvec_t rstd;
for (int i=0; i<realvec_t::size; ++i) {
rstd.set_elt(i, fstd(x[i]));
}
realvec_t const rvml = fvml(x);
realvec_t const dr = rstd - rvml;
if (any(fabs(dr) >
realvec_t(accuracy) * (fabs(rstd) + fabs(rvml) + realvec_t(1.0))))
{
++ num_errors;
cout << setprecision(realvec_t::digits10+2)
<< "Error in " << func << "(" << x << "):\n"
<< " fstd(x)=" << rstd << "\n"
<< " fvml(x)=" << rvml << "\n";
}
}
template<typename A, typename B>
static void check(char const* const func,
real_t fstd(typename A::scalar_t, typename B::scalar_t),
realvec_t fvml(A, B),
A const x, B const y,
real_t const accuracy)
{
realvec_t rstd;
for (int i=0; i<realvec_t::size; ++i) {
rstd.set_elt(i, fstd(x[i], y[i]));
}
realvec_t const rvml = fvml(x, y);
realvec_t const dr = rstd - rvml;
if (any(fabs(dr) >
realvec_t(accuracy) * (fabs(rstd) + fabs(rvml) + realvec_t(1.0))))
{
++ num_errors;
cout << setprecision(realvec_t::digits10+1)
<< "Error in " << func << "(" << x << "," << y << "):\n"
<< " fstd(x,y)=" << rstd << "\n"
<< " fvml(x,y)=" << rvml << "\n";
}
}
template<typename A>
static void check(char const* const func,
int_t fstd(typename A::scalar_t), intvec_t fvml(A),
A const x)
{
intvec_t rstd;
for (int i=0; i<intvec_t::size; ++i) {
rstd.set_elt(i, fstd(x[i]));
}
intvec_t const rvml = fvml(x);
intvec_t const dr = rstd - rvml;
if (any(convert_bool(dr))) {
cout << setprecision(realvec_t::digits10+1)
<< "Error in " << func << "(" << x << "):\n"
<< " fstd(x)=" << rstd << "\n"
<< " fvml(x)=" << rvml << "\n";
}
}
template<typename A, typename B>
static void check(char const* const func,
int fstd(typename A::scalar_t, typename B::scalar_t),
intvec_t fvml(A, B),
A const x, B const y)
{
intvec_t rstd;
for (int i=0; i<intvec_t::size; ++i) {
rstd.set_elt(i, fstd(x[i], y[i]));
}
intvec_t const rvml = fvml(x, y);
intvec_t const dr = rstd - rvml;
if (any(convert_bool(dr))) {
++ num_errors;
cout << setprecision(realvec_t::digits10+1)
<< "Error in " << func << "(" << x << "," << y << "):\n"
<< " fstd(x,y)=" << rstd << "\n"
<< " fvml(x,y)=" << rvml << "\n";
}
}
template<typename A>
static void check(char const* const func,
bool fstd(typename A::scalar_t), boolvec_t fvml(A),
A const x)
{
boolvec_t rstd;
for (int i=0; i<boolvec_t::size; ++i) {
rstd.set_elt(i, fstd(x[i]));
}
boolvec_t const rvml = fvml(x);
boolvec_t const dr = rstd != rvml;
if (any(dr)) {
cout << setprecision(realvec_t::digits10+1)
<< "Error in " << func << "(" << x << "):\n"
<< " fstd(x)=" << rstd << "\n"
<< " fvml(x)=" << rvml << "\n";
}
}
static int_t ilogb(real_t x) { return std::ilogb(x); }
static real_t scalbn(real_t x, int_t n) { return std::scalbn(x, n); }
static void test_fabs()
{
cout << " testing copysign fabs ilogb scalbn signbit...\n";
for (int i=0; i<imax; ++i) {
realvec_t const x = random(R(-10.0), R(+10.0));
realvec_t const y = random(R(-10.0), R(+10.0));
intvec_t const n = random(int_t(-10), int_t(+10));
check("copysign", copysign, vecmathlib::copysign, x, y, 0.0);
check("fabs", fabs, vecmathlib::fabs, x, 0.0);
check("ilogb", ilogb, vecmathlib::ilogb, x);
check("scalbn", scalbn, vecmathlib::scalbn, x, n, 0.0);
check("signbit", signbit, vecmathlib::signbit, x);
}
}
static void test_convert()
{
cout << " testing ceil convert_float convert_int floor round...\n";
for (int i=0; i<imax; ++i) {
realvec_t const x = random(R(-10.0), R(+10.0));
intvec_t const n = random(int_t(-10), int_t(+10));
check("ceil", ceil, vecmathlib::ceil, x, accuracy);
check("convert_float",
FP::convert_float, vecmathlib::convert_float, n, accuracy);
check("convert_int", FP::convert_int, vecmathlib::convert_int, x);
check("floor", floor, vecmathlib::floor, x, accuracy);
check("round", round, vecmathlib::round, x, accuracy);
}
}
static void test_asin()
{
cout << " testing asin acos atan...\n";
for (int i=0; i<imax; ++i) {
realvec_t const x = random(R(-1.0), R(+1.0));
check("asin", asin, vecmathlib::asin, x, accuracy);
check("acos", acos, vecmathlib::acos, x, accuracy);
}
for (int i=0; i<imax; ++i) {
realvec_t const x = random(R(-100.0), R(+100.0));
check("atan", atan, vecmathlib::atan, x, accuracy);
}
}
static void test_asinh()
{
cout << " testing asinh acosh atanh...\n";
for (int i=0; i<imax; ++i) {
realvec_t const x = random(R(-1000.0), R(+1000.0));
check("asinh", asinh, vecmathlib::asinh, x, accuracy);
}
for (int i=0; i<imax; ++i) {
realvec_t const x = random(R(1.0), R(1000.0));
check("acosh", acosh, vecmathlib::acosh, x, accuracy);
}
for (int i=0; i<imax; ++i) {
realvec_t const x = random(R(-1.0), R(+1.0));
check("atanh", atanh, vecmathlib::atanh, x, accuracy);
}
}
static real_t exp10(real_t x) { return pow(R(10.0), x); }
static void test_exp()
{
cout << " testing exp exp10 exp2 expm1...\n";
for (int i=0; i<imax; ++i) {
realvec_t const x = random(R(-100.0), R(+100.0));
check("exp", exp, vecmathlib::exp, x, accuracy);
check("exp10", exp10, vecmathlib::exp10, x, accuracy);
check("exp2", exp2, vecmathlib::exp2, x, accuracy);
check("expm1", expm1, vecmathlib::expm1, x, accuracy);
}
}
static void test_log()
{
cout << " testing log log10 log1p log2...\n";
for (int i=0; i<imax; ++i) {
realvec_t const x = random(R(1.0e-10), R(1.0e+10));
check("log", log, vecmathlib::log, x, accuracy);
check("log10", log10, vecmathlib::log10, x, accuracy);
check("log1p", log1p, vecmathlib::log1p, x, accuracy);
check("log2", log2, vecmathlib::log2, x, accuracy);
}
}
static void test_pow()
{
cout << " testing pow...\n";
for (int i=0; i<imax; ++i) {
realvec_t const x = random(R(0.001), R(1000.0));
realvec_t const y = random(R(-10.0), R(+10.0));
check("pow", pow, vecmathlib::pow, x, y, accuracy);
}
}
static real_t rcp(real_t x) { return R(1.0)/x; }
static void test_rcp()
{
cout << " testing fmod rcp remainder...\n";
for (int i=0; i<imax; ++i) {
realvec_t const x = random(R(-10.0), R(+10.0));
realvec_t const y = random(R(-10.0), R(+10.0));
check("fmod", fmod, vecmathlib::fmod, x, y, accuracy);
check("rcp", rcp, vecmathlib::rcp, x, accuracy);
check("remainder", remainder, vecmathlib::remainder, x, y, accuracy);
}
}
static void test_sin()
{
cout << " testing cos sin tan...\n";
for (int i=0; i<imax; ++i) {
realvec_t const x = random(R(-10.0), R(+10.0));
check("sin", sin, vecmathlib::sin, x, accuracy);
check("cos", cos, vecmathlib::cos, x, accuracy);
// tan loses accuracy (by definition, not by implementation?)
check("tan", tan, vecmathlib::tan, x, R(10.0)*accuracy);
}
}
static void test_sinh()
{
cout << " testing cosh sinh tanh...\n";
for (int i=0; i<imax; ++i) {
realvec_t const x = random(R(-10.0), R(+10.0));
check("sinh", sinh, vecmathlib::sinh, x, accuracy);
check("cosh", cosh, vecmathlib::cosh, x, accuracy);
check("tanh", tanh, vecmathlib::tanh, x, accuracy);
}
}
static real_t rsqrt(real_t x) { return R(1.0)/sqrt(x); }
static void test_sqrt()
{
cout << " testing rsqrt sqrt...\n";
for (int i=0; i<imax; ++i) {
realvec_t const x = random(R(0.0), R(1.0e+3));
check("rsqrt", rsqrt, vecmathlib::rsqrt, x, accuracy);
check("sqrt", sqrt, vecmathlib::sqrt, x, accuracy);
}
}
static void test()
{
cout << "\n"
<< "Testing math functions for type "
// << typeid(realvec_t).name()
<< realvec_t::name
<< ":\n";
test_fabs();
test_convert();
test_asin();
test_asinh();
test_exp();
test_log();
test_pow();
test_rcp();
test_sin();
test_sinh();
test_sqrt();
}
};
int main(int argc, char** argv)
{
using namespace vecmathlib;
cout << "Testing math functions:\n";
vecmathlib_test<realvec<float,1>>::test();
vecmathlib_test<realvec<double,4>>::test();
cout << "\n";
if (num_errors == 0) {
cout << "SUCCESS";
} else {
cout << "FAILURE";
}
cout << ": " << num_errors << " errors found\n";
return num_errors == 0 ? 0 : 1;
}
|