Merge release-5-0 into master
[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 "config.h"
36
37 #include <math.h>
38 #include "gromacs/math/utilities.h"
39
40 #include "simd4.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_SIMD4_HAVE_REAL
54
55 /*! \brief Test fixture for SIMD4 floating-point operations (identical to the SIMD4 \ref Simd4Test) */
56 typedef Simd4Test Simd4FloatingpointTest;
57
58 TEST_F(Simd4FloatingpointTest, gmxSimd4SetZeroR)
59 {
60     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(0.0), gmx_simd4_setzero_r());
61 }
62
63 TEST_F(Simd4FloatingpointTest, gmxSimd4Set1R)
64 {
65     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(1.0), gmx_simd4_set1_r(1.0));
66 }
67
68 TEST_F(Simd4FloatingpointTest, gmxSimd4Load1R)
69 {
70     real r = 2.0;
71     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(r), gmx_simd4_load1_r(&r));
72 }
73
74 TEST_F(Simd4FloatingpointTest, gmxSimd4AddR)
75 {
76     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
77 }
78
79 TEST_F(Simd4FloatingpointTest, gmxSimd4SubR)
80 {
81     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
82 }
83
84 TEST_F(Simd4FloatingpointTest, gmxSimd4MulR)
85 {
86     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(4, 10, 18), gmx_simd4_mul_r(rSimd4_1_2_3, rSimd4_4_5_6));
87 }
88
89 TEST_F(Simd4FloatingpointTest, gmxSimd4FmaddR)
90 {
91     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.
92 }
93
94 TEST_F(Simd4FloatingpointTest, gmxSimd4FmsubR)
95 {
96     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.
97 }
98
99 TEST_F(Simd4FloatingpointTest, gmxSimd4FnmaddR)
100 {
101     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.
102 }
103
104 TEST_F(Simd4FloatingpointTest, gmxSimd4FnmsubR)
105 {
106     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.
107 }
108
109 TEST_F(Simd4FloatingpointTest, gmxSimd4FabsR)
110 {
111     GMX_EXPECT_SIMD4_REAL_EQ(rSimd4_1_2_3, gmx_simd4_fabs_r(rSimd4_1_2_3));    // fabs(x)=x
112     GMX_EXPECT_SIMD4_REAL_EQ(rSimd4_1_2_3, gmx_simd4_fabs_r(rSimd4_m1_m2_m3)); // fabs(-x)=x
113 }
114
115 TEST_F(Simd4FloatingpointTest, gmxSimd4FnegR)
116 {
117     GMX_EXPECT_SIMD4_REAL_EQ(rSimd4_m1_m2_m3, gmx_simd4_fneg_r(rSimd4_1_2_3));   // fneg(x)=-x
118     GMX_EXPECT_SIMD4_REAL_EQ(rSimd4_1_2_3,   gmx_simd4_fneg_r(rSimd4_m1_m2_m3)); // fneg(-x)=x
119 }
120
121 #ifdef GMX_SIMD4_HAVE_LOGICAL
122 TEST_F(Simd4FloatingpointTest, gmxSimd4AndR)
123 {
124     GMX_EXPECT_SIMD4_REAL_EQ(rSimd4_Bits3, gmx_simd4_and_r(rSimd4_Bits1, rSimd4_Bits2)); // Bits1 & Bits2 = Bits3
125 }
126
127 TEST_F(Simd4FloatingpointTest, gmxSimd4AndnotR)
128 {
129     GMX_EXPECT_SIMD4_REAL_EQ(rSimd4_Bits4, gmx_simd4_andnot_r(rSimd4_Bits1, rSimd4_Bits2)); // (~Bits1) & Bits2 = Bits3
130 }
131
132 TEST_F(Simd4FloatingpointTest, gmxSimd4OrR)
133 {
134     GMX_EXPECT_SIMD4_REAL_EQ(rSimd4_Bits5, gmx_simd4_or_r(rSimd4_Bits1, rSimd4_Bits2)); // Bits1 | Bits2 = Bits3
135 }
136
137 TEST_F(Simd4FloatingpointTest, gmxSimd4XorR)
138 {
139     GMX_EXPECT_SIMD4_REAL_EQ(rSimd4_Bits6, gmx_simd4_xor_r(rSimd4_Bits1, rSimd4_Bits2)); // Bits1 ^ Bits2 = Bits3
140 }
141 #endif
142
143 TEST_F(Simd4FloatingpointTest, gmxSimd4MaxR)
144 {
145     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(3, 2, 4), gmx_simd4_max_r(rSimd4_1_2_3, rSimd4_3_1_4));
146     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(3, 2, 4), gmx_simd4_max_r(rSimd4_3_1_4, rSimd4_1_2_3));
147     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(-1, -1, -3), gmx_simd4_max_r(rSimd4_m1_m2_m3, rSimd4_m3_m1_m4));
148     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(-1, -1, -3), gmx_simd4_max_r(rSimd4_m3_m1_m4, rSimd4_m1_m2_m3));
149 }
150
151 TEST_F(Simd4FloatingpointTest, gmxSimd4MinR)
152 {
153     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(1, 1, 3), gmx_simd4_min_r(rSimd4_1_2_3, rSimd4_3_1_4));
154     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(1, 1, 3), gmx_simd4_min_r(rSimd4_3_1_4, rSimd4_1_2_3));
155     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(-3, -2, -4), gmx_simd4_min_r(rSimd4_m1_m2_m3, rSimd4_m3_m1_m4));
156     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(-3, -2, -4), gmx_simd4_min_r(rSimd4_m3_m1_m4, rSimd4_m1_m2_m3));
157 }
158
159 TEST_F(Simd4FloatingpointTest, gmxSimd4RoundR)
160 {
161     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(2), gmx_simd4_round_r(gmx_simd4_set1_r(2.25)));
162     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(4), gmx_simd4_round_r(gmx_simd4_set1_r(3.75)));
163     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(-2), gmx_simd4_round_r(gmx_simd4_set1_r(-2.25)));
164     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(-4), gmx_simd4_round_r(gmx_simd4_set1_r(-3.75)));
165 }
166
167 TEST_F(Simd4FloatingpointTest, gmxSimd4TruncR)
168 {
169     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(2), gmx_simd4_trunc_r(rSimd4_2p25));
170     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(3), gmx_simd4_trunc_r(rSimd4_3p75));
171     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(-2), gmx_simd4_trunc_r(rSimd4_m2p25));
172     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(-3), gmx_simd4_trunc_r(rSimd4_m3p75));
173 }
174
175 /* We do extensive 1/sqrt(x) and 1/x accuracy testing in the tests for
176  * the SIMD math functions, so we just make sure the lookup instructions
177  * appear to work for a few values here.
178  */
179 TEST_F(Simd4FloatingpointTest, gmxSimd4RsqrtR)
180 {
181     gmx_simd4_real_t x      = setSimd4RealFrom3R(4.0, M_PI, 1234567890.0);
182     gmx_simd4_real_t ref    = setSimd4RealFrom3R(0.5, 1.0/sqrt(M_PI), 1.0/sqrt(1234567890.0));
183
184     // The allowed Ulp deviation is 2 to the power of the number of mantissa
185     // digits, minus the number of bits provided by the table lookup
186     setUlpTol(1LL << (std::numeric_limits<real>::digits-GMX_SIMD_RSQRT_BITS));
187     GMX_EXPECT_SIMD4_REAL_NEAR(ref, gmx_simd4_rsqrt_r(x));
188 }
189
190 TEST_F(Simd4FloatingpointTest, gmxSimd4BoolCmpEqAndBlendZeroR)
191 {
192     gmx_simd4_bool_t eq   = gmx_simd4_cmpeq_r(rSimd4_5_7_9, rSimd4_7_8_9);
193     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(0, 0, 3), gmx_simd4_blendzero_r(rSimd4_1_2_3, eq));
194 }
195
196 TEST_F(Simd4FloatingpointTest, gmxSimd4BlendNotZeroR)
197 {
198     gmx_simd4_bool_t eq   = gmx_simd4_cmpeq_r(rSimd4_5_7_9, rSimd4_7_8_9);
199     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(1, 2, 0), gmx_simd4_blendnotzero_r(rSimd4_1_2_3, eq));
200 }
201
202 TEST_F(Simd4FloatingpointTest, gmxSimd4BoolCmpLER)
203 {
204     gmx_simd4_bool_t le   = gmx_simd4_cmple_r(rSimd4_5_7_9, rSimd4_7_8_9);
205     GMX_EXPECT_SIMD4_REAL_EQ(rSimd4_1_2_3, gmx_simd4_blendzero_r(rSimd4_1_2_3, le));
206 }
207
208 TEST_F(Simd4FloatingpointTest, gmxSimd4BoolCmpLTR)
209 {
210     gmx_simd4_bool_t lt   = gmx_simd4_cmplt_r(rSimd4_5_7_9, rSimd4_7_8_9);
211     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(1, 2, 0), gmx_simd4_blendzero_r(rSimd4_1_2_3, lt));
212 }
213
214 TEST_F(Simd4FloatingpointTest, gmxSimd4BoolAndB)
215 {
216     gmx_simd4_bool_t eq   = gmx_simd4_cmpeq_r(rSimd4_5_7_9, rSimd4_7_8_9);
217     gmx_simd4_bool_t le   = gmx_simd4_cmple_r(rSimd4_5_7_9, rSimd4_7_8_9);
218     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(0, 0, 3), gmx_simd4_blendzero_r(rSimd4_1_2_3, gmx_simd4_and_b(eq, le)));
219 }
220
221 TEST_F(Simd4FloatingpointTest, gmxSimd4BoolOrB)
222 {
223     gmx_simd4_bool_t eq   = gmx_simd4_cmpeq_r(rSimd4_5_7_9, rSimd4_7_8_9);
224     gmx_simd4_bool_t lt   = gmx_simd4_cmplt_r(rSimd4_5_7_9, rSimd4_7_8_9);
225     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(1, 2, 3), gmx_simd4_blendzero_r(rSimd4_1_2_3, gmx_simd4_or_b(eq, lt)));
226 }
227
228 TEST_F(Simd4FloatingpointTest, gmxSimd4AnytrueB)
229 {
230     gmx_simd4_bool_t eq;
231
232     /* this test is a bit tricky since we don't know the simd width.
233      * We cannot check for truth values for "any" element beyond the first,
234      * since that part of the data will not be used if simd width is 1.
235      */
236     eq = gmx_simd4_cmpeq_r(rSimd4_5_7_9, setSimd4RealFrom3R(5, 0, 0));
237     EXPECT_NE(0, gmx_simd4_anytrue_b(eq));
238
239     eq = gmx_simd4_cmpeq_r(rSimd4_1_2_3, rSimd4_4_5_6);
240     EXPECT_EQ(0, gmx_simd4_anytrue_b(eq));
241 }
242
243 TEST_F(Simd4FloatingpointTest, gmxSimd4BlendvR)
244 {
245     gmx_simd4_bool_t lt   = gmx_simd4_cmplt_r(rSimd4_5_7_9, rSimd4_7_8_9);
246     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(4, 5, 3), gmx_simd4_blendv_r(rSimd4_1_2_3, rSimd4_4_5_6, lt));
247 }
248
249 TEST_F(Simd4FloatingpointTest, gmxSimd4ReduceR)
250 {
251     // The horizontal sum of the SIMD variable depends on the width, so
252     // simply store it an extra time and calculate what the sum should be
253     std::vector<real> v   = simd4Real2Vector(rSimd4_1_2_3);
254     real              sum = 0.0;
255
256     for (int i = 0; i < GMX_SIMD4_WIDTH; i++)
257     {
258         sum += v[i];
259     }
260
261     EXPECT_EQ(sum, gmx_simd4_reduce_r(rSimd4_1_2_3));
262 }
263
264
265 TEST_F(Simd4FloatingpointTest, gmxSimd4Dotproduct3R)
266 {
267     gmx_simd4_real_t v1 = setSimd4RealFrom3R(1, 4, 5);
268     gmx_simd4_real_t v2 = setSimd4RealFrom3R(3, 8, 2);
269 #    ifdef GMX_DOUBLE
270     EXPECT_DOUBLE_EQ(45.0, gmx_simd4_dotproduct3_r(v1, v2));
271 #    else
272     EXPECT_FLOAT_EQ(45.0, gmx_simd4_dotproduct3_r(v1, v2));
273 #    endif
274 }
275
276 #endif      // GMX_SIMD4_HAVE_REAL
277
278 /*! \} */
279 /*! \endcond */
280
281 }      // namespace
282 }      // namespace
283 }      // namespace