Enable clang tidy/warnings for tests
[alexxy/gromacs.git] / src / gromacs / simd / tests / scalar.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, 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 <gtest/gtest.h>
40
41 #include "gromacs/simd/simd.h"
42 #include "gromacs/utility/basedefinitions.h"
43 #include "gromacs/utility/real.h"
44
45 #include "testutils/testasserts.h"
46
47 #include "data.h"
48
49 namespace gmx
50 {
51 namespace test
52 {
53
54 namespace
55 {
56
57 /*! \cond internal */
58 /*! \addtogroup module_simd */
59 /*! \{ */
60
61 /************************
62  * Floating-point tests *
63  ************************/
64
65 TEST(SimdScalarTest, load)
66 {
67     real val = load<real>(&c1);
68
69     EXPECT_EQ(c1, val);
70 }
71
72 TEST(SimdScalarTest, loadU)
73 {
74     real val = loadU<real>(&c1);
75
76     EXPECT_EQ(c1, val);
77 }
78
79 TEST(SimdScalarTest, store)
80 {
81     real val;
82
83     store(&val, c1);
84
85     EXPECT_EQ(c1, val);
86 }
87
88 TEST(SimdScalarTest, storeU)
89 {
90     real val;
91
92     store(&val, c1);
93
94     EXPECT_EQ(c1, val);
95 }
96
97 TEST(SimdScalarTest, setZero)
98 {
99     real val = setZero();
100
101     EXPECT_EQ(0, val);
102 }
103
104 TEST(SimdScalarTest, andNot)
105 {
106     real signBit = GMX_REAL_NEGZERO;
107
108     EXPECT_EQ(c1, andNot(signBit, -c1));
109     EXPECT_EQ(c2, andNot(signBit, c2));
110 }
111
112 TEST(SimdScalarTest, fma)
113 {
114     EXPECT_REAL_EQ_TOL(c1*c2+c3, fma(real(c1), real(c2), real(c3)), defaultRealTolerance());
115 }
116
117 TEST(SimdScalarTest, fms)
118 {
119     EXPECT_REAL_EQ_TOL(c1*c2-c3, fms(c1, c2, c3), defaultRealTolerance());
120 }
121
122 TEST(SimdScalarTest, fnma)
123 {
124     EXPECT_REAL_EQ_TOL(-c1*c2+c3, fnma(c1, c2, c3), defaultRealTolerance());
125 }
126
127 TEST(SimdScalarTest, fnms)
128 {
129     EXPECT_REAL_EQ_TOL(-c1*c2-c3, fnms(c1, c2, c3), defaultRealTolerance());
130 }
131
132 TEST(SimdScalarTest, maskAdd)
133 {
134     EXPECT_REAL_EQ_TOL(c1, maskAdd(c1, c2, false), defaultRealTolerance());
135     EXPECT_REAL_EQ_TOL(c1+c2, maskAdd(c1, c2, true), defaultRealTolerance());
136 }
137
138 TEST(SimdScalarTest, maskzMul)
139 {
140     EXPECT_REAL_EQ_TOL(czero, maskzMul(c1, c2, false), defaultRealTolerance());
141     EXPECT_REAL_EQ_TOL(c1*c2, maskzMul(c1, c2, true), defaultRealTolerance());
142 }
143
144 TEST(SimdScalarTest, maskzFma)
145 {
146     EXPECT_REAL_EQ_TOL(czero, maskzFma(c1, c2, c3, false), defaultRealTolerance());
147     EXPECT_REAL_EQ_TOL(c1*c2+c3, maskzFma(c1, c2, c3, true), defaultRealTolerance());
148 }
149
150 TEST(SimdScalarTest, abs)
151 {
152     EXPECT_EQ(c1, abs(-c1));
153 }
154
155 TEST(SimdScalarTest, max)
156 {
157     EXPECT_EQ(c3, max(c1, c3));
158 }
159
160 TEST(SimdScalarTest, min)
161 {
162     EXPECT_EQ(c1, min(c1, c3));
163 }
164
165 TEST(SimdScalarTest, round)
166 {
167     EXPECT_EQ(real(2), round(real(2.25)));
168     EXPECT_EQ(real(4), round(real(3.75)));
169     EXPECT_EQ(real(-2), round(real(-2.25)));
170     EXPECT_EQ(real(-4), round(real(-3.75)));
171 }
172
173 TEST(SimdScalarTest, trunc)
174 {
175     EXPECT_EQ(real(2), trunc(real(2.25)));
176     EXPECT_EQ(real(3), trunc(real(3.75)));
177     EXPECT_EQ(real(-2), trunc(real(-2.25)));
178     EXPECT_EQ(real(-3), trunc(real(-3.75)));
179 }
180
181 TEST(SimdScalarTest, reduce)
182 {
183     EXPECT_EQ(c1, reduce(c1));
184 }
185
186 TEST(SimdScalarTest, testBits)
187 {
188     EXPECT_TRUE(testBits(c1));
189     EXPECT_TRUE(testBits(GMX_REAL_NEGZERO));
190     EXPECT_FALSE(testBits(czero));
191 }
192
193 TEST(SimdScalarTest, anyTrue)
194 {
195     EXPECT_TRUE(anyTrue(true));
196     EXPECT_FALSE(anyTrue(false));
197 }
198
199 TEST(SimdScalarTest, selectByMask)
200 {
201     EXPECT_EQ(c1, selectByMask(c1, true));
202     EXPECT_EQ(czero, selectByMask(c1, false));
203 }
204
205 TEST(SimdScalarTest, selectByNotMask)
206 {
207     EXPECT_EQ(czero, selectByNotMask(c1, true));
208     EXPECT_EQ(c1, selectByNotMask(c1, false));
209 }
210
211 TEST(SimdScalarTest, blend)
212 {
213     EXPECT_EQ(c2, blend(c1, c2, true));
214     EXPECT_EQ(c1, blend(c1, c2, false));
215 }
216
217 TEST(SimdScalarTest, cvtR2I)
218 {
219     EXPECT_EQ(std::int32_t(4), cvtR2I(3.75));
220     EXPECT_EQ(std::int32_t(-4), cvtR2I(-3.75));
221 }
222
223 TEST(SimdScalarTest, cvttR2I)
224 {
225     EXPECT_EQ(std::int32_t(3), cvttR2I(3.75));
226     EXPECT_EQ(std::int32_t(-3), cvttR2I(-3.75));
227 }
228
229 TEST(SimdScalarTest, cvtI2R)
230 {
231     EXPECT_EQ(real(2.0), cvtI2R(2));
232     EXPECT_EQ(real(-2.0), cvtI2R(-2));
233 }
234
235 TEST(SimdScalarTest, cvtF2D)
236 {
237     float f = 1.23456789;
238
239     EXPECT_EQ(double(f), cvtF2D(f));
240 }
241
242 TEST(SimdScalarTest, cvtD2D)
243 {
244     double d = 1.23456789;
245
246     EXPECT_EQ(float(d), cvtD2F(d));
247 }
248
249
250 /*****************
251  * Integer tests *
252  *****************/
253
254
255 TEST(SimdScalarTest, loadI)
256 {
257     std::int32_t ref = 42;
258     std::int32_t val = load<int32_t>(&ref);
259
260     EXPECT_EQ(ref, val);
261 }
262
263 TEST(SimdScalarTest, loadUI)
264 {
265     std::int32_t ref = 42;
266     std::int32_t val = loadU<int32_t>(&ref);
267
268     EXPECT_EQ(ref, val);
269 }
270
271 TEST(SimdScalarTest, storeI)
272 {
273     std::int32_t ref = 42;
274     std::int32_t val;
275
276     storeU(&val, ref);
277
278     EXPECT_EQ(ref, val);
279 }
280
281 TEST(SimdScalarTest, storeUI)
282 {
283     std::int32_t ref = 42;
284     std::int32_t val;
285
286     storeU(&val, ref);
287
288     EXPECT_EQ(ref, val);
289 }
290
291 TEST(SimdScalarTest, andNotI)
292 {
293     EXPECT_EQ(std::int32_t(0x0C0C0C0C),
294               andNot(std::int32_t(0xF0F0F0F0), std::int32_t(0xCCCCCCCC)));
295 }
296
297 TEST(SimdScalarTest, testBitsI)
298 {
299     EXPECT_TRUE(testBits(1));
300     EXPECT_FALSE(testBits(0));
301 }
302
303 TEST(SimdScalarTest, selectByMaskI)
304 {
305     EXPECT_EQ(5, selectByMask(5, true));
306     EXPECT_EQ(0, selectByMask(5, false));
307 }
308
309 TEST(SimdScalarTest, selectByNotMaskI)
310 {
311     EXPECT_EQ(0, selectByNotMask(5, true));
312     EXPECT_EQ(5, selectByNotMask(5, false));
313 }
314
315 TEST(SimdScalarTest, blendI)
316 {
317     EXPECT_EQ(5, blend(3, 5, true));
318     EXPECT_EQ(3, blend(3, 5, false));
319 }
320
321 TEST(SimdScalarTest, cvtB2IB)
322 {
323     EXPECT_TRUE(cvtB2IB(true));
324     EXPECT_FALSE(cvtB2IB(false));
325 }
326
327 TEST(SimdScalarTest, cvtIB2B)
328 {
329     EXPECT_TRUE(cvtIB2B(true));
330     EXPECT_FALSE(cvtIB2B(false));
331 }
332
333 /*! \} */
334 /*! \endcond internal */
335
336 }      // namespace anonymous
337 }      // namespace test
338 }      // namespace gmx