Merge release-4-6 into master
[alexxy/gromacs.git] / src / gromacs / simd / tests / simd.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2014, 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 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include "simd.h"
40
41 namespace gmx
42 {
43 namespace test
44 {
45
46 /*! \cond internal */
47 /*! \addtogroup module_simd */
48 /*! \{ */
49
50 /* Unfortunately we cannot keep static SIMD constants in the test fixture class.
51  * The problem is that SIMD memory need to be aligned, and in particular
52  * this applies to automatic storage of variables in classes. For SSE registers
53  * this means 16-byte alignment (which seems to work), but AVX requires 32-bit
54  * alignment. At least both gcc-4.7.3 and Apple clang-5.0 (OS X 10.9) fail to
55  * align these variables when they are stored as data in a class.
56  *
57  * In theory we could set some of these on-the-fly e.g. with setSimdRealFrom3R()
58  * instead (although that would mean repeating code between tests), but many of
59  * the constants depend on the current precision not to mention they
60  * occasionally have many digits that need to be exactly right, and keeping
61  * them in a single place makes sure they are consistent.
62  */
63 #ifdef GMX_SIMD_HAVE_REAL
64 const gmx_simd_real_t rSimd_1_2_3    = setSimdRealFrom3R(1, 2, 3);
65 const gmx_simd_real_t rSimd_4_5_6    = setSimdRealFrom3R(4, 5, 6);
66 const gmx_simd_real_t rSimd_7_8_9    = setSimdRealFrom3R(7, 8, 9);
67 const gmx_simd_real_t rSimd_5_7_9    = setSimdRealFrom3R(5, 7, 9);
68 const gmx_simd_real_t rSimd_m1_m2_m3 = setSimdRealFrom3R(-1, -2, -3);
69 const gmx_simd_real_t rSimd_3_1_4    = setSimdRealFrom3R(3, 1, 4);
70 const gmx_simd_real_t rSimd_m3_m1_m4 = setSimdRealFrom3R(-3, -1, -4);
71 const gmx_simd_real_t rSimd_2p25     = setSimdRealFrom1R(2.25);
72 const gmx_simd_real_t rSimd_3p75     = setSimdRealFrom1R(3.75);
73 const gmx_simd_real_t rSimd_m2p25    = setSimdRealFrom1R(-2.25);
74 const gmx_simd_real_t rSimd_m3p75    = setSimdRealFrom1R(-3.75);
75 const gmx_simd_real_t rSimd_Exp      = setSimdRealFrom3R( 1.4055235171027452623914516e+18,
76                                                           5.3057102734253445623914516e-13,
77                                                           -2.1057102745623934534514516e+16);
78 #    if (defined GMX_SIMD_HAVE_DOUBLE) && (defined GMX_DOUBLE)
79 // Make sure we also test exponents outside single precision when we use double
80 const gmx_simd_real_t rSimd_ExpDouble = setSimdRealFrom3R( 6.287393598732017379054414e+176,
81                                                            8.794495252903116023030553e-140,
82                                                            -3.637060701570496477655022e+202);
83 // Magic FP numbers corresponding to specific bit patterns
84 const gmx_simd_real_t rSimd_Bits1    = setSimdRealFrom1R(-1.07730874267432137e+236);
85 const gmx_simd_real_t rSimd_Bits2    = setSimdRealFrom1R(-9.25596313493178307e+061);
86 const gmx_simd_real_t rSimd_Bits3    = setSimdRealFrom1R(-8.57750588235293981e+003);
87 const gmx_simd_real_t rSimd_Bits4    = setSimdRealFrom1R( 1.22416778341839096e-250);
88 const gmx_simd_real_t rSimd_Bits5    = setSimdRealFrom1R(-1.15711777004554095e+294);
89 const gmx_simd_real_t rSimd_Bits6    = setSimdRealFrom1R( 1.53063836115600621e-018);
90 #    else
91 // Magic FP numbers corresponding to specific bit patterns
92 const gmx_simd_real_t rSimd_Bits1    = setSimdRealFrom1R(-5.9654142337e+29);
93 const gmx_simd_real_t rSimd_Bits2    = setSimdRealFrom1R(-1.0737417600e+08);
94 const gmx_simd_real_t rSimd_Bits3    = setSimdRealFrom1R(-6.0235290527e+00);
95 const gmx_simd_real_t rSimd_Bits4    = setSimdRealFrom1R( 1.0788832913e-31);
96 const gmx_simd_real_t rSimd_Bits5    = setSimdRealFrom1R(-1.0508719529e+37);
97 const gmx_simd_real_t rSimd_Bits6    = setSimdRealFrom1R( 1.1488970369e-02);
98 #    endif
99 #endif  // GMX_SIMD_HAVE_REAL
100 #ifdef GMX_SIMD_HAVE_INT32
101 const gmx_simd_int32_t iSimd_1_2_3      = setSimdIntFrom3I(1, 2, 3);
102 const gmx_simd_int32_t iSimd_4_5_6      = setSimdIntFrom3I(4, 5, 6);
103 const gmx_simd_int32_t iSimd_7_8_9      = setSimdIntFrom3I(7, 8, 9);
104 const gmx_simd_int32_t iSimd_5_7_9      = setSimdIntFrom3I(5, 7, 9);
105 const gmx_simd_int32_t iSimd_1M_2M_3M   = setSimdIntFrom3I(1000000, 2000000, 3000000);
106 const gmx_simd_int32_t iSimd_4M_5M_6M   = setSimdIntFrom3I(4000000, 5000000, 6000000);
107 const gmx_simd_int32_t iSimd_5M_7M_9M   = setSimdIntFrom3I(5000000, 7000000, 9000000);
108 const gmx_simd_int32_t iSimd_0xF0F0F0F0 = setSimdIntFrom1I(0xF0F0F0F0);
109 const gmx_simd_int32_t iSimd_0xCCCCCCCC = setSimdIntFrom1I(0xCCCCCCCC);
110 #endif  // GMX_SIMD_HAVE_INT32
111
112 #ifdef GMX_SIMD_HAVE_REAL
113 ::std::vector<real>
114 simdReal2Vector(const gmx_simd_real_t simd)
115 {
116     real                mem[GMX_SIMD_REAL_WIDTH*2];
117     real *              p = gmx_simd_align_r(mem);
118
119     gmx_simd_store_r(p, simd);
120     std::vector<real>   v(p, p+GMX_SIMD_REAL_WIDTH);
121
122     return v;
123 }
124
125 gmx_simd_real_t
126 vector2SimdReal(const std::vector<real> &v)
127 {
128     real                mem[GMX_SIMD_REAL_WIDTH*2];
129     real *              p = gmx_simd_align_r(mem);
130
131     for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
132     {
133         p[i] = v[i % v.size()];  // repeat vector contents to fill simd width
134     }
135     return gmx_simd_load_r(p);
136 }
137
138 gmx_simd_real_t
139 setSimdRealFrom3R(real r0, real r1, real r2)
140 {
141     std::vector<real> v(3);
142     v[0] = r0;
143     v[1] = r1;
144     v[2] = r2;
145     return vector2SimdReal(v);
146 }
147
148 gmx_simd_real_t
149 setSimdRealFrom1R(real value)
150 {
151     std::vector<real> v(GMX_SIMD_REAL_WIDTH);
152     for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
153     {
154         v[i] = value;
155     }
156     return vector2SimdReal(v);
157 }
158
159 testing::AssertionResult
160 SimdTest::compareSimdRealUlp(const char *  refExpr,     const char *  tstExpr,
161                              const gmx_simd_real_t ref, const gmx_simd_real_t tst)
162 {
163     return compareVectorRealUlp(refExpr, tstExpr, simdReal2Vector(ref), simdReal2Vector(tst));
164 }
165
166 testing::AssertionResult
167 SimdTest::compareSimdRealEq(const char * refExpr, const char * tstExpr,
168                             const gmx_simd_real_t ref, const gmx_simd_real_t tst)
169 {
170     return compareVectorEq(refExpr, tstExpr, simdReal2Vector(ref), simdReal2Vector(tst));
171 }
172
173 #endif  // GMX_SIMD_HAVE_REAL
174
175 #ifdef GMX_SIMD_HAVE_INT32
176 std::vector<int>
177 simdInt2Vector(const gmx_simd_int32_t simd)
178 {
179     int                 mem[GMX_SIMD_INT32_WIDTH*2];
180     int *               p = gmx_simd_align_i(mem);
181
182     gmx_simd_store_i(p, simd);
183     std::vector<int>    v(p, p+GMX_SIMD_INT32_WIDTH);
184
185     return v;
186 }
187
188 gmx_simd_int32_t
189 vector2SimdInt(const std::vector<int> &v)
190 {
191     int                 mem[GMX_SIMD_INT32_WIDTH*2];
192     int *               p = gmx_simd_align_i(mem);
193
194     for (int i = 0; i < GMX_SIMD_INT32_WIDTH; i++)
195     {
196         p[i] = v[i % v.size()];  // repeat vector contents to fill simd width
197     }
198     return gmx_simd_load_i(p);
199 }
200
201 gmx_simd_int32_t
202 setSimdIntFrom3I(int i0, int i1, int i2)
203 {
204     std::vector<int> v(3);
205     v[0] = i0;
206     v[1] = i1;
207     v[2] = i2;
208     return vector2SimdInt(v);
209 }
210
211 gmx_simd_int32_t
212 setSimdIntFrom1I(int value)
213 {
214     std::vector<int> v(GMX_SIMD_INT32_WIDTH);
215     for (int i = 0; i < GMX_SIMD_INT32_WIDTH; i++)
216     {
217         v[i] = value;
218     }
219     return vector2SimdInt(v);
220 }
221
222 ::testing::AssertionResult
223 SimdTest::compareSimdInt32(const char *  refExpr,      const char *  tstExpr,
224                            const gmx_simd_int32_t ref, const gmx_simd_int32_t tst)
225 {
226     return compareVectorEq(refExpr, tstExpr, simdInt2Vector(ref), simdInt2Vector(tst));
227 }
228
229 #endif  // GMX_SIMD_HAVE_INT32
230
231 /*! \} */
232 /*! \endcond */
233
234 }      // namespace
235 }      // namespace