Fix cppcheck 1.64 warnings
[alexxy/gromacs.git] / src / gromacs / simd / tests / base.h
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
36 #ifndef GMX_SIMD_TESTS_BASE_H
37 #define GMX_SIMD_TESTS_BASE_H
38
39 /*! \internal \file
40  * \brief
41  * Declares common base class for testing SIMD and SIMD4.
42  *
43  * The base class contains the settings for absolute and ulp tolerances,
44  * as well as testing ranges used for both SIMD and SIMD4 tests, mainly
45  * to keep everything symmetric and clean. The class also defines a couple
46  * of generic tests that compare vectors of elements with arbitrary length for
47  * either exact or approximate matching (in terms of ulp). These are used in
48  * derived classes that convert either SIMD or SIMD4 values to
49  * std::vector<real> and then performs the comparison.
50  *
51  * \author Erik Lindahl <erik.lindahl@scilifelab.se>
52  * \ingroup module_simd
53  */
54 #include <vector>
55
56 #include <gtest/gtest.h>
57
58 #include "gromacs/legacyheaders/types/simple.h"
59
60 #include "gromacs/simd/simd.h"
61
62 namespace gmx
63 {
64 namespace test
65 {
66
67 /*! \internal
68  * \brief
69  * Base class for SIMD test fixtures.
70  *
71  * This class contains settings that are common for SIMD and SIMD4 tests,
72  * and it is thus not used directly for any tests, but derived separately
73  * in simd.h and simd4.h.
74  *
75  * \ingroup module_simd
76  */
77 class SimdBaseTest : public ::testing::Test
78 {
79     public:
80         /*! \brief Initialize new SIMD test fixture with default tolerances.
81          *
82          * The default absolute tolerance is set to 0, which means the we always
83          * check the ulp tolerance by default (passing the absolute tolerance
84          * test would otherwise mean we approve the test instantly).
85          * The default ulp tolerance is set to 10 units in single, and 255 units
86          * in double precision.
87          * Most SIMD math functions achieve 2-3 ulp accuracy in single, but by
88          * being a bit liberal we avoid tests failing on aggressive compilers.
89          *
90          * For double precision we only aim to achieve twice the accuracy of
91          * single. This way we can make do with a single extra iteration
92          * in some algorithms, in particular 1/sqrt(x).
93          *
94          * The range is used by derived classes to test math functions. The
95          * default test range will be [1,10], which is intentionally
96          * conservative so it works with (inverse) square root, division,
97          * exponentials, logarithms, and error functions.
98          */
99         SimdBaseTest() :
100 #ifdef GMX_DOUBLE
101             ulpTol_(255LL), // Aim for roughly twice the precision we have in single.
102 #else
103             ulpTol_(10LL),  // Be a bit liberal so compiler optimization doesn't bite us.
104 #endif
105             absTol_(0), range_(std::pair<real, real>(1, 10))
106         {
107         }
108
109         /*! \brief Adjust ulp tolerance from the default 10 (float) or 255 (double). */
110         void setUlpTol(gmx_int64_t newTol)   { ulpTol_ = newTol; }
111
112         /*! \brief Adjust the absolute tolerance from the default 0.
113          *
114          * If values are closer than the absolute tolerance, the test will pass
115          * no matter what their ulp difference is.
116          */
117         void setAbsTol(real newTol)          { absTol_ = newTol; }
118
119         /*! \brief Change math function testing range from the default [1,10]. */
120         void setRange(real low, real high) { range_.first = low; range_.second = high; }
121
122         static int  s_nPoints;    //!< Number of test points to use, settable on command line.
123
124         /*! \brief Compare two std::vector<real> for approximate equality.
125          *
126          * This is an internal implementation routine that will be used by
127          * routines in derived child classes that first convert SIMD or SIMD4
128          * variables to std::vector<real>. Do not call it directly.
129          *
130          * This routine is designed according to the Google test specs, so the char
131          * strings will describe the arguments to the macro.
132          *
133          * The comparison is applied to each element, and it returns true if each element
134          * in the vector test variable is within the class tolerances of the corresponding
135          * reference elements.
136          */
137         ::testing::AssertionResult
138         compareVectorRealUlp(const char * refExpr,  const char * tstExpr,
139                              const std::vector<real> &ref, const std::vector<real> &tst);
140
141         /*! \brief Compare std::vectors for exact equality.
142          *
143          * The template in this class makes it usable for testing both
144          * SIMD floating-point and integers variables, after conversion to
145          * vectors.
146          * This is an internal implementation routine that will be used by
147          * routines in derived child classes that first convert SIMD or SIMD4
148          * variables to std::vector<real>. Do not call it directly.
149          *
150          * This routine is designed according to the Google test specs, so the char
151          * strings will describe the arguments to the macro.
152          *
153          * The comparison is applied to each element, and it returns true if each element
154          * in the vector test variable is within the class tolerances of the corresponding
155          * reference elements.
156          */
157         template <typename T> ::testing::AssertionResult
158         compareVectorEq(const char * refExpr,  const char * tstExpr,
159                         const std::vector<T> &ref, const std::vector<T> &tst)
160         {
161             if (ref == tst)
162             {
163                 return ::testing::AssertionSuccess();
164             }
165             else
166             {
167                 return ::testing::AssertionFailure()
168                        << "Failing SIMD comparison between " << refExpr << " and " << tstExpr << std::endl
169                        << "Ref. values: " << ::testing::PrintToString(ref) << std::endl
170                        << "Test values: " << ::testing::PrintToString(tst) << std::endl;
171             }
172         }
173
174     protected:
175         gmx_int64_t            ulpTol_;       //!< Current tolerance in units-in-last-position.
176         real                   absTol_;       //!< Current absolute tolerance.
177         std::pair<real, real>  range_;        //!< Range for math function tests.
178 };
179
180 }      // namespace
181 }      // namespace
182
183 #endif // GMX_SIMD_TESTS_BASE_H