ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
mask.cpp
Go to the documentation of this file.
1 /* This file is part of the Vc library.
2 
3  Copyright (C) 2009-2011 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 #include "vectormemoryhelper.h"
23 #include <cmath>
24 
25 using namespace Vc;
26 
27 template<typename Vec> void testInc()
28 {
30  typedef typename Vec::EntryType T;
31  typedef typename Vec::Mask Mask;
32  T *data = mem;
33  for (int borderI = 0; borderI < Vec::Size; ++borderI) {
34  const T border = static_cast<T>(borderI);
35  for (int i = 0; i < Vec::Size; ++i) {
36  data[i] = static_cast<T>(i);
37  data[i + Vec::Size] = data[i] + static_cast<T>(data[i] < border ? 1 : 0);
38  }
39  Vec a(&data[0]);
40  Vec b(&data[Vec::Size]);
41  Mask m = a < border;
42  Vec aa(a);
43  COMPARE(aa(m)++, a) << ", border: " << border << ", m: " << m;
44  COMPARE(aa, b) << ", border: " << border << ", m: " << m;
45  COMPARE(++a(m), b) << ", border: " << border << ", m: " << m;
46  COMPARE(a, b) << ", border: " << border << ", m: " << m;
47  }
48 }
49 
50 template<typename Vec> void testDec()
51 {
53  typedef typename Vec::EntryType T;
54  typedef typename Vec::Mask Mask;
55  T *data = mem;
56  for (int borderI = 0; borderI < Vec::Size; ++borderI) {
57  const T border = static_cast<T>(borderI);
58  for (int i = 0; i < Vec::Size; ++i) {
59  data[i] = static_cast<T>(i + 1);
60  data[i + Vec::Size] = data[i] - static_cast<T>(data[i] < border ? 1 : 0);
61  }
62  Vec a(&data[0]);
63  Vec b(&data[Vec::Size]);
64  Mask m = a < border;
65  Vec aa(a);
66  COMPARE(aa(m)--, a);
67  COMPARE(--a(m), b);
68  COMPARE(a, b);
69  COMPARE(aa, b);
70  }
71 }
72 
73 template<typename Vec> void testPlusEq()
74 {
76  typedef typename Vec::EntryType T;
77  typedef typename Vec::Mask Mask;
78  T *data = mem;
79  for (int borderI = 0; borderI < Vec::Size; ++borderI) {
80  const T border = static_cast<T>(borderI);
81  for (int i = 0; i < Vec::Size; ++i) {
82  data[i] = static_cast<T>(i + 1);
83  data[i + Vec::Size] = data[i] + static_cast<T>(data[i] < border ? 2 : 0);
84  }
85  Vec a(&data[0]);
86  Vec b(&data[Vec::Size]);
87  Mask m = a < border;
88  COMPARE(a(m) += static_cast<T>(2), b);
89  COMPARE(a, b);
90  }
91 }
92 
93 template<typename Vec> void testMinusEq()
94 {
96  typedef typename Vec::EntryType T;
97  typedef typename Vec::Mask Mask;
98  T *data = mem;
99  for (int borderI = 0; borderI < Vec::Size; ++borderI) {
100  const T border = static_cast<T>(borderI);
101  for (int i = 0; i < Vec::Size; ++i) {
102  data[i] = static_cast<T>(i + 2);
103  data[i + Vec::Size] = data[i] - static_cast<T>(data[i] < border ? 2 : 0);
104  }
105  Vec a(&data[0]);
106  Vec b(&data[Vec::Size]);
107  Mask m = a < border;
108  COMPARE(a(m) -= static_cast<T>(2), b);
109  COMPARE(a, b);
110  }
111 }
112 
113 template<typename Vec> void testTimesEq()
114 {
116  typedef typename Vec::EntryType T;
117  typedef typename Vec::Mask Mask;
118  T *data = mem;
119  for (int borderI = 0; borderI < Vec::Size; ++borderI) {
120  const T border = static_cast<T>(borderI);
121  for (int i = 0; i < Vec::Size; ++i) {
122  data[i] = static_cast<T>(i);
123  data[i + Vec::Size] = data[i] * static_cast<T>(data[i] < border ? 2 : 1);
124  }
125  Vec a(&data[0]);
126  Vec b(&data[Vec::Size]);
127  Mask m = a < border;
128  COMPARE(a(m) *= static_cast<T>(2), b);
129  COMPARE(a, b);
130  }
131 }
132 
133 template<typename Vec> void testDivEq()
134 {
136  typedef typename Vec::EntryType T;
137  typedef typename Vec::Mask Mask;
138  T *data = mem;
139  for (int borderI = 0; borderI < Vec::Size; ++borderI) {
140  const T border = static_cast<T>(borderI);
141  for (int i = 0; i < Vec::Size; ++i) {
142  data[i] = static_cast<T>(5 * i);
143  data[i + Vec::Size] = data[i] / static_cast<T>(data[i] < border ? 3 : 1);
144  }
145  Vec a(&data[0]);
146  Vec b(&data[Vec::Size]);
147  Mask m = a < border;
148  COMPARE(a(m) /= static_cast<T>(3), b);
149  COMPARE(a, b);
150  }
151 }
152 
153 template<typename Vec> void testAssign()
154 {
156  typedef typename Vec::EntryType T;
157  typedef typename Vec::Mask Mask;
158  T *data = mem;
159  for (int borderI = 0; borderI < Vec::Size; ++borderI) {
160  const T border = static_cast<T>(borderI);
161  for (int i = 0; i < Vec::Size; ++i) {
162  data[i] = static_cast<T>(i);
163  data[i + Vec::Size] = data[i] + static_cast<T>(data[i] < border ? 2 : 0);
164  }
165  Vec a(&data[0]);
166  Vec b(&data[Vec::Size]);
167  Mask m = a < border;
168  COMPARE(a(m) = b, b);
169  COMPARE(a, b);
170  }
171 }
172 
173 template<typename Vec> void testZero()
174 {
175  typedef typename Vec::EntryType T;
176  typedef typename Vec::Mask Mask;
177  typedef typename Vec::IndexType I;
178 
179  for (int cut = 0; cut < Vec::Size; ++cut) {
180  const Mask mask(I(Vc::IndexesFromZero) < cut);
181  //std::cout << mask << std::endl;
182 
183  const T aa = 4;
184  Vec a(aa);
185  Vec b(Vc::Zero);
186 
187  b(!mask) = a;
188  a.setZero(mask);
189 
190  COMPARE(a, b);
191  }
192 }
193 
194 template<typename Vec> void testCount()
195 {
196  for_all_masks(Vec, m) {
197  int count = 0;
198  for (int i = 0; i < Vec::Size; ++i) {
199  if (m[i]) {
200  ++count;
201  }
202  }
203  COMPARE(m.count(), count) << ", m = " << m;
204  }
205 }
206 
207 template<typename Vec> void testFirstOne()
208 {
209  typedef typename Vec::IndexType I;
210  typedef typename Vec::Mask M;
211 
212  for (int i = 0; i < Vec::Size; ++i) {
213  const M mask(I(Vc::IndexesFromZero) == i);
214  COMPARE(mask.firstOne(), i);
215  }
216 }
217 
218 template<typename M1, typename M2> void testLogicalOperatorsImpl()
219 {
220  VERIFY((M1(true) && M2(true)).isFull());
221  VERIFY((M1(true) && M2(false)).isEmpty());
222  VERIFY((M1(true) || M2(true)).isFull());
223  VERIFY((M1(true) || M2(false)).isFull());
224  VERIFY((M1(false) || M2(false)).isEmpty());
225 }
226 
227 template<typename M1, typename M2> void testBinaryOperatorsImpl()
228 {
229  testLogicalOperatorsImpl<M1, M2>();
230 
231  VERIFY((M1(true) & M2(true)).isFull());
232  VERIFY((M1(true) & M2(false)).isEmpty());
233  VERIFY((M1(true) | M2(true)).isFull());
234  VERIFY((M1(true) | M2(false)).isFull());
235  VERIFY((M1(false) | M2(false)).isEmpty());
236  VERIFY((M1(true) ^ M2(true)).isEmpty());
237  VERIFY((M1(true) ^ M2(false)).isFull());
238 }
239 
241 {
242  testLogicalOperatorsImpl< short_m, sfloat_m>();
243  testLogicalOperatorsImpl<ushort_m, sfloat_m>();
244  testLogicalOperatorsImpl<sfloat_m, short_m>();
245  testLogicalOperatorsImpl<sfloat_m, ushort_m>();
246 
247  testBinaryOperatorsImpl< short_m, short_m>();
248  testBinaryOperatorsImpl< short_m, ushort_m>();
249  testBinaryOperatorsImpl<ushort_m, short_m>();
250  testBinaryOperatorsImpl<ushort_m, ushort_m>();
251  testBinaryOperatorsImpl<sfloat_m, sfloat_m>();
252 
253  testBinaryOperatorsImpl< int_m, int_m>();
254  testBinaryOperatorsImpl< int_m, uint_m>();
255  testBinaryOperatorsImpl< int_m, float_m>();
256  testBinaryOperatorsImpl< uint_m, int_m>();
257  testBinaryOperatorsImpl< uint_m, uint_m>();
258  testBinaryOperatorsImpl< uint_m, float_m>();
259  testBinaryOperatorsImpl< float_m, int_m>();
260  testBinaryOperatorsImpl< float_m, uint_m>();
261  testBinaryOperatorsImpl< float_m, float_m>();
262 
263  testBinaryOperatorsImpl<double_m, double_m>();
264 }
265 
266 #ifdef VC_IMPL_SSE
267 void testFloat8GatherMask()
268 {
270  short_v::Memory andMemory;
271  for (int i = 0; i < short_v::Size; ++i) {
272  andMemory[i] = 1 << i;
273  }
274  const short_v andMask(andMemory);
275 
276  for (unsigned int i = 0; i < data.vectorsCount(); ++i) {
277  data.vector(i) = andMask & i;
278  }
279 
280  for (unsigned int i = 0; i < data.vectorsCount(); ++i) {
281  const short_m mask = data.vector(i) == short_v::Zero();
282 
284  gatherMaskA(mask),
285  gatherMaskB(static_cast<sfloat_m>(mask));
286  COMPARE(gatherMaskA.toInt(), gatherMaskB.toInt());
287  }
288 }
289 #endif
290 
291 int main(int argc, char **argv)
292 {
293  initTest(argc, argv);
294 
306 
307 #ifdef VC_IMPL_SSE
308  runTest(testFloat8GatherMask);
309 #endif
310 
311  return 0;
312 }
void testMinusEq()
Definition: mask.cpp:93
void initTest(int argc, char **argv)
Definition: unittest.h:169
const char * Size
Definition: TXMLSetup.cxx:56
void testBinaryOperators()
Definition: mask.cpp:240
_VC_CONSTEXPR size_t vectorsCount() const
Definition: memory.h:169
TArc * a
Definition: textangle.C:12
void testLogicalOperatorsImpl()
Definition: mask.cpp:218
int main(int argc, char **argv)
Definition: mask.cpp:291
VECTOR_NAMESPACE::short_v short_v
Definition: vector.h:90
TTree * T
#define COMPARE(a, b)
Definition: unittest.h:509
void testAssign()
Definition: mask.cpp:153
void testBinaryOperatorsImpl()
Definition: mask.cpp:227
#define testAllTypes(name)
Definition: unittest.h:43
void testFirstOne()
Definition: mask.cpp:207
Vc_ALWAYS_INLINE Vc_PURE VectorPointerHelper< V, AlignedFlag > vector(size_t i)
Definition: memorybase.h:310
#define for_all_masks(VecType, _mask_)
Definition: unittest.h:677
void testDivEq()
Definition: mask.cpp:133
TMarker * m
Definition: textangle.C:8
void testPlusEq()
Definition: mask.cpp:73
#define VERIFY(cond)
Definition: unittest.h:515
void testInc()
Definition: mask.cpp:27
A helper class for fixed-size two-dimensional arrays.
Definition: memory.h:120
void testCount()
Definition: mask.cpp:194
void testDec()
Definition: mask.cpp:50
#define I(x, y, z)
short_v::Mask short_m
Definition: vector.h:91
void testZero()
Definition: mask.cpp:173
void testTimesEq()
Definition: mask.cpp:113
#define runTest(name)
Definition: unittest.h:42