SYCL: Avoid using no_init read accessor in rocFFT
[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,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 <array>
41
42 #include "gromacs/math/units.h"
43 #include "gromacs/math/utilities.h"
44 #include "gromacs/simd/simd.h"
45 #include "gromacs/utility/basedefinitions.h"
46
47 #include "testutils/testasserts.h"
48
49 #include "data.h"
50 #include "simd.h"
51
52 #if GMX_SIMD
53
54 namespace gmx
55 {
56 namespace test
57 {
58
59 namespace
60 {
61
62 /*! \cond internal */
63 /*! \addtogroup module_simd */
64 /*! \{ */
65
66 #    if GMX_SIMD_HAVE_REAL
67
68 /*! \brief Test fixture for floating-point tests (identical to the generic \ref SimdTest) */
69 typedef SimdTest SimdFloatingpointTest;
70
71 TEST_F(SimdFloatingpointTest, setZero)
72 {
73     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(0.0), setZero());
74 }
75
76 TEST_F(SimdFloatingpointTest, set)
77 {
78     const real* p = &c0;
79     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(c1), SimdReal(c1));
80     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(c0), SimdReal(*p));
81 }
82
83 TEST_F(SimdFloatingpointTest, add)
84 {
85     GMX_EXPECT_SIMD_REAL_NEAR(setSimdRealFrom3R(c0 + c3, c1 + c4, c2 + c5), rSimd_c0c1c2 + rSimd_c3c4c5);
86 }
87
88 TEST_F(SimdFloatingpointTest, maskAdd)
89 {
90     SimdBool m = setSimdRealFrom3R(c6, 0, c7) != setZero();
91     GMX_EXPECT_SIMD_REAL_NEAR(setSimdRealFrom3R(c0 + c3, c1 + 0.0, c2 + c5),
92                               maskAdd(rSimd_c0c1c2, rSimd_c3c4c5, m));
93 }
94
95 TEST_F(SimdFloatingpointTest, sub)
96 {
97     GMX_EXPECT_SIMD_REAL_NEAR(setSimdRealFrom3R(c0 - c3, c1 - c4, c2 - c5), rSimd_c0c1c2 - rSimd_c3c4c5);
98 }
99
100 TEST_F(SimdFloatingpointTest, mul)
101 {
102     GMX_EXPECT_SIMD_REAL_NEAR(setSimdRealFrom3R(c0 * c3, c1 * c4, c2 * c5), rSimd_c0c1c2 * rSimd_c3c4c5);
103 }
104
105 TEST_F(SimdFloatingpointTest, maskzMul)
106 {
107     SimdBool m = setSimdRealFrom3R(c1, 0, c1) != setZero();
108     GMX_EXPECT_SIMD_REAL_NEAR(setSimdRealFrom3R(c0 * c3, 0.0, c2 * c5),
109                               maskzMul(rSimd_c0c1c2, rSimd_c3c4c5, m));
110 }
111
112 TEST_F(SimdFloatingpointTest, fma)
113 {
114     // The last bit of FMA operations depends on hardware, so we don't require exact match
115     GMX_EXPECT_SIMD_REAL_NEAR(setSimdRealFrom3R(c0 * c3 + c6, c1 * c4 + c7, c2 * c5 + c8),
116                               fma(rSimd_c0c1c2, rSimd_c3c4c5, rSimd_c6c7c8));
117 }
118
119
120 TEST_F(SimdFloatingpointTest, maskzFma)
121 {
122     SimdBool m = setSimdRealFrom3R(c2, 0, c3) != setZero();
123     // The last bit of FMA operations depends on hardware, so we don't require exact match
124     GMX_EXPECT_SIMD_REAL_NEAR(setSimdRealFrom3R(c0 * c3 + c6, 0.0, c2 * c5 + c8),
125                               maskzFma(rSimd_c0c1c2, rSimd_c3c4c5, rSimd_c6c7c8, m));
126 }
127
128 TEST_F(SimdFloatingpointTest, fms)
129 {
130     // The last bit of FMA operations depends on hardware, so we don't require exact match
131     GMX_EXPECT_SIMD_REAL_NEAR(setSimdRealFrom3R(c0 * c3 - c6, c1 * c4 - c7, c2 * c5 - c8),
132                               fms(rSimd_c0c1c2, rSimd_c3c4c5, rSimd_c6c7c8));
133 }
134
135 TEST_F(SimdFloatingpointTest, fnma)
136 {
137     // The last bit of FMA operations depends on hardware, so we don't require exact match
138     GMX_EXPECT_SIMD_REAL_NEAR(setSimdRealFrom3R(c6 - c0 * c3, c7 - c1 * c4, c8 - c2 * c5),
139                               fnma(rSimd_c0c1c2, rSimd_c3c4c5, rSimd_c6c7c8));
140 }
141
142 TEST_F(SimdFloatingpointTest, fnms)
143 {
144     // The last bit of FMA operations depends on hardware, so we don't require exact match
145     GMX_EXPECT_SIMD_REAL_NEAR(setSimdRealFrom3R(-c0 * c3 - c6, -c1 * c4 - c7, -c2 * c5 - c8),
146                               fnms(rSimd_c0c1c2, rSimd_c3c4c5, rSimd_c6c7c8));
147 }
148
149 TEST_F(SimdFloatingpointTest, abs)
150 {
151     GMX_EXPECT_SIMD_REAL_EQ(rSimd_c0c1c2, abs(rSimd_c0c1c2)); // fabs(x)=x
152     GMX_EXPECT_SIMD_REAL_EQ(rSimd_c0c1c2, abs(rSimd_m0m1m2)); // fabs(-x)=x
153 }
154
155 TEST_F(SimdFloatingpointTest, neg)
156 {
157     GMX_EXPECT_SIMD_REAL_EQ(rSimd_m0m1m2, -(rSimd_c0c1c2)); // fneg(x)=-x
158     GMX_EXPECT_SIMD_REAL_EQ(rSimd_c0c1c2, -(rSimd_m0m1m2)); // fneg(-x)=x
159 }
160
161 #        if GMX_SIMD_HAVE_LOGICAL
162 TEST_F(SimdFloatingpointTest, and)
163 {
164     GMX_EXPECT_SIMD_REAL_EQ(rSimd_logicalResultAnd, (rSimd_logicalA & rSimd_logicalB));
165 }
166
167 TEST_F(SimdFloatingpointTest, or)
168 {
169     GMX_EXPECT_SIMD_REAL_EQ(rSimd_logicalResultOr, (rSimd_logicalA | rSimd_logicalB));
170 }
171
172 TEST_F(SimdFloatingpointTest, xor)
173 {
174     /* Test xor by taking xor with a number and its negative. This should result
175      * in only the sign bit being set. We then use this bit change the sign of
176      * different numbers.
177      */
178     SimdReal signbit = SimdReal(c1) ^ SimdReal(-c1);
179     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(-c2, c3, -c4), (signbit ^ setSimdRealFrom3R(c2, -c3, c4)));
180 }
181
182 TEST_F(SimdFloatingpointTest, andNot)
183 {
184     /* Use xor (which we already tested, so fix that first if both tests fail)
185      * to extract the sign bit, and then use andnot to take absolute values.
186      */
187     SimdReal signbit = SimdReal(c1) ^ SimdReal(-c1);
188     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(c2, c3, c4),
189                             andNot(signbit, setSimdRealFrom3R(-c2, c3, -c4)));
190 }
191
192 #        endif
193
194 TEST_F(SimdFloatingpointTest, max)
195 {
196     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(c3, c1, c4), max(rSimd_c0c1c2, rSimd_c3c0c4));
197     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(c3, c1, c4), max(rSimd_c3c0c4, rSimd_c0c1c2));
198     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(-c0, -c0, -c2), max(rSimd_m0m1m2, rSimd_m3m0m4));
199     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(-c0, -c0, -c2), max(rSimd_m3m0m4, rSimd_m0m1m2));
200 }
201
202 TEST_F(SimdFloatingpointTest, min)
203 {
204     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(c0, c0, c2), min(rSimd_c0c1c2, rSimd_c3c0c4));
205     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(c0, c0, c2), min(rSimd_c3c0c4, rSimd_c0c1c2));
206     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(-c3, -c1, -c4), min(rSimd_m0m1m2, rSimd_m3m0m4));
207     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(-c3, -c1, -c4), min(rSimd_m3m0m4, rSimd_m0m1m2));
208 }
209
210 TEST_F(SimdFloatingpointTest, round)
211 {
212     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(2), round(rSimd_2p25));
213     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(4), round(rSimd_3p75));
214     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(-2), round(rSimd_m2p25));
215     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(-4), round(rSimd_m3p75));
216 }
217
218 TEST_F(SimdFloatingpointTest, roundMode)
219 {
220     /* Rounding mode needs to be consistent between round and cvtR2I */
221     SimdReal x0 = setSimdRealFrom3R(0.5, 11.5, 99.5);
222     SimdReal x1 = setSimdRealFrom3R(-0.5, -11.5, -99.5);
223
224     GMX_EXPECT_SIMD_REAL_EQ(round(x0), cvtI2R(cvtR2I(x0)));
225     GMX_EXPECT_SIMD_REAL_EQ(round(x1), cvtI2R(cvtR2I(x1)));
226 }
227
228 TEST_F(SimdFloatingpointTest, trunc)
229 {
230     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(2), trunc(rSimd_2p25));
231     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(3), trunc(rSimd_3p75));
232     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(-2), trunc(rSimd_m2p25));
233     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(-3), trunc(rSimd_m3p75));
234 }
235
236 // We explicitly test the exponent/mantissa routines with double precision data,
237 // since these usually rely on direct manipulation and shift of the SIMD registers,
238 // where it is easy to make mistakes with single vs double precision.
239
240 TEST_F(SimdFloatingpointTest, frexp)
241 {
242     SimdReal  fraction;
243     SimdInt32 exponent;
244
245
246     fraction = frexp(rSimd_Exp, &exponent);
247     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(0.609548660288905419513128,
248                                               0.5833690139241746175358116,
249                                               -0.584452007502232362412542),
250                             fraction);
251     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom3I(61, -40, 55), exponent);
252
253     // Test the unsafe flavor too, in case they use different branches
254     fraction = frexp<MathOptimization::Unsafe>(rSimd_Exp, &exponent);
255     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(0.609548660288905419513128,
256                                               0.5833690139241746175358116,
257                                               -0.584452007502232362412542),
258                             fraction);
259     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom3I(61, -40, 55), exponent);
260
261     // Use ulp testing with 0 bit ulp tolerance for testing to separate 0.0 and -0.0
262     setUlpTol(0);
263     fraction = frexp(setSimdRealFrom1R(0.0), &exponent);
264     GMX_EXPECT_SIMD_REAL_NEAR(setSimdRealFrom1R(0.0), fraction);
265     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(0), exponent);
266
267     // Second -0.0.
268     fraction = frexp(setSimdRealFrom1R(-0.0), &exponent);
269     GMX_EXPECT_SIMD_REAL_NEAR(setSimdRealFrom1R(-0.0), fraction);
270     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(0), exponent);
271
272     // Reset to default ulp tolerance
273     setUlpTol(defaultRealUlpTol());
274
275 #        if GMX_SIMD_HAVE_DOUBLE && GMX_DOUBLE
276     // Test exponents larger than what fit in single precision, as well as mixtures of 0 and non-zero values, to
277     // make sure the shuffling operations in the double-precision implementations don't do anything bad.
278     fraction = frexp(rSimd_ExpDouble1, &exponent);
279     GMX_EXPECT_SIMD_REAL_EQ(
280             setSimdRealFrom3R(0.0, 0.5236473618795619566768096, -0.9280331023751380303821179), fraction);
281     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom3I(0, -461, 673), exponent);
282
283     fraction = frexp(rSimd_ExpDouble2, &exponent);
284     GMX_EXPECT_SIMD_REAL_EQ(
285             setSimdRealFrom3R(0.6206306194761728178832527, 0.0, -0.9280331023751380303821179), fraction);
286     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom3I(588, 0, 673), exponent);
287 #        endif
288 }
289
290 TEST_F(SimdFloatingpointTest, ldexp)
291 {
292     SimdReal one = setSimdRealFrom1R(1.0);
293
294     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(pow(2.0, 60.0), pow(2.0, -41.0), pow(2.0, 54.0)),
295                             ldexp<MathOptimization::Unsafe>(one, setSimdIntFrom3I(60, -41, 54)));
296 #        if GMX_SIMD_HAVE_DOUBLE && GMX_DOUBLE
297     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(pow(2.0, 587.0), pow(2.0, -462.0), pow(2.0, 672.0)),
298                             ldexp<MathOptimization::Unsafe>(one, setSimdIntFrom3I(587, -462, 672)));
299 #        endif
300     // The default safe version must be able to handle very negative arguments too
301     GMX_EXPECT_SIMD_REAL_EQ(setZero(), ldexp(one, setSimdIntFrom3I(-2000, -1000000, -1000000000)));
302 }
303
304 /*
305  * We do extensive 1/sqrt(x) and 1/x accuracy testing in the math module, so
306  * we just make sure the lookup instructions appear to work here
307  */
308
309 TEST_F(SimdFloatingpointTest, rsqrt)
310 {
311     SimdReal x   = setSimdRealFrom3R(4.0, M_PI, 1234567890.0);
312     SimdReal ref = setSimdRealFrom3R(0.5, 1.0 / std::sqrt(M_PI), 1.0 / std::sqrt(1234567890.0));
313     int      shiftbits = std::numeric_limits<real>::digits - GMX_SIMD_RSQRT_BITS;
314
315     if (shiftbits < 0)
316     {
317         shiftbits = 0;
318     }
319
320     /* Set the allowed ulp error as 2 to the power of the number of bits in
321      * the mantissa that do not have to be correct after the table lookup.
322      */
323     setUlpTol(1LL << shiftbits);
324     GMX_EXPECT_SIMD_REAL_NEAR(ref, rsqrt(x));
325 }
326
327 TEST_F(SimdFloatingpointTest, maskzRsqrt)
328 {
329     SimdReal x = setSimdRealFrom3R(M_PI, -4.0, 0.0);
330     // simdCmpLe is tested separately further down
331     SimdBool m         = setZero() < x;
332     SimdReal ref       = setSimdRealFrom3R(1.0 / std::sqrt(M_PI), 0.0, 0.0);
333     int      shiftbits = std::numeric_limits<real>::digits - GMX_SIMD_RSQRT_BITS;
334
335     if (shiftbits < 0)
336     {
337         shiftbits = 0;
338     }
339
340     /* Set the allowed ulp error as 2 to the power of the number of bits in
341      * the mantissa that do not have to be correct after the table lookup.
342      */
343     setUlpTol(1LL << shiftbits);
344     GMX_EXPECT_SIMD_REAL_NEAR(ref, maskzRsqrt(x, m));
345 }
346
347 TEST_F(SimdFloatingpointTest, rcp)
348 {
349     SimdReal x         = setSimdRealFrom3R(4.0, M_PI, 1234567890.0);
350     SimdReal ref       = setSimdRealFrom3R(0.25, 1.0 / M_PI, 1.0 / 1234567890.0);
351     int      shiftbits = std::numeric_limits<real>::digits - GMX_SIMD_RCP_BITS;
352
353     if (shiftbits < 0)
354     {
355         shiftbits = 0;
356     }
357
358     /* Set the allowed ulp error as 2 to the power of the number of bits in
359      * the mantissa that do not have to be correct after the table lookup.
360      */
361     setUlpTol(1LL << shiftbits);
362     GMX_EXPECT_SIMD_REAL_NEAR(ref, rcp(x));
363 }
364
365 TEST_F(SimdFloatingpointTest, maskzRcp)
366 {
367     SimdReal x         = setSimdRealFrom3R(M_PI, 0.0, -1234567890.0);
368     SimdBool m         = (x != setZero());
369     SimdReal ref       = setSimdRealFrom3R(1.0 / M_PI, 0.0, -1.0 / 1234567890.0);
370     int      shiftbits = std::numeric_limits<real>::digits - GMX_SIMD_RCP_BITS;
371
372     if (shiftbits < 0)
373     {
374         shiftbits = 0;
375     }
376
377     /* Set the allowed ulp error as 2 to the power of the number of bits in
378      * the mantissa that do not have to be correct after the table lookup.
379      */
380     setUlpTol(1LL << shiftbits);
381     GMX_EXPECT_SIMD_REAL_NEAR(ref, maskzRcp(x, m));
382 }
383
384 TEST_F(SimdFloatingpointTest, cmpEqAndSelectByMask)
385 {
386     SimdBool eq = rSimd_c4c6c8 == rSimd_c6c7c8;
387     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(0, 0, c2), selectByMask(rSimd_c0c1c2, eq));
388 }
389
390 TEST_F(SimdFloatingpointTest, selectByNotMask)
391 {
392     SimdBool eq = rSimd_c4c6c8 == rSimd_c6c7c8;
393     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(c0, c1, 0), selectByNotMask(rSimd_c0c1c2, eq));
394 }
395
396 TEST_F(SimdFloatingpointTest, cmpNe)
397 {
398     SimdBool eq = rSimd_c4c6c8 != rSimd_c6c7c8;
399     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(c0, c1, 0), selectByMask(rSimd_c0c1c2, eq));
400 }
401
402 TEST_F(SimdFloatingpointTest, cmpLe)
403 {
404     SimdBool le = rSimd_c4c6c8 <= rSimd_c6c7c8;
405     GMX_EXPECT_SIMD_REAL_EQ(rSimd_c0c1c2, selectByMask(rSimd_c0c1c2, le));
406 }
407
408 TEST_F(SimdFloatingpointTest, cmpLt)
409 {
410     SimdBool lt = rSimd_c4c6c8 < rSimd_c6c7c8;
411     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(c0, c1, 0), selectByMask(rSimd_c0c1c2, lt));
412 }
413
414 #        if GMX_SIMD_HAVE_INT32_LOGICAL || GMX_SIMD_HAVE_LOGICAL
415 TEST_F(SimdFloatingpointTest, testBits)
416 {
417     SimdBool eq = testBits(setSimdRealFrom3R(c1, 0, c1));
418     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(c0, 0, c2), selectByMask(rSimd_c0c1c2, eq));
419
420     // Test if we detect only the sign bit being set
421     eq = testBits(setSimdRealFrom1R(GMX_REAL_NEGZERO));
422     GMX_EXPECT_SIMD_REAL_EQ(rSimd_c0c1c2, selectByMask(rSimd_c0c1c2, eq));
423 }
424 #        endif
425
426 TEST_F(SimdFloatingpointTest, andB)
427 {
428     SimdBool eq = rSimd_c4c6c8 == rSimd_c6c7c8;
429     SimdBool le = rSimd_c4c6c8 <= rSimd_c6c7c8;
430     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(0, 0, c2), selectByMask(rSimd_c0c1c2, (eq && le)));
431 }
432
433 TEST_F(SimdFloatingpointTest, orB)
434 {
435     SimdBool eq = rSimd_c4c6c8 == rSimd_c6c7c8;
436     SimdBool lt = rSimd_c4c6c8 < rSimd_c6c7c8;
437     GMX_EXPECT_SIMD_REAL_EQ(rSimd_c0c1c2, selectByMask(rSimd_c0c1c2, (eq || lt)));
438 }
439
440 TEST_F(SimdFloatingpointTest, anyTrueB)
441 {
442     alignas(GMX_SIMD_ALIGNMENT) std::array<real, GMX_SIMD_REAL_WIDTH> mem{};
443
444     // Test the false case
445     EXPECT_FALSE(anyTrue(setZero() < load<SimdReal>(mem.data())));
446
447     // Test each bit (these should all be true)
448     for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
449     {
450         mem.fill(0.0);
451         mem[i] = 1.0;
452         EXPECT_TRUE(anyTrue(setZero() < load<SimdReal>(mem.data())))
453                 << "Not detecting true in element " << i;
454     }
455 }
456
457 TEST_F(SimdFloatingpointTest, blend)
458 {
459     SimdBool lt = rSimd_c4c6c8 < rSimd_c6c7c8;
460     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(c3, c4, c2), blend(rSimd_c0c1c2, rSimd_c3c4c5, lt));
461 }
462
463 TEST_F(SimdFloatingpointTest, reduce)
464 {
465     // The horizontal sum of the SIMD variable depends on the width, so
466     // simply store it an extra time and calculate what the sum should be
467     std::vector<real> v   = simdReal2Vector(rSimd_c3c4c5);
468     real              sum = 0.0;
469
470     for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
471     {
472         sum += v[i];
473     }
474
475     EXPECT_REAL_EQ_TOL(sum, reduce(rSimd_c3c4c5), defaultRealTolerance());
476 }
477
478 #    endif // GMX_SIMD_HAVE_REAL
479
480 #    if GMX_SIMD_HAVE_FLOAT && GMX_SIMD_HAVE_DOUBLE
481 TEST_F(SimdFloatingpointTest, cvtFloat2Double)
482 {
483     alignas(GMX_SIMD_ALIGNMENT) float f[GMX_SIMD_FLOAT_WIDTH];
484     alignas(GMX_SIMD_ALIGNMENT) double d[GMX_SIMD_FLOAT_WIDTH]; // Yes, double array length should be same as float
485
486     int                    i;
487     SimdFloat              vf;
488     SimdDouble             vd0;
489     FloatingPointTolerance tolerance(defaultRealTolerance());
490
491     for (i = 0; i < GMX_SIMD_FLOAT_WIDTH; i++)
492     {
493         // Scale by 1+100*eps to use low bits too.
494         // Due to the conversions we want to avoid being too sensitive to fluctuations in last bit
495         f[i] = i * (1.0 + 100 * GMX_FLOAT_EPS);
496     }
497
498     vf = load<SimdFloat>(f);
499 #        if (GMX_SIMD_FLOAT_WIDTH == 2 * GMX_SIMD_DOUBLE_WIDTH)
500     SimdDouble vd1;
501     cvtF2DD(vf, &vd0, &vd1);
502     store(d + GMX_SIMD_DOUBLE_WIDTH, vd1); // Store upper part halfway through array
503 #        elif (GMX_SIMD_FLOAT_WIDTH == GMX_SIMD_DOUBLE_WIDTH)
504     vd0 = cvtF2D(vf);
505 #        else
506 #            error Width of float SIMD must either be identical to double, or twice the width.
507 #        endif
508     store(d, vd0); // store lower (or whole) part from start of vector
509
510     for (i = 0; i < GMX_SIMD_FLOAT_WIDTH; i++)
511     {
512         EXPECT_REAL_EQ_TOL(f[i], d[i], tolerance);
513     }
514 }
515
516 TEST_F(SimdFloatingpointTest, cvtDouble2Float)
517 {
518     alignas(GMX_SIMD_ALIGNMENT) float f[GMX_SIMD_FLOAT_WIDTH];
519     alignas(GMX_SIMD_ALIGNMENT) double d[GMX_SIMD_FLOAT_WIDTH]; // Yes, double array length should be same as float
520     int                                i;
521     SimdFloat                          vf;
522     SimdDouble                         vd0;
523     FloatingPointTolerance tolerance(defaultRealTolerance());
524
525     // This fills elements for pd1 too when double width is 2*single width
526     for (i = 0; i < GMX_SIMD_FLOAT_WIDTH; i++)
527     {
528         // Scale by 1+eps to use low bits too.
529         // Due to the conversions we want to avoid being too sensitive to fluctuations in last bit
530         d[i] = i * (1.0 + 100 * GMX_FLOAT_EPS);
531     }
532
533     vd0 = load<SimdDouble>(d);
534 #        if (GMX_SIMD_FLOAT_WIDTH == 2 * GMX_SIMD_DOUBLE_WIDTH)
535     SimdDouble vd1 = load<SimdDouble>(d + GMX_SIMD_DOUBLE_WIDTH); // load upper half of data
536     vf             = cvtDD2F(vd0, vd1);
537 #        elif (GMX_SIMD_FLOAT_WIDTH == GMX_SIMD_DOUBLE_WIDTH)
538     vf  = cvtD2F(vd0);
539 #        else
540 #            error Width of float SIMD must either be identical to double, or twice the width.
541 #        endif
542     store(f, vf);
543
544     // This will check elements in pd1 too when double width is 2*single width
545     for (i = 0; i < GMX_SIMD_FLOAT_WIDTH; i++)
546     {
547         EXPECT_FLOAT_EQ_TOL(d[i], f[i], tolerance);
548     }
549 }
550 #    endif // GMX_SIMD_HAVE_FLOAT && GMX_SIMD_HAVE_DOUBLE
551
552 /*! \} */
553 /*! \endcond */
554
555 } // namespace
556 } // namespace test
557 } // namespace gmx
558
559 #endif // GMX_SIMD