556e5b420592b847008834d5581b2cb740691585
[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,2019, 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), static_cast<float>(invSingleAccuracy(x0)));
259 }
260
261 TEST(SimdScalarMathTest, maskzInvsqrtSingleAccuracy)
262 {
263     double x0 = c1;
264
265     EXPECT_EQ(invsqrt(static_cast<float>(x0)), static_cast<float>(maskzInvsqrtSingleAccuracy(x0, true)));
266     EXPECT_EQ(0.0F, static_cast<float>(maskzInvsqrtSingleAccuracy(x0, false)));
267 }
268
269 TEST(SimdScalarMathTest, logSingleAccuracy)
270 {
271     double x0 = c1;
272
273     EXPECT_EQ(std::log(static_cast<float>(x0)), static_cast<float>(logSingleAccuracy(x0)));
274 }
275
276 TEST(SimdScalarMathTest, exp2SingleAccuracy)
277 {
278     double x0 = c1;
279
280     EXPECT_EQ(std::exp2(static_cast<float>(x0)), static_cast<float>(exp2SingleAccuracy(x0)));
281 }
282
283 TEST(SimdScalarMathTest, expSingleAccuracy)
284 {
285     double x0 = c1;
286
287     EXPECT_EQ(std::exp(static_cast<float>(x0)), static_cast<float>(expSingleAccuracy(x0)));
288 }
289
290 TEST(SimdScalarMathTest, erfSingleAccuracy)
291 {
292     double x0 = c0;
293
294     EXPECT_EQ(std::erf(static_cast<float>(x0)), static_cast<float>(erfSingleAccuracy(x0)));
295 }
296
297 TEST(SimdScalarMathTest, erfcSingleAccuracy)
298 {
299     double x0 = c0;
300
301     EXPECT_EQ(std::erfc(static_cast<float>(x0)), static_cast<float>(erfcSingleAccuracy(x0)));
302 }
303
304 TEST(SimdScalarMathTest, sincosSingleAccuracy)
305 {
306     double x0 = c0;
307     double s, c;
308
309     sincosSingleAccuracy(x0, &s, &c);
310
311     EXPECT_EQ(std::sin(static_cast<float>(x0)), static_cast<float>(s));
312     EXPECT_EQ(std::cos(static_cast<float>(x0)), static_cast<float>(c));
313 }
314
315 TEST(SimdScalarMathTest, sinSingleAccuracy)
316 {
317     double x0 = c0;
318
319     EXPECT_EQ(std::sin(static_cast<float>(x0)), static_cast<float>(sinSingleAccuracy(x0)));
320 }
321
322 TEST(SimdScalarMathTest, cosSingleAccuracy)
323 {
324     double x0 = c0;
325
326     EXPECT_EQ(std::cos(static_cast<float>(x0)), static_cast<float>(cosSingleAccuracy(x0)));
327 }
328
329 TEST(SimdScalarMathTest, tanSingleAccuracy)
330 {
331     double x0 = c0;
332
333     EXPECT_EQ(std::tan(static_cast<float>(x0)), static_cast<float>(tanSingleAccuracy(x0)));
334 }
335
336
337 TEST(SimdScalarMathTest, asinSingleAccuracy)
338 {
339     double x0 = c0;
340
341     EXPECT_EQ(std::asin(static_cast<float>(x0)), static_cast<float>(asinSingleAccuracy(x0)));
342 }
343
344 TEST(SimdScalarMathTest, acosSingleAccuracy)
345 {
346     double x0 = c0;
347
348     EXPECT_EQ(std::acos(static_cast<float>(x0)), static_cast<float>(acosSingleAccuracy(x0)));
349 }
350
351 TEST(SimdScalarMathTest, atanSingleAccuracy)
352 {
353     double x0 = c0;
354
355     EXPECT_EQ(std::atan(static_cast<float>(x0)), static_cast<float>(atanSingleAccuracy(x0)));
356 }
357
358 TEST(SimdScalarMathTest, atan2SingleAccuracy)
359 {
360     double x = c0;
361     double y = std::sqrt(c0);
362
363
364     EXPECT_EQ(std::atan2(static_cast<float>(y), static_cast<float>(x)),
365               static_cast<float>(atan2SingleAccuracy(y, x)));
366 }
367
368 TEST(SimdScalarMathTest, pmeForceCorrectionSingleAccuracy)
369 {
370     double z2 = c0;
371
372     // Calculate reference value for z2!=0 in single precision
373     float z   = std::sqrt(static_cast<float>(z2));
374     float ref = 2.0 * std::exp(static_cast<float>(-z2)) / (std::sqrt(static_cast<float>(M_PI)) * z2)
375                 - std::erf(z) / (z2 * z);
376
377     // Pme correction only needs to be ~1e-6 accuracy single
378     FloatingPointTolerance tolerance(relativeToleranceAsFloatingPoint(1.0, 5e-6));
379
380     EXPECT_REAL_EQ_TOL(ref, static_cast<float>(pmeForceCorrectionSingleAccuracy(z2)), tolerance);
381 }
382
383 TEST(SimdScalarMathTest, pmePotentialCorrectionSingleAccuracy)
384 {
385     double z2 = c0;
386
387     // Calculate reference value for z2!=0 in single precision
388     float z   = std::sqrt(static_cast<float>(z2));
389     float ref = std::erf(z) / z;
390
391     // Pme correction only needs to be ~1e-6 accuracy single
392     FloatingPointTolerance tolerance(relativeToleranceAsFloatingPoint(1.0, 5e-6));
393
394     EXPECT_REAL_EQ_TOL(ref, static_cast<float>(pmePotentialCorrectionSingleAccuracy(z2)), tolerance);
395 }
396
397 /*! \} */
398 /*! \endcond internal */
399
400 } // namespace
401 } // namespace test
402 } // namespace gmx