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.
39 #include "gromacs/simd/simd.h"
40 #include "gromacs/utility/basedefinitions.h"
50 /*! \addtogroup module_simd */
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
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
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
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);
118 #if GMX_SIMD_HAVE_INT32_LOGICAL
119 const SimdInt32 iSimd_0xF0F0F0F0 = setSimdIntFrom1I(0xF0F0F0F0);
120 const SimdInt32 iSimd_0xCCCCCCCC = setSimdIntFrom1I(0xCCCCCCCC);
123 #if GMX_SIMD_HAVE_REAL
124 TEST(SimdTest, GmxAligned)
126 // Test alignment with two variables that must be aligned, and one that
127 // doesn't have to be. The order of variables is up to the compiler, but
128 // if it ignores alignment it is highly unlikely that both r1/r3 still end
129 // up being aligned by mistake.
130 alignas(GMX_SIMD_ALIGNMENT) real r1;
132 alignas(GMX_SIMD_ALIGNMENT) real r3;
134 std::uint64_t addr1 = reinterpret_cast<std::uint64_t>(&r1);
135 std::uint64_t addr2 = reinterpret_cast<std::uint64_t>(&r2);
136 std::uint64_t addr3 = reinterpret_cast<std::uint64_t>(&r3);
138 EXPECT_EQ(0, addr1 % GMX_SIMD_ALIGNMENT);
139 EXPECT_NE(0, addr2); // Just so r2 is not optimized away
140 EXPECT_EQ(0, addr3 % GMX_SIMD_ALIGNMENT);
142 alignas(GMX_SIMD_ALIGNMENT) std::int32_t i1;
144 alignas(GMX_SIMD_ALIGNMENT) std::int32_t i3;
146 addr1 = reinterpret_cast<std::uint64_t>(&i1);
147 addr2 = reinterpret_cast<std::uint64_t>(&i2);
148 addr3 = reinterpret_cast<std::uint64_t>(&i3);
150 EXPECT_EQ(0, addr1 % GMX_SIMD_ALIGNMENT);
151 EXPECT_NE(0, addr2); // Just so i2 is not optimized away
152 EXPECT_EQ(0, addr3 % GMX_SIMD_ALIGNMENT);
157 simdReal2Vector(const SimdReal simd)
159 alignas(GMX_SIMD_ALIGNMENT) real mem[GMX_SIMD_REAL_WIDTH];
162 std::vector<real> v(mem, mem+GMX_SIMD_REAL_WIDTH);
168 vector2SimdReal(const std::vector<real> &v)
170 alignas(GMX_SIMD_ALIGNMENT) real mem[GMX_SIMD_REAL_WIDTH];
172 for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
174 mem[i] = v[i % v.size()]; // repeat vector contents to fill simd width
176 return load<SimdReal>(mem);
180 setSimdRealFrom3R(real r0, real r1, real r2)
182 std::vector<real> v(3);
186 return vector2SimdReal(v);
190 setSimdRealFrom1R(real value)
192 std::vector<real> v(GMX_SIMD_REAL_WIDTH);
193 for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
197 return vector2SimdReal(v);
200 testing::AssertionResult
201 SimdTest::compareSimdRealUlp(const char * refExpr, const char * tstExpr,
202 const SimdReal ref, const SimdReal tst)
204 return compareVectorRealUlp(refExpr, tstExpr, simdReal2Vector(ref), simdReal2Vector(tst));
207 testing::AssertionResult
208 SimdTest::compareSimdEq(const char * refExpr, const char * tstExpr,
209 const SimdReal ref, const SimdReal tst)
211 return compareVectorEq(refExpr, tstExpr, simdReal2Vector(ref), simdReal2Vector(tst));
214 std::vector<std::int32_t>
215 simdInt2Vector(const SimdInt32 simd)
217 alignas(GMX_SIMD_ALIGNMENT) std::int32_t mem[GMX_SIMD_REAL_WIDTH];
220 std::vector<std::int32_t> v(mem, mem+GMX_SIMD_REAL_WIDTH);
226 vector2SimdInt(const std::vector<std::int32_t> &v)
228 alignas(GMX_SIMD_ALIGNMENT) std::int32_t mem[GMX_SIMD_REAL_WIDTH];
230 for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
232 mem[i] = v[i % v.size()]; // repeat vector contents to fill simd width
234 return load<SimdInt32>(mem);
238 setSimdIntFrom3I(int i0, int i1, int i2)
240 std::vector<int> v(3);
244 return vector2SimdInt(v);
248 setSimdIntFrom1I(int value)
250 std::vector<int> v(GMX_SIMD_REAL_WIDTH);
251 for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
255 return vector2SimdInt(v);
258 ::testing::AssertionResult
259 SimdTest::compareSimdEq(const char * refExpr, const char * tstExpr,
260 const SimdInt32 ref, const SimdInt32 tst)
262 return compareVectorEq(refExpr, tstExpr, simdInt2Vector(ref), simdInt2Vector(tst));
265 #endif // GMX_SIMD_HAVE_REAL