Make sure frexp() returns correct for argument 0.0
[gromacs.git] / src / gromacs / simd / tests / simd.cpp
blobc742ba1134b2e93bf48b6a4e7c7fc371c8f8f64a
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2014,2015,2016,2017,2018 by the GROMACS development team.
5 * Copyright (c) 2019,2020, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
36 #include "gmxpre.h"
38 #include "simd.h"
40 #include "gromacs/simd/simd.h"
41 #include "gromacs/utility/basedefinitions.h"
43 #if GMX_SIMD
45 namespace gmx
47 namespace test
50 /*! \cond internal */
51 /*! \addtogroup module_simd */
52 /*! \{ */
54 /* Unfortunately we cannot keep static SIMD constants in the test fixture class.
55 * The problem is that SIMD memory need to be aligned, and in particular
56 * this applies to automatic storage of variables in classes. For SSE registers
57 * this means 16-byte alignment (which seems to work), but AVX requires 32-bit
58 * alignment. At least both gcc-4.7.3 and Apple clang-5.0 (OS X 10.9) fail to
59 * align these variables when they are stored as data in a class.
61 * In theory we could set some of these on-the-fly e.g. with setSimdRealFrom3R()
62 * instead (although that would mean repeating code between tests), but many of
63 * the constants depend on the current precision not to mention they
64 * occasionally have many digits that need to be exactly right, and keeping
65 * them in a single place makes sure they are consistent.
67 # if GMX_SIMD_HAVE_REAL
68 const SimdReal rSimd_c0c1c2 = setSimdRealFrom3R(c0, c1, c2);
69 const SimdReal rSimd_c3c4c5 = setSimdRealFrom3R(c3, c4, c5);
70 const SimdReal rSimd_c6c7c8 = setSimdRealFrom3R(c6, c7, c8);
71 const SimdReal rSimd_c3c0c4 = setSimdRealFrom3R(c3, c0, c4);
72 const SimdReal rSimd_c4c6c8 = setSimdRealFrom3R(c4, c6, c8);
73 const SimdReal rSimd_c7c2c3 = setSimdRealFrom3R(c7, c2, c3);
74 const SimdReal rSimd_m0m1m2 = setSimdRealFrom3R(-c0, -c1, -c2);
75 const SimdReal rSimd_m3m0m4 = setSimdRealFrom3R(-c3, -c0, -c4);
77 const SimdReal rSimd_2p25 = setSimdRealFrom1R(2.25);
78 const SimdReal rSimd_3p25 = setSimdRealFrom1R(3.25);
79 const SimdReal rSimd_3p75 = setSimdRealFrom1R(3.75);
80 const SimdReal rSimd_m2p25 = setSimdRealFrom1R(-2.25);
81 const SimdReal rSimd_m3p25 = setSimdRealFrom1R(-3.25);
82 const SimdReal rSimd_m3p75 = setSimdRealFrom1R(-3.75);
83 const SimdReal rSimd_Exp = setSimdRealFrom3R(1.4055235171027452623914516e+18,
84 5.3057102734253445623914516e-13,
85 -2.1057102745623934534514516e+16);
87 # if GMX_SIMD_HAVE_DOUBLE && GMX_DOUBLE
88 // Make sure we also test exponents outside single precision when we use double
89 const SimdReal rSimd_ExpDouble1 =
90 setSimdRealFrom3R(0.0, 8.794495252903116023030553e-140, -3.637060701570496477655022e+202);
91 const SimdReal rSimd_ExpDouble2 =
92 setSimdRealFrom3R(6.287393598732017379054414e+176, 0.0, -3.637060701570496477655022e+202);
93 # endif // GMX_SIMD_HAVE_DOUBLE && GMX_DOUBLE
95 # if GMX_SIMD_HAVE_LOGICAL
96 // The numbers below all have exponent (2^0), which will not change with AND/OR operations.
97 // We also leave the last part of the mantissa as zeros, to avoid rounding issues in the compiler
98 # if GMX_DOUBLE
99 const SimdReal rSimd_logicalA =
100 setSimdRealFrom1R(1.3333333332557231188); // mantissa 01010101010101010101010101010101
101 const SimdReal rSimd_logicalB =
102 setSimdRealFrom1R(1.7999999998137354851); // mantissa 11001100110011001100110011001100
103 const SimdReal rSimd_logicalResultAnd =
104 setSimdRealFrom1R(1.266666666604578495); // mantissa 01000100010001000100010001000100
105 const SimdReal rSimd_logicalResultOr =
106 setSimdRealFrom1R(1.8666666664648801088); // mantissa 11011101110111011101110111011101
107 # else // GMX_DOUBLE
108 const SimdReal rSimd_logicalA = setSimdRealFrom1R(1.3333282470703125); // mantissa 0101010101010101
109 const SimdReal rSimd_logicalB = setSimdRealFrom1R(1.79998779296875); // mantissa 1100110011001100
110 const SimdReal rSimd_logicalResultAnd = setSimdRealFrom1R(1.26666259765625); // mantissa 0100010001000100
111 const SimdReal rSimd_logicalResultOr = setSimdRealFrom1R(1.8666534423828125); // mantissa 1101110111011101
112 # endif // GMX_DOUBLE
113 # endif // GMX_SIMD_HAVE_LOGICAL
115 # endif // GMX_SIMD_HAVE_REAL
116 # if GMX_SIMD_HAVE_INT32_ARITHMETICS
117 const SimdInt32 iSimd_1_2_3 = setSimdIntFrom3I(1, 2, 3);
118 const SimdInt32 iSimd_4_5_6 = setSimdIntFrom3I(4, 5, 6);
119 const SimdInt32 iSimd_7_8_9 = setSimdIntFrom3I(7, 8, 9);
120 const SimdInt32 iSimd_5_7_9 = setSimdIntFrom3I(5, 7, 9);
121 const SimdInt32 iSimd_1M_2M_3M = setSimdIntFrom3I(1000000, 2000000, 3000000);
122 const SimdInt32 iSimd_4M_5M_6M = setSimdIntFrom3I(4000000, 5000000, 6000000);
123 const SimdInt32 iSimd_5M_7M_9M = setSimdIntFrom3I(5000000, 7000000, 9000000);
124 # endif
125 # if GMX_SIMD_HAVE_INT32_LOGICAL
126 const SimdInt32 iSimd_0xF0F0F0F0 = setSimdIntFrom1I(0xF0F0F0F0);
127 const SimdInt32 iSimd_0xCCCCCCCC = setSimdIntFrom1I(0xCCCCCCCC);
128 # endif
130 # if GMX_SIMD_HAVE_REAL
131 TEST(SimdTest, GmxAligned)
133 // Test alignment with two variables that must be aligned, and one that
134 // doesn't have to be. The order of variables is up to the compiler, but
135 // if it ignores alignment it is highly unlikely that both r1/r3 still end
136 // up being aligned by mistake.
137 alignas(GMX_SIMD_ALIGNMENT) real r1;
138 real r2;
139 alignas(GMX_SIMD_ALIGNMENT) real r3;
141 std::uint64_t addr1 = reinterpret_cast<std::uint64_t>(&r1);
142 std::uint64_t addr2 = reinterpret_cast<std::uint64_t>(&r2);
143 std::uint64_t addr3 = reinterpret_cast<std::uint64_t>(&r3);
145 EXPECT_EQ(0, addr1 % GMX_SIMD_ALIGNMENT);
146 EXPECT_NE(0, addr2); // Just so r2 is not optimized away
147 EXPECT_EQ(0, addr3 % GMX_SIMD_ALIGNMENT);
149 alignas(GMX_SIMD_ALIGNMENT) std::int32_t i1;
150 std::int32_t i2;
151 alignas(GMX_SIMD_ALIGNMENT) std::int32_t i3;
153 addr1 = reinterpret_cast<std::uint64_t>(&i1);
154 addr2 = reinterpret_cast<std::uint64_t>(&i2);
155 addr3 = reinterpret_cast<std::uint64_t>(&i3);
157 EXPECT_EQ(0, addr1 % GMX_SIMD_ALIGNMENT);
158 EXPECT_NE(0, addr2); // Just so i2 is not optimized away
159 EXPECT_EQ(0, addr3 % GMX_SIMD_ALIGNMENT);
163 ::std::vector<real> simdReal2Vector(const SimdReal simd)
165 alignas(GMX_SIMD_ALIGNMENT) real mem[GMX_SIMD_REAL_WIDTH];
167 store(mem, simd);
168 std::vector<real> v(mem, mem + GMX_SIMD_REAL_WIDTH);
170 return v;
173 SimdReal vector2SimdReal(const std::vector<real>& v)
175 alignas(GMX_SIMD_ALIGNMENT) real mem[GMX_SIMD_REAL_WIDTH];
177 for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
179 mem[i] = v[i % v.size()]; // repeat vector contents to fill simd width
181 return load<SimdReal>(mem);
184 SimdReal setSimdRealFrom3R(real r0, real r1, real r2)
186 std::vector<real> v(3);
187 v[0] = r0;
188 v[1] = r1;
189 v[2] = r2;
190 return vector2SimdReal(v);
193 SimdReal setSimdRealFrom1R(real value)
195 std::vector<real> v(GMX_SIMD_REAL_WIDTH);
196 for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
198 v[i] = value;
200 return vector2SimdReal(v);
203 testing::AssertionResult SimdTest::compareSimdRealUlp(const char* refExpr,
204 const char* tstExpr,
205 const SimdReal ref,
206 const SimdReal tst)
208 return compareVectorRealUlp(refExpr, tstExpr, simdReal2Vector(ref), simdReal2Vector(tst));
211 testing::AssertionResult SimdTest::compareSimdEq(const char* refExpr,
212 const char* tstExpr,
213 const SimdReal ref,
214 const SimdReal tst)
216 return compareVectorEq(refExpr, tstExpr, simdReal2Vector(ref), simdReal2Vector(tst));
219 std::vector<std::int32_t> simdInt2Vector(const SimdInt32 simd)
221 alignas(GMX_SIMD_ALIGNMENT) std::int32_t mem[GMX_SIMD_REAL_WIDTH];
223 store(mem, simd);
224 std::vector<std::int32_t> v(mem, mem + GMX_SIMD_REAL_WIDTH);
226 return v;
229 SimdInt32 vector2SimdInt(const std::vector<std::int32_t>& v)
231 alignas(GMX_SIMD_ALIGNMENT) std::int32_t mem[GMX_SIMD_REAL_WIDTH];
233 for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
235 mem[i] = v[i % v.size()]; // repeat vector contents to fill simd width
237 return load<SimdInt32>(mem);
240 SimdInt32 setSimdIntFrom3I(int i0, int i1, int i2)
242 std::vector<int> v(3);
243 v[0] = i0;
244 v[1] = i1;
245 v[2] = i2;
246 return vector2SimdInt(v);
249 SimdInt32 setSimdIntFrom1I(int value)
251 std::vector<int> v(GMX_SIMD_REAL_WIDTH);
252 for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
254 v[i] = value;
256 return vector2SimdInt(v);
259 ::testing::AssertionResult SimdTest::compareSimdEq(const char* refExpr,
260 const char* tstExpr,
261 const SimdInt32 ref,
262 const SimdInt32 tst)
264 return compareVectorEq(refExpr, tstExpr, simdInt2Vector(ref), simdInt2Vector(tst));
267 # endif // GMX_SIMD_HAVE_REAL
269 /*! \} */
270 /*! \endcond */
272 } // namespace test
273 } // namespace gmx
275 #endif // GMX_SIMD