clang-tidy: google tests applicable
[alexxy/gromacs.git] / src / gromacs / simd / tests / scalar_math.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2014,2015,2016,2017,2018, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 #include "gmxpre.h"
36
37 #include <cmath>
38
39 #include <gtest/gtest.h>
40
41 #include "gromacs/math/utilities.h"
42 #include "gromacs/simd/simd.h"
43 #include "gromacs/utility/basedefinitions.h"
44
45 #include "testutils/testasserts.h"
46
47 #include "data.h"
48
49 namespace gmx
50 {
51 namespace test
52 {
53
54 namespace
55 {
56
57 /*! \cond internal */
58 /*! \addtogroup module_simd */
59 /*! \{ */
60
61 // Since these functions are mostly wrappers for similar standard library
62 // functions, we typically just test 1-2 values to make sure we call the right
63 // function.
64
65 /******************************************
66  * Default floating-point precision tests *
67  ******************************************/
68
69 TEST(SimdScalarMathTest, copysign)
70 {
71     EXPECT_EQ(real(-c1), copysign(real(c1), real(-c2)));
72     EXPECT_EQ(real(c2), copysign(real(c2), real(c3)));
73 }
74
75 TEST(SimdScalarMathTest, invsqrtPair)
76 {
77     real x0 = c1;
78     real x1 = c2;
79
80     real out0, out1;
81
82     invsqrtPair(x0, x1, &out0, &out1);
83
84     EXPECT_EQ(invsqrt(x0), out0);
85     EXPECT_EQ(invsqrt(x1), out1);
86 }
87
88 TEST(SimdScalarMathTest, inv)
89 {
90     real x0 = c0;
91
92     EXPECT_EQ(real(1.0)/x0, inv(x0));
93 }
94
95 TEST(SimdScalarMathTest, maskzInvsqrt)
96 {
97     real x0 = c0;
98
99     EXPECT_EQ(invsqrt(x0), maskzInvsqrt(x0, true));
100     EXPECT_EQ(real(0), maskzInvsqrt(x0, false));
101 }
102
103 TEST(SimdScalarMathTest, log)
104 {
105     real x0 = c0;
106
107     EXPECT_EQ(std::log(x0), log(x0));
108 }
109
110 TEST(SimdScalarMathTest, exp2)
111 {
112     real x0 = c0;
113
114     EXPECT_EQ(std::exp2(x0), exp2(x0));
115 }
116
117 TEST(SimdScalarMathTest, exp)
118 {
119     real x0 = c0;
120
121     EXPECT_EQ(std::exp(x0), exp(x0));
122 }
123
124 TEST(SimdScalarMathTest, erf)
125 {
126     real x0 = c0;
127
128     EXPECT_EQ(std::erf(x0), erf(x0));
129 }
130
131 TEST(SimdScalarMathTest, erfc)
132 {
133     real x0 = c0;
134
135     EXPECT_EQ(std::erfc(x0), erfc(x0));
136 }
137
138 TEST(SimdScalarMathTest, sincos)
139 {
140     real x0 = c0;
141     real s, c;
142
143     sincos(x0, &s, &c);
144
145     EXPECT_EQ(std::sin(x0), s);
146     EXPECT_EQ(std::cos(x0), c);
147 }
148
149 TEST(SimdScalarMathTest, sin)
150 {
151     real x0 = c0;
152
153     EXPECT_EQ(std::sin(x0), sin(x0));
154 }
155
156 TEST(SimdScalarMathTest, cos)
157 {
158     real x0 = c0;
159
160     EXPECT_EQ(std::cos(x0), cos(x0));
161 }
162
163 TEST(SimdScalarMathTest, tan)
164 {
165     real x0 = c0;
166
167     EXPECT_EQ(std::tan(x0), tan(x0));
168 }
169
170
171 TEST(SimdScalarMathTest, asin)
172 {
173     real x0 = c0;
174
175     EXPECT_EQ(std::asin(x0), asin(x0));
176 }
177
178 TEST(SimdScalarMathTest, acos)
179 {
180     real x0 = c0;
181
182     EXPECT_EQ(std::acos(x0), acos(x0));
183 }
184
185 TEST(SimdScalarMathTest, atan)
186 {
187     real x0 = c0;
188
189     EXPECT_EQ(std::atan(x0), atan(x0));
190 }
191
192 TEST(SimdScalarMathTest, atan2)
193 {
194     real x = c0;
195     real y = std::sqrt(c0);
196
197
198     EXPECT_EQ(std::atan2(y, x), atan2(y, x));
199 }
200
201 TEST(SimdScalarMathTest, pmeForceCorrection)
202 {
203     real z2 = c0;
204
205     // Calculate reference value for z2!=0
206     real z   = std::sqrt(z2);
207     real ref = 2.0*std::exp(-z2)/(std::sqrt(M_PI)*z2) - std::erf(z)/(z2*z);
208
209     // Pme correction only needs to be ~1e-6 accuracy single, 1e-10 double
210 #if GMX_DOUBLE
211     FloatingPointTolerance            tolerance(relativeToleranceAsFloatingPoint(1.0, 5e-10));
212 #else
213     FloatingPointTolerance            tolerance(relativeToleranceAsFloatingPoint(1.0, 5e-6));
214 #endif
215
216     EXPECT_REAL_EQ_TOL(ref, pmeForceCorrection(z2), tolerance);
217 }
218
219 TEST(SimdScalarMathTest, pmePotentialCorrection)
220 {
221     real z2 = c0;
222
223     // Calculate reference value for z2!=0
224     real z   = std::sqrt(z2);
225     real ref = std::erf(z)/z;
226
227     // Pme correction only needs to be ~1e-6 accuracy single, 1e-10 double
228 #if GMX_DOUBLE
229     FloatingPointTolerance            tolerance(relativeToleranceAsFloatingPoint(1.0, 5e-10));
230 #else
231     FloatingPointTolerance            tolerance(relativeToleranceAsFloatingPoint(1.0, 5e-6));
232 #endif
233
234     EXPECT_REAL_EQ_TOL(ref, pmePotentialCorrection(z2), tolerance);
235 }
236
237 /*******************************************
238  * Double precision, single accuracy tests *
239  *******************************************/
240
241 TEST(SimdScalarMathTest, invsqrtPairSingleAccuracy)
242 {
243     double x0 = c1;
244     double x1 = c2;
245
246     double out0, out1;
247
248     invsqrtPairSingleAccuracy(x0, x1, &out0, &out1);
249
250     EXPECT_EQ(invsqrt(static_cast<float>(x0)), static_cast<float>(out0));
251     EXPECT_EQ(invsqrt(static_cast<float>(x1)), static_cast<float>(out1));
252 }
253
254 TEST(SimdScalarMathTest, invSingleAccuracy)
255 {
256     double x0 = c1;
257
258     EXPECT_EQ(1.0f/static_cast<float>(x0),
259               static_cast<float>(invSingleAccuracy(x0)));
260 }
261
262 TEST(SimdScalarMathTest, maskzInvsqrtSingleAccuracy)
263 {
264     double x0 = c1;
265
266     EXPECT_EQ(invsqrt(static_cast<float>(x0)),
267               static_cast<float>(maskzInvsqrtSingleAccuracy(x0, true)));
268     EXPECT_EQ(0.0f,
269               static_cast<float>(maskzInvsqrtSingleAccuracy(x0, false)));
270 }
271
272 TEST(SimdScalarMathTest, logSingleAccuracy)
273 {
274     double x0 = c1;
275
276     EXPECT_EQ(std::log(static_cast<float>(x0)),
277               static_cast<float>(logSingleAccuracy(x0)));
278 }
279
280 TEST(SimdScalarMathTest, exp2SingleAccuracy)
281 {
282     double x0 = c1;
283
284     EXPECT_EQ(std::exp2(static_cast<float>(x0)),
285               static_cast<float>(exp2SingleAccuracy(x0)));
286 }
287
288 TEST(SimdScalarMathTest, expSingleAccuracy)
289 {
290     double x0 = c1;
291
292     EXPECT_EQ(std::exp(static_cast<float>(x0)),
293               static_cast<float>(expSingleAccuracy(x0)));
294 }
295
296 TEST(SimdScalarMathTest, erfSingleAccuracy)
297 {
298     double x0 = c0;
299
300     EXPECT_EQ(std::erf(static_cast<float>(x0)), static_cast<float>(erfSingleAccuracy(x0)));
301 }
302
303 TEST(SimdScalarMathTest, erfcSingleAccuracy)
304 {
305     double x0 = c0;
306
307     EXPECT_EQ(std::erfc(static_cast<float>(x0)), static_cast<float>(erfcSingleAccuracy(x0)));
308 }
309
310 TEST(SimdScalarMathTest, sincosSingleAccuracy)
311 {
312     double x0 = c0;
313     double s, c;
314
315     sincosSingleAccuracy(x0, &s, &c);
316
317     EXPECT_EQ(std::sin(static_cast<float>(x0)), static_cast<float>(s));
318     EXPECT_EQ(std::cos(static_cast<float>(x0)), static_cast<float>(c));
319 }
320
321 TEST(SimdScalarMathTest, sinSingleAccuracy)
322 {
323     double x0 = c0;
324
325     EXPECT_EQ(std::sin(static_cast<float>(x0)),
326               static_cast<float>(sinSingleAccuracy(x0)));
327 }
328
329 TEST(SimdScalarMathTest, cosSingleAccuracy)
330 {
331     double x0 = c0;
332
333     EXPECT_EQ(std::cos(static_cast<float>(x0)),
334               static_cast<float>(cosSingleAccuracy(x0)));
335 }
336
337 TEST(SimdScalarMathTest, tanSingleAccuracy)
338 {
339     double x0 = c0;
340
341     EXPECT_EQ(std::tan(static_cast<float>(x0)),
342               static_cast<float>(tanSingleAccuracy(x0)));
343 }
344
345
346 TEST(SimdScalarMathTest, asinSingleAccuracy)
347 {
348     double x0 = c0;
349
350     EXPECT_EQ(std::asin(static_cast<float>(x0)),
351               static_cast<float>(asinSingleAccuracy(x0)));
352 }
353
354 TEST(SimdScalarMathTest, acosSingleAccuracy)
355 {
356     double x0 = c0;
357
358     EXPECT_EQ(std::acos(static_cast<float>(x0)),
359               static_cast<float>(acosSingleAccuracy(x0)));
360 }
361
362 TEST(SimdScalarMathTest, atanSingleAccuracy)
363 {
364     double x0 = c0;
365
366     EXPECT_EQ(std::atan(static_cast<float>(x0)),
367               static_cast<float>(atanSingleAccuracy(x0)));
368 }
369
370 TEST(SimdScalarMathTest, atan2SingleAccuracy)
371 {
372     double x = c0;
373     double y = std::sqrt(c0);
374
375
376     EXPECT_EQ(std::atan2(static_cast<float>(y), static_cast<float>(x)),
377               static_cast<float>(atan2SingleAccuracy(y, x)));
378 }
379
380 TEST(SimdScalarMathTest, pmeForceCorrectionSingleAccuracy)
381 {
382     double z2 = c0;
383
384     // Calculate reference value for z2!=0 in single precision
385     float z   = std::sqrt(static_cast<float>(z2));
386     float ref = 2.0*std::exp(static_cast<float>(-z2))/(std::sqrt(static_cast<float>(M_PI))*z2) - std::erf(z)/(z2*z);
387
388     // Pme correction only needs to be ~1e-6 accuracy single
389     FloatingPointTolerance            tolerance(relativeToleranceAsFloatingPoint(1.0, 5e-6));
390
391     EXPECT_REAL_EQ_TOL(ref, static_cast<float>(pmeForceCorrectionSingleAccuracy(z2)), tolerance);
392 }
393
394 TEST(SimdScalarMathTest, pmePotentialCorrectionSingleAccuracy)
395 {
396     double z2 = c0;
397
398     // Calculate reference value for z2!=0 in single precision
399     float z   = std::sqrt(static_cast<float>(z2));
400     float ref = std::erf(z)/z;
401
402     // Pme correction only needs to be ~1e-6 accuracy single
403     FloatingPointTolerance            tolerance(relativeToleranceAsFloatingPoint(1.0, 5e-6));
404
405     EXPECT_REAL_EQ_TOL(ref, static_cast<float>(pmePotentialCorrectionSingleAccuracy(z2)), tolerance);
406 }
407
408 /*! \} */
409 /*! \endcond internal */
410
411 }  // namespace
412 }  // namespace test
413 }  // namespace gmx