Support Simd4N for SimdRealWidth<4
[gromacs.git] / src / gromacs / simd / tests / simd_memory.cpp
blob24aab65dbc8830bc1a4d0f345318c750edbfb724
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2015,2016,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.
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.
35 /*! \internal \file
36 * \brief Tests for gmx::ArrayRef for SIMD types.
38 * \author Roland Schulz<roland.schulz@intel.com>
39 * \ingroup module_simd
41 #include "gmxpre.h"
43 #include <numeric>
44 #include <vector>
46 #include <gtest/gtest.h>
48 #include "gromacs/simd/simd.h"
50 #include "simd.h"
52 namespace gmx
55 namespace
58 #if GMX_SIMD_HAVE_REAL
60 TEST(EmptyArrayRefTest, IsEmpty)
62 EmptyArrayRef emptyArray = {};
63 ArrayRef<SimdReal> empty(emptyArray);
65 EXPECT_EQ(0U, empty.size());
66 EXPECT_TRUE(empty.empty());
69 #ifdef GTEST_HAS_TYPED_TEST
71 /*! \brief Permit all the tests to run on all kinds of ArrayRefs
73 * The main objective is to verify that all the different kinds of
74 * construction lead to the expected result. */
75 template <typename TypeParam>
76 class ArrayRefTest : public test::SimdTest
78 public:
79 using ArrayRefType = TypeParam;
80 using PointerType = typename ArrayRefType::pointer;
81 using ValueType = typename ArrayRefType::value_type;
82 using ElementType = typename std::remove_const<typename gmx::internal::SimdTraits<ValueType>::type>::type;
83 static constexpr int width = gmx::internal::SimdTraits<ValueType>::width;
85 /*! \brief Run the same tests all the time
87 * Note that test cases must call this->runTests(), because
88 * that's how the derived-class templates that implement
89 * type-parameterized tests actually work. */
90 void runReadOnlyTests(PointerType a,
91 size_t aSize,
92 ArrayRefType &arrayRef)
94 ASSERT_EQ(aSize, arrayRef.size());
95 ASSERT_FALSE(arrayRef.empty());
97 GMX_EXPECT_SIMD_EQ(load<ValueType>(a), arrayRef.front());
98 GMX_EXPECT_SIMD_EQ(load<ValueType>(a+(aSize-1)*width), arrayRef.back());
100 auto it = arrayRef.begin();
101 for (size_t i = 0; i != aSize; ++i, ++it)
103 GMX_EXPECT_SIMD_EQ(load<ValueType>(a+i*width), arrayRef[i]);
104 GMX_EXPECT_SIMD_EQ(load<ValueType>(a+i*width), *it);
107 EXPECT_EQ(aSize, arrayRef.end()-arrayRef.begin());
108 EXPECT_EQ(arrayRef.begin()+1, arrayRef.end()-(aSize-1));
109 EXPECT_LT(arrayRef.end()-1, arrayRef.end());
110 EXPECT_GT(arrayRef.begin()+1, arrayRef.begin());
111 EXPECT_LE(arrayRef.end()-1, arrayRef.end());
112 EXPECT_GE(arrayRef.begin()+1, arrayRef.begin());
114 it = arrayRef.begin();
115 it++;
116 ASSERT_EQ(arrayRef.begin()+1, it);
117 it--;
118 ASSERT_EQ(arrayRef.begin(), it);
119 it += 1;
120 ASSERT_EQ(arrayRef.begin()+1, it);
121 it -= 1;
122 ASSERT_EQ(arrayRef.begin(), it);
123 ++it;
124 ASSERT_EQ(arrayRef.begin()+1, it);
125 --it;
126 ASSERT_EQ(arrayRef.begin(), it);
130 using ArrayRefTypes = ::testing::Types<ArrayRef<SimdReal>, ArrayRef<const SimdReal>,
131 ArrayRef<SimdInt32>, ArrayRef<const SimdInt32> >;
132 TYPED_TEST_CASE(ArrayRefTest, ArrayRefTypes);
134 TYPED_TEST(ArrayRefTest, ConstructFromPointersWorks)
136 alignas(TestFixture::width*sizeof(typename TestFixture::ElementType))
137 std::array<typename TestFixture::ElementType, TestFixture::width*3> a;
139 std::iota(a.begin(), a.end(), 0);
140 typename TestFixture::ArrayRefType arrayRef(a.data(), a.data()+a.size());
141 this->runReadOnlyTests(a.data(), 3, arrayRef);
144 TYPED_TEST(ArrayRefTest, ConstructFromArrayRefWorks)
146 alignas(TestFixture::width*sizeof(typename TestFixture::ElementType))
147 std::array<typename TestFixture::ElementType, TestFixture::width*3> a;
149 std::iota(a.begin(), a.end(), 0);
150 ArrayRef<typename std::remove_const<typename TestFixture::ValueType>::type>
151 ref(a.data(), a.data()+a.size());
152 typename TestFixture::ArrayRefType arrayRef(ref);
153 this->runReadOnlyTests(a.data(), 3, arrayRef);
156 TYPED_TEST(ArrayRefTest, ConstructFromArrayWorks)
158 alignas(TestFixture::width*sizeof(typename TestFixture::ElementType))
159 std::array<typename TestFixture::ElementType, TestFixture::width*3> a;
161 std::iota(a.begin(), a.end(), 0);
162 typename TestFixture::ArrayRefType arrayRef(a);
163 this->runReadOnlyTests(a.data(), 3, arrayRef);
166 template <typename TypeParam>
167 using ArrayRefReadWriteTest = ArrayRefTest<TypeParam>;
169 using ArrayRefReadWriteTypes = ::testing::Types< ArrayRef<SimdReal>, ArrayRef<SimdInt32> >;
170 TYPED_TEST_CASE(ArrayRefReadWriteTest, ArrayRefReadWriteTypes);
172 TYPED_TEST(ArrayRefReadWriteTest, Assignment)
174 constexpr int width = TestFixture::width;
176 alignas(width*sizeof(typename TestFixture::ElementType))
177 std::array<typename TestFixture::ElementType, width*4> a;
179 typename TestFixture::ArrayRefType arrayRef(a.data(), a.data()+a.size());
181 arrayRef.front() = 1;
182 EXPECT_EQ(1, a[0*width]);
183 (arrayRef.front() = 1) = 2;
184 EXPECT_EQ(2, a[0*width]);
186 arrayRef[1] = 2;
187 EXPECT_EQ(2, a[1*width]);
188 *(arrayRef.begin()+2) = 3;
189 EXPECT_EQ(3, a[2*width]);
190 arrayRef.back() = 4;
191 EXPECT_EQ(4, a[3*width]);
194 template <typename TypeParam>
195 using ArrayRefArithmeticTest = ArrayRefTest<TypeParam>;
197 using ArrayRefArithmeticTypes = ::testing::Types< ArrayRef<SimdReal>
198 #if GMX_SIMD_HAVE_INT32_ARITHMETICS
199 , ArrayRef<SimdInt32>
200 #endif
202 TYPED_TEST_CASE(ArrayRefArithmeticTest, ArrayRefArithmeticTypes);
204 TYPED_TEST(ArrayRefArithmeticTest, Basic)
206 constexpr int width = TestFixture::width;
208 alignas(width*sizeof(typename TestFixture::ElementType))
209 std::array<typename TestFixture::ElementType, width> a;
211 typename TestFixture::ArrayRefType arrayRef(a.data(), a.data()+a.size());
213 arrayRef.front() = 1;
214 ASSERT_EQ(1, a[0*width]);
215 arrayRef.front() += 1;
216 ASSERT_EQ(2, a[0*width]);
217 arrayRef.front() *= 2;
218 ASSERT_EQ(4, a[0*width]);
219 arrayRef.front() -= 3;
220 ASSERT_EQ(1, a[0*width]);
223 #endif // GTEST_HAS_TYPED_TEST
225 #endif // GMX_HAVE_SIMD_REAL
227 } // namespace
229 } // namespace