Implement changes for CMake policy 0068
[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, 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> void
86 loadStoreTester(TSimd gmx_simdcall loadFn(const T* mem), void gmx_simdcall storeFn(T* mem, TSimd),
87                 const int loadOffset, const int storeOffset)
88 {
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.
93      */
94     alignas(GMX_SIMD_ALIGNMENT) T        src[simdWidth*4];
95     alignas(GMX_SIMD_ALIGNMENT) T        dst[simdWidth*4];
96
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;
100     int              i;
101
102     for (i = 0; i < simdWidth*4; i++)
103     {
104         src[i] =  1+i;
105         dst[i] = -1-i;
106     }
107
108     storeFn(pCopyDst, loadFn(pCopySrc));
109
110     for (i = 0; i < simdWidth; i++)
111     {
112         EXPECT_EQ(pCopySrc[i], pCopyDst[i]) << "SIMD load or store not moving data correctly for element " << i;
113     }
114
115     for (i = 0; i < simdWidth*4; i++)
116     {
117         EXPECT_EQ(src[i], (T)(1+i)) << "Side effect on source memory, i = " << i;
118         if (dst+i < pCopyDst || dst+i >= pCopyDst+simdWidth)
119         {
120             EXPECT_EQ(dst[i], (T)(-1-i)) << "Side effect on destination memory, i = " << i;
121         }
122     }
123 }
124
125 /*! \brief Wrapper to handle proxy objects returned by some load functions.
126  *
127  * \tparam T      Type of scalar object
128  * \tparam TSimd  Corresponding SIMD type
129  * \param  m      Memory address to load from
130  */
131 template <typename T, typename TSimd> TSimd gmx_simdcall
132 loadWrapper(const T * m) { return load<TSimd>(m); }
133
134 /*! \brief Wrapper to handle proxy objects returned by some loadU functions.
135  *
136  * \tparam T      Type of scalar object
137  * \tparam TSimd  Corresponding SIMD type
138  * \param  m      Memory address to load from
139  */
140 template <typename T, typename TSimd> TSimd gmx_simdcall
141 loadUWrapper(const T * m) { return loadU<TSimd>(m); }
142
143
144 #if GMX_SIMD_HAVE_REAL
145 TEST(SimdBootstrapTest, loadStore)
146 {
147     loadStoreTester<real, SimdReal, GMX_SIMD_REAL_WIDTH>(loadWrapper, store, 0, 0);
148 }
149
150 #    if GMX_SIMD_HAVE_LOADU
151 TEST(SimdBootstrapTest, loadU)
152 {
153     for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
154     {
155         loadStoreTester<real, SimdReal, GMX_SIMD_REAL_WIDTH>(loadUWrapper, store, i, 0);
156     }
157 }
158 #    endif  // GMX_SIMD_HAVE_LOADU
159
160 #    if GMX_SIMD_HAVE_STOREU
161 TEST(SimdBootstrapTest, storeU)
162 {
163     for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
164     {
165         loadStoreTester<real, SimdReal, GMX_SIMD_REAL_WIDTH>(loadWrapper, storeU, 0, i);
166     }
167 }
168 #    endif  // GMX_SIMD_HAVE_STOREU
169
170 // Tests for SimdInt32 load & store operations
171 TEST(SimdBootstrapTest, loadStoreI)
172 {
173     loadStoreTester<int, SimdInt32, GMX_SIMD_REAL_WIDTH>(loadWrapper, store, 0, 0);
174 }
175
176 #    if GMX_SIMD_HAVE_LOADU
177 TEST(SimdBootstrapTest, loadUI)
178 {
179     for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
180     {
181         loadStoreTester<int, SimdInt32, GMX_SIMD_REAL_WIDTH>(loadUWrapper, store, i, 0);
182     }
183 }
184 #    endif  // GMX_SIMD_HAVE_LOADU
185
186 #    if GMX_SIMD_HAVE_STOREU
187 TEST(SimdBootstrapTest, storeUI)
188 {
189     for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
190     {
191         loadStoreTester<int, SimdInt32, GMX_SIMD_REAL_WIDTH>(loadWrapper, storeU, 0, i);
192     }
193 }
194 #    endif // GMX_SIMD_HAVE_STOREU
195 #endif     // GMX_SIMD_HAVE_REAL
196
197 #if GMX_SIMD4_HAVE_REAL
198 TEST(SimdBootstrapTest, simd4LoadStore)
199 {
200     loadStoreTester<real, Simd4Real, GMX_SIMD4_WIDTH>(load4, store4, 0, 0);
201 }
202
203 #    if GMX_SIMD_HAVE_LOADU
204 TEST(SimdBootstrapTest, simd4LoadU)
205 {
206     for (int i = 0; i < GMX_SIMD4_WIDTH; i++)
207     {
208         loadStoreTester<real, Simd4Real, GMX_SIMD4_WIDTH>(load4U, store4, i, 0);
209     }
210 }
211 #    endif  // GMX_SIMD_HAVE_LOADU
212
213 #    if GMX_SIMD_HAVE_STOREU
214 TEST(SimdBootstrapTest, simd4StoreU)
215 {
216     for (int i = 0; i < GMX_SIMD4_WIDTH; i++)
217     {
218         loadStoreTester<real, Simd4Real, GMX_SIMD4_WIDTH>(load4, store4U, 0, i);
219     }
220 }
221 #    endif // GMX_SIMD_HAVE_STOREU
222 #endif     // GMX_SIMD4_HAVE_REAL
223
224 /*! \} */
225 /*! \endcond */
226
227 }      // namespace
228
229 }      // namespace test
230
231 }      // namespace gmx
232
233 #endif // GMX_SIMD