ROOT  6.06/09
Reference Guide
swizzles.cpp
Go to the documentation of this file.
1 /* This file is part of the Vc library. {{{
2 
3  Copyright (C) 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 enum Swizzle {
26 };
27 
28 template<typename V> V scalarSwizzle(VC_ALIGNED_PARAMETER(V) v, Swizzle s)
29 {
30  V r = v;
31  for (int i = 0; i + 4 <= V::Size; i += 4) {
32  switch (s) {
33  case BADC:
34  r[i + 0] = v[i + 1];
35  r[i + 1] = v[i + 0];
36  r[i + 2] = v[i + 3];
37  r[i + 3] = v[i + 2];
38  break;
39  case CDAB:
40  r[i + 0] = v[i + 2];
41  r[i + 1] = v[i + 3];
42  r[i + 2] = v[i + 0];
43  r[i + 3] = v[i + 1];
44  break;
45  case AAAA:
46  r[i + 0] = v[i + 0];
47  r[i + 1] = v[i + 0];
48  r[i + 2] = v[i + 0];
49  r[i + 3] = v[i + 0];
50  break;
51  case BBBB:
52  r[i + 0] = v[i + 1];
53  r[i + 1] = v[i + 1];
54  r[i + 2] = v[i + 1];
55  r[i + 3] = v[i + 1];
56  break;
57  case CCCC:
58  r[i + 0] = v[i + 2];
59  r[i + 1] = v[i + 2];
60  r[i + 2] = v[i + 2];
61  r[i + 3] = v[i + 2];
62  break;
63  case DDDD:
64  r[i + 0] = v[i + 3];
65  r[i + 1] = v[i + 3];
66  r[i + 2] = v[i + 3];
67  r[i + 3] = v[i + 3];
68  break;
69  case BCAD:
70  r[i + 0] = v[i + 1];
71  r[i + 1] = v[i + 2];
72  r[i + 2] = v[i + 0];
73  r[i + 3] = v[i + 3];
74  break;
75  case BCDA:
76  r[i + 0] = v[i + 1];
77  r[i + 1] = v[i + 2];
78  r[i + 2] = v[i + 3];
79  r[i + 3] = v[i + 0];
80  break;
81  case DABC:
82  r[i + 0] = v[i + 3];
83  r[i + 1] = v[i + 0];
84  r[i + 2] = v[i + 1];
85  r[i + 3] = v[i + 2];
86  break;
87  case ACBD:
88  r[i + 0] = v[i + 0];
89  r[i + 1] = v[i + 2];
90  r[i + 2] = v[i + 1];
91  r[i + 3] = v[i + 3];
92  break;
93  case DBCA:
94  r[i + 0] = v[i + 3];
95  r[i + 1] = v[i + 1];
96  r[i + 2] = v[i + 2];
97  r[i + 3] = v[i + 0];
98  break;
99  case DCBA:
100  r[i + 0] = v[i + 3];
101  r[i + 1] = v[i + 2];
102  r[i + 2] = v[i + 1];
103  r[i + 3] = v[i + 0];
104  break;
105  }
106  }
107  return r;
108 }
109 
110 template<typename V> void testSwizzle()
111 {
112  for (int i = 0; i < 100; ++i) {
113  const V test = V::Random();
114  COMPARE(test.abcd(), test);
115  COMPARE(test.badc(), scalarSwizzle(test, BADC));
116  COMPARE(test.cdab(), scalarSwizzle(test, CDAB));
117  COMPARE(test.aaaa(), scalarSwizzle(test, AAAA));
118  COMPARE(test.bbbb(), scalarSwizzle(test, BBBB));
119  COMPARE(test.cccc(), scalarSwizzle(test, CCCC));
120  COMPARE(test.dddd(), scalarSwizzle(test, DDDD));
121  COMPARE(test.bcad(), scalarSwizzle(test, BCAD));
122  COMPARE(test.bcda(), scalarSwizzle(test, BCDA));
123  COMPARE(test.dabc(), scalarSwizzle(test, DABC));
124  COMPARE(test.acbd(), scalarSwizzle(test, ACBD));
125  COMPARE(test.dbca(), scalarSwizzle(test, DBCA));
126  COMPARE(test.dcba(), scalarSwizzle(test, DCBA));
127  }
128 }
129 
130 int main(int argc, char **argv)
131 {
132  initTest(argc, argv);
133 
134 #if VC_DOUBLE_V_SIZE >= 4 || VC_DOUBLE_V_SIZE == 1
135  runTest(testSwizzle<double_v>);
136 #endif
137  runTest(testSwizzle<float_v>);
138  runTest(testSwizzle<sfloat_v>);
139  runTest(testSwizzle<int_v>);
140  runTest(testSwizzle<uint_v>);
141  runTest(testSwizzle<short_v>);
142  runTest(testSwizzle<ushort_v>);
143 
144  return 0;
145 }
void initTest(int argc, char **argv)
Definition: unittest.h:169
const char * Size
Definition: TXMLSetup.cxx:56
V scalarSwizzle(VC_ALIGNED_PARAMETER(V) v, Swizzle s)
Definition: swizzles.cpp:28
void testSwizzle()
Definition: swizzles.cpp:110
#define COMPARE(a, b)
Definition: unittest.h:509
#define VC_ALIGNED_PARAMETER(_Type)
Definition: macros.h:368
ROOT::R::TRInterface & r
Definition: Object.C:4
SVector< double, 2 > v
Definition: Dict.h:5
Swizzle
Definition: swizzles.cpp:24
void Random()
Definition: utils.cpp:154
Definition: casts.h:28
int main(int argc, char **argv)
Definition: swizzles.cpp:130
#define runTest(name)
Definition: unittest.h:42