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