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