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