Improved SIMD test data to use all bits
[gromacs.git] / src / gromacs / simd / tests / scalar.cpp
blob540d3b1398795aa0af7187cc792bf52fd8fecdb3
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2014,2015,2016,2017, 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 "data.h"
47 namespace gmx
49 namespace test
52 namespace
55 /*! \cond internal */
56 /*! \addtogroup module_simd */
57 /*! \{ */
59 /************************
60 * Floating-point tests *
61 ************************/
63 TEST(SimdScalarTest, load)
65 real val = load(&c1);
67 EXPECT_EQ(c1, val);
70 TEST(SimdScalarTest, loadU)
72 real val = loadU(&c1);
74 EXPECT_EQ(c1, val);
77 TEST(SimdScalarTest, store)
79 real val;
81 store(&val, c1);
83 EXPECT_EQ(c1, val);
86 TEST(SimdScalarTest, storeU)
88 real val;
90 store(&val, c1);
92 EXPECT_EQ(c1, val);
95 TEST(SimdScalarTest, setZero)
97 real val = setZero();
99 EXPECT_EQ(0, val);
102 TEST(SimdScalarTest, andNot)
104 real signBit = GMX_REAL_NEGZERO;
106 EXPECT_EQ(c1, andNot(signBit, -c1));
107 EXPECT_EQ(c2, andNot(signBit, c2));
110 TEST(SimdScalarTest, fma)
112 EXPECT_EQ(c1*c2+c3, fma(real(c1), real(c2), real(c3)));
115 TEST(SimdScalarTest, fms)
117 EXPECT_EQ(c1*c2-c3, fms(c1, c2, c3));
120 TEST(SimdScalarTest, fnma)
122 EXPECT_EQ(-c1*c2+c3, fnma(c1, c2, c3));
125 TEST(SimdScalarTest, fnms)
127 EXPECT_EQ(-c1*c2-c3, fnms(c1, c2, c3));
130 TEST(SimdScalarTest, maskAdd)
132 EXPECT_EQ(c1, maskAdd(c1, c2, false));
133 EXPECT_EQ(c1+c2, maskAdd(c1, c2, true));
136 TEST(SimdScalarTest, maskzMul)
138 EXPECT_EQ(czero, maskzMul(c1, c2, false));
139 EXPECT_EQ(c1*c2, maskzMul(c1, c2, true));
142 TEST(SimdScalarTest, maskzFma)
144 EXPECT_EQ(czero, maskzFma(c1, c2, c3, false));
145 EXPECT_EQ(c1*c2+c3, maskzFma(c1, c2, c3, true));
148 TEST(SimdScalarTest, abs)
150 EXPECT_EQ(c1, abs(-c1));
153 TEST(SimdScalarTest, max)
155 EXPECT_EQ(c3, max(c1, c3));
158 TEST(SimdScalarTest, min)
160 EXPECT_EQ(c1, min(c1, c3));
163 TEST(SimdScalarTest, round)
165 EXPECT_EQ(real(2), round(real(2.25)));
166 EXPECT_EQ(real(4), round(real(3.75)));
167 EXPECT_EQ(real(-2), round(real(-2.25)));
168 EXPECT_EQ(real(-4), round(real(-3.75)));
171 TEST(SimdScalarTest, trunc)
173 EXPECT_EQ(real(2), trunc(real(2.25)));
174 EXPECT_EQ(real(3), trunc(real(3.75)));
175 EXPECT_EQ(real(-2), trunc(real(-2.25)));
176 EXPECT_EQ(real(-3), trunc(real(-3.75)));
179 TEST(SimdScalarTest, reduce)
181 EXPECT_EQ(c1, reduce(c1));
184 TEST(SimdScalarTest, testBits)
186 EXPECT_TRUE(testBits(c1));
187 EXPECT_TRUE(testBits(GMX_REAL_NEGZERO));
188 EXPECT_FALSE(testBits(czero));
191 TEST(SimdScalarTest, anyTrue)
193 EXPECT_TRUE(anyTrue(true));
194 EXPECT_FALSE(anyTrue(false));
197 TEST(SimdScalarTest, selectByMask)
199 EXPECT_EQ(c1, selectByMask(c1, true));
200 EXPECT_EQ(czero, selectByMask(c1, false));
203 TEST(SimdScalarTest, selectByNotMask)
205 EXPECT_EQ(czero, selectByNotMask(c1, true));
206 EXPECT_EQ(c1, selectByNotMask(c1, false));
209 TEST(SimdScalarTest, blend)
211 EXPECT_EQ(c2, blend(c1, c2, true));
212 EXPECT_EQ(c1, blend(c1, c2, false));
215 TEST(SimdScalarTest, cvtR2I)
217 EXPECT_EQ(std::int32_t(4), cvtR2I(3.75));
218 EXPECT_EQ(std::int32_t(-4), cvtR2I(-3.75));
221 TEST(SimdScalarTest, cvttR2I)
223 EXPECT_EQ(std::int32_t(3), cvttR2I(3.75));
224 EXPECT_EQ(std::int32_t(-3), cvttR2I(-3.75));
227 TEST(SimdScalarTest, cvtI2R)
229 EXPECT_EQ(real(2.0), cvtI2R(2));
230 EXPECT_EQ(real(-2.0), cvtI2R(-2));
233 TEST(SimdScalarTest, cvtF2D)
235 float f = 1.23456789;
237 EXPECT_EQ(double(f), cvtF2D(f));
240 TEST(SimdScalarTest, cvtD2D)
242 double d = 1.23456789;
244 EXPECT_EQ(float(d), cvtD2F(d));
248 /*****************
249 * Integer tests *
250 *****************/
253 TEST(SimdScalarTest, loadI)
255 std::int32_t ref = 42;
256 std::int32_t val = load(&ref);
258 EXPECT_EQ(ref, val);
261 TEST(SimdScalarTest, loadUI)
263 std::int32_t ref = 42;
264 std::int32_t val = load(&ref);
266 EXPECT_EQ(ref, val);
269 TEST(SimdScalarTest, storeI)
271 std::int32_t ref = 42;
272 std::int32_t val;
274 storeU(&val, ref);
276 EXPECT_EQ(ref, val);
279 TEST(SimdScalarTest, storeUI)
281 std::int32_t ref = 42;
282 std::int32_t val;
284 storeU(&val, ref);
286 EXPECT_EQ(ref, val);
289 TEST(SimdScalarTest, andNotI)
291 EXPECT_EQ(std::int32_t(0x0C0C0C0C),
292 andNot(std::int32_t(0xF0F0F0F0), std::int32_t(0xCCCCCCCC)));
295 TEST(SimdScalarTest, testBitsI)
297 EXPECT_TRUE(testBits(1));
298 EXPECT_FALSE(testBits(0));
301 TEST(SimdScalarTest, selectByMaskI)
303 EXPECT_EQ(5, selectByMask(5, true));
304 EXPECT_EQ(0, selectByMask(5, false));
307 TEST(SimdScalarTest, selectByNotMaskI)
309 EXPECT_EQ(0, selectByNotMask(5, true));
310 EXPECT_EQ(5, selectByNotMask(5, false));
313 TEST(SimdScalarTest, blendI)
315 EXPECT_EQ(5, blend(3, 5, true));
316 EXPECT_EQ(3, blend(3, 5, false));
319 TEST(SimdScalarTest, cvtB2IB)
321 EXPECT_TRUE(cvtB2IB(true));
322 EXPECT_FALSE(cvtB2IB(false));
325 TEST(SimdScalarTest, cvtIB2B)
327 EXPECT_TRUE(cvtIB2B(true));
328 EXPECT_FALSE(cvtIB2B(false));
331 /*! \} */
332 /*! \endcond internal */
334 } // namespace anonymous
335 } // namespace test
336 } // namespace gmx