Introduce SimulatorBuilder
[gromacs.git] / src / gromacs / simd / tests / scalar.cpp
blob9ae28508eb3ccdda04c4a6c20e5f9780765a10e4
1 /*
2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
35 #include "gmxpre.h"
37 #include <cmath>
39 #include <gtest/gtest.h>
41 #include "gromacs/simd/simd.h"
42 #include "gromacs/utility/basedefinitions.h"
43 #include "gromacs/utility/real.h"
45 #include "testutils/testasserts.h"
47 #include "data.h"
49 namespace gmx
51 namespace test
54 namespace
57 /*! \cond internal */
58 /*! \addtogroup module_simd */
59 /*! \{ */
61 /************************
62 * Floating-point tests *
63 ************************/
65 TEST(SimdScalarTest, load)
67 real val = load<real>(&c1);
69 EXPECT_EQ(c1, val);
72 TEST(SimdScalarTest, loadU)
74 real val = loadU<real>(&c1);
76 EXPECT_EQ(c1, val);
79 TEST(SimdScalarTest, store)
81 real val;
83 store(&val, c1);
85 EXPECT_EQ(c1, val);
88 TEST(SimdScalarTest, storeU)
90 real val;
92 store(&val, c1);
94 EXPECT_EQ(c1, val);
97 TEST(SimdScalarTest, setZero)
99 real val = setZero();
101 EXPECT_EQ(0, val);
104 TEST(SimdScalarTest, andNot)
106 real signBit = GMX_REAL_NEGZERO;
108 EXPECT_EQ(c1, andNot(signBit, -c1));
109 EXPECT_EQ(c2, andNot(signBit, c2));
112 TEST(SimdScalarTest, fma)
114 EXPECT_REAL_EQ_TOL(c1*c2+c3, fma(real(c1), real(c2), real(c3)), defaultRealTolerance());
117 TEST(SimdScalarTest, fms)
119 EXPECT_REAL_EQ_TOL(c1*c2-c3, fms(c1, c2, c3), defaultRealTolerance());
122 TEST(SimdScalarTest, fnma)
124 EXPECT_REAL_EQ_TOL(-c1*c2+c3, fnma(c1, c2, c3), defaultRealTolerance());
127 TEST(SimdScalarTest, fnms)
129 EXPECT_REAL_EQ_TOL(-c1*c2-c3, fnms(c1, c2, c3), defaultRealTolerance());
132 TEST(SimdScalarTest, maskAdd)
134 EXPECT_REAL_EQ_TOL(c1, maskAdd(c1, c2, false), defaultRealTolerance());
135 EXPECT_REAL_EQ_TOL(c1+c2, maskAdd(c1, c2, true), defaultRealTolerance());
138 TEST(SimdScalarTest, maskzMul)
140 EXPECT_REAL_EQ_TOL(czero, maskzMul(c1, c2, false), defaultRealTolerance());
141 EXPECT_REAL_EQ_TOL(c1*c2, maskzMul(c1, c2, true), defaultRealTolerance());
144 TEST(SimdScalarTest, maskzFma)
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());
150 TEST(SimdScalarTest, abs)
152 EXPECT_EQ(c1, abs(-c1));
155 TEST(SimdScalarTest, max)
157 EXPECT_EQ(c3, max(c1, c3));
160 TEST(SimdScalarTest, min)
162 EXPECT_EQ(c1, min(c1, c3));
165 TEST(SimdScalarTest, round)
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)));
173 TEST(SimdScalarTest, trunc)
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)));
181 TEST(SimdScalarTest, reduce)
183 EXPECT_EQ(c1, reduce(c1));
186 TEST(SimdScalarTest, testBits)
188 EXPECT_TRUE(testBits(c1));
189 EXPECT_TRUE(testBits(GMX_REAL_NEGZERO));
190 EXPECT_FALSE(testBits(czero));
193 TEST(SimdScalarTest, anyTrue)
195 EXPECT_TRUE(anyTrue(true));
196 EXPECT_FALSE(anyTrue(false));
199 TEST(SimdScalarTest, selectByMask)
201 EXPECT_EQ(c1, selectByMask(c1, true));
202 EXPECT_EQ(czero, selectByMask(c1, false));
205 TEST(SimdScalarTest, selectByNotMask)
207 EXPECT_EQ(czero, selectByNotMask(c1, true));
208 EXPECT_EQ(c1, selectByNotMask(c1, false));
211 TEST(SimdScalarTest, blend)
213 EXPECT_EQ(c2, blend(c1, c2, true));
214 EXPECT_EQ(c1, blend(c1, c2, false));
217 TEST(SimdScalarTest, cvtR2I)
219 EXPECT_EQ(std::int32_t(4), cvtR2I(3.75));
220 EXPECT_EQ(std::int32_t(-4), cvtR2I(-3.75));
223 TEST(SimdScalarTest, cvttR2I)
225 EXPECT_EQ(std::int32_t(3), cvttR2I(3.75));
226 EXPECT_EQ(std::int32_t(-3), cvttR2I(-3.75));
229 TEST(SimdScalarTest, cvtI2R)
231 EXPECT_EQ(real(2.0), cvtI2R(2));
232 EXPECT_EQ(real(-2.0), cvtI2R(-2));
235 TEST(SimdScalarTest, cvtF2D)
237 float f = 1.23456789;
239 EXPECT_EQ(double(f), cvtF2D(f));
242 TEST(SimdScalarTest, cvtD2D)
244 double d = 1.23456789;
246 EXPECT_EQ(float(d), cvtD2F(d));
250 /*****************
251 * Integer tests *
252 *****************/
255 TEST(SimdScalarTest, loadI)
257 std::int32_t ref = 42;
258 std::int32_t val = load<int32_t>(&ref);
260 EXPECT_EQ(ref, val);
263 TEST(SimdScalarTest, loadUI)
265 std::int32_t ref = 42;
266 std::int32_t val = loadU<int32_t>(&ref);
268 EXPECT_EQ(ref, val);
271 TEST(SimdScalarTest, storeI)
273 std::int32_t ref = 42;
274 std::int32_t val;
276 storeU(&val, ref);
278 EXPECT_EQ(ref, val);
281 TEST(SimdScalarTest, storeUI)
283 std::int32_t ref = 42;
284 std::int32_t val;
286 storeU(&val, ref);
288 EXPECT_EQ(ref, val);
291 TEST(SimdScalarTest, andNotI)
293 EXPECT_EQ(std::int32_t(0x0C0C0C0C),
294 andNot(std::int32_t(0xF0F0F0F0), std::int32_t(0xCCCCCCCC)));
297 TEST(SimdScalarTest, testBitsI)
299 EXPECT_TRUE(testBits(1));
300 EXPECT_FALSE(testBits(0));
303 TEST(SimdScalarTest, selectByMaskI)
305 EXPECT_EQ(5, selectByMask(5, true));
306 EXPECT_EQ(0, selectByMask(5, false));
309 TEST(SimdScalarTest, selectByNotMaskI)
311 EXPECT_EQ(0, selectByNotMask(5, true));
312 EXPECT_EQ(5, selectByNotMask(5, false));
315 TEST(SimdScalarTest, blendI)
317 EXPECT_EQ(5, blend(3, 5, true));
318 EXPECT_EQ(3, blend(3, 5, false));
321 TEST(SimdScalarTest, cvtB2IB)
323 EXPECT_TRUE(cvtB2IB(true));
324 EXPECT_FALSE(cvtB2IB(false));
327 TEST(SimdScalarTest, cvtIB2B)
329 EXPECT_TRUE(cvtIB2B(true));
330 EXPECT_FALSE(cvtIB2B(false));
333 /*! \} */
334 /*! \endcond internal */
336 } // namespace
337 } // namespace test
338 } // namespace gmx