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