2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
40 #include <gtest/gtest.h>
42 #include "gromacs/simd/simd.h"
43 #include "gromacs/utility/basedefinitions.h"
44 #include "gromacs/utility/real.h"
46 #include "testutils/testasserts.h"
59 /*! \addtogroup module_simd */
62 /************************
63 * Floating-point tests *
64 ************************/
66 TEST(SimdScalarTest
, load
)
68 real val
= load
<real
>(&c1
);
73 TEST(SimdScalarTest
, loadU
)
75 real val
= loadU
<real
>(&c1
);
80 TEST(SimdScalarTest
, store
)
89 TEST(SimdScalarTest
, storeU
)
98 TEST(SimdScalarTest
, setZero
)
100 real val
= setZero();
105 TEST(SimdScalarTest
, andNot
)
107 real signBit
= GMX_REAL_NEGZERO
;
109 EXPECT_EQ(c1
, andNot(signBit
, -c1
));
110 EXPECT_EQ(c2
, andNot(signBit
, c2
));
113 TEST(SimdScalarTest
, fma
)
115 EXPECT_REAL_EQ_TOL(c1
* c2
+ c3
, fma(real(c1
), real(c2
), real(c3
)), defaultRealTolerance());
118 TEST(SimdScalarTest
, fms
)
120 EXPECT_REAL_EQ_TOL(c1
* c2
- c3
, fms(c1
, c2
, c3
), defaultRealTolerance());
123 TEST(SimdScalarTest
, fnma
)
125 EXPECT_REAL_EQ_TOL(-c1
* c2
+ c3
, fnma(c1
, c2
, c3
), defaultRealTolerance());
128 TEST(SimdScalarTest
, fnms
)
130 EXPECT_REAL_EQ_TOL(-c1
* c2
- c3
, fnms(c1
, c2
, c3
), defaultRealTolerance());
133 TEST(SimdScalarTest
, maskAdd
)
135 EXPECT_REAL_EQ_TOL(c1
, maskAdd(c1
, c2
, false), defaultRealTolerance());
136 EXPECT_REAL_EQ_TOL(c1
+ c2
, maskAdd(c1
, c2
, true), defaultRealTolerance());
139 TEST(SimdScalarTest
, maskzMul
)
141 EXPECT_REAL_EQ_TOL(czero
, maskzMul(c1
, c2
, false), defaultRealTolerance());
142 EXPECT_REAL_EQ_TOL(c1
* c2
, maskzMul(c1
, c2
, true), defaultRealTolerance());
145 TEST(SimdScalarTest
, maskzFma
)
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());
151 TEST(SimdScalarTest
, abs
)
153 EXPECT_EQ(c1
, abs(-c1
));
156 TEST(SimdScalarTest
, max
)
158 EXPECT_EQ(c3
, max(c1
, c3
));
161 TEST(SimdScalarTest
, min
)
163 EXPECT_EQ(c1
, min(c1
, c3
));
166 TEST(SimdScalarTest
, round
)
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)));
174 TEST(SimdScalarTest
, trunc
)
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)));
182 TEST(SimdScalarTest
, reduce
)
184 EXPECT_EQ(c1
, reduce(c1
));
187 TEST(SimdScalarTest
, testBits
)
189 EXPECT_TRUE(testBits(c1
));
190 EXPECT_TRUE(testBits(GMX_REAL_NEGZERO
));
191 EXPECT_FALSE(testBits(czero
));
194 TEST(SimdScalarTest
, anyTrue
)
196 EXPECT_TRUE(anyTrue(true));
197 EXPECT_FALSE(anyTrue(false));
200 TEST(SimdScalarTest
, selectByMask
)
202 EXPECT_EQ(c1
, selectByMask(c1
, true));
203 EXPECT_EQ(czero
, selectByMask(c1
, false));
206 TEST(SimdScalarTest
, selectByNotMask
)
208 EXPECT_EQ(czero
, selectByNotMask(c1
, true));
209 EXPECT_EQ(c1
, selectByNotMask(c1
, false));
212 TEST(SimdScalarTest
, blend
)
214 EXPECT_EQ(c2
, blend(c1
, c2
, true));
215 EXPECT_EQ(c1
, blend(c1
, c2
, false));
218 TEST(SimdScalarTest
, cvtR2I
)
220 EXPECT_EQ(std::int32_t(4), cvtR2I(3.75));
221 EXPECT_EQ(std::int32_t(-4), cvtR2I(-3.75));
224 TEST(SimdScalarTest
, cvttR2I
)
226 EXPECT_EQ(std::int32_t(3), cvttR2I(3.75));
227 EXPECT_EQ(std::int32_t(-3), cvttR2I(-3.75));
230 TEST(SimdScalarTest
, cvtI2R
)
232 EXPECT_EQ(real(2.0), cvtI2R(2));
233 EXPECT_EQ(real(-2.0), cvtI2R(-2));
236 TEST(SimdScalarTest
, cvtF2D
)
238 float f
= 1.23456789;
240 EXPECT_EQ(double(f
), cvtF2D(f
));
243 TEST(SimdScalarTest
, cvtD2D
)
245 double d
= 1.23456789;
247 EXPECT_EQ(float(d
), cvtD2F(d
));
256 TEST(SimdScalarTest
, loadI
)
258 std::int32_t ref
= 42;
259 std::int32_t val
= load
<int32_t>(&ref
);
264 TEST(SimdScalarTest
, loadUI
)
266 std::int32_t ref
= 42;
267 std::int32_t val
= loadU
<int32_t>(&ref
);
272 TEST(SimdScalarTest
, storeI
)
274 std::int32_t ref
= 42;
282 TEST(SimdScalarTest
, storeUI
)
284 std::int32_t ref
= 42;
292 TEST(SimdScalarTest
, andNotI
)
294 EXPECT_EQ(std::int32_t(0x0C0C0C0C), 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));
334 /*! \endcond internal */