2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2014,2015,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.
39 * Separate test of SIMD load/store, before we use them in the SIMD test classes.
41 * Simple tests without using any classes/utilities, so we can use load/store
42 * functions inside our test utilities after this has passed.
44 * This file tests both the aligned and (if available) unaligned load and store
45 * operatations for SimdReal, SimdInt32, and Simd4Real.
47 * Note that you probably do not have to add more tests in this (complicated)
48 * file; once the bootstrapping tests have passed we can use the working basic
49 * load/store operations to test higher-level load/store operations too.
51 * \author Erik Lindahl <erik.lindahl@scilifelab.se>
52 * \ingroup module_simd
57 #include <gtest/gtest.h>
59 #include "gromacs/simd/simd.h"
60 #include "gromacs/utility/basedefinitions.h"
61 #include "gromacs/utility/real.h"
75 /*! \addtogroup module_simd */
79 /*! \brief Generic routine to test load & store of SIMD, and check for side effects.
81 * The tests for load, store, unaligned load and unaligned store both for
82 * real and int are pretty much similar, so we use a template function with
83 * additional function pointers for the actual load/store calls.
85 template <typename T
, typename TSimd
, int simdWidth
> void
86 loadStoreTester(TSimd gmx_simdcall
loadFn(const T
* mem
), void gmx_simdcall
storeFn(T
* mem
, TSimd
),
87 const int loadOffset
, const int storeOffset
)
89 /* We need simdWidth storage in the first place, another simdWidth elements
90 * so we can create (deliberately) offset un-aligned pointers, and finally
91 * simdWidth elements at the beginning and end
92 * to test we are not polluting memory there either. Sum=4*simdWidth.
94 alignas(GMX_SIMD_ALIGNMENT
) T src
[simdWidth
*4];
95 alignas(GMX_SIMD_ALIGNMENT
) T dst
[simdWidth
*4];
97 // Make sure we have memory to check both before and after the test pointers
98 T
* pCopySrc
= src
+ simdWidth
+ loadOffset
;
99 T
* pCopyDst
= dst
+ simdWidth
+ storeOffset
;
102 for (i
= 0; i
< simdWidth
*4; i
++)
108 storeFn(pCopyDst
, loadFn(pCopySrc
));
110 for (i
= 0; i
< simdWidth
; i
++)
112 EXPECT_EQ(pCopySrc
[i
], pCopyDst
[i
]) << "SIMD load or store not moving data correctly for element " << i
;
115 for (i
= 0; i
< simdWidth
*4; i
++)
117 EXPECT_EQ(src
[i
], (T
)(1+i
)) << "Side effect on source memory, i = " << i
;
118 if (dst
+i
< pCopyDst
|| dst
+i
>= pCopyDst
+simdWidth
)
120 EXPECT_EQ(dst
[i
], (T
)(-1-i
)) << "Side effect on destination memory, i = " << i
;
125 /*! \brief Wrapper to handle proxy objects returned by some load functions.
127 * \tparam T Type of scalar object
128 * \tparam TSimd Corresponding SIMD type
129 * \param m Memory address to load from
131 template <typename T
, typename TSimd
> TSimd gmx_simdcall
132 loadWrapper(const T
* m
) { return load
<TSimd
>(m
); }
134 /*! \brief Wrapper to handle proxy objects returned by some loadU functions.
136 * \tparam T Type of scalar object
137 * \tparam TSimd Corresponding SIMD type
138 * \param m Memory address to load from
140 template <typename T
, typename TSimd
> TSimd gmx_simdcall
141 loadUWrapper(const T
* m
) { return loadU
<TSimd
>(m
); }
144 #if GMX_SIMD_HAVE_REAL
145 TEST(SimdBootstrapTest
, loadStore
)
147 loadStoreTester
<real
, SimdReal
, GMX_SIMD_REAL_WIDTH
>(loadWrapper
, store
, 0, 0);
150 # if GMX_SIMD_HAVE_LOADU
151 TEST(SimdBootstrapTest
, loadU
)
153 for (int i
= 0; i
< GMX_SIMD_REAL_WIDTH
; i
++)
155 loadStoreTester
<real
, SimdReal
, GMX_SIMD_REAL_WIDTH
>(loadUWrapper
, store
, i
, 0);
158 # endif // GMX_SIMD_HAVE_LOADU
160 # if GMX_SIMD_HAVE_STOREU
161 TEST(SimdBootstrapTest
, storeU
)
163 for (int i
= 0; i
< GMX_SIMD_REAL_WIDTH
; i
++)
165 loadStoreTester
<real
, SimdReal
, GMX_SIMD_REAL_WIDTH
>(loadWrapper
, storeU
, 0, i
);
168 # endif // GMX_SIMD_HAVE_STOREU
170 // Tests for SimdInt32 load & store operations
171 TEST(SimdBootstrapTest
, loadStoreI
)
173 loadStoreTester
<int, SimdInt32
, GMX_SIMD_REAL_WIDTH
>(loadWrapper
, store
, 0, 0);
176 # if GMX_SIMD_HAVE_LOADU
177 TEST(SimdBootstrapTest
, loadUI
)
179 for (int i
= 0; i
< GMX_SIMD_REAL_WIDTH
; i
++)
181 loadStoreTester
<int, SimdInt32
, GMX_SIMD_REAL_WIDTH
>(loadUWrapper
, store
, i
, 0);
184 # endif // GMX_SIMD_HAVE_LOADU
186 # if GMX_SIMD_HAVE_STOREU
187 TEST(SimdBootstrapTest
, storeUI
)
189 for (int i
= 0; i
< GMX_SIMD_REAL_WIDTH
; i
++)
191 loadStoreTester
<int, SimdInt32
, GMX_SIMD_REAL_WIDTH
>(loadWrapper
, storeU
, 0, i
);
194 # endif // GMX_SIMD_HAVE_STOREU
195 #endif // GMX_SIMD_HAVE_REAL
197 #if GMX_SIMD4_HAVE_REAL
198 TEST(SimdBootstrapTest
, simd4LoadStore
)
200 loadStoreTester
<real
, Simd4Real
, GMX_SIMD4_WIDTH
>(load4
, store4
, 0, 0);
203 # if GMX_SIMD_HAVE_LOADU
204 TEST(SimdBootstrapTest
, simd4LoadU
)
206 for (int i
= 0; i
< GMX_SIMD4_WIDTH
; i
++)
208 loadStoreTester
<real
, Simd4Real
, GMX_SIMD4_WIDTH
>(load4U
, store4
, i
, 0);
211 # endif // GMX_SIMD_HAVE_LOADU
213 # if GMX_SIMD_HAVE_STOREU
214 TEST(SimdBootstrapTest
, simd4StoreU
)
216 for (int i
= 0; i
< GMX_SIMD4_WIDTH
; i
++)
218 loadStoreTester
<real
, Simd4Real
, GMX_SIMD4_WIDTH
>(load4
, store4U
, 0, i
);
221 # endif // GMX_SIMD_HAVE_STOREU
222 #endif // GMX_SIMD4_HAVE_REAL