Remove unnecessary config.h includes
[alexxy/gromacs.git] / src / gromacs / simd / tests / simd_floatingpoint.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 #include "gmxpre.h"
36
37 #include <math.h>
38 #include "gromacs/math/utilities.h"
39
40 #include "simd.h"
41
42 namespace gmx
43 {
44 namespace test
45 {
46 namespace
47 {
48
49 /*! \cond internal */
50 /*! \addtogroup module_simd */
51 /*! \{ */
52
53 #ifdef GMX_SIMD_HAVE_REAL
54
55 /*! \brief Test fixture for floating-point tests (identical to the generic \ref SimdTest) */
56 typedef SimdTest SimdFloatingpointTest;
57
58 TEST_F(SimdFloatingpointTest, gmxSimdSetZeroR)
59 {
60     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(0.0), gmx_simd_setzero_r());
61 }
62
63 TEST_F(SimdFloatingpointTest, gmxSimdSet1R)
64 {
65     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(1.0), gmx_simd_set1_r(1.0));
66 }
67
68 TEST_F(SimdFloatingpointTest, gmxSimdLoad1R)
69 {
70     real r = 2.0;
71     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(r), gmx_simd_load1_r(&r));
72 }
73
74 TEST_F(SimdFloatingpointTest, gmxSimdAddR)
75 {
76     GMX_EXPECT_SIMD_REAL_EQ(rSimd_5_7_9,
77                             gmx_simd_add_r(rSimd_1_2_3, rSimd_4_5_6)); // 1+4=5, 2+5=7, 3+6=9
78 }
79
80 TEST_F(SimdFloatingpointTest, gmxSimdSubR)
81 {
82     GMX_EXPECT_SIMD_REAL_EQ(rSimd_4_5_6,
83                             gmx_simd_sub_r(rSimd_5_7_9, rSimd_1_2_3)); // 5-1=4, 7-2=5, 9-3=6
84 }
85
86 TEST_F(SimdFloatingpointTest, gmxSimdMulR)
87 {
88     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(4, 10, 18),
89                             gmx_simd_mul_r(rSimd_1_2_3, rSimd_4_5_6));
90 }
91
92 TEST_F(SimdFloatingpointTest, gmxSimdFmaddR)
93 {
94     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(11, 18, 27),
95                             gmx_simd_fmadd_r(rSimd_1_2_3, rSimd_4_5_6, rSimd_7_8_9)); // 1*4+7, etc.
96 }
97
98 TEST_F(SimdFloatingpointTest, gmxSimdFmsubR)
99 {
100     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(-3, 2, 9),
101                             gmx_simd_fmsub_r(rSimd_1_2_3, rSimd_4_5_6, rSimd_7_8_9)); // 1*4-7, etc.
102 }
103
104 TEST_F(SimdFloatingpointTest, gmxSimdFnmaddR)
105 {
106     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(3, -2, -9),
107                             gmx_simd_fnmadd_r(rSimd_1_2_3, rSimd_4_5_6, rSimd_7_8_9)); // -1*4+7, etc.
108 }
109
110 TEST_F(SimdFloatingpointTest, gmxSimdFnmsubR)
111 {
112     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(-11, -18, -27),
113                             gmx_simd_fnmsub_r(rSimd_1_2_3, rSimd_4_5_6, rSimd_7_8_9)); // -1*4-7, etc.
114 }
115
116 TEST_F(SimdFloatingpointTest, gmxSimdFabsR)
117 {
118     GMX_EXPECT_SIMD_REAL_EQ(rSimd_1_2_3, gmx_simd_fabs_r(rSimd_1_2_3));    // fabs(x)=x
119     GMX_EXPECT_SIMD_REAL_EQ(rSimd_1_2_3, gmx_simd_fabs_r(rSimd_m1_m2_m3)); // fabs(-x)=x
120 }
121
122 TEST_F(SimdFloatingpointTest, gmxSimdFnegR)
123 {
124     GMX_EXPECT_SIMD_REAL_EQ(rSimd_m1_m2_m3, gmx_simd_fneg_r(rSimd_1_2_3));    // fneg(x)=-x
125     GMX_EXPECT_SIMD_REAL_EQ(rSimd_1_2_3,    gmx_simd_fneg_r(rSimd_m1_m2_m3)); // fneg(-x)=x
126 }
127
128 #ifdef GMX_SIMD_HAVE_LOGICAL
129 /* 1.3333282470703125 has mantissa 0101010101010101 (followed by zeros)
130  * 1.79998779296875   has mantissa 1100110011001100 (followed by zeros)
131  * 1.26666259765625   has mantissa 0100010001000100 (followed by zeros)
132  * 1.8666534423828125 has mantissa 1101110111011101 (followed by zeros)
133  *
134  * Since all of them have the same exponent (2^0), the exponent will
135  * not change with AND or OR operations.
136  */
137 TEST_F(SimdFloatingpointTest, gmxSimdAndR)
138 {
139     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(1.26666259765625),
140                             gmx_simd_and_r(gmx_simd_set1_r(1.3333282470703125),
141                                            gmx_simd_set1_r(1.79998779296875)));
142 }
143
144 TEST_F(SimdFloatingpointTest, gmxSimdOrR)
145 {
146     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(1.8666534423828125),
147                             gmx_simd_or_r(gmx_simd_set1_r(1.3333282470703125),
148                                           gmx_simd_set1_r(1.79998779296875)));
149 }
150
151 TEST_F(SimdFloatingpointTest, gmxSimdXorR)
152 {
153     /* Test xor by taking xor with a number and its negative. This should result
154      * in only the sign bit being set. We then use this bit change the sign of
155      * different numbers.
156      */
157     gmx_simd_real_t signbit = gmx_simd_xor_r(gmx_simd_set1_r(1.5), gmx_simd_set1_r(-1.5));
158     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(-1, 2, -3), gmx_simd_xor_r(signbit, setSimdRealFrom3R(1, -2, 3)));
159 }
160
161 TEST_F(SimdFloatingpointTest, gmxSimdAndnotR)
162 {
163     /* Use xor (which we already tested, so fix that first if both tests fail)
164      * to extract the sign bit, and then use andnot to take absolute values.
165      */
166     gmx_simd_real_t signbit = gmx_simd_xor_r(gmx_simd_set1_r(1.5), gmx_simd_set1_r(-1.5));
167     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(1, 2, 3), gmx_simd_andnot_r(signbit, setSimdRealFrom3R(-1, 2, -3)));
168 }
169
170 #endif
171
172 TEST_F(SimdFloatingpointTest, gmxSimdMaxR)
173 {
174     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(3, 2, 4), gmx_simd_max_r(rSimd_1_2_3, rSimd_3_1_4));
175     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(3, 2, 4), gmx_simd_max_r(rSimd_3_1_4, rSimd_1_2_3));
176     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(-1, -1, -3), gmx_simd_max_r(rSimd_m1_m2_m3, rSimd_m3_m1_m4));
177     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(-1, -1, -3), gmx_simd_max_r(rSimd_m3_m1_m4, rSimd_m1_m2_m3));
178 }
179
180 TEST_F(SimdFloatingpointTest, gmxSimdMinR)
181 {
182     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(1, 1, 3), gmx_simd_min_r(rSimd_1_2_3, rSimd_3_1_4));
183     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(1, 1, 3), gmx_simd_min_r(rSimd_3_1_4, rSimd_1_2_3));
184     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(-3, -2, -4), gmx_simd_min_r(rSimd_m1_m2_m3, rSimd_m3_m1_m4));
185     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(-3, -2, -4), gmx_simd_min_r(rSimd_m3_m1_m4, rSimd_m1_m2_m3));
186 }
187
188 TEST_F(SimdFloatingpointTest, gmxSimdRoundR)
189 {
190     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(2), gmx_simd_round_r(gmx_simd_set1_r(2.25)));
191     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(4), gmx_simd_round_r(gmx_simd_set1_r(3.75)));
192     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(-2), gmx_simd_round_r(gmx_simd_set1_r(-2.25)));
193     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(-4), gmx_simd_round_r(gmx_simd_set1_r(-3.75)));
194 }
195
196 TEST_F(SimdFloatingpointTest, gmxSimdTruncR)
197 {
198     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(2), gmx_simd_trunc_r(rSimd_2p25));
199     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(3), gmx_simd_trunc_r(rSimd_3p75));
200     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(-2), gmx_simd_trunc_r(rSimd_m2p25));
201     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(-3), gmx_simd_trunc_r(rSimd_m3p75));
202 }
203
204 TEST_F(SimdFloatingpointTest, gmxSimdFractionR)
205 {
206     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(0.25), gmx_simd_fraction_r(rSimd_2p25));   // fract(2.25)=0.25
207     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(0.75), gmx_simd_fraction_r(rSimd_3p75));   // fract(3.75)=0.75
208     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(-0.25), gmx_simd_fraction_r(rSimd_m2p25)); // fract(-2.25)=-0.25
209     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(-0.75), gmx_simd_fraction_r(rSimd_m3p75)); // fract(-3.75)=-0.75
210 }
211
212 // We explicitly test the exponent/mantissa routines with double precision data,
213 // since these usually rely on direct manipulation and shift of the SIMD registers,
214 // where it is easy to make mistakes with single vs double precision.
215
216 TEST_F(SimdFloatingpointTest, gmxSimdGetExponentR)
217 {
218     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(60.0, -41.0, 54.0), gmx_simd_get_exponent_r(rSimd_Exp));
219 #if (defined GMX_SIMD_HAVE_DOUBLE) && (defined GMX_DOUBLE)
220     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(587.0, -462.0, 672.0), gmx_simd_get_exponent_r(rSimd_ExpDouble));
221 #endif
222 }
223
224 TEST_F(SimdFloatingpointTest, gmxSimdGetMantissaR)
225 {
226     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(1.219097320577810839026256,
227                                               1.166738027848349235071623,
228                                               1.168904015004464724825084), gmx_simd_get_mantissa_r(rSimd_Exp));
229 #if (defined GMX_SIMD_HAVE_DOUBLE) && (defined GMX_DOUBLE)
230     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(1.241261238952345623563251,
231                                               1.047294723759123852359232,
232                                               1.856066204750275957395734), gmx_simd_get_mantissa_r(rSimd_ExpDouble));
233 #endif
234 }
235
236 TEST_F(SimdFloatingpointTest, gmxSimdSetExponentR)
237 {
238     gmx_simd_real_t x0 = setSimdRealFrom3R(0.5, 11.5, 99.5);
239     gmx_simd_real_t x1 = setSimdRealFrom3R(-0.5, -11.5, -99.5);
240
241     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(pow(2.0, 60.0), pow(2.0, -41.0), pow(2.0, 54.0)),
242                             gmx_simd_set_exponent_r(setSimdRealFrom3R(60.0, -41.0, 54.0)));
243 #if (defined GMX_SIMD_HAVE_DOUBLE) && (defined GMX_DOUBLE)
244     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(pow(2.0, 587.0), pow(2.0, -462.0), pow(2.0, 672.0)),
245                             gmx_simd_set_exponent_r(setSimdRealFrom3R(587.0, -462.0, 672.0)));
246 #endif
247     /* Rounding mode in gmx_simd_set_exponent_r() must be consistent with gmx_simd_round_r() */
248     GMX_EXPECT_SIMD_REAL_EQ(gmx_simd_set_exponent_r(gmx_simd_round_r(x0)), gmx_simd_set_exponent_r(x0));
249     GMX_EXPECT_SIMD_REAL_EQ(gmx_simd_set_exponent_r(gmx_simd_round_r(x1)), gmx_simd_set_exponent_r(x1));
250 }
251
252 /*
253  * We do extensive 1/sqrt(x) and 1/x accuracy testing in the math module, so
254  * we just make sure the lookup instructions appear to work here
255  */
256
257 TEST_F(SimdFloatingpointTest, gmxSimdRsqrtR)
258 {
259     gmx_simd_real_t x      = setSimdRealFrom3R(4.0, M_PI, 1234567890.0);
260     gmx_simd_real_t ref    = setSimdRealFrom3R(0.5, 1.0/sqrt(M_PI), 1.0/sqrt(1234567890.0));
261
262     /* Set the allowed ulp error as 2 to the power of the number of bits in
263      * the mantissa that do not have to be correct after the table lookup.
264      */
265     setUlpTol(1LL << (std::numeric_limits<real>::digits-GMX_SIMD_RSQRT_BITS));
266
267     GMX_EXPECT_SIMD_REAL_NEAR(ref, gmx_simd_rsqrt_r(x));
268 }
269
270 TEST_F(SimdFloatingpointTest, gmxSimdRcpR)
271 {
272     gmx_simd_real_t x      = setSimdRealFrom3R(4.0, M_PI, 1234567890.0);
273     gmx_simd_real_t ref    = setSimdRealFrom3R(0.25, 1.0/M_PI, 1.0/1234567890.0);
274
275     /* Set the allowed ulp error as 2 to the power of the number of bits in
276      * the mantissa that do not have to be correct after the table lookup.
277      */
278     setUlpTol(1LL << (std::numeric_limits<real>::digits-GMX_SIMD_RCP_BITS));
279
280     GMX_EXPECT_SIMD_REAL_NEAR(ref, gmx_simd_rcp_r(x));
281 }
282
283 TEST_F(SimdFloatingpointTest, gmxSimdBoolCmpEqAndBlendZeroR)
284 {
285     gmx_simd_bool_t eq   = gmx_simd_cmpeq_r(rSimd_5_7_9, rSimd_7_8_9);
286     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(0, 0, 3), gmx_simd_blendzero_r(rSimd_1_2_3, eq));
287 }
288
289 TEST_F(SimdFloatingpointTest, gmxSimdBlendNotZeroR)
290 {
291     gmx_simd_bool_t eq   = gmx_simd_cmpeq_r(rSimd_5_7_9, rSimd_7_8_9);
292     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(1, 2, 0), gmx_simd_blendnotzero_r(rSimd_1_2_3, eq));
293 }
294
295 TEST_F(SimdFloatingpointTest, gmxSimdBoolCmpLER)
296 {
297     gmx_simd_bool_t le   = gmx_simd_cmple_r(rSimd_5_7_9, rSimd_7_8_9);
298     GMX_EXPECT_SIMD_REAL_EQ(rSimd_1_2_3, gmx_simd_blendzero_r(rSimd_1_2_3, le));
299 }
300
301 TEST_F(SimdFloatingpointTest, gmxSimdBoolCmpLTR)
302 {
303     gmx_simd_bool_t lt   = gmx_simd_cmplt_r(rSimd_5_7_9, rSimd_7_8_9);
304     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(1, 2, 0), gmx_simd_blendzero_r(rSimd_1_2_3, lt));
305 }
306
307 TEST_F(SimdFloatingpointTest, gmxSimdBoolAndB)
308 {
309     gmx_simd_bool_t eq   = gmx_simd_cmpeq_r(rSimd_5_7_9, rSimd_7_8_9);
310     gmx_simd_bool_t le   = gmx_simd_cmple_r(rSimd_5_7_9, rSimd_7_8_9);
311     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(0, 0, 3), gmx_simd_blendzero_r(rSimd_1_2_3, gmx_simd_and_b(eq, le)));
312 }
313
314 TEST_F(SimdFloatingpointTest, gmxSimdBoolOrB)
315 {
316     gmx_simd_bool_t eq   = gmx_simd_cmpeq_r(rSimd_5_7_9, rSimd_7_8_9);
317     gmx_simd_bool_t lt   = gmx_simd_cmplt_r(rSimd_5_7_9, rSimd_7_8_9);
318     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(1, 2, 3), gmx_simd_blendzero_r(rSimd_1_2_3, gmx_simd_or_b(eq, lt)));
319 }
320
321 TEST_F(SimdFloatingpointTest, gmxSimdAnytrueB)
322 {
323     gmx_simd_bool_t eq;
324
325     /* this test is a bit tricky since we don't know the simd width.
326      * We cannot check for truth values for "any" element beyond the first,
327      * since that part of the data will not be used if simd width is 1.
328      */
329     eq = gmx_simd_cmpeq_r(rSimd_5_7_9, setSimdRealFrom3R(5, 0, 0));
330     EXPECT_NE(0, gmx_simd_anytrue_b(eq));
331
332     eq = gmx_simd_cmpeq_r(rSimd_1_2_3, rSimd_4_5_6);
333     EXPECT_EQ(0, gmx_simd_anytrue_b(eq));
334 }
335
336 TEST_F(SimdFloatingpointTest, gmxSimdBlendvR)
337 {
338     gmx_simd_bool_t lt   = gmx_simd_cmplt_r(rSimd_5_7_9, rSimd_7_8_9);
339     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(4, 5, 3), gmx_simd_blendv_r(rSimd_1_2_3, rSimd_4_5_6, lt));
340 }
341
342 TEST_F(SimdFloatingpointTest, gmxSimdReduceR)
343 {
344     // The horizontal sum of the SIMD variable depends on the width, so
345     // simply store it an extra time and calculate what the sum should be
346     std::vector<real> v   = simdReal2Vector(rSimd_4_5_6);
347     real              sum = 0.0;
348
349     for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
350     {
351         sum += v[i];
352     }
353
354     EXPECT_EQ(sum, gmx_simd_reduce_r(rSimd_4_5_6));
355 }
356
357 #endif      // GMX_SIMD_HAVE_REAL
358
359 /*! \} */
360 /*! \endcond */
361
362 }      // namespace
363 }      // namespace
364 }      // namespace