2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2014,2015, 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 #if GMX_SIMD4_WIDTH > GMX_SIMD_REAL_WIDTH
95 GMX_ALIGNED(T
, GMX_SIMD4_WIDTH
) src
[simdWidth
*4];
96 GMX_ALIGNED(T
, GMX_SIMD4_WIDTH
) dst
[simdWidth
*4];
98 GMX_ALIGNED(T
, GMX_SIMD_REAL_WIDTH
) src
[simdWidth
*4];
99 GMX_ALIGNED(T
, GMX_SIMD_REAL_WIDTH
) dst
[simdWidth
*4];
102 // Make sure we have memory to check both before and after the test pointers
103 T
* pCopySrc
= src
+ simdWidth
+ loadOffset
;
104 T
* pCopyDst
= dst
+ simdWidth
+ storeOffset
;
107 for (i
= 0; i
< simdWidth
*4; i
++)
113 storeFn(pCopyDst
, loadFn(pCopySrc
));
115 for (i
= 0; i
< simdWidth
; i
++)
117 EXPECT_EQ(pCopySrc
[i
], pCopyDst
[i
]) << "SIMD load or store not moving data correctly for element " << i
;
120 for (i
= 0; i
< simdWidth
*4; i
++)
122 EXPECT_EQ(src
[i
], (T
)(1+i
)) << "Side effect on source memory, i = " << i
;
123 if (dst
+i
< pCopyDst
|| dst
+i
>= pCopyDst
+simdWidth
)
125 EXPECT_EQ(dst
[i
], (T
)(-1-i
)) << "Side effect on destination memory, i = " << i
;
130 /*! \brief Wrapper to handle proxy objects returned by some load functions.
132 * \tparam T Type of scalar object
133 * \tparam TSimd Corresponding SIMD type
134 * \param m Memory address to load from
136 template <typename T
, typename TSimd
> TSimd gmx_simdcall
137 loadWrapper(const T
* m
) { return load(m
); }
139 /*! \brief Wrapper to handle proxy objects returned by some loadU functions.
141 * \tparam T Type of scalar object
142 * \tparam TSimd Corresponding SIMD type
143 * \param m Memory address to load from
145 template <typename T
, typename TSimd
> TSimd gmx_simdcall
146 loadUWrapper(const T
* m
) { return loadU(m
); }
149 #if GMX_SIMD_HAVE_REAL
150 TEST(SimdBootstrapTest
, loadStore
)
152 loadStoreTester
<real
, SimdReal
, GMX_SIMD_REAL_WIDTH
>(loadWrapper
, store
, 0, 0);
155 # if GMX_SIMD_HAVE_LOADU
156 TEST(SimdBootstrapTest
, loadU
)
158 for (int i
= 0; i
< GMX_SIMD_REAL_WIDTH
; i
++)
160 loadStoreTester
<real
, SimdReal
, GMX_SIMD_REAL_WIDTH
>(loadUWrapper
, store
, i
, 0);
163 # endif // GMX_SIMD_HAVE_LOADU
165 # if GMX_SIMD_HAVE_STOREU
166 TEST(SimdBootstrapTest
, storeU
)
168 for (int i
= 0; i
< GMX_SIMD_REAL_WIDTH
; i
++)
170 loadStoreTester
<real
, SimdReal
, GMX_SIMD_REAL_WIDTH
>(loadWrapper
, storeU
, 0, i
);
173 # endif // GMX_SIMD_HAVE_STOREU
175 // Tests for SimdInt32 load & store operations
176 TEST(SimdBootstrapTest
, loadStoreI
)
178 loadStoreTester
<int, SimdInt32
, GMX_SIMD_REAL_WIDTH
>(loadWrapper
, store
, 0, 0);
181 # if GMX_SIMD_HAVE_LOADU
182 TEST(SimdBootstrapTest
, loadUI
)
184 for (int i
= 0; i
< GMX_SIMD_REAL_WIDTH
; i
++)
186 loadStoreTester
<int, SimdInt32
, GMX_SIMD_REAL_WIDTH
>(loadUWrapper
, store
, i
, 0);
189 # endif // GMX_SIMD_HAVE_LOADU
191 # if GMX_SIMD_HAVE_STOREU
192 TEST(SimdBootstrapTest
, storeUI
)
194 for (int i
= 0; i
< GMX_SIMD_REAL_WIDTH
; i
++)
196 loadStoreTester
<int, SimdInt32
, GMX_SIMD_REAL_WIDTH
>(loadWrapper
, storeU
, 0, i
);
199 # endif // GMX_SIMD_HAVE_STOREU
200 #endif // GMX_SIMD_HAVE_REAL
202 #if GMX_SIMD4_HAVE_REAL
203 TEST(SimdBootstrapTest
, simd4LoadStore
)
205 loadStoreTester
<real
, Simd4Real
, GMX_SIMD4_WIDTH
>(load4
, store4
, 0, 0);
208 # if GMX_SIMD_HAVE_LOADU
209 TEST(SimdBootstrapTest
, simd4LoadU
)
211 for (int i
= 0; i
< GMX_SIMD4_WIDTH
; i
++)
213 loadStoreTester
<real
, Simd4Real
, GMX_SIMD4_WIDTH
>(load4U
, store4
, i
, 0);
216 # endif // GMX_SIMD_HAVE_LOADU
218 # if GMX_SIMD_HAVE_STOREU
219 TEST(SimdBootstrapTest
, simd4StoreU
)
221 for (int i
= 0; i
< GMX_SIMD4_WIDTH
; i
++)
223 loadStoreTester
<real
, Simd4Real
, GMX_SIMD4_WIDTH
>(load4
, store4U
, 0, i
);
226 # endif // GMX_SIMD_HAVE_STOREU
227 #endif // GMX_SIMD4_HAVE_REAL