Improved SIMD test data to use all bits
[gromacs.git] / src / gromacs / simd / tests / simd.cpp
blob6aa0fa04ff311658760d41b9d77ebf203ca1ebfc
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2014,2015,2016,2017, 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.
35 #include "gmxpre.h"
37 #include "simd.h"
39 #include "gromacs/simd/simd.h"
40 #include "gromacs/utility/basedefinitions.h"
42 #if GMX_SIMD
44 namespace gmx
46 namespace test
49 /*! \cond internal */
50 /*! \addtogroup module_simd */
51 /*! \{ */
53 /* Unfortunately we cannot keep static SIMD constants in the test fixture class.
54 * The problem is that SIMD memory need to be aligned, and in particular
55 * this applies to automatic storage of variables in classes. For SSE registers
56 * this means 16-byte alignment (which seems to work), but AVX requires 32-bit
57 * alignment. At least both gcc-4.7.3 and Apple clang-5.0 (OS X 10.9) fail to
58 * align these variables when they are stored as data in a class.
60 * In theory we could set some of these on-the-fly e.g. with setSimdRealFrom3R()
61 * instead (although that would mean repeating code between tests), but many of
62 * the constants depend on the current precision not to mention they
63 * occasionally have many digits that need to be exactly right, and keeping
64 * them in a single place makes sure they are consistent.
66 #if GMX_SIMD_HAVE_REAL
67 const SimdReal rSimd_c0c1c2 = setSimdRealFrom3R( c0, c1, c2);
68 const SimdReal rSimd_c3c4c5 = setSimdRealFrom3R( c3, c4, c5);
69 const SimdReal rSimd_c6c7c8 = setSimdRealFrom3R( c6, c7, c8);
70 const SimdReal rSimd_c3c0c4 = setSimdRealFrom3R( c3, c0, c4);
71 const SimdReal rSimd_c4c6c8 = setSimdRealFrom3R( c4, c6, c8);
72 const SimdReal rSimd_c7c2c3 = setSimdRealFrom3R( c7, c2, c3);
73 const SimdReal rSimd_m0m1m2 = setSimdRealFrom3R(-c0, -c1, -c2);
74 const SimdReal rSimd_m3m0m4 = setSimdRealFrom3R(-c3, -c0, -c4);
76 const SimdReal rSimd_2p25 = setSimdRealFrom1R( 2.25);
77 const SimdReal rSimd_3p25 = setSimdRealFrom1R( 3.25);
78 const SimdReal rSimd_3p75 = setSimdRealFrom1R( 3.75);
79 const SimdReal rSimd_m2p25 = setSimdRealFrom1R(-2.25);
80 const SimdReal rSimd_m3p25 = setSimdRealFrom1R(-3.25);
81 const SimdReal rSimd_m3p75 = setSimdRealFrom1R(-3.75);
82 const SimdReal rSimd_Exp = setSimdRealFrom3R( 1.4055235171027452623914516e+18,
83 5.3057102734253445623914516e-13,
84 -2.1057102745623934534514516e+16);
85 #if GMX_SIMD_HAVE_DOUBLE && GMX_DOUBLE
86 // Make sure we also test exponents outside single precision when we use double
87 const SimdReal rSimd_ExpDouble = setSimdRealFrom3R( 6.287393598732017379054414e+176,
88 8.794495252903116023030553e-140,
89 -3.637060701570496477655022e+202);
90 #endif // GMX_SIMD_HAVE_DOUBLE && GMX_DOUBLE
92 #if GMX_SIMD_HAVE_LOGICAL
93 // The numbers below all have exponent (2^0), which will not change with AND/OR operations.
94 // We also leave the last part of the mantissa as zeros, to avoid rounding issues in the compiler
95 #if GMX_DOUBLE
96 const SimdReal rSimd_logicalA = setSimdRealFrom1R(1.3333333332557231188); // mantissa 01010101010101010101010101010101
97 const SimdReal rSimd_logicalB = setSimdRealFrom1R(1.7999999998137354851); // mantissa 11001100110011001100110011001100
98 const SimdReal rSimd_logicalResultAnd = setSimdRealFrom1R(1.266666666604578495); // mantissa 01000100010001000100010001000100
99 const SimdReal rSimd_logicalResultOr = setSimdRealFrom1R(1.8666666664648801088); // mantissa 11011101110111011101110111011101
100 #else // GMX_DOUBLE
101 const SimdReal rSimd_logicalA = setSimdRealFrom1R(1.3333282470703125); // mantissa 0101010101010101
102 const SimdReal rSimd_logicalB = setSimdRealFrom1R(1.79998779296875); // mantissa 1100110011001100
103 const SimdReal rSimd_logicalResultAnd = setSimdRealFrom1R(1.26666259765625); // mantissa 0100010001000100
104 const SimdReal rSimd_logicalResultOr = setSimdRealFrom1R(1.8666534423828125); // mantissa 1101110111011101
105 #endif // GMX_DOUBLE
106 #endif // GMX_SIMD_HAVE_LOGICAL
108 #endif // GMX_SIMD_HAVE_REAL
109 #if GMX_SIMD_HAVE_INT32_ARITHMETICS
110 const SimdInt32 iSimd_1_2_3 = setSimdIntFrom3I(1, 2, 3);
111 const SimdInt32 iSimd_4_5_6 = setSimdIntFrom3I(4, 5, 6);
112 const SimdInt32 iSimd_7_8_9 = setSimdIntFrom3I(7, 8, 9);
113 const SimdInt32 iSimd_5_7_9 = setSimdIntFrom3I(5, 7, 9);
114 const SimdInt32 iSimd_1M_2M_3M = setSimdIntFrom3I(1000000, 2000000, 3000000);
115 const SimdInt32 iSimd_4M_5M_6M = setSimdIntFrom3I(4000000, 5000000, 6000000);
116 const SimdInt32 iSimd_5M_7M_9M = setSimdIntFrom3I(5000000, 7000000, 9000000);
117 #endif
118 #if GMX_SIMD_HAVE_INT32_LOGICAL
119 const SimdInt32 iSimd_0xF0F0F0F0 = setSimdIntFrom1I(0xF0F0F0F0);
120 const SimdInt32 iSimd_0xCCCCCCCC = setSimdIntFrom1I(0xCCCCCCCC);
121 #endif
123 #if GMX_SIMD_HAVE_REAL
124 ::std::vector<real>
125 simdReal2Vector(const SimdReal simd)
127 GMX_ALIGNED(real, GMX_SIMD_REAL_WIDTH) mem[GMX_SIMD_REAL_WIDTH];
129 store(mem, simd);
130 std::vector<real> v(mem, mem+GMX_SIMD_REAL_WIDTH);
132 return v;
135 SimdReal
136 vector2SimdReal(const std::vector<real> &v)
138 GMX_ALIGNED(real, GMX_SIMD_REAL_WIDTH) mem[GMX_SIMD_REAL_WIDTH];
140 for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
142 mem[i] = v[i % v.size()]; // repeat vector contents to fill simd width
144 return load(mem);
147 SimdReal
148 setSimdRealFrom3R(real r0, real r1, real r2)
150 std::vector<real> v(3);
151 v[0] = r0;
152 v[1] = r1;
153 v[2] = r2;
154 return vector2SimdReal(v);
157 SimdReal
158 setSimdRealFrom1R(real value)
160 std::vector<real> v(GMX_SIMD_REAL_WIDTH);
161 for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
163 v[i] = value;
165 return vector2SimdReal(v);
168 testing::AssertionResult
169 SimdTest::compareSimdRealUlp(const char * refExpr, const char * tstExpr,
170 const SimdReal ref, const SimdReal tst)
172 return compareVectorRealUlp(refExpr, tstExpr, simdReal2Vector(ref), simdReal2Vector(tst));
175 testing::AssertionResult
176 SimdTest::compareSimdRealEq(const char * refExpr, const char * tstExpr,
177 const SimdReal ref, const SimdReal tst)
179 return compareVectorEq(refExpr, tstExpr, simdReal2Vector(ref), simdReal2Vector(tst));
182 std::vector<int>
183 simdInt2Vector(const SimdInt32 simd)
185 GMX_ALIGNED(int, GMX_SIMD_REAL_WIDTH) mem[GMX_SIMD_REAL_WIDTH];
187 store(mem, simd);
188 std::vector<int> v(mem, mem+GMX_SIMD_REAL_WIDTH);
190 return v;
193 SimdInt32
194 vector2SimdInt(const std::vector<int> &v)
196 GMX_ALIGNED(int, GMX_SIMD_REAL_WIDTH) mem[GMX_SIMD_REAL_WIDTH];
198 for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
200 mem[i] = v[i % v.size()]; // repeat vector contents to fill simd width
202 return load(mem);
205 SimdInt32
206 setSimdIntFrom3I(int i0, int i1, int i2)
208 std::vector<int> v(3);
209 v[0] = i0;
210 v[1] = i1;
211 v[2] = i2;
212 return vector2SimdInt(v);
215 SimdInt32
216 setSimdIntFrom1I(int value)
218 std::vector<int> v(GMX_SIMD_REAL_WIDTH);
219 for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
221 v[i] = value;
223 return vector2SimdInt(v);
226 ::testing::AssertionResult
227 SimdTest::compareSimdInt32(const char * refExpr, const char * tstExpr,
228 const SimdInt32 ref, const SimdInt32 tst)
230 return compareVectorEq(refExpr, tstExpr, simdInt2Vector(ref), simdInt2Vector(tst));
233 #endif // GMX_SIMD_HAVE_REAL
235 /*! \} */
236 /*! \endcond */
238 } // namespace
239 } // namespace
241 #endif // GMX_SIMD