128-bit AVX2 SIMD for AMD Ryzen
[gromacs.git] / src / gromacs / simd / tests / scalar.cpp
blob2d6418b1c25f0de1650d3caf9bc0fcbf9dd281a5
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2014,2015,2016, 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 namespace gmx
47 namespace test
50 namespace
53 /*! \cond internal */
54 /*! \addtogroup module_simd */
55 /*! \{ */
57 /************************
58 * Floating-point tests *
59 ************************/
61 TEST(SimdScalarTest, load)
63 real ref = 1.234;
64 real val = load(&ref);
66 EXPECT_EQ(ref, val);
69 TEST(SimdScalarTest, loadU)
71 real ref = 1.234;
72 real val = loadU(&ref);
74 EXPECT_EQ(ref, val);
77 TEST(SimdScalarTest, store)
79 real ref = 1.234;
80 real val;
82 store(&val, ref);
84 EXPECT_EQ(ref, val);
87 TEST(SimdScalarTest, storeU)
89 real ref = 1.234;
90 real val;
92 store(&val, ref);
94 EXPECT_EQ(ref, 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(real(1), andNot(signBit, real(-1)));
109 EXPECT_EQ(real(2), andNot(signBit, real(2)));
112 TEST(SimdScalarTest, fma)
114 EXPECT_EQ(real(27), fma(real(3), real(6), real(9))); // 3*6+9=27
117 TEST(SimdScalarTest, fms)
119 EXPECT_EQ(real(9), fms(real(3), real(6), real(9))); // 3*6-9=9
122 TEST(SimdScalarTest, fnma)
124 EXPECT_EQ(real(-9), fnma(real(3), real(6), real(9))); // -3*6+9=-9
127 TEST(SimdScalarTest, fnms)
129 EXPECT_EQ(real(-27), fnms(real(3), real(6), real(9))); // -3*6-9=-27
132 TEST(SimdScalarTest, maskAdd)
134 EXPECT_EQ(real(3), maskAdd(real(3), real(6), false));
135 EXPECT_EQ(real(9), maskAdd(real(3), real(6), true));
138 TEST(SimdScalarTest, maskzMul)
140 EXPECT_EQ(real(0), maskzMul(real(3), real(6), false));
141 EXPECT_EQ(real(18), maskzMul(real(3), real(6), true));
144 TEST(SimdScalarTest, maskzFma)
146 EXPECT_EQ(real(0), maskzFma(real(3), real(6), real(9), false));
147 EXPECT_EQ(real(27), maskzFma(real(3), real(6), real(9), true));
150 TEST(SimdScalarTest, abs)
152 EXPECT_EQ(real(3), abs(real(-3)));
155 TEST(SimdScalarTest, max)
157 EXPECT_EQ(real(3), max(real(1), real(3)));
160 TEST(SimdScalarTest, min)
162 EXPECT_EQ(real(1), min(real(1), real(3)));
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(real(5), reduce(real(5)));
186 TEST(SimdScalarTest, testBits)
188 EXPECT_TRUE(testBits(real(1)));
189 EXPECT_TRUE(testBits(GMX_REAL_NEGZERO));
190 EXPECT_FALSE(testBits(real(0)));
193 TEST(SimdScalarTest, anyTrue)
195 EXPECT_TRUE(anyTrue(true));
196 EXPECT_FALSE(anyTrue(false));
199 TEST(SimdScalarTest, selectByMask)
201 EXPECT_EQ(real(5.0), selectByMask(real(5.0), true));
202 EXPECT_EQ(real(0.0), selectByMask(real(5.0), false));
205 TEST(SimdScalarTest, selectByNotMask)
207 EXPECT_EQ(real(0.0), selectByNotMask(real(5.0), true));
208 EXPECT_EQ(real(5.0), selectByNotMask(real(5.0), false));
211 TEST(SimdScalarTest, blend)
213 EXPECT_EQ(real(5.0), blend(real(3.0), real(5.0), true));
214 EXPECT_EQ(real(3.0), blend(real(3.0), real(5.0), 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(&ref);
260 EXPECT_EQ(ref, val);
263 TEST(SimdScalarTest, loadUI)
265 std::int32_t ref = 42;
266 std::int32_t val = load(&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 anonymous
337 } // namespace test
338 } // namespace gmx