Update SIMD math tests and improve accuracy
[gromacs.git] / src / gromacs / simd / tests / base.h
blob04d8c07737808d045c32fe5a624f46369980c69c
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2014,2015,2018,2019, 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.
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.
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.
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.
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.
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.
36 #ifndef GMX_SIMD_TESTS_BASE_H
37 #define GMX_SIMD_TESTS_BASE_H
39 /*! \internal \file
40 * \brief
41 * Declares common base class for testing SIMD and SIMD4.
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.
51 * \author Erik Lindahl <erik.lindahl@scilifelab.se>
52 * \ingroup module_simd
54 #include "config.h"
56 #include <cstdint>
58 #include <limits>
59 #include <vector>
61 #include <gtest/gtest.h>
63 #include "gromacs/utility/basedefinitions.h"
64 #include "gromacs/utility/gmxassert.h"
65 #include "gromacs/utility/real.h"
67 namespace gmx
69 namespace test
72 //! \internal \brief Test-time utility macro for current precision accuracy
73 #define GMX_SIMD_ACCURACY_BITS_REAL (GMX_DOUBLE ? GMX_SIMD_ACCURACY_BITS_DOUBLE : GMX_SIMD_ACCURACY_BITS_SINGLE)
75 /*! \internal
76 * \brief
77 * Base class for SIMD test fixtures.
79 * This class contains settings that are common for SIMD and SIMD4 tests,
80 * and it is thus not used directly for any tests, but derived separately
81 * in simd.h and simd4.h.
83 * \ingroup module_simd
85 class SimdBaseTest : public ::testing::Test
87 public:
88 /*! \brief Return the default ulp tolerance for current precision
90 static constexpr std::int64_t
91 defaultRealUlpTol()
93 return (1LL << (2 + std::numeric_limits<real>::digits-GMX_SIMD_ACCURACY_BITS_REAL));
96 /*! \brief Initialize new SIMD test fixture with default tolerances.
98 * The default absolute tolerance is set to 0, which means the we always
99 * check the ulp tolerance by default (passing the absolute tolerance
100 * test would otherwise mean we approve the test instantly).
102 * The default ulp tolerance is set based on the target number of
103 * bits requested for single or double precision, depending on what
104 * the default Gromacs precision is. We add two bits to avoid
105 * tests failing due to corner cases where compiler optimization might
106 * cause a slight precision loss e.g. for very small numbers.
108 * Most SIMD math functions actually achieve 2-3 ulp accuracy in single,
109 * but by being a bit liberal we only catch real errors rather than
110 * doing compiler-standard-compliance debugging.
112 * The range is used by derived classes to test math functions. The
113 * default test range will be [1,10], which is intentionally
114 * conservative so it works with (inverse) square root, division,
115 * exponentials, logarithms, and error functions.
117 SimdBaseTest() :
118 ulpTol_(defaultRealUlpTol()), absTol_(0)
122 /*! \brief Adjust ulp tolerance from the default 10 (float) or 255 (double). */
123 void setUlpTol(std::int64_t newTol) { ulpTol_ = newTol; }
125 /*! \brief Adjust ulp tolerance for single accuracy functions. */
126 void setUlpTolSingleAccuracy(std::int64_t newTol)
128 const int realBits = std::numeric_limits<real>::digits;
129 const int singleBits = std::numeric_limits<float>::digits;
130 // In single precision the expression (1LL << 0) evaluates to 1.
131 setUlpTol(newTol * (1LL << (realBits - singleBits)));
134 /*! \brief Adjust the absolute tolerance from the default 0.
136 * If values are closer than the absolute tolerance, the test will pass
137 * no matter what their ulp difference is.
139 void setAbsTol(real newTol) { absTol_ = newTol; }
141 /*! \brief Number of test points to use, settable on command line.
143 * \note While this has to be a static non-const variable for the
144 * command-line option to work, you should never change it
145 * manually in any of the tests, because the static storage
146 * class will make the value apply to all subsequent tests
147 * unless you remember to reset it.
149 static int s_nPoints;
151 /*! \brief Compare two std::vector<real> for approximate equality.
153 * This is an internal implementation routine that will be used by
154 * routines in derived child classes that first convert SIMD or SIMD4
155 * variables to std::vector<real>. Do not call it directly.
157 * This routine is designed according to the Google test specs, so the char
158 * strings will describe the arguments to the macro.
160 * The comparison is applied to each element, and it returns true if each element
161 * in the vector test variable is within the class tolerances of the corresponding
162 * reference elements.
164 ::testing::AssertionResult
165 compareVectorRealUlp(const char * refExpr, const char * tstExpr,
166 const std::vector<real> &ref, const std::vector<real> &tst);
168 /*! \brief Compare std::vectors for exact equality.
170 * The template in this class makes it usable for testing both
171 * SIMD floating-point and integers variables, after conversion to
172 * vectors.
173 * This is an internal implementation routine that will be used by
174 * routines in derived child classes that first convert SIMD or SIMD4
175 * variables to std::vector<real>. Do not call it directly.
177 * This routine is designed according to the Google test specs, so the char
178 * strings will describe the arguments to the macro.
180 * The comparison is applied to each element, and it returns true if each element
181 * in the vector test variable is within the class tolerances of the corresponding
182 * reference elements.
184 template <typename T> ::testing::AssertionResult
185 compareVectorEq(const char * refExpr, const char * tstExpr,
186 const std::vector<T> &ref, const std::vector<T> &tst)
188 if (ref == tst)
190 return ::testing::AssertionSuccess();
192 else
194 return ::testing::AssertionFailure()
195 << "Failing SIMD comparison between " << refExpr << " and " << tstExpr << std::endl
196 << "Ref. values: " << ::testing::PrintToString(ref) << std::endl
197 << "Test values: " << ::testing::PrintToString(tst) << std::endl;
201 protected:
202 std::int64_t ulpTol_; //!< Current tolerance in units-in-last-position.
203 real absTol_; //!< Current absolute tolerance.
206 } // namespace test
207 } // namespace gmx
209 #endif // GMX_SIMD_TESTS_BASE_H