SYCL: Avoid using no_init read accessor in rocFFT
[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,2015,2016,2017,2018 by the GROMACS development team.
5  * Copyright (c) 2019,2020,2021, by the GROMACS development team, led by
6  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7  * and including many others, as listed in the AUTHORS file in the
8  * top-level source directory and at http://www.gromacs.org.
9  *
10  * GROMACS is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2.1
13  * of the License, or (at your option) any later version.
14  *
15  * GROMACS is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with GROMACS; if not, see
22  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24  *
25  * If you want to redistribute modifications to GROMACS, please
26  * consider that scientific software is very special. Version
27  * control is crucial - bugs must be traceable. We will be happy to
28  * consider code for inclusion in the official distribution, but
29  * derived work must not be called official GROMACS. Details are found
30  * in the README & COPYING files - if they are missing, get the
31  * official version at http://www.gromacs.org.
32  *
33  * To help us fund GROMACS development, we humbly ask that you cite
34  * the research papers on the package. Check out http://www.gromacs.org.
35  */
36 #include "gmxpre.h"
37
38 #include <cmath>
39
40 #include "gromacs/math/units.h"
41 #include "gromacs/math/utilities.h"
42 #include "gromacs/simd/simd.h"
43 #include "gromacs/utility/basedefinitions.h"
44
45 #include "testutils/testasserts.h"
46
47 #include "data.h"
48 #include "simd4.h"
49
50 #if GMX_SIMD
51
52 namespace gmx
53 {
54 namespace test
55 {
56 namespace
57 {
58
59 /*! \cond internal */
60 /*! \addtogroup module_simd */
61 /*! \{ */
62
63 #    if GMX_SIMD4_HAVE_REAL
64
65 /*! \brief Test fixture for SIMD4 floating-point operations (identical to the SIMD4 \ref Simd4Test) */
66 typedef Simd4Test Simd4FloatingpointTest;
67
68 TEST_F(Simd4FloatingpointTest, setZero)
69 {
70     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(0.0), setZero());
71 }
72
73 TEST_F(Simd4FloatingpointTest, set)
74 {
75     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(c1), Simd4Real(c1));
76 }
77
78 TEST_F(Simd4FloatingpointTest, add)
79 {
80     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(c0 + c3, c1 + c4, c2 + c5), rSimd4_c0c1c2 + rSimd4_c3c4c5);
81 }
82
83 TEST_F(Simd4FloatingpointTest, sub)
84 {
85     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(c0 - c3, c1 - c4, c2 - c5), rSimd4_c0c1c2 - rSimd4_c3c4c5);
86 }
87
88 TEST_F(Simd4FloatingpointTest, mul)
89 {
90     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(c0 * c3, c1 * c4, c2 * c5), rSimd4_c0c1c2 * rSimd4_c3c4c5);
91 }
92
93 TEST_F(Simd4FloatingpointTest, fma)
94 {
95     // The last bit of FMA operations depends on hardware, so we don't require exact match
96     GMX_EXPECT_SIMD4_REAL_NEAR(setSimd4RealFrom3R(c0 * c3 + c6, c1 * c4 + c7, c2 * c5 + c8),
97                                fma(rSimd4_c0c1c2, rSimd4_c3c4c5, rSimd4_c6c7c8));
98 }
99
100 TEST_F(Simd4FloatingpointTest, fms)
101 {
102     // The last bit of FMA operations depends on hardware, so we don't require exact match
103     GMX_EXPECT_SIMD4_REAL_NEAR(setSimd4RealFrom3R(c0 * c3 - c6, c1 * c4 - c7, c2 * c5 - c8),
104                                fms(rSimd4_c0c1c2, rSimd4_c3c4c5, rSimd4_c6c7c8));
105 }
106
107 TEST_F(Simd4FloatingpointTest, fnma)
108 {
109     // The last bit of FMA operations depends on hardware, so we don't require exact match
110     GMX_EXPECT_SIMD4_REAL_NEAR(setSimd4RealFrom3R(c6 - c0 * c3, c7 - c1 * c4, c8 - c2 * c5),
111                                fnma(rSimd4_c0c1c2, rSimd4_c3c4c5, rSimd4_c6c7c8));
112 }
113
114 TEST_F(Simd4FloatingpointTest, fnms)
115 {
116     // The last bit of FMA operations depends on hardware, so we don't require exact match
117     GMX_EXPECT_SIMD4_REAL_NEAR(setSimd4RealFrom3R(-c0 * c3 - c6, -c1 * c4 - c7, -c2 * c5 - c8),
118                                fnms(rSimd4_c0c1c2, rSimd4_c3c4c5, rSimd4_c6c7c8));
119 }
120
121 TEST_F(Simd4FloatingpointTest, abs)
122 {
123     GMX_EXPECT_SIMD4_REAL_EQ(rSimd4_c0c1c2, abs(rSimd4_c0c1c2)); // fabs(x)=x
124     GMX_EXPECT_SIMD4_REAL_EQ(rSimd4_c0c1c2, abs(rSimd4_m0m1m2)); // fabs(-x)=x
125 }
126
127 TEST_F(Simd4FloatingpointTest, neg)
128 {
129     GMX_EXPECT_SIMD4_REAL_EQ(rSimd4_m0m1m2, -(rSimd4_c0c1c2)); // fneg(x)=-x
130     GMX_EXPECT_SIMD4_REAL_EQ(rSimd4_c0c1c2, -(rSimd4_m0m1m2)); // fneg(-x)=x
131 }
132
133 #        if GMX_SIMD_HAVE_LOGICAL
134 TEST_F(Simd4FloatingpointTest, and)
135 {
136     GMX_EXPECT_SIMD4_REAL_EQ(rSimd4_logicalResultAnd, (rSimd4_logicalA & rSimd4_logicalB));
137 }
138
139 TEST_F(Simd4FloatingpointTest, or)
140 {
141     GMX_EXPECT_SIMD4_REAL_EQ(rSimd4_logicalResultOr, (rSimd4_logicalA | rSimd4_logicalB));
142 }
143
144 TEST_F(Simd4FloatingpointTest, xor)
145 {
146     /* Test xor by taking xor with a number and its negative. This should result
147      * in only the sign bit being set. We then use this bit change the sign of
148      * different numbers.
149      */
150     Simd4Real signbit = Simd4Real(c1) ^ Simd4Real(-c1);
151     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(-c2, c3, -c4), signbit ^ setSimd4RealFrom3R(c2, -c3, c4));
152 }
153
154 TEST_F(Simd4FloatingpointTest, andNot)
155 {
156     /* Use xor (which we already tested, so fix that first if both tests fail)
157      * to extract the sign bit, and then use andnot to take absolute values.
158      */
159     Simd4Real signbit = Simd4Real(c1) ^ Simd4Real(-c1);
160     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(c2, c3, c4),
161                              andNot(signbit, setSimd4RealFrom3R(-c2, c3, -c4)));
162 }
163
164 #        endif
165
166 TEST_F(Simd4FloatingpointTest, max)
167 {
168     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(c3, c1, c4), max(rSimd4_c0c1c2, rSimd4_c3c0c4));
169     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(c3, c1, c4), max(rSimd4_c3c0c4, rSimd4_c0c1c2));
170     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(-c0, -c0, -c2), max(rSimd4_m0m1m2, rSimd4_m3m0m4));
171     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(-c0, -c0, -c2), max(rSimd4_m3m0m4, rSimd4_m0m1m2));
172 }
173
174 TEST_F(Simd4FloatingpointTest, min)
175 {
176     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(c0, c0, c2), min(rSimd4_c0c1c2, rSimd4_c3c0c4));
177     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(c0, c0, c2), min(rSimd4_c3c0c4, rSimd4_c0c1c2));
178     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(-c3, -c1, -c4), min(rSimd4_m0m1m2, rSimd4_m3m0m4));
179     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(-c3, -c1, -c4), min(rSimd4_m3m0m4, rSimd4_m0m1m2));
180 }
181
182 TEST_F(Simd4FloatingpointTest, round)
183 {
184     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(2), round(Simd4Real(2.25)));
185     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(4), round(Simd4Real(3.75)));
186     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(-2), round(Simd4Real(-2.25)));
187     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(-4), round(Simd4Real(-3.75)));
188 }
189
190 TEST_F(Simd4FloatingpointTest, trunc)
191 {
192     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(2), trunc(rSimd4_2p25));
193     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(3), trunc(rSimd4_3p75));
194     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(-2), trunc(rSimd4_m2p25));
195     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom1R(-3), trunc(rSimd4_m3p75));
196 }
197
198 /* We do extensive 1/sqrt(x) and 1/x accuracy testing in the tests for
199  * the SIMD math functions, so we just make sure the lookup instructions
200  * appear to work for a few values here.
201  */
202 TEST_F(Simd4FloatingpointTest, gmxSimd4RsqrtR)
203 {
204     Simd4Real x   = setSimd4RealFrom3R(4.0, M_PI, 1234567890.0);
205     Simd4Real ref = setSimd4RealFrom3R(0.5, 1.0 / std::sqrt(M_PI), 1.0 / std::sqrt(1234567890.0));
206     int       shiftbits = std::numeric_limits<real>::digits - GMX_SIMD_RSQRT_BITS;
207
208     if (shiftbits < 0)
209     {
210         shiftbits = 0;
211     }
212
213     // The allowed Ulp deviation is 2 to the power of the number of mantissa
214     // digits, minus the number of bits provided by the table lookup
215     setUlpTol(1LL << shiftbits);
216     GMX_EXPECT_SIMD4_REAL_NEAR(ref, rsqrt(x));
217 }
218
219 TEST_F(Simd4FloatingpointTest, cmpEqAndSelectByMask)
220 {
221     Simd4Bool eq = rSimd4_c4c6c8 == rSimd4_c6c7c8;
222     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(0, 0, c2), selectByMask(rSimd4_c0c1c2, eq));
223 }
224
225 TEST_F(Simd4FloatingpointTest, selectByNotMask)
226 {
227     Simd4Bool eq = rSimd4_c4c6c8 == rSimd4_c6c7c8;
228     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(c0, c1, 0), selectByNotMask(rSimd4_c0c1c2, eq));
229 }
230
231 TEST_F(Simd4FloatingpointTest, cmpNe)
232 {
233     Simd4Bool eq = rSimd4_c4c6c8 != rSimd4_c6c7c8;
234     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(c0, c1, 0), selectByMask(rSimd4_c0c1c2, eq));
235 }
236
237 TEST_F(Simd4FloatingpointTest, cmpLe)
238 {
239     Simd4Bool le = rSimd4_c4c6c8 <= rSimd4_c6c7c8;
240     GMX_EXPECT_SIMD4_REAL_EQ(rSimd4_c0c1c2, selectByMask(rSimd4_c0c1c2, le));
241 }
242
243 TEST_F(Simd4FloatingpointTest, cmpLt)
244 {
245     Simd4Bool lt = rSimd4_c4c6c8 < rSimd4_c6c7c8;
246     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(c0, c1, 0), selectByMask(rSimd4_c0c1c2, lt));
247 }
248
249 TEST_F(Simd4FloatingpointTest, andB)
250 {
251     Simd4Bool eq = rSimd4_c4c6c8 == rSimd4_c6c7c8;
252     Simd4Bool le = rSimd4_c4c6c8 <= rSimd4_c6c7c8;
253     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(0, 0, c2), selectByMask(rSimd4_c0c1c2, (eq && le)));
254 }
255
256 TEST_F(Simd4FloatingpointTest, orB)
257 {
258     Simd4Bool eq = rSimd4_c4c6c8 == rSimd4_c6c7c8;
259     Simd4Bool lt = rSimd4_c4c6c8 < rSimd4_c6c7c8;
260     GMX_EXPECT_SIMD4_REAL_EQ(rSimd4_c0c1c2, selectByMask(rSimd4_c0c1c2, (eq || lt)));
261 }
262
263 TEST_F(Simd4FloatingpointTest, anyTrue)
264 {
265     Simd4Bool eq;
266
267     /* this test is a bit tricky since we don't know the simd width.
268      * We cannot check for truth values for "any" element beyond the first,
269      * since that part of the data will not be used if simd width is 1.
270      */
271     eq = (rSimd4_c4c6c8 == setSimd4RealFrom3R(c4, 0, 0));
272     EXPECT_TRUE(anyTrue(eq));
273
274     eq = (rSimd4_c0c1c2 == rSimd4_c3c4c5);
275     EXPECT_FALSE(anyTrue(eq));
276 }
277
278 TEST_F(Simd4FloatingpointTest, blend)
279 {
280     Simd4Bool lt = rSimd4_c4c6c8 < rSimd4_c6c7c8;
281     GMX_EXPECT_SIMD4_REAL_EQ(setSimd4RealFrom3R(c3, c4, c2), blend(rSimd4_c0c1c2, rSimd4_c3c4c5, lt));
282 }
283
284 TEST_F(Simd4FloatingpointTest, reduce)
285 {
286     // The horizontal sum of the SIMD variable depends on the width, so
287     // simply store it an extra time and calculate what the sum should be
288     std::vector<real> v   = simd4Real2Vector(rSimd4_c3c4c5);
289     real              sum = 0.0;
290
291     for (int i = 0; i < GMX_SIMD4_WIDTH; i++)
292     {
293         sum += v[i];
294     }
295
296     EXPECT_REAL_EQ_TOL(sum, reduce(rSimd4_c3c4c5), defaultRealTolerance());
297 }
298
299
300 TEST_F(Simd4FloatingpointTest, dotProduct)
301 {
302     real res = c0 * c3 + c1 * c4 + c2 * c5;
303
304     EXPECT_REAL_EQ_TOL(res, dotProduct(rSimd4_c0c1c2, rSimd4_c3c4c5), defaultRealTolerance());
305 }
306
307 TEST_F(Simd4FloatingpointTest, transpose)
308 {
309     Simd4Real v0, v1, v2, v3;
310     int       i;
311     // aligned pointers
312     alignas(GMX_SIMD_ALIGNMENT) real p0[4 * GMX_SIMD4_WIDTH];
313     real*                            p1 = p0 + GMX_SIMD4_WIDTH;
314     real*                            p2 = p0 + 2 * GMX_SIMD4_WIDTH;
315     real*                            p3 = p0 + 3 * GMX_SIMD4_WIDTH;
316
317     // Assign data with tens as row, single-digit as column
318     for (i = 0; i < 4; i++)
319     {
320         // Scale by 1+100*eps to use low bits tii
321         p0[i] = (0 * 10 + i * 1) * (1.0 + 100 * GMX_REAL_EPS);
322         p1[i] = (1 * 10 + i * 1) * (1.0 + 100 * GMX_REAL_EPS);
323         p2[i] = (2 * 10 + i * 1) * (1.0 + 100 * GMX_REAL_EPS);
324         p3[i] = (3 * 10 + i * 1) * (1.0 + 100 * GMX_REAL_EPS);
325     }
326
327     v0 = load4(p0);
328     v1 = load4(p1);
329     v2 = load4(p2);
330     v3 = load4(p3);
331
332     transpose(&v0, &v1, &v2, &v3);
333
334     store4(p0, v0);
335     store4(p1, v1);
336     store4(p2, v2);
337     store4(p3, v3);
338
339     for (i = 0; i < 4; i++)
340     {
341         EXPECT_REAL_EQ_TOL((i * 10 + 0) * (1.0 + 100 * GMX_REAL_EPS), p0[i], defaultRealTolerance());
342         EXPECT_REAL_EQ_TOL((i * 10 + 1) * (1.0 + 100 * GMX_REAL_EPS), p1[i], defaultRealTolerance());
343         EXPECT_REAL_EQ_TOL((i * 10 + 2) * (1.0 + 100 * GMX_REAL_EPS), p2[i], defaultRealTolerance());
344         EXPECT_REAL_EQ_TOL((i * 10 + 3) * (1.0 + 100 * GMX_REAL_EPS), p3[i], defaultRealTolerance());
345     }
346 }
347
348 #    endif // GMX_SIMD4_HAVE_REAL
349
350 /*! \} */
351 /*! \endcond */
352
353 } // namespace
354 } // namespace test
355 } // namespace gmx
356
357 #endif // GMX_SIMD