9e94c2ea73b4c726e3a6d36632696c6ff224f510
[alexxy/gromacs.git] / src / gromacs / simd / tests / simd.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2014,2015,2016,2017,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.
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 #ifndef GMX_SIMD_TESTS_SIMD_H
36 #define GMX_SIMD_TESTS_SIMD_H
37
38 /*! \internal \file
39  * \brief
40  * Declares fixture for testing of normal SIMD (not SIMD4) functionality.
41  *
42  * The SIMD tests are both simple and complicated. The actual testing logic
43  * is \a very straightforward since we just need to test single values against
44  * the math library, and for some math functions we need to do it in a loop.
45  * This could have been achieved in minutes with the default Google Test tools,
46  * if it wasn't for the problem that we cannot access or compare SIMD contents
47  * directly without using lots of other SIMD functionality. For this reason
48  * we have separate the basic testing of load/store operations into a separate
49  * bootstrapping test. Once this works, we use a set of utility routines to
50  * convert SIMD contents to/from std:vector<> and perform the rest of the tests,
51  * which then can farmed out to the base class SimdBaseTest that is common
52  * to SIMD and SIMD4.
53  *
54  * Another complication is that the width of the SIMD implementation will
55  * depend on the hardware and precision. For some simple operations it is
56  * sufficient to set all SIMD elements to the same value, and check that the
57  * result is present in all elements. However, for a few more complex
58  * instructions that might rely on shuffling under-the-hood it is important
59  * that we can test operations with different elements. We achieve this by
60  * having test code that can initialize a SIMD variable from an std::vector
61  * of arbitrary length; the vector is simply repeated to fill all elements in
62  * the SIMD variable. We also have similar routines to compare a SIMD result
63  * with values in a vector, which returns true iff all elements match.
64  *
65  * This way we can write simple tests that use different values for all SIMD
66  * elements. Personally I like using vectors of length 3, since this means
67  * there are no simple repeated patterns in low/high halves of SIMD variables
68  * that are 2,4,8,or 16 elements wide, and we still don't have to care about
69  * the exact SIMD width of the underlying implementation.
70  *
71  * Note that this utility uses a few SIMD load/store instructions internally -
72  * those have been tested separately in the bootstrap_loadstore.cpp file.
73  *
74  * \author Erik Lindahl <erik.lindahl@scilifelab.se>
75  * \ingroup module_simd
76  */
77 #include <vector>
78
79 #include <gtest/gtest.h>
80
81 #include "gromacs/simd/simd.h"
82
83 #include "base.h"
84 #include "data.h"
85
86 #if GMX_SIMD
87
88 namespace gmx
89 {
90 namespace test
91 {
92
93
94 /*! \cond internal */
95 /*! \addtogroup module_simd */
96 /*! \{ */
97
98 /* Unfortunately we cannot keep static SIMD constants in the test fixture class.
99  * The problem is that SIMD memory need to be aligned, and in particular
100  * this applies to automatic storage of variables in classes. For SSE registers
101  * this means 16-byte alignment (which seems to work), but AVX requires 32-bit
102  * alignment. At least both gcc-4.7.3 and Apple clang-5.0 (OS X 10.9) fail to
103  * align these variables when they are stored as data in a class.
104  *
105  * In theory we could set some of these on-the-fly e.g. with setSimdFrom3R()
106  * instead (although that would mean repeating code between tests), but many of
107  * the constants depend on the current precision not to mention they
108  * occasionally have many digits that need to be exactly right, and keeping
109  * them in a single place makes sure they are consistent.
110  */
111 #    if GMX_SIMD_HAVE_REAL
112 extern const SimdReal rSimd_c0c1c2; //!< c0,c1,c2 repeated
113 extern const SimdReal rSimd_c3c4c5; //!< c3,c4,c5 repeated
114 extern const SimdReal rSimd_c6c7c8; //!< c6,c7,c8 repeated
115 extern const SimdReal rSimd_c3c0c4; //!< c3,c0,c4 repeated
116 extern const SimdReal rSimd_c4c6c8; //!< c4,c6,c8 repeated
117 extern const SimdReal rSimd_c7c2c3; //!< c7,c2,c3 repeated
118 extern const SimdReal rSimd_m0m1m2; //!< -c0,-c1,-c2 repeated
119 extern const SimdReal rSimd_m3m0m4; //!< -c3,-c0,-c4 repeated
120
121 extern const SimdReal rSimd_2p25;  //!< Value that rounds down.
122 extern const SimdReal rSimd_3p25;  //!< Value that rounds down.
123 extern const SimdReal rSimd_3p75;  //!< Value that rounds up.
124 extern const SimdReal rSimd_m2p25; //!< Negative value that rounds up.
125 extern const SimdReal rSimd_m3p25; //!< Negative value that rounds up.
126 extern const SimdReal rSimd_m3p75; //!< Negative value that rounds down.
127 //! Three large floating-point values whose exponents are >32.
128 extern const SimdReal rSimd_Exp;
129
130 #        if GMX_SIMD_HAVE_LOGICAL
131 extern const SimdReal rSimd_logicalA;         //!< Bit pattern to test logical ops
132 extern const SimdReal rSimd_logicalB;         //!< Bit pattern to test logical ops
133 extern const SimdReal rSimd_logicalResultOr;  //!< Result or bitwise 'or' of A and B
134 extern const SimdReal rSimd_logicalResultAnd; //!< Result or bitwise 'and' of A and B
135 #        endif                                // GMX_SIMD_HAVE_LOGICAL
136
137 #        if GMX_SIMD_HAVE_DOUBLE && GMX_DOUBLE
138 // Make sure we also test exponents outside single precision when we use double
139 extern const SimdReal rSimd_ExpDouble;
140 #        endif
141 // Magic FP numbers corresponding to specific bit patterns
142 extern const SimdReal rSimd_Bits1; //!< Pattern F0 repeated to fill single/double.
143 extern const SimdReal rSimd_Bits2; //!< Pattern CC repeated to fill single/double.
144 extern const SimdReal rSimd_Bits3; //!< Pattern C0 repeated to fill single/double.
145 extern const SimdReal rSimd_Bits4; //!< Pattern 0C repeated to fill single/double.
146 extern const SimdReal rSimd_Bits5; //!< Pattern FC repeated to fill single/double.
147 extern const SimdReal rSimd_Bits6; //!< Pattern 3C repeated to fill single/double.
148 #    endif                         // GMX_SIMD_HAVE_REAL
149 #    if GMX_SIMD_HAVE_INT32_ARITHMETICS
150 extern const SimdInt32 iSimd_1_2_3;    //!< Three generic ints.
151 extern const SimdInt32 iSimd_4_5_6;    //!< Three generic ints.
152 extern const SimdInt32 iSimd_7_8_9;    //!< Three generic ints.
153 extern const SimdInt32 iSimd_5_7_9;    //!< iSimd_1_2_3 + iSimd_4_5_6.
154 extern const SimdInt32 iSimd_1M_2M_3M; //!< Term1 for 32bit add/sub.
155 extern const SimdInt32 iSimd_4M_5M_6M; //!< Term2 for 32bit add/sub.
156 extern const SimdInt32 iSimd_5M_7M_9M; //!< iSimd_1M_2M_3M + iSimd_4M_5M_6M.
157 #    endif
158 #    if GMX_SIMD_HAVE_INT32_LOGICAL
159 extern const SimdInt32 iSimd_0xF0F0F0F0; //!< Bitpattern to test integer logical operations.
160 extern const SimdInt32 iSimd_0xCCCCCCCC; //!< Bitpattern to test integer logical operations.
161 #    endif
162
163
164 /*! \internal
165  * \brief
166  * Test fixture for SIMD tests.
167  *
168  * This is a very simple test fixture that basically just takes the common
169  * SIMD/SIMD4 functionality from SimdBaseTest and creates wrapper routines
170  * specific for normal SIMD functionality.
171  */
172 class SimdTest : public SimdBaseTest
173 {
174 public:
175 #    if GMX_SIMD_HAVE_REAL
176     /*! \brief Compare two real SIMD variables for approximate equality.
177      *
178      * This is an internal implementation routine. YOu should always use
179      * GMX_EXPECT_SIMD_REAL_NEAR() instead.
180      *
181      * This routine is designed according to the Google test specs, so the char
182      * strings will describe the arguments to the macro.
183      *
184      * The comparison is applied to each element, and it returns true if each element
185      * in the SIMD test variable is within the class tolerances of the corresponding
186      * reference element.
187      */
188
189
190     ::testing::AssertionResult
191     compareSimdRealUlp(const char* refExpr, const char* tstExpr, SimdReal ref, SimdReal tst);
192
193     /*! \brief Compare two real SIMD variables for exact equality.
194      *
195      * This is an internal implementation routine. YOu should always use
196      * GMX_EXPECT_SIMD_REAL_NEAR() instead.
197      *
198      * This routine is designed according to the Google test specs, so the char
199      * strings will describe the arguments to the macro.
200      *
201      * The comparison is applied to each element, and it returns true if each element
202      * in the SIMD test variable is within the class tolerances of the corresponding
203      * reference element.
204      */
205     ::testing::AssertionResult compareSimdEq(const char* refExpr, const char* tstExpr, SimdReal ref, SimdReal tst);
206
207     /*! \brief Compare two 32-bit integer SIMD variables.
208      *
209      * This is an internal implementation routine. YOu should always use
210      * GMX_EXPECT_SIMD_INT_EQ() instead.
211      *
212      * This routine is designed according to the Google test specs, so the char
213      * strings will describe the arguments to the macro, while the SIMD and
214      * tolerance arguments are used to decide if the values are approximately equal.
215      *
216      * The comparison is applied to each element, and it returns true if each element
217      * in the SIMD variable tst is identical to the corresponding reference element.
218      */
219     ::testing::AssertionResult compareSimdEq(const char* refExpr, const char* tstExpr, SimdInt32 ref, SimdInt32 tst);
220 #    endif
221 };
222
223 #    if GMX_SIMD_HAVE_REAL
224 /*! \brief Convert SIMD real to std::vector<real>.
225  *
226  * The returned vector will have the same length as the SIMD width.
227  */
228 std::vector<real> simdReal2Vector(SimdReal simd);
229
230 /*! \brief Return floating-point SIMD value from std::vector<real>.
231  *
232  * If the vector is longer than SIMD width, only the first elements will be used.
233  * If it is shorter, the contents will be repeated to fill the SIMD register.
234  */
235 SimdReal vector2SimdReal(const std::vector<real>& v);
236
237 /*! \brief Set SIMD register contents from three real values.
238  *
239  * Our reason for using three values is that 3 is not a factor in any known
240  * SIMD width, so this way there will not be any simple repeated patterns e.g.
241  * between the low/high 64/128/256 bits in the SIMD register, which could hide bugs.
242  */
243 SimdReal setSimdRealFrom3R(real r0, real r1, real r2);
244
245 /*! \brief Set SIMD register contents from single real value.
246  *
247  * All elements is set from the given value. This is effectively the same
248  * operation as simdSet1(), but is implemented using only load/store
249  * operations that have been tested separately in the bootstrapping tests.
250  */
251 SimdReal setSimdRealFrom1R(real value);
252
253 /*! \brief Test if a SIMD real is bitwise identical to reference SIMD value. */
254 #        define GMX_EXPECT_SIMD_REAL_EQ(ref, tst) EXPECT_PRED_FORMAT2(compareSimdEq, ref, tst)
255
256 /*! \brief Test if a SIMD is bitwise identical to reference SIMD value. */
257 #        define GMX_EXPECT_SIMD_EQ(ref, tst) EXPECT_PRED_FORMAT2(compareSimdEq, ref, tst)
258
259 /*! \brief Test if a SIMD real is within tolerance of reference SIMD value. */
260 #        define GMX_EXPECT_SIMD_REAL_NEAR(ref, tst) \
261             EXPECT_PRED_FORMAT2(compareSimdRealUlp, ref, tst)
262
263 /*! \brief Convert SIMD integer to std::vector<int>.
264  *
265  * The returned vector will have the same length as the SIMD width.
266  */
267 std::vector<std::int32_t> simdInt2Vector(SimdInt32 simd);
268
269 /*! \brief Return 32-bit integer SIMD value from std::vector<int>.
270  *
271  * If the vector is longer than SIMD width, only the first elements will be used.
272  * If it is shorter, the contents will be repeated to fill the SIMD register.
273  */
274 SimdInt32 vector2SimdInt(const std::vector<std::int32_t>& v);
275
276 /*! \brief Set SIMD register contents from three int values.
277  *
278  * Our reason for using three values is that 3 is not a factor in any known
279  * SIMD width, so this way there will not be any simple repeated patterns e.g.
280  * between the low/high 64/128/256 bits in the SIMD register, which could hide bugs.
281  */
282 SimdInt32 setSimdIntFrom3I(int i0, int i1, int i2);
283
284 /*! \brief Set SIMD register contents from single integer value.
285  *
286  * All elements is set from the given value. This is effectively the same
287  * operation as simdSet1I(), but is implemented using only load/store
288  * operations that have been tested separately in the bootstrapping tests.
289  */
290 SimdInt32 setSimdIntFrom1I(int value);
291
292 /*! \brief Macro that checks SIMD integer expression against SIMD or reference int.
293  *
294  * If the reference argument is a scalar integer it will be expanded into
295  * the width of the SIMD register and tested against all elements.
296  */
297 #        define GMX_EXPECT_SIMD_INT_EQ(ref, tst) EXPECT_PRED_FORMAT2(compareSimdEq, ref, tst)
298
299 #    endif // GMX_SIMD_HAVE_REAL
300
301 /*! \} */
302 /*! \endcond */
303
304 } // namespace test
305 } // namespace gmx
306
307 #endif // GMX_SIMD
308
309 #endif // GMX_SIMD_TESTS_SIMD_H