ROOT  6.06/09
Reference Guide
gather.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 #include <iostream>
22 
23 using namespace Vc;
24 
25 template<typename Vec> void maskedGatherArray()
26 {
27  typedef typename Vec::IndexType It;
28  typedef typename Vec::EntryType T;
29 
30  T mem[Vec::Size];
31  for (int i = 0; i < Vec::Size; ++i) {
32  mem[i] = i + 1;
33  }
34 
35  It indexes = It::IndexesFromZero();
36  for_all_masks(Vec, m) {
37  const Vec a(mem, indexes, m);
38  for (int i = 0; i < Vec::Size; ++i) {
39  COMPARE(a[i], m[i] ? mem[i] : 0) << " i = " << i << ", m = " << m;
40  }
41 
42  T x = Vec::Size + 1;
43  Vec b = x;
44  b.gather(mem, indexes, m);
45  for (int i = 0; i < Vec::Size; ++i) {
46  COMPARE(b[i], m[i] ? mem[i] : x) << " i = " << i << ", m = " << m;
47  }
48  }
49 }
50 
51 template<typename Vec> void gatherArray()
52 {
53  typedef typename Vec::IndexType It;
54  typedef typename Vec::EntryType T;
55  typedef typename It::Mask M;
56 
57  const int count = 39999;
58  T array[count];
59  for (int i = 0; i < count; ++i) {
60  array[i] = i + 1;
61  }
62  M mask;
63  for (It i = It(IndexesFromZero); !(mask = (i < count)).isEmpty(); i += Vec::Size) {
64  const Vec ii(i + 1);
65  const typename Vec::Mask castedMask = static_cast<typename Vec::Mask>(mask);
66  if (castedMask.isFull()) {
67  Vec a(array, i);
68  COMPARE(a, ii) << "\n i: " << i;
69  Vec b(Zero);
70  b.gather(array, i);
71  COMPARE(b, ii);
72  COMPARE(a, b);
73  }
74  Vec b(Zero);
75  b.gather(array, i, castedMask);
76  COMPARE(castedMask, (b == ii)) << ", b = " << b << ", ii = " << ii << ", i = " << i;
77  if (!castedMask.isFull()) {
78  COMPARE(!castedMask, b == Vec(Zero));
79  }
80  }
81 
82  const typename Vec::Mask k(Zero);
83  Vec a(One);
84  a.gather(array, It(IndexesFromZero), k);
85  COMPARE(a, Vec(One));
86 }
87 
88 template<typename T> struct Struct
89 {
90  T a;
91  char x;
92  T b;
93  short y;
94  T c;
95  char z;
96 };
97 
98 template<typename Vec> void gatherStruct()
99 {
100  typedef typename Vec::IndexType It;
101  typedef typename Vec::EntryType T;
102  typedef Struct<T> S;
103  const int count = 3999;
104  S array[count];
105  for (int i = 0; i < count; ++i) {
106  array[i].a = i;
107  array[i].b = i + 1;
108  array[i].c = i + 2;
109  }
110  typename It::Mask mask;
111  for (It i = It(IndexesFromZero); !(mask = (i < count)).isEmpty(); i += Vec::Size) {
112  // if Vec is double_v the cast keeps only the lower two values, which is why the ==
113  // comparison works
114  const Vec i0(i);
115  const Vec i1(i + 1);
116  const Vec i2(i + 2);
117  const typename Vec::Mask castedMask(mask);
118 
119  if (castedMask.isFull()) {
120  Vec a(array, &S::a, i);
121  COMPARE(a, i0) << "\ni: " << i;
122  a.gather(array, &S::b, i);
123  COMPARE(a, i1);
124  a.gather(array, &S::c, i);
125  COMPARE(a, i2);
126  }
127 
128  Vec b(Zero);
129  b.gather(array, &S::a, i, castedMask);
130  COMPARE(castedMask, (b == i0));
131  if (!castedMask.isFull()) {
132  COMPARE(!castedMask, b == Vec(Zero));
133  }
134  b.gather(array, &S::b, i, castedMask);
135  COMPARE(castedMask, (b == i1));
136  if (!castedMask.isFull()) {
137  COMPARE(!castedMask, b == Vec(Zero));
138  }
139  b.gather(array, &S::c, i, castedMask);
140  COMPARE(castedMask, (b == i2));
141  if (!castedMask.isFull()) {
142  COMPARE(!castedMask, b == Vec(Zero));
143  }
144  }
145 }
146 
147 template<typename T> struct Row
148 {
149  T *data;
150 };
151 
152 template<typename Vec> void gather2dim()
153 {
154  typedef typename Vec::IndexType It;
155  typedef typename Vec::EntryType T;
156  const int count = 399;
157  typedef Row<T> S;
158  S array[count];
159  for (int i = 0; i < count; ++i) {
160  array[i].data = new T[count];
161  for (int j = 0; j < count; ++j) {
162  array[i].data[j] = 2 * i + j + 1;
163  }
164  }
165 
166  typename It::Mask mask;
167  for (It i = It(IndexesFromZero); !(mask = (i < count)).isEmpty(); i += Vec::Size) {
168  for (It j = It(IndexesFromZero); !(mask &= (j < count)).isEmpty(); j += Vec::Size) {
169  const Vec i0(i * 2 + j + 1);
170  const typename Vec::Mask castedMask(mask);
171 
172  Vec a(array, &S::data, i, j, castedMask);
173  COMPARE(castedMask, castedMask && (a == i0)) << ", a = " << a << ", i0 = " << i0 << ", i = " << i << ", j = " << j;
174 
175  Vec b(Zero);
176  b.gather(array, &S::data, i, j, castedMask);
177  COMPARE(castedMask, (b == i0));
178  if (!castedMask.isFull()) {
179  COMPARE(!castedMask, b == Vec(Zero));
180  } else {
181  Vec c(array, &S::data, i, j);
182  VERIFY((c == i0).isFull());
183 
184  Vec d(Zero);
185  d.gather(array, &S::data, i, j);
186  VERIFY((d == i0).isFull());
187  }
188  }
189  }
190  for (int i = 0; i < count; ++i) {
191  delete[] array[i].data;
192  }
193 }
194 
195 int main(int argc, char **argv)
196 {
197  initTest(argc, argv);
198 
201 #if defined(VC_CLANG) && VC_CLANG <= 0x030000
202  // clang fails with:
203  // candidate template ignored: failed template argument deduction
204  // template<typename S1, typename IT> inline Vector(const S1 *array, const T S1::* member1, IT indexes, Mask mask = true)
205 #warning "Skipping compilation of tests gatherStruct and gather2dim because of clang bug"
206 #else
209 #endif
210 
211  return 0;
212 }
void gatherStruct()
Definition: gather.cpp:98
void initTest(int argc, char **argv)
Definition: unittest.h:169
int main(int argc, char **argv)
Definition: gather.cpp:195
const char * Size
Definition: TXMLSetup.cxx:56
double T(double x)
Definition: ChebyshevPol.h:34
void gatherArray()
Definition: gather.cpp:51
TArc * a
Definition: textangle.C:12
Double_t x[n]
Definition: legend1.C:17
#define COMPARE(a, b)
Definition: unittest.h:509
void gather2dim()
Definition: gather.cpp:152
#define testAllTypes(name)
Definition: unittest.h:43
#define for_all_masks(VecType, _mask_)
Definition: unittest.h:677
TMarker * m
Definition: textangle.C:8
#define VERIFY(cond)
Definition: unittest.h:515
static const float S
Definition: mandel.cpp:113
Double_t y[n]
Definition: legend1.C:17
Definition: casts.h:28
void maskedGatherArray()
Definition: gather.cpp:25