2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2014,2015,2017,2018,2019,2021, 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>
86 void loadStoreTester(TSimd gmx_simdcall loadFn(const T* mem),
87 void gmx_simdcall storeFn(T* mem, TSimd),
89 const int storeOffset)
91 /* We need simdWidth storage in the first place, another simdWidth elements
92 * so we can create (deliberately) offset un-aligned pointers, and finally
93 * simdWidth elements at the beginning and end
94 * to test we are not polluting memory there either. Sum=4*simdWidth.
96 alignas(GMX_SIMD_ALIGNMENT) T src[simdWidth * 4];
97 alignas(GMX_SIMD_ALIGNMENT) T dst[simdWidth * 4];
99 // Make sure we have memory to check both before and after the test pointers
100 T* pCopySrc = src + simdWidth + loadOffset;
101 T* pCopyDst = dst + simdWidth + storeOffset;
104 for (i = 0; i < simdWidth * 4; i++)
110 storeFn(pCopyDst, loadFn(pCopySrc));
112 for (i = 0; i < simdWidth; i++)
114 EXPECT_EQ(pCopySrc[i], pCopyDst[i])
115 << "SIMD load or store not moving data correctly for element " << i;
118 for (i = 0; i < simdWidth * 4; i++)
120 EXPECT_EQ(src[i], T(1 + i)) << "Side effect on source memory, i = " << i;
121 if (dst + i < pCopyDst || dst + i >= pCopyDst + simdWidth)
123 EXPECT_EQ(dst[i], T(-1 - i)) << "Side effect on destination memory, i = " << i;
128 /*! \brief Wrapper to handle proxy objects returned by some load functions.
130 * \tparam T Type of scalar object
131 * \tparam TSimd Corresponding SIMD type
132 * \param m Memory address to load from
134 template<typename T, typename TSimd>
135 TSimd gmx_simdcall loadWrapper(const T* m)
137 return load<TSimd>(m);
140 /*! \brief Wrapper to handle proxy objects returned by some loadU functions.
142 * \tparam T Type of scalar object
143 * \tparam TSimd Corresponding SIMD type
144 * \param m Memory address to load from
146 template<typename T, typename TSimd>
147 TSimd gmx_simdcall loadUWrapper(const T* m)
149 return loadU<TSimd>(m);
153 # if GMX_SIMD_HAVE_REAL
154 TEST(SimdBootstrapTest, loadStore)
156 loadStoreTester<real, SimdReal, GMX_SIMD_REAL_WIDTH>(loadWrapper, store, 0, 0);
159 # if GMX_SIMD_HAVE_LOADU
160 TEST(SimdBootstrapTest, loadU)
162 for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
164 loadStoreTester<real, SimdReal, GMX_SIMD_REAL_WIDTH>(loadUWrapper, store, i, 0);
167 # endif // GMX_SIMD_HAVE_LOADU
169 # if GMX_SIMD_HAVE_STOREU
170 TEST(SimdBootstrapTest, storeU)
172 for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
174 loadStoreTester<real, SimdReal, GMX_SIMD_REAL_WIDTH>(loadWrapper, storeU, 0, i);
177 # endif // GMX_SIMD_HAVE_STOREU
179 // Tests for SimdInt32 load & store operations
180 TEST(SimdBootstrapTest, loadStoreI)
182 loadStoreTester<int, SimdInt32, GMX_SIMD_REAL_WIDTH>(loadWrapper, store, 0, 0);
185 # if GMX_SIMD_HAVE_LOADU
186 TEST(SimdBootstrapTest, loadUI)
188 for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
190 loadStoreTester<int, SimdInt32, GMX_SIMD_REAL_WIDTH>(loadUWrapper, store, i, 0);
193 # endif // GMX_SIMD_HAVE_LOADU
195 # if GMX_SIMD_HAVE_STOREU
196 TEST(SimdBootstrapTest, storeUI)
198 for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
200 loadStoreTester<int, SimdInt32, GMX_SIMD_REAL_WIDTH>(loadWrapper, storeU, 0, i);
203 # endif // GMX_SIMD_HAVE_STOREU
204 # endif // GMX_SIMD_HAVE_REAL
206 # if GMX_SIMD4_HAVE_REAL
207 TEST(SimdBootstrapTest, simd4LoadStore)
209 loadStoreTester<real, Simd4Real, GMX_SIMD4_WIDTH>(load4, store4, 0, 0);
212 # if GMX_SIMD_HAVE_LOADU
213 TEST(SimdBootstrapTest, simd4LoadU)
215 for (int i = 0; i < GMX_SIMD4_WIDTH; i++)
217 loadStoreTester<real, Simd4Real, GMX_SIMD4_WIDTH>(load4U, store4, i, 0);
220 # endif // GMX_SIMD_HAVE_LOADU
222 # if GMX_SIMD_HAVE_STOREU
223 TEST(SimdBootstrapTest, simd4StoreU)
225 for (int i = 0; i < GMX_SIMD4_WIDTH; i++)
227 loadStoreTester<real, Simd4Real, GMX_SIMD4_WIDTH>(load4, store4U, 0, i);
230 # endif // GMX_SIMD_HAVE_STOREU
231 # endif // GMX_SIMD4_HAVE_REAL