5d7a92251e589bd64cdb7da83d36c14fb51f45e1
[alexxy/gromacs.git] / src / gromacs / simd / tests / simd4_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 "config.h"
38
39 #include <math.h>
40 #include "gromacs/math/utilities.h"
41
42 #include "simd4.h"
43
44 namespace gmx
45 {
46 namespace test
47 {
48 namespace
49 {
50
51 /*! \cond internal */
52 /*! \addtogroup module_simd */
53 /*! \{ */
54
55 #ifdef GMX_SIMD4_HAVE_REAL
56
57 /*! \brief Test fixture for SIMD4 floating-point operations (identical to the SIMD4 \ref Simd4Test) */
58 typedef Simd4Test Simd4FloatingpointTest;
59
60 TEST_F(Simd4FloatingpointTest, gmxSimd4SetZeroR)
61 {
62     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(0.0), gmx_simd4_setzero_r());
63 }
64
65 TEST_F(Simd4FloatingpointTest, gmxSimd4Set1R)
66 {
67     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(1.0), gmx_simd4_set1_r(1.0));
68 }
69
70 TEST_F(Simd4FloatingpointTest, gmxSimd4Load1R)
71 {
72     real r = 2.0;
73     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(r), gmx_simd4_load1_r(&r));
74 }
75
76 TEST_F(Simd4FloatingpointTest, gmxSimd4AddR)
77 {
78     GMX_EXPECT_SIMD4_REAL_EQ(rSimd4_5_7_9, gmx_simd4_add_r(rSimd4_1_2_3, rSimd4_4_5_6)); // 1+4=5, 2+5=7, 3+6=9
79 }
80
81 TEST_F(Simd4FloatingpointTest, gmxSimd4SubR)
82 {
83     GMX_EXPECT_SIMD4_REAL_EQ(rSimd4_4_5_6, gmx_simd4_sub_r(rSimd4_5_7_9, rSimd4_1_2_3)); // 5-1=4, 7-2=5, 9-3=6
84 }
85
86 TEST_F(Simd4FloatingpointTest, gmxSimd4MulR)
87 {
88     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(4, 10, 18), gmx_simd4_mul_r(rSimd4_1_2_3, rSimd4_4_5_6));
89 }
90
91 TEST_F(Simd4FloatingpointTest, gmxSimd4FmaddR)
92 {
93     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(11, 18, 27), gmx_simd4_fmadd_r(rSimd4_1_2_3, rSimd4_4_5_6, rSimd4_7_8_9)); // 1*4+7, etc.
94 }
95
96 TEST_F(Simd4FloatingpointTest, gmxSimd4FmsubR)
97 {
98     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(-3, 2, 9), gmx_simd4_fmsub_r(rSimd4_1_2_3, rSimd4_4_5_6, rSimd4_7_8_9)); // 1*4-7, etc.
99 }
100
101 TEST_F(Simd4FloatingpointTest, gmxSimd4FnmaddR)
102 {
103     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(3, -2, -9), gmx_simd4_fnmadd_r(rSimd4_1_2_3, rSimd4_4_5_6, rSimd4_7_8_9)); // -1*4+7, etc.
104 }
105
106 TEST_F(Simd4FloatingpointTest, gmxSimd4FnmsubR)
107 {
108     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(-11, -18, -27), gmx_simd4_fnmsub_r(rSimd4_1_2_3, rSimd4_4_5_6, rSimd4_7_8_9)); // -1*4-7, etc.
109 }
110
111 TEST_F(Simd4FloatingpointTest, gmxSimd4FabsR)
112 {
113     GMX_EXPECT_SIMD4_REAL_EQ(rSimd4_1_2_3, gmx_simd4_fabs_r(rSimd4_1_2_3));    // fabs(x)=x
114     GMX_EXPECT_SIMD4_REAL_EQ(rSimd4_1_2_3, gmx_simd4_fabs_r(rSimd4_m1_m2_m3)); // fabs(-x)=x
115 }
116
117 TEST_F(Simd4FloatingpointTest, gmxSimd4FnegR)
118 {
119     GMX_EXPECT_SIMD4_REAL_EQ(rSimd4_m1_m2_m3, gmx_simd4_fneg_r(rSimd4_1_2_3));   // fneg(x)=-x
120     GMX_EXPECT_SIMD4_REAL_EQ(rSimd4_1_2_3,   gmx_simd4_fneg_r(rSimd4_m1_m2_m3)); // fneg(-x)=x
121 }
122
123 #ifdef GMX_SIMD4_HAVE_LOGICAL
124 /* 1.3333282470703125 has mantissa 0101010101010101 (followed by zeros)
125  * 1.79998779296875   has mantissa 1100110011001100 (followed by zeros)
126  * 1.26666259765625   has mantissa 0100010001000100 (followed by zeros)
127  * 1.8666534423828125 has mantissa 1101110111011101 (followed by zeros)
128  *
129  * Since all of them have the same exponent (2^0), the exponent will
130  * not change with AND or OR operations.
131  */
132 TEST_F(Simd4FloatingpointTest, gmxSimd4AndR)
133 {
134     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(1.26666259765625),
135                              gmx_simd4_and_r(gmx_simd4_set1_r(1.3333282470703125),
136                                              gmx_simd4_set1_r(1.79998779296875)));
137 }
138
139 TEST_F(Simd4FloatingpointTest, gmxSimd4OrR)
140 {
141     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(1.8666534423828125),
142                              gmx_simd4_or_r(gmx_simd4_set1_r(1.3333282470703125),
143                                             gmx_simd4_set1_r(1.79998779296875)));
144 }
145
146 TEST_F(Simd4FloatingpointTest, gmxSimd4XorR)
147 {
148     /* Test xor by taking xor with a number and its negative. This should result
149      * in only the sign bit being set. We then use this bit change the sign of
150      * different numbers.
151      */
152     gmx_simd4_real_t signbit = gmx_simd4_xor_r(gmx_simd4_set1_r(1.5), gmx_simd4_set1_r(-1.5));
153     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(-1, 2, -3), gmx_simd4_xor_r(signbit, setSimd4RealFrom3R(1, -2, 3)));
154 }
155
156 TEST_F(Simd4FloatingpointTest, gmxSimd4AndnotR)
157 {
158     /* Use xor (which we already tested, so fix that first if both tests fail)
159      * to extract the sign bit, and then use andnot to take absolute values.
160      */
161     gmx_simd4_real_t signbit = gmx_simd4_xor_r(gmx_simd4_set1_r(1.5), gmx_simd4_set1_r(-1.5));
162     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(1, 2, 3), gmx_simd4_andnot_r(signbit, setSimd4RealFrom3R(-1, 2, -3)));
163 }
164
165 #endif
166
167 TEST_F(Simd4FloatingpointTest, gmxSimd4MaxR)
168 {
169     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(3, 2, 4), gmx_simd4_max_r(rSimd4_1_2_3, rSimd4_3_1_4));
170     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(3, 2, 4), gmx_simd4_max_r(rSimd4_3_1_4, rSimd4_1_2_3));
171     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(-1, -1, -3), gmx_simd4_max_r(rSimd4_m1_m2_m3, rSimd4_m3_m1_m4));
172     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(-1, -1, -3), gmx_simd4_max_r(rSimd4_m3_m1_m4, rSimd4_m1_m2_m3));
173 }
174
175 TEST_F(Simd4FloatingpointTest, gmxSimd4MinR)
176 {
177     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(1, 1, 3), gmx_simd4_min_r(rSimd4_1_2_3, rSimd4_3_1_4));
178     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(1, 1, 3), gmx_simd4_min_r(rSimd4_3_1_4, rSimd4_1_2_3));
179     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(-3, -2, -4), gmx_simd4_min_r(rSimd4_m1_m2_m3, rSimd4_m3_m1_m4));
180     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(-3, -2, -4), gmx_simd4_min_r(rSimd4_m3_m1_m4, rSimd4_m1_m2_m3));
181 }
182
183 TEST_F(Simd4FloatingpointTest, gmxSimd4RoundR)
184 {
185     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(2), gmx_simd4_round_r(gmx_simd4_set1_r(2.25)));
186     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(4), gmx_simd4_round_r(gmx_simd4_set1_r(3.75)));
187     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(-2), gmx_simd4_round_r(gmx_simd4_set1_r(-2.25)));
188     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(-4), gmx_simd4_round_r(gmx_simd4_set1_r(-3.75)));
189 }
190
191 TEST_F(Simd4FloatingpointTest, gmxSimd4TruncR)
192 {
193     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(2), gmx_simd4_trunc_r(rSimd4_2p25));
194     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(3), gmx_simd4_trunc_r(rSimd4_3p75));
195     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(-2), gmx_simd4_trunc_r(rSimd4_m2p25));
196     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(-3), gmx_simd4_trunc_r(rSimd4_m3p75));
197 }
198
199 /* We do extensive 1/sqrt(x) and 1/x accuracy testing in the tests for
200  * the SIMD math functions, so we just make sure the lookup instructions
201  * appear to work for a few values here.
202  */
203 TEST_F(Simd4FloatingpointTest, gmxSimd4RsqrtR)
204 {
205     gmx_simd4_real_t x      = setSimd4RealFrom3R(4.0, M_PI, 1234567890.0);
206     gmx_simd4_real_t ref    = setSimd4RealFrom3R(0.5, 1.0/sqrt(M_PI), 1.0/sqrt(1234567890.0));
207
208     // The allowed Ulp deviation is 2 to the power of the number of mantissa
209     // digits, minus the number of bits provided by the table lookup
210     setUlpTol(1LL << (std::numeric_limits<real>::digits-GMX_SIMD_RSQRT_BITS));
211     GMX_EXPECT_SIMD4_REAL_NEAR(ref, gmx_simd4_rsqrt_r(x));
212 }
213
214 TEST_F(Simd4FloatingpointTest, gmxSimd4BoolCmpEqAndBlendZeroR)
215 {
216     gmx_simd4_bool_t eq   = gmx_simd4_cmpeq_r(rSimd4_5_7_9, rSimd4_7_8_9);
217     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(0, 0, 3), gmx_simd4_blendzero_r(rSimd4_1_2_3, eq));
218 }
219
220 TEST_F(Simd4FloatingpointTest, gmxSimd4BlendNotZeroR)
221 {
222     gmx_simd4_bool_t eq   = gmx_simd4_cmpeq_r(rSimd4_5_7_9, rSimd4_7_8_9);
223     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(1, 2, 0), gmx_simd4_blendnotzero_r(rSimd4_1_2_3, eq));
224 }
225
226 TEST_F(Simd4FloatingpointTest, gmxSimd4BoolCmpLER)
227 {
228     gmx_simd4_bool_t le   = gmx_simd4_cmple_r(rSimd4_5_7_9, rSimd4_7_8_9);
229     GMX_EXPECT_SIMD4_REAL_EQ(rSimd4_1_2_3, gmx_simd4_blendzero_r(rSimd4_1_2_3, le));
230 }
231
232 TEST_F(Simd4FloatingpointTest, gmxSimd4BoolCmpLTR)
233 {
234     gmx_simd4_bool_t lt   = gmx_simd4_cmplt_r(rSimd4_5_7_9, rSimd4_7_8_9);
235     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(1, 2, 0), gmx_simd4_blendzero_r(rSimd4_1_2_3, lt));
236 }
237
238 TEST_F(Simd4FloatingpointTest, gmxSimd4BoolAndB)
239 {
240     gmx_simd4_bool_t eq   = gmx_simd4_cmpeq_r(rSimd4_5_7_9, rSimd4_7_8_9);
241     gmx_simd4_bool_t le   = gmx_simd4_cmple_r(rSimd4_5_7_9, rSimd4_7_8_9);
242     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(0, 0, 3), gmx_simd4_blendzero_r(rSimd4_1_2_3, gmx_simd4_and_b(eq, le)));
243 }
244
245 TEST_F(Simd4FloatingpointTest, gmxSimd4BoolOrB)
246 {
247     gmx_simd4_bool_t eq   = gmx_simd4_cmpeq_r(rSimd4_5_7_9, rSimd4_7_8_9);
248     gmx_simd4_bool_t lt   = gmx_simd4_cmplt_r(rSimd4_5_7_9, rSimd4_7_8_9);
249     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(1, 2, 3), gmx_simd4_blendzero_r(rSimd4_1_2_3, gmx_simd4_or_b(eq, lt)));
250 }
251
252 TEST_F(Simd4FloatingpointTest, gmxSimd4AnytrueB)
253 {
254     gmx_simd4_bool_t eq;
255
256     /* this test is a bit tricky since we don't know the simd width.
257      * We cannot check for truth values for "any" element beyond the first,
258      * since that part of the data will not be used if simd width is 1.
259      */
260     eq = gmx_simd4_cmpeq_r(rSimd4_5_7_9, setSimd4RealFrom3R(5, 0, 0));
261     EXPECT_NE(0, gmx_simd4_anytrue_b(eq));
262
263     eq = gmx_simd4_cmpeq_r(rSimd4_1_2_3, rSimd4_4_5_6);
264     EXPECT_EQ(0, gmx_simd4_anytrue_b(eq));
265 }
266
267 TEST_F(Simd4FloatingpointTest, gmxSimd4BlendvR)
268 {
269     gmx_simd4_bool_t lt   = gmx_simd4_cmplt_r(rSimd4_5_7_9, rSimd4_7_8_9);
270     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(4, 5, 3), gmx_simd4_blendv_r(rSimd4_1_2_3, rSimd4_4_5_6, lt));
271 }
272
273 TEST_F(Simd4FloatingpointTest, gmxSimd4ReduceR)
274 {
275     // The horizontal sum of the SIMD variable depends on the width, so
276     // simply store it an extra time and calculate what the sum should be
277     std::vector<real> v   = simd4Real2Vector(rSimd4_1_2_3);
278     real              sum = 0.0;
279
280     for (int i = 0; i < GMX_SIMD4_WIDTH; i++)
281     {
282         sum += v[i];
283     }
284
285     EXPECT_EQ(sum, gmx_simd4_reduce_r(rSimd4_1_2_3));
286 }
287
288
289 TEST_F(Simd4FloatingpointTest, gmxSimd4Dotproduct3R)
290 {
291     gmx_simd4_real_t v1 = setSimd4RealFrom3R(1, 4, 5);
292     gmx_simd4_real_t v2 = setSimd4RealFrom3R(3, 8, 2);
293 #    ifdef GMX_DOUBLE
294     EXPECT_DOUBLE_EQ(45.0, gmx_simd4_dotproduct3_r(v1, v2));
295 #    else
296     EXPECT_FLOAT_EQ(45.0, gmx_simd4_dotproduct3_r(v1, v2));
297 #    endif
298 }
299
300 #endif      // GMX_SIMD4_HAVE_REAL
301
302 /*! \} */
303 /*! \endcond */
304
305 }      // namespace
306 }      // namespace
307 }      // namespace