ROOT  6.06/09
Reference Guide
memory.cpp
Go to the documentation of this file.
1 /* This file is part of the Vc library.
2 
3  Copyright (C) 2009-2012 Matthias Kretz <kretz@kde.org>
4 
5  Vc is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as
7  published by the Free Software Foundation, either version 3 of
8  the License, or (at your option) any later version.
9 
10  Vc is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with Vc. If not, see <http://www.gnu.org/licenses/>.
17 
18 */
19 
20 #include "unittest.h"
21 
22 using namespace Vc;
23 
24 template<typename V, unsigned int Size, template<typename V2, unsigned int Size2> class TestClass> struct TestWrapper
25 {
26  static inline void run()
27  {
28  TestWrapper<V, Size/2, TestClass>::run();
29  TestClass<V, Size>::test();
30  TestClass<V, Size - 1>::test();
31  }
32 };
33 template<typename V, template<typename V2, unsigned int Size> class TestClass> struct TestWrapper<V, 1, TestClass> {
34  static inline void run() {}
35 };
36 
37 template<typename V, unsigned int Size> struct TestEntries { static void test() {
38  typedef typename V::EntryType T;
39  const T x = Size;
41  const Memory<V, Size> &m2 = m;
42  Memory<V> m3(Size);
43  for (unsigned int i = 0; i < Size; ++i) {
44  m[i] = x;
45  m3[i] = x;
46  }
47  for (unsigned int i = 0; i < Size; ++i) {
48  COMPARE(m[i], x);
49  COMPARE(m2[i], x);
50  COMPARE(m3[i], x);
51  }
52  for (unsigned int i = 0; i < Size; ++i) {
53  COMPARE(m.entries()[i], x);
54  COMPARE(m2.entries()[i], x);
55  COMPARE(m3.entries()[i], x);
56  }
57  const T *ptr = m2;
58  for (unsigned int i = 0; i < Size; ++i) {
59  COMPARE(ptr[i], x);
60  }
61  ptr = m3;
62  for (unsigned int i = 0; i < Size; ++i) {
63  COMPARE(ptr[i], x);
64  }
65 }};
66 
67 template<typename V, unsigned int Size> struct TestEntries2D { static void test() {
68  typedef typename V::EntryType T;
69  const T x = Size;
71  const Memory<V, Size, Size> &m2 = m;
72 
73  for (size_t i = 0; i < Size; ++i) {
74  for (size_t j = 0; j < Size; ++j) {
75  m[i][j] = x + i + j;
76  }
77  }
78  for (size_t i = 0; i < Size; ++i) {
79  for (size_t j = 0; j < Size; ++j) {
80  COMPARE(m[i][j], T(x + i + j));
81  COMPARE(m2[i][j], T(x + i + j));
82  }
83  }
84  for (size_t i = 0; i < Size; ++i) {
85  for (size_t j = 0; j < Size; ++j) {
86  COMPARE(m[i].entries()[j], T(x + i + j));
87  COMPARE(m2[i].entries()[j], T(x + i + j));
88  }
89  }
90  for (size_t i = 0; i < Size; ++i) {
91  const T *ptr = m2[i];
92  for (size_t j = 0; j < Size; ++j) {
93  COMPARE(ptr[j], T(x + i + j));
94  }
95  }
96 }};
97 
98 template<typename V, unsigned int Size> struct TestVectors { static void test()
99 {
100  const V startX(V::IndexType::IndexesFromZero() + Size);
102  const Memory<V, Size> &m2 = m;
103  Memory<V> m3(Size);
104  V x = startX;
105  for (unsigned int i = 0; i < m.vectorsCount(); ++i, x += V::Size) {
106  m.vector(i) = x;
107  m3.vector(i) = x;
108  }
109  x = startX;
110  unsigned int i;
111  for (i = 0; i + 1 < m.vectorsCount(); ++i) {
112  COMPARE(V(m.vector(i)), x);
113  COMPARE(V(m2.vector(i)), x);
114  COMPARE(V(m3.vector(i)), x);
115  for (int shift = 0; shift < V::Size; ++shift, ++x) {
116  COMPARE(V(m.vector(i, shift)), x);
117  COMPARE(V(m2.vector(i, shift)), x);
118  COMPARE(V(m3.vector(i, shift)), x);
119  }
120  }
121  COMPARE(V(m.vector(i)), x);
122  COMPARE(V(m2.vector(i)), x);
123  COMPARE(V(m3.vector(i)), x);
124 }};
125 
126 template<typename V, unsigned int Size> struct TestVectors2D { static void test()
127 {
128  const V startX(V::IndexType::IndexesFromZero() + Size);
130  const Memory<V, Size, Size> &m2 = m;
131  V x = startX;
132  for (size_t i = 0; i < m.rowsCount(); ++i, x += V::Size) {
133  Memory<V, Size> &mrow = m[i];
134  for (size_t j = 0; j < mrow.vectorsCount(); ++j, x += V::Size) {
135  mrow.vector(j) = x;
136  }
137  }
138  x = startX;
139  for (size_t i = 0; i < m.rowsCount(); ++i, x += V::Size) {
140  Memory<V, Size> &mrow = m[i];
141  const Memory<V, Size> &m2row = m2[i];
142  size_t j;
143  for (j = 0; j < mrow.vectorsCount() - 1; ++j) {
144  COMPARE(V(mrow.vector(j)), x);
145  COMPARE(V(m2row.vector(j)), x);
146  for (int shift = 0; shift < V::Size; ++shift, ++x) {
147  COMPARE(V(mrow.vector(j, shift)), x);
148  COMPARE(V(m2row.vector(j, shift)), x);
149  }
150  }
151  COMPARE(V(mrow.vector(j)), x) << i << " " << j;
152  COMPARE(V(m2row.vector(j)), x);
153  x += V::Size;
154  }
155 }};
156 
157 template<typename V, unsigned int Size> struct TestVectorReorganization { static void test()
158 {
159  typename V::Memory init;
160  for (unsigned int i = 0; i < V::Size; ++i) {
161  init[i] = i;
162  }
163  V x(init);
165  Memory<V> m3(Size);
166  for (unsigned int i = 0; i < m.vectorsCount(); ++i) {
167  m.vector(i) = x;
168  m3.vector(i) = x;
169  x += V::Size;
170  }
171  ///////////////////////////////////////////////////////////////////////////
172  x = V(init);
173  for (unsigned int i = 0; i < m.vectorsCount(); ++i) {
174  COMPARE(V(m.vector(i)), x);
175  COMPARE(V(m3.vector(i)), x);
176  x += V::Size;
177  }
178  ///////////////////////////////////////////////////////////////////////////
179  x = V(init);
180  unsigned int indexes[Size];
181  for (unsigned int i = 0; i < Size; ++i) {
182  indexes[i] = i;
183  }
184  for (unsigned int i = 0; i + V::Size < Size; ++i) {
185  COMPARE(m.gather(&indexes[i]), x);
186  COMPARE(m3.gather(&indexes[i]), x);
187  x += 1;
188  }
189  ///////////////////////////////////////////////////////////////////////////
190  for (unsigned int i = 0; i < V::Size; ++i) {
191  init[i] = i * 2;
192  }
193  x = V(init);
194  for (unsigned int i = 0; i < Size; ++i) {
195  indexes[i] = (i * 2) % Size;
196  }
197  for (unsigned int i = 0; i + V::Size < Size; ++i) {
198  COMPARE(m.gather(&indexes[i]), x);
199  COMPARE(m3.gather(&indexes[i]), x);
200  x += 2;
201  x(x >= Size) -= Size;
202  }
203 }};
204 
205 template<typename V> void testEntries()
206 {
208 }
209 
210 template<typename V> void testEntries2D()
211 {
213 }
214 
215 template<typename V> void testVectors()
216 {
218 }
219 
220 template<typename V> void testVectors2D()
221 {
223 }
224 
225 template<typename V> void testVectorReorganization()
226 {
228 }
229 
230 template<typename V> void memoryOperators()
231 {
232  Memory<V, 129> m1, m2;
233  m1.setZero();
234  m2.setZero();
235  VERIFY(m1 == m2);
236  VERIFY(!(m1 != m2));
237  VERIFY(!(m1 < m2));
238  VERIFY(!(m1 > m2));
239  m1 += m2;
240  VERIFY(m1 == m2);
241  VERIFY(m1 <= m2);
242  VERIFY(m1 >= m2);
243  m1 += 1;
244  VERIFY(m1 != m2);
245  VERIFY(m1 > m2);
246  VERIFY(m1 >= m2);
247  VERIFY(m2 < m1);
248  VERIFY(m2 <= m1);
249  VERIFY(!(m1 == m2));
250  VERIFY(!(m1 <= m2));
251  VERIFY(!(m2 >= m1));
252  m2 += m1;
253  VERIFY(m1 == m2);
254  m2 *= 2;
255  m1 += 1;
256  VERIFY(m1 == m2);
257  m2 /= 2;
258  m1 -= 1;
259  VERIFY(m1 == m2);
260  m1 *= m2;
261  VERIFY(m1 == m2);
262  m1 /= m2;
263  VERIFY(m1 == m2);
264  m1 -= m2;
265  m2 -= m2;
266  VERIFY(m1 == m2);
267 }
268 
269 template<typename V> void testCCtor()
270 {
271  Memory<V> m1(5);
272  for (size_t i = 0; i < m1.entriesCount(); ++i) {
273  m1[i] = i;
274  }
275  Memory<V> m2(m1);
276  for (size_t i = 0; i < m1.entriesCount(); ++i) {
277  m1[i] += 1;
278  }
279  for (size_t i = 0; i < m1.entriesCount(); ++i) {
280  COMPARE(m1[i], m2[i] + 1);
281  }
282 }
283 
284 template<typename V> void testCopyAssignment()
285 {
286  typedef typename V::EntryType T;
287 
288  Memory<V, 99> m1;
289  m1.setZero();
290 
291  Memory<V, 99> m2(m1);
292  for (size_t i = 0; i < m2.entriesCount(); ++i) {
293  COMPARE(m2[i], T(0));
294  m2[i] += 1;
295  }
296  m1 = m2;
297  for (size_t i = 0; i < m2.entriesCount(); ++i) {
298  COMPARE(m1[i], T(1));
299  }
300 }
301 
302 int main()
303 {
312 
313  return 0;
314 }
void testEntries()
Definition: memory.cpp:205
const char * Size
Definition: TXMLSetup.cxx:56
double T(double x)
Definition: ChebyshevPol.h:34
void testVectors()
Definition: memory.cpp:215
_VC_CONSTEXPR size_t vectorsCount() const
Definition: memory.h:169
_VC_CONSTEXPR size_t entriesCount() const
Definition: memory.h:163
Double_t x[n]
Definition: legend1.C:17
#define COMPARE(a, b)
Definition: unittest.h:509
int main()
Definition: memory.cpp:302
void testCCtor()
Definition: memory.cpp:269
#define testAllTypes(name)
Definition: unittest.h:43
Vc_ALWAYS_INLINE Vc_PURE VectorPointerHelper< V, AlignedFlag > vector(size_t i)
Definition: memorybase.h:310
TMarker * m
Definition: textangle.C:8
void testEntries2D()
Definition: memory.cpp:210
void run(bool only_compile=false)
Definition: run.C:1
#define VERIFY(cond)
Definition: unittest.h:515
void testVectorReorganization()
Definition: memory.cpp:225
A helper class for fixed-size two-dimensional arrays.
Definition: memory.h:120
void testVectors2D()
Definition: memory.cpp:220
static Int_t init()
Vc_ALWAYS_INLINE Vc_PURE V gather(const unsigned char *indexes) const
Definition: memorybase.h:435
_VC_CONSTEXPR size_t rowsCount() const
Definition: memory.h:154
void memoryOperators()
Definition: memory.cpp:230
Definition: casts.h:28
void testCopyAssignment()
Definition: memory.cpp:284