Rework -Weverything
[alexxy/gromacs.git] / src / gromacs / simd / tests / bootstrap_loadstore.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 #include "gmxpre.h"
36
37 /*! \internal \file
38  * \brief
39  * Separate test of SIMD load/store, before we use them in the SIMD test classes.
40  *
41  * Simple tests without using any classes/utilities, so we can use load/store
42  * functions inside our test utilities after this has passed.
43  *
44  * This file tests both the aligned and (if available) unaligned load and store
45  * operatations for SimdReal, SimdInt32, and Simd4Real.
46  *
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.
50  *
51  * \author Erik Lindahl <erik.lindahl@scilifelab.se>
52  * \ingroup module_simd
53  */
54
55 #include "config.h"
56
57 #include <gtest/gtest.h>
58
59 #include "gromacs/simd/simd.h"
60 #include "gromacs/utility/basedefinitions.h"
61 #include "gromacs/utility/real.h"
62
63 #if GMX_SIMD
64
65 namespace gmx
66 {
67
68 namespace test
69 {
70
71 namespace
72 {
73
74 /*! \cond internal */
75 /*! \addtogroup module_simd */
76 /*! \{ */
77
78
79 /*! \brief Generic routine to test load & store of SIMD, and check for side effects.
80  *
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.
84  */
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),
88                      const int          loadOffset,
89                      const int          storeOffset)
90 {
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.
95      */
96     alignas(GMX_SIMD_ALIGNMENT) T src[simdWidth * 4];
97     alignas(GMX_SIMD_ALIGNMENT) T dst[simdWidth * 4];
98
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;
102     int i;
103
104     for (i = 0; i < simdWidth * 4; i++)
105     {
106         src[i] = 1 + i;
107         dst[i] = -1 - i;
108     }
109
110     storeFn(pCopyDst, loadFn(pCopySrc));
111
112     for (i = 0; i < simdWidth; i++)
113     {
114         EXPECT_EQ(pCopySrc[i], pCopyDst[i])
115                 << "SIMD load or store not moving data correctly for element " << i;
116     }
117
118     for (i = 0; i < simdWidth * 4; i++)
119     {
120         EXPECT_EQ(src[i], T(1 + i)) << "Side effect on source memory, i = " << i;
121         if (dst + i < pCopyDst || dst + i >= pCopyDst + simdWidth)
122         {
123             EXPECT_EQ(dst[i], T(-1 - i)) << "Side effect on destination memory, i = " << i;
124         }
125     }
126 }
127
128 /*! \brief Wrapper to handle proxy objects returned by some load functions.
129  *
130  * \tparam T      Type of scalar object
131  * \tparam TSimd  Corresponding SIMD type
132  * \param  m      Memory address to load from
133  */
134 template<typename T, typename TSimd>
135 TSimd gmx_simdcall loadWrapper(const T* m)
136 {
137     return load<TSimd>(m);
138 }
139
140 /*! \brief Wrapper to handle proxy objects returned by some loadU functions.
141  *
142  * \tparam T      Type of scalar object
143  * \tparam TSimd  Corresponding SIMD type
144  * \param  m      Memory address to load from
145  */
146 template<typename T, typename TSimd>
147 TSimd gmx_simdcall loadUWrapper(const T* m)
148 {
149     return loadU<TSimd>(m);
150 }
151
152
153 #    if GMX_SIMD_HAVE_REAL
154 TEST(SimdBootstrapTest, loadStore)
155 {
156     loadStoreTester<real, SimdReal, GMX_SIMD_REAL_WIDTH>(loadWrapper, store, 0, 0);
157 }
158
159 #        if GMX_SIMD_HAVE_LOADU
160 TEST(SimdBootstrapTest, loadU)
161 {
162     for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
163     {
164         loadStoreTester<real, SimdReal, GMX_SIMD_REAL_WIDTH>(loadUWrapper, store, i, 0);
165     }
166 }
167 #        endif // GMX_SIMD_HAVE_LOADU
168
169 #        if GMX_SIMD_HAVE_STOREU
170 TEST(SimdBootstrapTest, storeU)
171 {
172     for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
173     {
174         loadStoreTester<real, SimdReal, GMX_SIMD_REAL_WIDTH>(loadWrapper, storeU, 0, i);
175     }
176 }
177 #        endif // GMX_SIMD_HAVE_STOREU
178
179 // Tests for SimdInt32 load & store operations
180 TEST(SimdBootstrapTest, loadStoreI)
181 {
182     loadStoreTester<int, SimdInt32, GMX_SIMD_REAL_WIDTH>(loadWrapper, store, 0, 0);
183 }
184
185 #        if GMX_SIMD_HAVE_LOADU
186 TEST(SimdBootstrapTest, loadUI)
187 {
188     for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
189     {
190         loadStoreTester<int, SimdInt32, GMX_SIMD_REAL_WIDTH>(loadUWrapper, store, i, 0);
191     }
192 }
193 #        endif // GMX_SIMD_HAVE_LOADU
194
195 #        if GMX_SIMD_HAVE_STOREU
196 TEST(SimdBootstrapTest, storeUI)
197 {
198     for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
199     {
200         loadStoreTester<int, SimdInt32, GMX_SIMD_REAL_WIDTH>(loadWrapper, storeU, 0, i);
201     }
202 }
203 #        endif // GMX_SIMD_HAVE_STOREU
204 #    endif     // GMX_SIMD_HAVE_REAL
205
206 #    if GMX_SIMD4_HAVE_REAL
207 TEST(SimdBootstrapTest, simd4LoadStore)
208 {
209     loadStoreTester<real, Simd4Real, GMX_SIMD4_WIDTH>(load4, store4, 0, 0);
210 }
211
212 #        if GMX_SIMD_HAVE_LOADU
213 TEST(SimdBootstrapTest, simd4LoadU)
214 {
215     for (int i = 0; i < GMX_SIMD4_WIDTH; i++)
216     {
217         loadStoreTester<real, Simd4Real, GMX_SIMD4_WIDTH>(load4U, store4, i, 0);
218     }
219 }
220 #        endif // GMX_SIMD_HAVE_LOADU
221
222 #        if GMX_SIMD_HAVE_STOREU
223 TEST(SimdBootstrapTest, simd4StoreU)
224 {
225     for (int i = 0; i < GMX_SIMD4_WIDTH; i++)
226     {
227         loadStoreTester<real, Simd4Real, GMX_SIMD4_WIDTH>(load4, store4U, 0, i);
228     }
229 }
230 #        endif // GMX_SIMD_HAVE_STOREU
231 #    endif     // GMX_SIMD4_HAVE_REAL
232
233 /*! \} */
234 /*! \endcond */
235
236 } // namespace
237
238 } // namespace test
239
240 } // namespace gmx
241
242 #endif // GMX_SIMD