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