ROOT  6.06/09
Reference Guide
store.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 <cstring>
23 
24 using namespace Vc;
25 
26 template<typename Vec> void alignedStore()
27 {
28  typedef typename Vec::EntryType T;
29  enum {
30  Count = 256 * 1024 / sizeof(T)
31  };
32 
33  Memory<Vec, Count> array;
34  // do the memset to make sure the array doesn't have the old data from a previous call which
35  // would mask a real problem
36  std::memset(array, 0xff, Count * sizeof(T));
37  T xValue = 1;
38  const Vec x(xValue);
39  for (int i = 0; i < Count; i += Vec::Size) {
40  x.store(&array[i]);
41  }
42 
43  for (int i = 0; i < Count; ++i) {
44  COMPARE(array[i], xValue);
45  }
46 }
47 
48 template<typename Vec> void unalignedStore()
49 {
50  typedef typename Vec::EntryType T;
51  enum {
52  Count = 256 * 1024 / sizeof(T)
53  };
54 
55  Memory<Vec, Count> array;
56  // do the memset to make sure the array doesn't have the old data from a previous call which
57  // would mask a real problem
58  std::memset(array, 0xff, Count * sizeof(T));
59  T xValue = 1;
60  const Vec x(xValue);
61  for (int i = 1; i < Count - Vec::Size + 1; i += Vec::Size) {
62  x.store(&array[i], Unaligned);
63  }
64 
65  for (int i = 1; i < Count - Vec::Size + 1; ++i) {
66  COMPARE(array[i], xValue);
67  }
68 }
69 
70 template<typename Vec> void streamingAndAlignedStore()
71 {
72  typedef typename Vec::EntryType T;
73  enum {
74  Count = 256 * 1024 / sizeof(T)
75  };
76 
77  Memory<Vec, Count> array;
78  // do the memset to make sure the array doesn't have the old data from a previous call which
79  // would mask a real problem
80  std::memset(array, 0xff, Count * sizeof(T));
81  T xValue = 1;
82  const Vec x(xValue);
83  for (int i = 0; i < Count; i += Vec::Size) {
84  x.store(&array[i], Streaming | Aligned);
85  }
86 
87  for (int i = 0; i < Count; ++i) {
88  COMPARE(array[i], xValue);
89  }
90 }
91 
92 template<typename Vec> void streamingAndUnalignedStore()
93 {
94  typedef typename Vec::EntryType T;
95  enum {
96  Count = 256 * 1024 / sizeof(T)
97  };
98 
99  Memory<Vec, Count> array;
100  // do the memset to make sure the array doesn't have the old data from a previous call which
101  // would mask a real problem
102  std::memset(array, 0xff, Count * sizeof(T));
103  T xValue = 1;
104  const Vec x(xValue);
105  for (int i = 1; i < Count - Vec::Size + 1; i += Vec::Size) {
106  x.store(&array[i], Streaming | Unaligned);
107  }
108 
109  for (int i = 1; i < Count - Vec::Size + 1; ++i) {
110  COMPARE(array[i], xValue);
111  }
112 }
113 
114 template<typename Vec> void maskedStore()
115 {
116  typedef typename Vec::EntryType T;
117  typedef typename Vec::Mask M;
118  M mask;
119  {
120  typedef typename Vec::IndexType I;
121  const I tmp(IndexesFromZero);
122  const typename I::Mask k = (tmp & I(One)) > 0;
123  mask = M(k);
124  }
125 
126  const int count = 256 * 1024 / sizeof(T);
127  const int outerCount = count / Vec::Size;
128  Vc::Memory<Vec> array(count);
129  array.setZero();
130  const T nullValue = 0;
131  const T setValue = 170;
132  const Vec x(setValue);
133  for (int i = 0; i < count; i += Vec::Size) {
134  x.store(&array[i], mask);
135  }
136 
137  for (int i = 1; i < count; i += 2) {
138  COMPARE(array[i], setValue) << ", i: " << i << ", count: " << count << ", outer: " << outerCount;
139  }
140  for (int i = 0; i < count; i += 2) {
141  COMPARE(array[i], nullValue) << ", i: " << i << ", count: " << count << ", outer: " << outerCount;
142  }
143 }
144 
145 int main(int argc, char **argv)
146 {
147  initTest(argc, argv);
148 
153 
154  if (float_v::Size > 1) {
155  runTest(maskedStore<int_v>);
156  runTest(maskedStore<uint_v>);
157  runTest(maskedStore<float_v>);
158  runTest(maskedStore<double_v>);
159  runTest(maskedStore<short_v>);
160  runTest(maskedStore<ushort_v>);
161  runTest(maskedStore<sfloat_v>);
162  }
163  return 0;
164 }
void initTest(int argc, char **argv)
Definition: unittest.h:169
void unalignedStore()
Definition: store.cpp:48
const char * Size
Definition: TXMLSetup.cxx:56
double T(double x)
Definition: ChebyshevPol.h:34
void alignedStore()
Definition: store.cpp:26
void streamingAndAlignedStore()
Definition: store.cpp:70
void maskedStore()
Definition: store.cpp:114
Double_t x[n]
Definition: legend1.C:17
#define COMPARE(a, b)
Definition: unittest.h:509
#define testAllTypes(name)
Definition: unittest.h:43
A helper class for fixed-size two-dimensional arrays.
Definition: memory.h:120
int main(int argc, char **argv)
Definition: store.cpp:145
Definition: casts.h:28
#define I(x, y, z)
void streamingAndUnalignedStore()
Definition: store.cpp:92
#define runTest(name)
Definition: unittest.h:42