clang-tidy: google tests applicable
[alexxy/gromacs.git] / src / gromacs / simd / tests / simd4_math.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2014,2015,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 "config.h"
38
39 #include <cmath>
40 #include <cstdint>
41
42 #include <vector>
43
44 #include "gromacs/math/utilities.h"
45 #include "gromacs/options/basicoptions.h"
46 #include "gromacs/simd/simd.h"
47 #include "gromacs/simd/simd_math.h"
48
49 #include "simd4.h"
50
51 #if GMX_SIMD
52
53 namespace gmx
54 {
55 namespace test
56 {
57
58 /*! \cond internal */
59 /*! \addtogroup module_simd */
60 /*! \{ */
61
62 #if GMX_SIMD4_HAVE_REAL
63
64 class Simd4MathTest : public Simd4Test
65 {
66     public:
67         ::testing::AssertionResult
68                              compareSimd4MathFunction(const char * refFuncExpr, const char *simd4FuncExpr,
69                                                       real refFunc(real x),     Simd4Real gmx_simdcall simd4Func(Simd4Real x));
70 };
71
72 /*! \brief Test approximate equality of SIMD4 vs reference version of a function.
73  *
74  * This macro takes vanilla C and SIMD flavors of a function and tests it with
75  * the number of points, range, and tolerances specified by the test fixture class.
76  */
77 #define GMX_EXPECT_SIMD4_FUNC_NEAR(refFunc, tstFunc) \
78     EXPECT_PRED_FORMAT2(compareSimd4MathFunction, refFunc, tstFunc)
79
80
81 /*! \brief Implementation routine to compare SIMD4 vs reference functions.
82  *
83  * \param refFuncExpr    Description of reference function expression
84  * \param simd4FuncExpr  Description of SIMD function expression
85  * \param refFunc        Reference math function pointer
86  * \param simd4Func      SIMD math function pointer
87  *
88  * The function will be tested with the range and tolerances specified in
89  * the SimdBaseTest class. You should not never call this function directly,
90  * but use the macro GMX_EXPECT_SIMD4_FUNC_NEAR(refFunc,tstFunc) instead.
91  */
92 ::testing::AssertionResult
93 Simd4MathTest::compareSimd4MathFunction(const char * refFuncExpr, const char *simd4FuncExpr,
94                                         real refFunc(real x),     Simd4Real gmx_simdcall simd4Func(Simd4Real x))
95 {
96     std::vector<real>            vx(GMX_SIMD4_WIDTH);
97     std::vector<real>            vref(GMX_SIMD4_WIDTH);
98     std::vector<real>            vtst(GMX_SIMD4_WIDTH);
99     real                         dx;
100     std::int64_t                 ulpDiff, maxUlpDiff;
101     real                         maxUlpDiffPos;
102     real                         refValMaxUlpDiff, simdValMaxUlpDiff;
103     int                          niter   = s_nPoints/GMX_SIMD4_WIDTH;
104     int                          npoints = niter*GMX_SIMD4_WIDTH;
105 #    if GMX_DOUBLE
106     union {
107         double r; std::int64_t i;
108     } conv0, conv1;
109 #    else
110     union {
111         float  r; std::int32_t i;
112     } conv0, conv1;
113 #    endif
114
115     maxUlpDiff = 0;
116     dx         = (range_.second-range_.first)/npoints;
117
118     for (int iter = 0; iter < niter; iter++)
119     {
120         for (int i = 0; i < GMX_SIMD4_WIDTH; i++)
121         {
122             vx[i]   = range_.first+dx*(iter*GMX_SIMD4_WIDTH+i);
123             vref[i] = refFunc(vx[i]);
124         }
125         vtst  = simd4Real2Vector(simd4Func(vector2Simd4Real(vx)));
126
127         bool eq = true, signOk = true;
128         for (int i = 0; i < GMX_SIMD4_WIDTH && eq; i++)
129         {
130             eq     = eq && ( std::abs(vref[i]-vtst[i]) < absTol_ );
131             signOk = signOk && ( vref[i]*vtst[i] >= 0 );
132         }
133         if (eq)
134         {
135             // Go to next point if everything within absolute tolerance
136             continue;
137         }
138         else if (!signOk)
139         {
140             return ::testing::AssertionFailure()
141                    << "Failing SIMD4 math function comparison due to sign differences." << std::endl
142                    << "Reference function: " << refFuncExpr << std::endl
143                    << "Simd function:      " << simd4FuncExpr << std::endl
144                    << "Test range is ( " << range_.first << " , " << range_.second << " ) " << std::endl
145                    << "First sign difference around x=" << std::setprecision(20) << ::testing::PrintToString(vx) << std::endl
146                    << "Ref values:   " << std::setprecision(20) << ::testing::PrintToString(vref) << std::endl
147                    << "SIMD4 values: " << std::setprecision(20) << ::testing::PrintToString(vtst) << std::endl;
148         }
149         /* We replicate the trivial ulp differences comparison here rather than
150          * calling the lower-level routine for comparing them, since this enables
151          * us to run through the entire test range and report the largest deviation
152          * without lots of extra glue routines.
153          */
154         for (int i = 0; i < GMX_SIMD4_WIDTH; i++)
155         {
156             conv0.r = vref[i];
157             conv1.r = vtst[i];
158             ulpDiff = llabs(conv0.i-conv1.i);
159             if (ulpDiff > maxUlpDiff)
160             {
161                 maxUlpDiff        = ulpDiff;
162                 maxUlpDiffPos     = vx[i];
163                 refValMaxUlpDiff  = vref[i];
164                 simdValMaxUlpDiff = vtst[i];
165             }
166         }
167     }
168     GMX_RELEASE_ASSERT(ulpTol_ >= 0, "Invalid ulp value.");
169     if (maxUlpDiff <= ulpTol_)
170     {
171         return ::testing::AssertionSuccess();
172     }
173     else
174     {
175         return ::testing::AssertionFailure()
176                << "Failing SIMD4 math function ulp comparison between " << refFuncExpr << " and " << simd4FuncExpr << std::endl
177                << "Requested ulp tolerance: " << ulpTol_ << std::endl
178                << "Requested abs tolerance: " << absTol_ << std::endl
179                << "Largest Ulp difference occurs for x=" << std::setprecision(20) << maxUlpDiffPos << std::endl
180                << "Ref  values:  " << std::setprecision(20) << refValMaxUlpDiff << std::endl
181                << "SIMD4 values: " << std::setprecision(20) << simdValMaxUlpDiff << std::endl
182                << "Ulp diff.:   " << std::setprecision(20) << maxUlpDiff << std::endl;
183     }
184 }
185
186 /*! \} */
187 /*! \endcond */
188
189 // Actual math function tests below
190
191 namespace
192 {
193
194 /*! \cond internal */
195 /*! \addtogroup module_simd */
196 /*! \{ */
197
198 /*! \brief Function wrapper to evaluate reference 1/sqrt(x) */
199 real
200 refInvsqrt(real x)
201 {
202     return 1.0/std::sqrt(x);
203 }
204
205 TEST_F(Simd4MathTest, invsqrt)
206 {
207     setRange(1e-10, 1e10);
208     GMX_EXPECT_SIMD4_FUNC_NEAR(refInvsqrt, invsqrt);
209 }
210
211 TEST_F(Simd4MathTest, invsqrtSingleaccuracy)
212 {
213     setRange(1e-10, 1e10);
214     /* Increase the allowed error by the difference between the actual precision and single */
215     setUlpTolSingleAccuracy(ulpTol_);
216     GMX_EXPECT_SIMD4_FUNC_NEAR(refInvsqrt, invsqrtSingleAccuracy);
217 }
218
219 }      // namespace
220
221 #endif // GMX_SIMD4_HAVE_REAL
222
223 /*! \} */
224 /*! \endcond */
225
226 }      // namespace test
227 }      // namespace gmx
228
229 #endif // GMX_SIMD