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 "gromacs/simd/simd.h"
41 #include "gromacs/utility/basedefinitions.h"
51 /*! \addtogroup module_simd */
54 /* Unfortunately we cannot keep static SIMD constants in the test fixture class.
55 * The problem is that SIMD memory need to be aligned, and in particular
56 * this applies to automatic storage of variables in classes. For SSE registers
57 * this means 16-byte alignment (which seems to work), but AVX requires 32-bit
58 * alignment. At least both gcc-4.7.3 and Apple clang-5.0 (OS X 10.9) fail to
59 * align these variables when they are stored as data in a class.
61 * In theory we could set some of these on-the-fly e.g. with setSimdRealFrom3R()
62 * instead (although that would mean repeating code between tests), but many of
63 * the constants depend on the current precision not to mention they
64 * occasionally have many digits that need to be exactly right, and keeping
65 * them in a single place makes sure they are consistent.
67 # if GMX_SIMD_HAVE_REAL
68 const SimdReal rSimd_c0c1c2
= setSimdRealFrom3R(c0
, c1
, c2
);
69 const SimdReal rSimd_c3c4c5
= setSimdRealFrom3R(c3
, c4
, c5
);
70 const SimdReal rSimd_c6c7c8
= setSimdRealFrom3R(c6
, c7
, c8
);
71 const SimdReal rSimd_c3c0c4
= setSimdRealFrom3R(c3
, c0
, c4
);
72 const SimdReal rSimd_c4c6c8
= setSimdRealFrom3R(c4
, c6
, c8
);
73 const SimdReal rSimd_c7c2c3
= setSimdRealFrom3R(c7
, c2
, c3
);
74 const SimdReal rSimd_m0m1m2
= setSimdRealFrom3R(-c0
, -c1
, -c2
);
75 const SimdReal rSimd_m3m0m4
= setSimdRealFrom3R(-c3
, -c0
, -c4
);
77 const SimdReal rSimd_2p25
= setSimdRealFrom1R(2.25);
78 const SimdReal rSimd_3p25
= setSimdRealFrom1R(3.25);
79 const SimdReal rSimd_3p75
= setSimdRealFrom1R(3.75);
80 const SimdReal rSimd_m2p25
= setSimdRealFrom1R(-2.25);
81 const SimdReal rSimd_m3p25
= setSimdRealFrom1R(-3.25);
82 const SimdReal rSimd_m3p75
= setSimdRealFrom1R(-3.75);
83 const SimdReal rSimd_Exp
= setSimdRealFrom3R(1.4055235171027452623914516e+18,
84 5.3057102734253445623914516e-13,
85 -2.1057102745623934534514516e+16);
86 # if GMX_SIMD_HAVE_DOUBLE && GMX_DOUBLE
87 // Make sure we also test exponents outside single precision when we use double
88 const SimdReal rSimd_ExpDouble
= setSimdRealFrom3R(6.287393598732017379054414e+176,
89 8.794495252903116023030553e-140,
90 -3.637060701570496477655022e+202);
91 # endif // GMX_SIMD_HAVE_DOUBLE && GMX_DOUBLE
93 # if GMX_SIMD_HAVE_LOGICAL
94 // The numbers below all have exponent (2^0), which will not change with AND/OR operations.
95 // We also leave the last part of the mantissa as zeros, to avoid rounding issues in the compiler
97 const SimdReal rSimd_logicalA
=
98 setSimdRealFrom1R(1.3333333332557231188); // mantissa 01010101010101010101010101010101
99 const SimdReal rSimd_logicalB
=
100 setSimdRealFrom1R(1.7999999998137354851); // mantissa 11001100110011001100110011001100
101 const SimdReal rSimd_logicalResultAnd
=
102 setSimdRealFrom1R(1.266666666604578495); // mantissa 01000100010001000100010001000100
103 const SimdReal rSimd_logicalResultOr
=
104 setSimdRealFrom1R(1.8666666664648801088); // mantissa 11011101110111011101110111011101
106 const SimdReal rSimd_logicalA
= setSimdRealFrom1R(1.3333282470703125); // mantissa 0101010101010101
107 const SimdReal rSimd_logicalB
= setSimdRealFrom1R(1.79998779296875); // mantissa 1100110011001100
108 const SimdReal rSimd_logicalResultAnd
= setSimdRealFrom1R(1.26666259765625); // mantissa 0100010001000100
109 const SimdReal rSimd_logicalResultOr
= setSimdRealFrom1R(1.8666534423828125); // mantissa 1101110111011101
110 # endif // GMX_DOUBLE
111 # endif // GMX_SIMD_HAVE_LOGICAL
113 # endif // GMX_SIMD_HAVE_REAL
114 # if GMX_SIMD_HAVE_INT32_ARITHMETICS
115 const SimdInt32 iSimd_1_2_3
= setSimdIntFrom3I(1, 2, 3);
116 const SimdInt32 iSimd_4_5_6
= setSimdIntFrom3I(4, 5, 6);
117 const SimdInt32 iSimd_7_8_9
= setSimdIntFrom3I(7, 8, 9);
118 const SimdInt32 iSimd_5_7_9
= setSimdIntFrom3I(5, 7, 9);
119 const SimdInt32 iSimd_1M_2M_3M
= setSimdIntFrom3I(1000000, 2000000, 3000000);
120 const SimdInt32 iSimd_4M_5M_6M
= setSimdIntFrom3I(4000000, 5000000, 6000000);
121 const SimdInt32 iSimd_5M_7M_9M
= setSimdIntFrom3I(5000000, 7000000, 9000000);
123 # if GMX_SIMD_HAVE_INT32_LOGICAL
124 const SimdInt32 iSimd_0xF0F0F0F0
= setSimdIntFrom1I(0xF0F0F0F0);
125 const SimdInt32 iSimd_0xCCCCCCCC
= setSimdIntFrom1I(0xCCCCCCCC);
128 # if GMX_SIMD_HAVE_REAL
129 TEST(SimdTest
, GmxAligned
)
131 // Test alignment with two variables that must be aligned, and one that
132 // doesn't have to be. The order of variables is up to the compiler, but
133 // if it ignores alignment it is highly unlikely that both r1/r3 still end
134 // up being aligned by mistake.
135 alignas(GMX_SIMD_ALIGNMENT
) real r1
;
137 alignas(GMX_SIMD_ALIGNMENT
) real r3
;
139 std::uint64_t addr1
= reinterpret_cast<std::uint64_t>(&r1
);
140 std::uint64_t addr2
= reinterpret_cast<std::uint64_t>(&r2
);
141 std::uint64_t addr3
= reinterpret_cast<std::uint64_t>(&r3
);
143 EXPECT_EQ(0, addr1
% GMX_SIMD_ALIGNMENT
);
144 EXPECT_NE(0, addr2
); // Just so r2 is not optimized away
145 EXPECT_EQ(0, addr3
% GMX_SIMD_ALIGNMENT
);
147 alignas(GMX_SIMD_ALIGNMENT
) std::int32_t i1
;
149 alignas(GMX_SIMD_ALIGNMENT
) std::int32_t i3
;
151 addr1
= reinterpret_cast<std::uint64_t>(&i1
);
152 addr2
= reinterpret_cast<std::uint64_t>(&i2
);
153 addr3
= reinterpret_cast<std::uint64_t>(&i3
);
155 EXPECT_EQ(0, addr1
% GMX_SIMD_ALIGNMENT
);
156 EXPECT_NE(0, addr2
); // Just so i2 is not optimized away
157 EXPECT_EQ(0, addr3
% GMX_SIMD_ALIGNMENT
);
161 ::std::vector
<real
> simdReal2Vector(const SimdReal simd
)
163 alignas(GMX_SIMD_ALIGNMENT
) real mem
[GMX_SIMD_REAL_WIDTH
];
166 std::vector
<real
> v(mem
, mem
+ GMX_SIMD_REAL_WIDTH
);
171 SimdReal
vector2SimdReal(const std::vector
<real
>& v
)
173 alignas(GMX_SIMD_ALIGNMENT
) real mem
[GMX_SIMD_REAL_WIDTH
];
175 for (int i
= 0; i
< GMX_SIMD_REAL_WIDTH
; i
++)
177 mem
[i
] = v
[i
% v
.size()]; // repeat vector contents to fill simd width
179 return load
<SimdReal
>(mem
);
182 SimdReal
setSimdRealFrom3R(real r0
, real r1
, real r2
)
184 std::vector
<real
> v(3);
188 return vector2SimdReal(v
);
191 SimdReal
setSimdRealFrom1R(real value
)
193 std::vector
<real
> v(GMX_SIMD_REAL_WIDTH
);
194 for (int i
= 0; i
< GMX_SIMD_REAL_WIDTH
; i
++)
198 return vector2SimdReal(v
);
201 testing::AssertionResult
SimdTest::compareSimdRealUlp(const char* refExpr
,
206 return compareVectorRealUlp(refExpr
, tstExpr
, simdReal2Vector(ref
), simdReal2Vector(tst
));
209 testing::AssertionResult
SimdTest::compareSimdEq(const char* refExpr
,
214 return compareVectorEq(refExpr
, tstExpr
, simdReal2Vector(ref
), simdReal2Vector(tst
));
217 std::vector
<std::int32_t> simdInt2Vector(const SimdInt32 simd
)
219 alignas(GMX_SIMD_ALIGNMENT
) std::int32_t mem
[GMX_SIMD_REAL_WIDTH
];
222 std::vector
<std::int32_t> v(mem
, mem
+ GMX_SIMD_REAL_WIDTH
);
227 SimdInt32
vector2SimdInt(const std::vector
<std::int32_t>& v
)
229 alignas(GMX_SIMD_ALIGNMENT
) std::int32_t mem
[GMX_SIMD_REAL_WIDTH
];
231 for (int i
= 0; i
< GMX_SIMD_REAL_WIDTH
; i
++)
233 mem
[i
] = v
[i
% v
.size()]; // repeat vector contents to fill simd width
235 return load
<SimdInt32
>(mem
);
238 SimdInt32
setSimdIntFrom3I(int i0
, int i1
, int i2
)
240 std::vector
<int> v(3);
244 return vector2SimdInt(v
);
247 SimdInt32
setSimdIntFrom1I(int value
)
249 std::vector
<int> v(GMX_SIMD_REAL_WIDTH
);
250 for (int i
= 0; i
< GMX_SIMD_REAL_WIDTH
; i
++)
254 return vector2SimdInt(v
);
257 ::testing::AssertionResult
SimdTest::compareSimdEq(const char* refExpr
,
262 return compareVectorEq(refExpr
, tstExpr
, simdInt2Vector(ref
), simdInt2Vector(tst
));
265 # endif // GMX_SIMD_HAVE_REAL