Apply clang-format to source tree
[alexxy/gromacs.git] / src / gromacs / simd / tests / simd_integer.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 <array>
38
39 #include "gromacs/simd/simd.h"
40 #include "gromacs/utility/basedefinitions.h"
41
42 #include "simd.h"
43
44 /* Some notes on the setup of these tests:
45  *
46  * It might seem strange to mix different instructions for "setting" SIMD
47  * registers, but the difference is that the routines like setSimdIntFrom1I()
48  * only use the load/store operations that we already test separately in
49  * bootstrap_loadstore.cpp. Since these are "known good" if the bootstrap
50  * tests pass, we use them to test the normal SIMD implementation instructions.
51  */
52
53 #if GMX_SIMD
54
55 namespace gmx
56 {
57 namespace test
58 {
59 namespace
60 {
61
62 /*! \cond internal */
63 /*! \addtogroup module_simd */
64 /*! \{ */
65
66 /*! \brief Test fixture for integer tests (identical to the generic \ref SimdTest) */
67 typedef SimdTest SimdIntegerTest;
68
69 /* Yes, Virginia. We test for real even for integers. This is because we use
70  * the floating-point type when no real integer SIMD type exists (which in turn
71  * is because the results of real-to-integer conversions end up there). This
72  * means the basic integer SIMD type is available whenever the real one is,
73  * but depending on the precision selected that might not be the case.
74  *
75  * The second we have default-precision floating-point SIMD, we also have
76  * the integer SIMD dataype and the most fundamental load/store ops.
77  */
78 #    if GMX_SIMD_HAVE_REAL
79
80 TEST_F(SimdIntegerTest, setZero)
81 {
82     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(0), setZero());
83 }
84 TEST_F(SimdIntegerTest, set)
85 {
86     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(1), SimdInt32(1));
87 }
88 #    endif // GMX_SIMD_HAVE_REAL
89
90 #    if GMX_SIMD_HAVE_INT32_ARITHMETICS
91 TEST_F(SimdIntegerTest, add)
92 {
93     GMX_EXPECT_SIMD_INT_EQ(iSimd_5_7_9, iSimd_1_2_3 + iSimd_4_5_6);          // short add
94     GMX_EXPECT_SIMD_INT_EQ(iSimd_5M_7M_9M, iSimd_1M_2M_3M + iSimd_4M_5M_6M); // 32 bit add
95 }
96
97 TEST_F(SimdIntegerTest, sub)
98 {
99     GMX_EXPECT_SIMD_INT_EQ(iSimd_1_2_3, iSimd_5_7_9 - iSimd_4_5_6);          // short sub
100     GMX_EXPECT_SIMD_INT_EQ(iSimd_1M_2M_3M, iSimd_5M_7M_9M - iSimd_4M_5M_6M); // 32 bit sub
101 }
102
103 TEST_F(SimdIntegerTest, mul)
104 {
105     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom3I(4, 10, 18), iSimd_1_2_3 * iSimd_4_5_6); // 2*3=6 (short mul)
106     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(268435456),
107                            SimdInt32(16384) * SimdInt32(16384)); // 16384*16384 = 268435456 (long mul)
108 }
109
110 #    endif // GMX_SIMD_HAVE_INT32_ARITHMETICS
111
112 #    if GMX_SIMD_HAVE_INT32_LOGICAL
113 TEST_F(SimdIntegerTest, and)
114 {
115     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(0xC0C0C0C0), iSimd_0xF0F0F0F0 & iSimd_0xCCCCCCCC);
116 }
117
118 TEST_F(SimdIntegerTest, andNot)
119 {
120     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(0x0C0C0C0C), andNot(iSimd_0xF0F0F0F0, iSimd_0xCCCCCCCC));
121 }
122
123 TEST_F(SimdIntegerTest, or)
124 {
125     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(0xFCFCFCFC), iSimd_0xF0F0F0F0 | iSimd_0xCCCCCCCC);
126 }
127
128 TEST_F(SimdIntegerTest, xor)
129 {
130     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(0x3C3C3C3C), iSimd_0xF0F0F0F0 ^ iSimd_0xCCCCCCCC);
131 }
132 #    endif // GMX_SIMD_HAVE_INT32_LOGICAL
133
134 #    if GMX_SIMD_HAVE_INT32_EXTRACT
135 TEST_F(SimdIntegerTest, extract)
136 {
137     alignas(GMX_SIMD_ALIGNMENT) std::int32_t idata[GMX_SIMD_REAL_WIDTH];
138     SimdInt32                                simd;
139
140     for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
141     {
142         idata[i] = i + 1;
143     }
144     simd = load<SimdInt32>(idata);
145
146     /* We cannot do a loop here, since
147      * - C++ gets confused about signed/unsigned if SSE macros are used in EXPECT_EQ()
148      * - Extract macros can only take immediates (not variables) on some archs,
149      *   and some compilers are not smart enough to expand the for loop.
150      *
151      * To solve this we use a few values manually instead of a for-loop.
152      */
153     int extracted_int;
154     extracted_int = extract<0>(simd);
155     EXPECT_EQ(1, extracted_int);
156 #        if GMX_SIMD_REAL_WIDTH >= 2
157     extracted_int = extract<1>(simd);
158     EXPECT_EQ(2, extracted_int);
159 #        endif
160 #        if GMX_SIMD_REAL_WIDTH >= 4
161     extracted_int = extract<3>(simd);
162     EXPECT_EQ(4, extracted_int);
163 #        endif
164 #        if GMX_SIMD_REAL_WIDTH >= 6
165     extracted_int = extract<5>(simd);
166     EXPECT_EQ(6, extracted_int);
167 #        endif
168 #        if GMX_SIMD_REAL_WIDTH >= 8
169     extracted_int = extract<7>(simd);
170     EXPECT_EQ(8, extracted_int);
171 #        endif
172 }
173 #    endif // GMX_SIMD_HAVE_INT32_EXTRACT
174
175 #    if GMX_SIMD_HAVE_REAL
176 TEST_F(SimdIntegerTest, cvtR2I)
177 {
178     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(2), cvtR2I(rSimd_2p25));
179     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(-2), cvtR2I(rSimd_m2p25));
180     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(4), cvtR2I(rSimd_3p75));
181     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(-4), cvtR2I(rSimd_m3p75));
182     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(3), cvtR2I(rSimd_3p25));
183     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(-3), cvtR2I(rSimd_m3p25));
184
185     // Test multi-byte numbers
186     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(123457), cvtR2I(setSimdRealFrom1R(123456.7)));
187     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(-123457), cvtR2I(setSimdRealFrom1R(-123456.7)));
188     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(123456), cvtR2I(setSimdRealFrom1R(123456.3)));
189     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(-123456), cvtR2I(setSimdRealFrom1R(-123456.3)));
190
191 #        if GMX_DOUBLE
192     // Test number with more digits than we can represent in single.
193     // Note that our SIMD integers are only 32 bits, so we cannot go beyond that.
194     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(12345679), cvtR2I(setSimdRealFrom1R(12345678.6)));
195     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(-12345679), cvtR2I(setSimdRealFrom1R(-12345678.6)));
196     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(12345678), cvtR2I(setSimdRealFrom1R(12345678.3)));
197     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(-12345678), cvtR2I(setSimdRealFrom1R(-12345678.3)));
198 #        endif
199 }
200
201 TEST_F(SimdIntegerTest, cvttR2I)
202 {
203     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(2), cvttR2I(rSimd_2p25));
204     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(-2), cvttR2I(rSimd_m2p25));
205     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(3), cvttR2I(rSimd_3p75));
206     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(-3), cvttR2I(rSimd_m3p75));
207     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(3), cvttR2I(rSimd_3p25));
208     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(-3), cvttR2I(rSimd_m3p25));
209
210     // Test multi-byte numbers
211     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(123456), cvttR2I(setSimdRealFrom1R(123456.7)));
212     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(-123456), cvttR2I(setSimdRealFrom1R(-123456.7)));
213     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(123456), cvttR2I(setSimdRealFrom1R(123456.3)));
214     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(-123456), cvttR2I(setSimdRealFrom1R(-123456.3)));
215
216 #        if GMX_DOUBLE
217     // Test number with more digits than we can represent in single.
218     // Note that our SIMD integers are only 32 bits, so we cannot go beyond that.
219     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(12345678), cvttR2I(setSimdRealFrom1R(12345678.6)));
220     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(-12345678), cvttR2I(setSimdRealFrom1R(-12345678.6)));
221     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(12345678), cvttR2I(setSimdRealFrom1R(12345678.3)));
222     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom1I(-12345678), cvttR2I(setSimdRealFrom1R(-12345678.3)));
223 #        endif
224 }
225
226 TEST_F(SimdIntegerTest, cvtI2R)
227 {
228     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(2.0), cvtI2R(SimdInt32(2)));
229     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(-2.0), cvtI2R(SimdInt32(-2)));
230     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(102448689), cvtI2R(SimdInt32(102448689)));
231     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom1R(-102448689), cvtI2R(SimdInt32(-102448689)));
232 }
233 #    endif // GMX_SIMD_HAVE_REAL
234
235 #    if GMX_SIMD_HAVE_INT32_ARITHMETICS
236 TEST_F(SimdIntegerTest, cmpEqAndSelectMask)
237 {
238     SimdIBool eq = (iSimd_5_7_9 == iSimd_7_8_9);
239     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom3I(0, 0, 3), selectByMask(iSimd_1_2_3, eq));
240 }
241
242 TEST_F(SimdIntegerTest, cmpEqAndSelectNotMask)
243 {
244     SimdIBool eq = (iSimd_5_7_9 == iSimd_7_8_9);
245     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom3I(1, 2, 0), selectByNotMask(iSimd_1_2_3, eq));
246 }
247
248 TEST_F(SimdIntegerTest, cmpLt)
249 {
250     SimdIBool lt = (iSimd_5_7_9 < iSimd_7_8_9);
251     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom3I(1, 2, 0), selectByMask(iSimd_1_2_3, lt));
252 }
253
254 TEST_F(SimdIntegerTest, testBits)
255 {
256     SimdIBool eq = testBits(setSimdIntFrom3I(1, 0, 2));
257     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom3I(1, 0, 3), selectByMask(iSimd_1_2_3, eq));
258
259     // Test if we detect only the sign bit being set
260     eq = testBits(setSimdIntFrom1I(0x80000000));
261     GMX_EXPECT_SIMD_INT_EQ(iSimd_1_2_3, selectByMask(iSimd_1_2_3, eq));
262 }
263
264 TEST_F(SimdIntegerTest, andB)
265 {
266     SimdIBool eq1 = (iSimd_5_7_9 == iSimd_7_8_9);
267     SimdIBool eq2 = (iSimd_5_7_9 == iSimd_5_7_9);
268     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom3I(0, 0, 3), selectByMask(iSimd_1_2_3, eq1 && eq2));
269 }
270
271 TEST_F(SimdIntegerTest, orB)
272 {
273     SimdIBool eq1 = (iSimd_5_7_9 == iSimd_7_8_9);
274     SimdIBool eq2 = (iSimd_5_7_9 == setSimdIntFrom3I(5, 0, 0));
275     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom3I(1, 0, 3), selectByMask(iSimd_1_2_3, eq1 || eq2));
276 }
277
278 TEST_F(SimdIntegerTest, anyTrue)
279 {
280     alignas(GMX_SIMD_ALIGNMENT) std::array<std::int32_t, GMX_SIMD_REAL_WIDTH> mem{};
281
282     // Test the false case
283     EXPECT_FALSE(anyTrue(setZero() < load<SimdInt32>(mem.data())));
284
285     // Test each bit (these should all be true)
286     for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
287     {
288         mem.fill(0);
289         mem[i] = 1;
290         EXPECT_TRUE(anyTrue(setZero() < load<SimdInt32>(mem.data())))
291                 << "Not detecting true in element " << i;
292     }
293 }
294
295 TEST_F(SimdIntegerTest, blend)
296 {
297     SimdIBool lt = (iSimd_5_7_9 < iSimd_7_8_9);
298     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom3I(4, 5, 3), blend(iSimd_1_2_3, iSimd_4_5_6, lt));
299 }
300 #    endif // GMX_SIMD_HAVE_INT32_ARITHMETICS
301
302 #    if GMX_SIMD_HAVE_REAL && GMX_SIMD_HAVE_INT32_ARITHMETICS
303 TEST_F(SimdIntegerTest, cvtB2IB)
304 {
305     SimdBool  eq  = (rSimd_c3c4c5 == rSimd_c3c0c4); // eq should be T,F,F
306     SimdIBool eqi = cvtB2IB(eq);
307     GMX_EXPECT_SIMD_INT_EQ(setSimdIntFrom3I(1, 0, 0), selectByMask(iSimd_1_2_3, eqi));
308 }
309
310 TEST_F(SimdIntegerTest, cvtIB2B)
311 {
312     SimdIBool eqi = (iSimd_5_7_9 == setSimdIntFrom3I(5, 0, 0)); // eq should be T,F,F
313     SimdBool  eq  = cvtIB2B(eqi);
314     GMX_EXPECT_SIMD_REAL_EQ(setSimdRealFrom3R(c0, 0, 0), selectByMask(rSimd_c0c1c2, eq));
315 }
316 #    endif // GMX_SIMD_HAVE_REAL && GMX_SIMD_HAVE_INT32_ARITHMETICS
317
318 /*! \} */
319 /*! \endcond */
320
321 } // namespace
322 } // namespace test
323 } // namespace gmx
324
325 #endif // GMX_SIMD