Modernize syntax of enable_if and traits to use _t helpers
[alexxy/gromacs.git] / src / gromacs / simd / tests / simd_memory.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2015,2016,2017,2019, 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 /*! \internal \file
36  * \brief Tests for gmx::ArrayRef for SIMD types.
37  *
38  * \author Roland Schulz<roland.schulz@intel.com>
39  * \ingroup module_simd
40  */
41 #include "gmxpre.h"
42
43 #include <numeric>
44 #include <vector>
45
46 #include <gtest/gtest.h>
47
48 #include "gromacs/simd/simd.h"
49
50 #include "simd.h"
51
52 namespace gmx
53 {
54
55 #if GMX_SIMD_HAVE_REAL
56
57 /* SimdInt32 is a strange type which would never belong in an interface,
58  * because its properties are peculiar to int-to-float conversions within
59  * SIMD types, so there is no need to support it as a specialization of
60  * SimdArrayRef. But it is useful here for better test coverage of
61  * SimdArrayRef. */
62
63 template<>
64 class ArrayRef<SimdInt32> : public internal::SimdArrayRef<SimdInt32>
65 {
66     using Base = internal::SimdArrayRef<SimdInt32>;
67     using Base::Base;
68 };
69 template<>
70 class ArrayRef<const SimdInt32> : public internal::SimdArrayRef<const SimdInt32>
71 {
72     using Base = internal::SimdArrayRef<const SimdInt32>;
73     using Base::Base;
74 };
75
76 namespace
77 {
78
79 TEST(EmptyArrayRefTest, IsEmpty)
80 {
81     ArrayRef<SimdReal> empty = ArrayRef<real>();
82
83     EXPECT_EQ(0U, empty.size());
84     EXPECT_TRUE(empty.empty());
85 }
86
87 #ifdef GTEST_HAS_TYPED_TEST
88
89 /*! \brief Permit all the tests to run on all kinds of ArrayRefs
90  *
91  * The main objective is to verify that all the different kinds of
92  * construction lead to the expected result. */
93 template <typename TypeParam>
94 class ArrayRefTest : public test::SimdTest
95 {
96     public:
97         using ArrayRefType = TypeParam;
98         using PointerType  = typename ArrayRefType::pointer;
99         using ValueType    = typename ArrayRefType::value_type;
100         using ElementType  = std::remove_const_t < typename gmx::internal::SimdTraits < ValueType>::type>;
101         static constexpr int width = gmx::internal::SimdTraits<ValueType>::width;
102
103         /*! \brief Run the same tests all the time
104          *
105          * Note that test cases must call this->runTests(), because
106          * that's how the derived-class templates that implement
107          * type-parameterized tests actually work. */
108         void runReadOnlyTests(PointerType   a,
109                               size_t        aSize,
110                               ArrayRefType &arrayRef)
111         {
112             ASSERT_EQ(aSize, arrayRef.size());
113             ASSERT_FALSE(arrayRef.empty());
114
115             GMX_EXPECT_SIMD_EQ(load<ValueType>(a), arrayRef.front());
116             GMX_EXPECT_SIMD_EQ(load<ValueType>(a+(aSize-1)*width), arrayRef.back());
117
118             auto it = arrayRef.begin();
119             for (size_t i = 0; i != aSize; ++i, ++it)
120             {
121                 GMX_EXPECT_SIMD_EQ(load<ValueType>(a+i*width), arrayRef[i]);
122                 GMX_EXPECT_SIMD_EQ(load<ValueType>(a+i*width), *it);
123             }
124
125             EXPECT_EQ(aSize, arrayRef.end()-arrayRef.begin());
126             EXPECT_EQ(arrayRef.begin()+1, arrayRef.end()-(aSize-1));
127             EXPECT_LT(arrayRef.end()-1, arrayRef.end());
128             EXPECT_GT(arrayRef.begin()+1, arrayRef.begin());
129             EXPECT_LE(arrayRef.end()-1, arrayRef.end());
130             EXPECT_GE(arrayRef.begin()+1, arrayRef.begin());
131
132             it = arrayRef.begin();
133             it++;
134             ASSERT_EQ(arrayRef.begin()+1, it);
135             it--;
136             ASSERT_EQ(arrayRef.begin(), it);
137             it += 1;
138             ASSERT_EQ(arrayRef.begin()+1, it);
139             it -= 1;
140             ASSERT_EQ(arrayRef.begin(), it);
141             ++it;
142             ASSERT_EQ(arrayRef.begin()+1, it);
143             --it;
144             ASSERT_EQ(arrayRef.begin(), it);
145         }
146 };
147
148 using ArrayRefTypes = ::testing::Types<ArrayRef<SimdReal>, ArrayRef<const SimdReal>,
149                                        ArrayRef<SimdInt32>, ArrayRef<const SimdInt32> >;
150 TYPED_TEST_CASE(ArrayRefTest, ArrayRefTypes);
151
152 TYPED_TEST(ArrayRefTest, ConstructFromPointersWorks)
153 {
154     alignas(TestFixture::width*sizeof(typename TestFixture::ElementType))
155     std::array<typename TestFixture::ElementType, TestFixture::width*3> a;
156
157     std::iota(a.begin(), a.end(), 0);
158     typename TestFixture::ArrayRefType arrayRef(a.data(), a.data()+a.size());
159     this->runReadOnlyTests(a.data(), 3, arrayRef);
160 }
161
162 TYPED_TEST(ArrayRefTest, ConstructFromArrayRefWorks)
163 {
164     alignas(TestFixture::width*sizeof(typename TestFixture::ElementType))
165     std::array<typename TestFixture::ElementType, TestFixture::width*3> a;
166
167     std::iota(a.begin(), a.end(), 0);
168     ArrayRef < std::remove_const_t < typename TestFixture::ValueType>>
169     ref(a.data(), a.data()+a.size());
170     typename TestFixture::ArrayRefType        arrayRef(ref);
171     this->runReadOnlyTests(a.data(), 3, arrayRef);
172 }
173
174 TYPED_TEST(ArrayRefTest, ConstructFromArrayWorks)
175 {
176     alignas(TestFixture::width*sizeof(typename TestFixture::ElementType))
177     std::array<typename TestFixture::ElementType, TestFixture::width*3> a;
178
179     std::iota(a.begin(), a.end(), 0);
180     typename TestFixture::ArrayRefType arrayRef(a);
181     this->runReadOnlyTests(a.data(), 3, arrayRef);
182 }
183
184 template <typename TypeParam>
185 using ArrayRefReadWriteTest = ArrayRefTest<TypeParam>;
186
187 using ArrayRefReadWriteTypes = ::testing::Types< ArrayRef<SimdReal>, ArrayRef<SimdInt32> >;
188 TYPED_TEST_CASE(ArrayRefReadWriteTest, ArrayRefReadWriteTypes);
189
190 TYPED_TEST(ArrayRefReadWriteTest, Assignment)
191 {
192     constexpr int width = TestFixture::width;
193
194     alignas(width*sizeof(typename TestFixture::ElementType))
195     std::array<typename TestFixture::ElementType, width*4> a;
196
197     typename TestFixture::ArrayRefType arrayRef(a.data(), a.data()+a.size());
198
199     arrayRef.front() = 1;
200     EXPECT_EQ(1, a[0*width]);
201     (arrayRef.front() = 1) = 2;
202     EXPECT_EQ(2, a[0*width]);
203
204     arrayRef[1] = 2;
205     EXPECT_EQ(2, a[1*width]);
206     *(arrayRef.begin()+2) = 3;
207     EXPECT_EQ(3, a[2*width]);
208     arrayRef.back() = 4;
209     EXPECT_EQ(4, a[3*width]);
210 }
211
212 template <typename TypeParam>
213 using ArrayRefArithmeticTest = ArrayRefTest<TypeParam>;
214
215 using ArrayRefArithmeticTypes = ::testing::Types< ArrayRef<SimdReal>
216 #if GMX_SIMD_HAVE_INT32_ARITHMETICS
217                                                   , ArrayRef<SimdInt32>
218 #endif
219                                                   >;
220 TYPED_TEST_CASE(ArrayRefArithmeticTest, ArrayRefArithmeticTypes);
221
222 TYPED_TEST(ArrayRefArithmeticTest, Basic)
223 {
224     constexpr int width = TestFixture::width;
225
226     alignas(width*sizeof(typename TestFixture::ElementType))
227     std::array<typename TestFixture::ElementType, width> a;
228
229     typename TestFixture::ArrayRefType arrayRef(a.data(), a.data()+a.size());
230
231     arrayRef.front() = 1;
232     ASSERT_EQ(1, a[0*width]);
233     arrayRef.front() += 1;
234     ASSERT_EQ(2, a[0*width]);
235     arrayRef.front() *= 2;
236     ASSERT_EQ(4, a[0*width]);
237     arrayRef.front() -= 3;
238     ASSERT_EQ(1, a[0*width]);
239 }
240
241 #endif // GTEST_HAS_TYPED_TEST
242
243 }      // namespace
244
245 #endif // GMX_HAVE_SIMD_REAL
246
247 }      // namespace gmx