ROOT  6.06/09
Reference Guide
scatter.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 // includes {{{1
20 #include "unittest.h"
21 #include <iostream>
22 #include <cstring>
23 
24 using namespace Vc;
25 
26 template<typename Vec> void maskedScatterArray() //{{{1
27 {
28  typedef typename Vec::IndexType It;
29  typedef typename Vec::EntryType T;
30 
31  T mem[Vec::Size];
32  const Vec v(It::IndexesFromZero() + 1);
33 
34  for_all_masks(Vec, m) {
35  Vec::Zero().store(mem, Vc::Unaligned);
36  v.scatter(&mem[0], It::IndexesFromZero(), m);
37 
38  for (int i = 0; i < Vec::Size; ++i) {
39  COMPARE(mem[i], m[i] ? v[i] : T(0)) << " i = " << i << ", m = " << m;
40  }
41  }
42 }
43 
44 template<typename Vec> void scatterArray() //{{{1
45 {
46  typedef typename Vec::IndexType It;
47  const int count = 31999;
48  typename Vec::EntryType array[count], out[count];
49  for (int i = 0; i < count; ++i) {
50  array[i] = i - 100;
51  }
52  typename It::Mask mask;
53  for (It i(IndexesFromZero); !(mask = (i < count)).isEmpty(); i += Vec::Size) {
54  typename Vec::Mask castedMask(mask);
55  if (castedMask.isFull()) {
56  Vec a(array, i);
57  a += Vec(One);
58  a.scatter(out, i);
59  } else {
60  Vec a(array, i, castedMask);
61  a += Vec(One);
62  a.scatter(out, i, castedMask);
63  }
64  }
65  for (int i = 0; i < count; ++i) {
66  array[i] += 1;
67  COMPARE(array[i], out[i]);
68  }
69  COMPARE(0, std::memcmp(array, out, count * sizeof(typename Vec::EntryType)));
70 }
71 
72 template<typename T> struct Struct //{{{1
73 {
74  T a;
75  char x;
76  T b;
77  short y;
78  T c;
79  char z;
80 };
81 
82 template<typename Vec> void scatterStruct() //{{{1
83 {
84  typedef typename Vec::IndexType It;
85  typedef Struct<typename Vec::EntryType> S;
86  const int count = 3999;
87  S array[count], out[count];
88  memset(array, 0, count * sizeof(S));
89  memset(out, 0, count * sizeof(S));
90  for (int i = 0; i < count; ++i) {
91  array[i].a = i;
92  array[i].b = i + 1;
93  array[i].c = i + 2;
94  }
95  typename It::Mask mask;
96  for (It i(IndexesFromZero); !(mask = (i < count)).isEmpty(); i += Vec::Size) {
97  typename Vec::Mask castedMask(mask);
98  Vec a(array, &S::a, i, castedMask);
99  Vec b(array, &S::b, i, castedMask);
100  Vec c(array, &S::c, i, castedMask);
101  a.scatter(out, &S::a, i, castedMask);
102  b.scatter(out, &S::b, i, castedMask);
103  c.scatter(out, &S::c, i, castedMask);
104  }
105  VERIFY(0 == memcmp(array, out, count * sizeof(S)));
106 }
107 
108 template<typename T> struct Struct2 //{{{1
109 {
110  char x;
111  Struct<T> b;
112  short y;
113 };
114 
115 template<typename Vec> void scatterStruct2() //{{{1
116 {
117  typedef typename Vec::IndexType It;
118  typedef Struct2<typename Vec::EntryType> S1;
119  typedef Struct<typename Vec::EntryType> S2;
120  const int count = 97;
121  S1 array[count], out[count];
122  memset(array, 0, count * sizeof(S1));
123  memset(out, 0, count * sizeof(S1));
124  for (int i = 0; i < count; ++i) {
125  array[i].b.a = i + 0;
126  array[i].b.b = i + 1;
127  array[i].b.c = i + 2;
128  }
129  typename It::Mask mask;
130  for (It i(IndexesFromZero); !(mask = (i < count)).isEmpty(); i += Vec::Size) {
131  typename Vec::Mask castedMask(mask);
132  Vec a(array, &S1::b, &S2::a, i, castedMask);
133  Vec b(array, &S1::b, &S2::b, i, castedMask);
134  Vec c(array, &S1::b, &S2::c, i, castedMask);
135  a.scatter(out, &S1::b, &S2::a, i, castedMask);
136  b.scatter(out, &S1::b, &S2::b, i, castedMask);
137  c.scatter(out, &S1::b, &S2::c, i, castedMask);
138  }
139  VERIFY(0 == memcmp(array, out, count * sizeof(S1)));
140 }
141 
142 int main(int argc, char **argv) //{{{1
143 {
144  initTest(argc, argv);
145 
146  runTest(scatterArray<int_v>);
147  runTest(scatterArray<uint_v>);
148  runTest(scatterArray<float_v>);
149  runTest(scatterArray<double_v>);
150  runTest(scatterArray<sfloat_v>);
151  runTest(scatterArray<short_v>);
152  runTest(scatterArray<ushort_v>);
154 #if defined(VC_CLANG) && VC_CLANG <= 0x030000
155  // clang fails with:
156  // candidate template ignored: failed template argument deduction
157  // template<typename S1, typename IT> inline Vector(const S1 *array, const T S1::*
158  // member1, IT indexes, Mask mask = true)
159 #warning "Skipping compilation of tests scatterStruct and scatterStruct2 because of clang bug"
160 #else
161  runTest(scatterStruct<int_v>);
162  runTest(scatterStruct<uint_v>);
163  runTest(scatterStruct<float_v>);
164  runTest(scatterStruct<double_v>);
165  runTest(scatterStruct<sfloat_v>);
166  runTest(scatterStruct<short_v>);
167  runTest(scatterStruct<ushort_v>);
169 #endif
170  return 0;
171 }
172 
173 // vim: foldmethod=marker
void scatterStruct2()
Definition: scatter.cpp:115
void initTest(int argc, char **argv)
Definition: unittest.h:169
const char * Size
Definition: TXMLSetup.cxx:56
double T(double x)
Definition: ChebyshevPol.h:34
TArc * a
Definition: textangle.C:12
void scatterStruct()
Definition: scatter.cpp:82
Double_t x[n]
Definition: legend1.C:17
#define COMPARE(a, b)
Definition: unittest.h:509
void maskedScatterArray()
Definition: scatter.cpp:26
#define testAllTypes(name)
Definition: unittest.h:43
#define for_all_masks(VecType, _mask_)
Definition: unittest.h:677
char * out
Definition: TBase64.cxx:29
SVector< double, 2 > v
Definition: Dict.h:5
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
void scatterArray()
Definition: scatter.cpp:44
Definition: casts.h:28
int main(int argc, char **argv)
Definition: scatter.cpp:142
#define runTest(name)
Definition: unittest.h:42