128-bit AVX2 SIMD for AMD Ryzen
[gromacs.git] / src / gromacs / simd / tests / simd.cpp
blobdd56b0d4255a61e064b34bba98986bb894f91f24
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2014,2015,2016, 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_1_2_3 = setSimdRealFrom3R(1, 2, 3);
68 const SimdReal rSimd_4_5_6 = setSimdRealFrom3R(4, 5, 6);
69 const SimdReal rSimd_7_8_9 = setSimdRealFrom3R(7, 8, 9);
70 const SimdReal rSimd_5_7_9 = setSimdRealFrom3R(5, 7, 9);
71 const SimdReal rSimd_m1_m2_m3 = setSimdRealFrom3R(-1, -2, -3);
72 const SimdReal rSimd_3_1_4 = setSimdRealFrom3R(3, 1, 4);
73 const SimdReal rSimd_m3_m1_m4 = setSimdRealFrom3R(-3, -1, -4);
74 const SimdReal rSimd_2p25 = setSimdRealFrom1R(2.25);
75 const SimdReal rSimd_3p25 = setSimdRealFrom1R(3.25);
76 const SimdReal rSimd_3p75 = setSimdRealFrom1R(3.75);
77 const SimdReal rSimd_m2p25 = setSimdRealFrom1R(-2.25);
78 const SimdReal rSimd_m3p25 = setSimdRealFrom1R(-3.25);
79 const SimdReal rSimd_m3p75 = setSimdRealFrom1R(-3.75);
80 const SimdReal rSimd_Exp = setSimdRealFrom3R( 1.4055235171027452623914516e+18,
81 5.3057102734253445623914516e-13,
82 -2.1057102745623934534514516e+16);
83 # if GMX_SIMD_HAVE_DOUBLE && GMX_DOUBLE
84 // Make sure we also test exponents outside single precision when we use double
85 const SimdReal rSimd_ExpDouble = setSimdRealFrom3R( 6.287393598732017379054414e+176,
86 8.794495252903116023030553e-140,
87 -3.637060701570496477655022e+202);
88 # endif
89 #endif // GMX_SIMD_HAVE_REAL
90 #if GMX_SIMD_HAVE_INT32_ARITHMETICS
91 const SimdInt32 iSimd_1_2_3 = setSimdIntFrom3I(1, 2, 3);
92 const SimdInt32 iSimd_4_5_6 = setSimdIntFrom3I(4, 5, 6);
93 const SimdInt32 iSimd_7_8_9 = setSimdIntFrom3I(7, 8, 9);
94 const SimdInt32 iSimd_5_7_9 = setSimdIntFrom3I(5, 7, 9);
95 const SimdInt32 iSimd_1M_2M_3M = setSimdIntFrom3I(1000000, 2000000, 3000000);
96 const SimdInt32 iSimd_4M_5M_6M = setSimdIntFrom3I(4000000, 5000000, 6000000);
97 const SimdInt32 iSimd_5M_7M_9M = setSimdIntFrom3I(5000000, 7000000, 9000000);
98 #endif
99 #if GMX_SIMD_HAVE_INT32_LOGICAL
100 const SimdInt32 iSimd_0xF0F0F0F0 = setSimdIntFrom1I(0xF0F0F0F0);
101 const SimdInt32 iSimd_0xCCCCCCCC = setSimdIntFrom1I(0xCCCCCCCC);
102 #endif
104 #if GMX_SIMD_HAVE_REAL
105 ::std::vector<real>
106 simdReal2Vector(const SimdReal simd)
108 GMX_ALIGNED(real, GMX_SIMD_REAL_WIDTH) mem[GMX_SIMD_REAL_WIDTH];
110 store(mem, simd);
111 std::vector<real> v(mem, mem+GMX_SIMD_REAL_WIDTH);
113 return v;
116 SimdReal
117 vector2SimdReal(const std::vector<real> &v)
119 GMX_ALIGNED(real, GMX_SIMD_REAL_WIDTH) mem[GMX_SIMD_REAL_WIDTH];
121 for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
123 mem[i] = v[i % v.size()]; // repeat vector contents to fill simd width
125 return load(mem);
128 SimdReal
129 setSimdRealFrom3R(real r0, real r1, real r2)
131 std::vector<real> v(3);
132 v[0] = r0;
133 v[1] = r1;
134 v[2] = r2;
135 return vector2SimdReal(v);
138 SimdReal
139 setSimdRealFrom1R(real value)
141 std::vector<real> v(GMX_SIMD_REAL_WIDTH);
142 for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
144 v[i] = value;
146 return vector2SimdReal(v);
149 testing::AssertionResult
150 SimdTest::compareSimdRealUlp(const char * refExpr, const char * tstExpr,
151 const SimdReal ref, const SimdReal tst)
153 return compareVectorRealUlp(refExpr, tstExpr, simdReal2Vector(ref), simdReal2Vector(tst));
156 testing::AssertionResult
157 SimdTest::compareSimdRealEq(const char * refExpr, const char * tstExpr,
158 const SimdReal ref, const SimdReal tst)
160 return compareVectorEq(refExpr, tstExpr, simdReal2Vector(ref), simdReal2Vector(tst));
163 std::vector<int>
164 simdInt2Vector(const SimdInt32 simd)
166 GMX_ALIGNED(int, GMX_SIMD_REAL_WIDTH) mem[GMX_SIMD_REAL_WIDTH];
168 store(mem, simd);
169 std::vector<int> v(mem, mem+GMX_SIMD_REAL_WIDTH);
171 return v;
174 SimdInt32
175 vector2SimdInt(const std::vector<int> &v)
177 GMX_ALIGNED(int, GMX_SIMD_REAL_WIDTH) mem[GMX_SIMD_REAL_WIDTH];
179 for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
181 mem[i] = v[i % v.size()]; // repeat vector contents to fill simd width
183 return load(mem);
186 SimdInt32
187 setSimdIntFrom3I(int i0, int i1, int i2)
189 std::vector<int> v(3);
190 v[0] = i0;
191 v[1] = i1;
192 v[2] = i2;
193 return vector2SimdInt(v);
196 SimdInt32
197 setSimdIntFrom1I(int value)
199 std::vector<int> v(GMX_SIMD_REAL_WIDTH);
200 for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
202 v[i] = value;
204 return vector2SimdInt(v);
207 ::testing::AssertionResult
208 SimdTest::compareSimdInt32(const char * refExpr, const char * tstExpr,
209 const SimdInt32 ref, const SimdInt32 tst)
211 return compareVectorEq(refExpr, tstExpr, simdInt2Vector(ref), simdInt2Vector(tst));
214 #endif // GMX_SIMD_HAVE_REAL
216 /*! \} */
217 /*! \endcond */
219 } // namespace
220 } // namespace
222 #endif // GMX_SIMD