CMS 3D CMS Logo

ExtVec.h
Go to the documentation of this file.
1 #ifndef DataFormat_Math_ExtVec_H
2 #define DataFormat_Math_ExtVec_H
3 
4 #include<type_traits>
5 
6 #ifdef __clang__
7 #define VECTOR_EXT(N) __attribute__( ( ext_vector_type( N ) ) )
8 #else
9 #define VECTOR_EXT(N) __attribute__( ( vector_size( N ) ) )
10 #endif
11 
12 typedef float VECTOR_EXT( 8 ) cms_float32x2_t;
13 typedef float VECTOR_EXT( 16 ) cms_float32x4_t;
14 typedef float VECTOR_EXT( 32 ) cms_float32x8_t;
15 typedef double VECTOR_EXT( 16 ) cms_float64x2_t;
16 typedef double VECTOR_EXT( 32 ) cms_float64x4_t;
17 typedef double VECTOR_EXT( 64 ) cms_float64x8_t;
18 
19 typedef long double VECTOR_EXT( 32 ) cms_float128x2_t;
20 typedef long double VECTOR_EXT( 64 ) cms_float128x4_t;
21 typedef long double VECTOR_EXT( 128 ) cms_float128x8_t;
22 
23 // template<typename T, int N> using ExtVec = T __attribute__( ( vector_size( N*sizeof(T) ) ) );
24 
25 template<typename T, int N>
26 struct ExtVecTraits {
27 // typedef T __attribute__( ( vector_size( N*sizeof(T) ) ) ) type;
28 };
29 
30 template<>
31 struct ExtVecTraits<float, 2> {
32  typedef float VECTOR_EXT( 2*sizeof(float) ) type;
33 };
34 
35 template<>
36 struct ExtVecTraits<float, 4> {
37  typedef float VECTOR_EXT(4*sizeof(float)) type;
38 };
39 
40 template<>
41 struct ExtVecTraits<double, 2> {
42  typedef double VECTOR_EXT( 2*sizeof(double) ) type;
43 };
44 
45 template<>
46 struct ExtVecTraits<double, 4> {
47  typedef double VECTOR_EXT( 4*sizeof(double) ) type;
48 };
49 
50 template<>
51 struct ExtVecTraits<long double, 2> {
52  typedef long double VECTOR_EXT( 2*sizeof(long double) ) type;
53 };
54 
55 template<>
56 struct ExtVecTraits<long double, 4> {
57  typedef long double VECTOR_EXT( 4*sizeof(long double) ) type;
58 };
59 
60 template<typename T, int N> using ExtVec = typename ExtVecTraits<T,N>::type;
61 
62 template<typename T> using Vec4 = ExtVec<T,4>;
63 template<typename T> using Vec2 = ExtVec<T,2>;
64 
65 template<typename V>
66 inline
68 {
70  return Vec2<T>{v[0],v[1]};
71 }
72 
73 template<typename V>
74 inline
76 {
78  return Vec2<T>{v[2],v[3]};
79 }
80 
81 template<typename Vec, typename F>
82 inline
83 Vec apply(Vec v, F f) {
85  constexpr int N = sizeof(Vec)/sizeof(T);
86  Vec ret;
87  for (int i=0;i!=N;++i) ret[i] = f(v[i]);
88  return ret;
89 }
90 
91 
92 template<typename Vec>
93 inline
94 Vec cross3(Vec x, Vec y) {
95  // typedef Vec4<T> Vec;
96  // yz - zy, zx - xz, xy - yx, 0
97  Vec x1200{ x[1], x[2], x[0], x[0] };
98  Vec y2010{ y[2], y[0], y[1], y[0] };
99  Vec x2010{ x[2], x[0], x[1], x[0] };
100  Vec y1200{ y[1], y[2], y[0], y[0] };
101  return x1200 * y2010 - x2010 * y1200;
102 }
103 
104 template<typename V1,typename V2>
105 inline
107  return x[0]*y[1]-x[1]*y[0];
108 }
109 
110 
111 
112 /*
113 template<typename T>
114 T dot_product(Vec4<T> x, Vec4<T> y) {
115  auto res = x*y;
116  T ret=0;
117  for (int i=0;i!=4;++i) ret+=res[i];
118  return ret;
119 }
120 */
121 
122 /*
123 template<typename V, int K>
124 inline
125 V get1(V v) { return (V){v[K]}; }
126 */
127 
128 /*
129 template<typename T, int N>
130 inline
131 T dot(ExtVec<T,N> x, ExtVec<T,N> y) {
132  T ret=0;
133  for (int i=0;i!=N;++i) ret+=x[i]*y[i];
134  return ret;
135 }
136 */
137 
138 template<typename V>
139 inline
142  constexpr int N = sizeof(V)/sizeof(T);
143  T ret=0;
144  for (int i=0;i!=N;++i) ret+=x[i]*y[i];
145  return ret;
146 }
147 
148 template<typename V1,typename V2 >
149 inline
152  T ret=0;
153  for (int i=0;i!=2;++i) ret+=x[i]*y[i];
154  return ret;
155 }
156 
157 
158 
159 
166 
167 /*
168 template<typename T>
169 struct As3D {
170  Vec4<T> const & v;
171  As3D(Vec4<T> const &iv ) : v(iv){}
172 };
173 template<typename T>
174 inline As3D<T> as3D(Vec4<T> const &v ) { return v;}
175 */
176 
177 template<typename V>
178 struct As3D {
179  V const & v;
180  As3D(V const &iv ) : v(iv){}
181 };
182 template<typename V>
183 inline As3D<V> as3D(V const &v ) { return v;}
184 
185 
186 // rotations
187 
188 template<typename T>
189 struct Rot3 {
190  typedef Vec4<T> Vec;
191  Vec axis[3];
192 
194  axis{{(Vec){T(1),0,0,0}},
195  {(Vec){0,T(1),0,0}},
196  {(Vec){0,0,T(1),0}}
197  }{}
198 
200  axis{ix,iy,iz}{}
201 
202  constexpr Rot3( T xx, T xy, T xz, T yx, T yy, T yz, T zx, T zy, T zz) :
203  axis{ {(Vec){xx,xy,xz,0}},
204  {(Vec){yx,yy,yz,0}},
205  {(Vec){zx,zy,zz,0}}
206  }{}
207 
209  return Rot3( axis[0][0], axis[1][0], axis[2][0],
210  axis[0][1], axis[1][1], axis[2][1],
211  axis[0][2], axis[1][2], axis[2][2]
212  );
213  }
214 
215  constexpr Vec4<T> x() const { return axis[0];}
216  constexpr Vec4<T> y() const { return axis[1];}
217  constexpr Vec4<T> z() const { return axis[2];}
218 
219  // toLocal...
221  return transpose().rotateBack(v);
222  }
223 
224 
225  // toGlobal...
227  return v[0]*axis[0] + v[1]*axis[1] + v[2]*axis[2];
228  }
229 
230  Rot3 rotate(Rot3 const& r) const {
231  Rot3 tr = transpose();
232  return Rot3(tr.rotateBack(r.axis[0]),tr.rotateBack(r.axis[1]),tr.rotateBack(r.axis[2]));
233  }
234 
235  constexpr Rot3 rotateBack(Rot3 const& r) const {
236  return Rot3(rotateBack(r.axis[0]),rotateBack(r.axis[1]),rotateBack(r.axis[2]));
237  }
238 
239 
240  };
241 
243 
245 
246 template<typename T>
247 inline constexpr Rot3<T> operator *(Rot3<T> const & rh, Rot3<T> const & lh) {
248  return lh.rotateBack(rh);
249 }
250 
251 
252 template<typename T>
253 struct Rot2 {
254  typedef Vec2<T> Vec;
255  Vec2<T> axis[2];
256 
258  axis{{(Vec){T(1),0}},
259  {(Vec){0,T(1)}}
260  }{}
261 
263  axis{ix, iy}{}
264 
265  constexpr Rot2( T xx, T xy, T yx, T yy) :
266  Rot2( (Vec){xx,xy},
267  (Vec){yx,yy}
268  ){}
269 
271  return Rot2( axis[0][0], axis[1][0],
272  axis[0][1], axis[1][1]
273  );
274  }
275 
276  constexpr Vec2<T> x() const { return axis[0];}
277  constexpr Vec2<T> y() const { return axis[1];}
278 
279  // toLocal...
281  return transpose().rotateBack(v);
282  }
283 
284 
285  // toGlobal...
287  return v[0]*axis[0] + v[1]*axis[1];
288  }
289 
290  Rot2 rotate(Rot2 const& r) const {
291  Rot2 tr = transpose();
292  return Rot2(tr.rotateBack(r.axis[0]),tr.rotateBack(r.axis[1]));
293  }
294 
295  constexpr Rot2 rotateBack(Rot2 const& r) const {
296  return Rot2(rotateBack(r.axis[0]),rotateBack(r.axis[1]));
297  }
298 
299 
300 };
301 
303 
305 
306 
307 
308 template<typename T>
309 inline constexpr Rot2<T> operator *(Rot2<T> const & rh, Rot2<T> const & lh) {
310  return lh.rotateBack(rh);
311 }
312 
313 
314 
315 #include <iosfwd>
316 std::ostream & operator<<(std::ostream & out, Vec2D const & v);
317 std::ostream & operator<<(std::ostream & out, Vec2F const & v);
318 std::ostream & operator<<(std::ostream & out, Vec4F const & v);
319 std::ostream & operator<<(std::ostream & out, Vec4D const & v);
320 
321 std::ostream & operator<<(std::ostream & out, As3D<Vec4F> const & v);
322 std::ostream & operator<<(std::ostream & out, As3D<Vec4D> const & v);
323 
324 std::ostream & operator<<(std::ostream & out, Rot3F const & v);
325 std::ostream & operator<<(std::ostream & out, Rot3D const & v);
326 std::ostream & operator<<(std::ostream & out, Rot2F const & v);
327 std::ostream & operator<<(std::ostream & out, Rot2D const & v);
328 
329 
330 #ifdef USE_INLINE_IO
331 #include <ostream>
332 std::ostream & operator<<(std::ostream & out, ::Vec4F const & v) {
333  return out << '(' << v[0] <<", " << v[1] <<", "<< v[2] <<", "<< v[3] <<')';
334 }
335 std::ostream & operator<<(std::ostream & out, ::Vec4D const & v) {
336  return out << '(' << v[0] <<", " << v[1] <<", "<< v[2] <<", "<< v[3] <<')';
337 }
338 std::ostream & operator<<(std::ostream & out, ::Vec2F const & v) {
339  return out << '(' << v[0] <<", " << v[1] <<')';
340 }
341 std::ostream & operator<<(std::ostream & out, ::Vec2D const & v) {
342  return out << '(' << v[0] <<", " << v[1] <<')';
343 }
344 
345 std::ostream & operator<<(std::ostream & out, ::As3D<Vec4F> const & v) {
346  return out << '(' << v.v[0] <<", " << v.v[1] <<", "<< v.v[2] <<')';
347 }
348 
349 std::ostream & operator<<(std::ostream & out, ::As3D<Vec4D> const & v) {
350  return out << '(' << v.v[0] <<", " << v.v[1] <<", "<< v.v[2] <<')';
351 }
352 
353 std::ostream & operator<<(std::ostream & out, ::Rot3F const & r){
354  return out << as3D(r.axis[0]) << '\n' << as3D(r.axis[1]) << '\n' << as3D(r.axis[2]);
355 }
356 
357 std::ostream & operator<<(std::ostream & out, ::Rot3D const & r){
358  return out << as3D(r.axis[0]) << '\n' << as3D(r.axis[1]) << '\n' << as3D(r.axis[2]);
359 }
360 
361 std::ostream & operator<<(std::ostream & out, ::Rot2F const & r){
362  return out << r.axis[0] << '\n' << r.axis[1];
363 }
364 
365 std::ostream & operator<<(std::ostream & out, ::Rot2D const & r){
366  return out << r.axis[0] << '\n' << r.axis[1];
367 }
368 #endif
369 
370 
371 #endif
type
Definition: HCALResponse.h:21
As3D(V const &iv)
Definition: ExtVec.h:180
Vec4< T > Vec
Definition: ExtVec.h:190
Vec2< T > Vec
Definition: ExtVec.h:254
def transpose(a)
Definition: geometryDiff.py:39
constexpr Vec2< T > rotate(Vec2< T > v) const
Definition: ExtVec.h:280
auto cross2(V1 x, V2 y) -> typename std::remove_reference< decltype(x[0])>::type
Definition: ExtVec.h:106
constexpr Vec4< T > y() const
Definition: ExtVec.h:216
constexpr Rot2(T xx, T xy, T yx, T yy)
Definition: ExtVec.h:265
Rot2< double > Rot2D
Definition: ExtVec.h:304
ExtVec< T, 4 > Vec4
Definition: ExtVec.h:62
constexpr Rot2 rotateBack(Rot2 const &r) const
Definition: ExtVec.h:295
typename ExtVecTraits< T, N >::type ExtVec
Definition: ExtVec.h:60
auto xy(V v) -> Vec2< typename std::remove_reference< decltype(v[0])>::type >
Definition: ExtVec.h:67
Rot2< float > Rot2F
Definition: ExtVec.h:302
Rot3< float > Rot3F
Definition: ExtVec.h:242
constexpr Rot2()
Definition: ExtVec.h:257
Vec4< float > Vec3F
Definition: ExtVec.h:162
constexpr Rot3 rotateBack(Rot3 const &r) const
Definition: ExtVec.h:235
Vec cross3(Vec x, Vec y)
Definition: ExtVec.h:94
bool int lh
Definition: SIMDVec.h:21
std::ostream & operator<<(std::ostream &out, Vec2D const &v)
Definition: SSEVec.cc:16
Vec4< float > Vec4F
Definition: ExtVec.h:161
constexpr Rot3 transpose() const
Definition: ExtVec.h:208
Vec4< double > Vec4D
Definition: ExtVec.h:165
Rot2 rotate(Rot2 const &r) const
Definition: ExtVec.h:290
constexpr Vec4< T > x() const
Definition: ExtVec.h:215
Vec2< T > axis[2]
Definition: ExtVec.h:255
Vec2< float > Vec2F
Definition: ExtVec.h:160
V const & v
Definition: ExtVec.h:179
Definition: ExtVec.h:178
double f[11][100]
def template(fileName, svg, replaceme="REPLACEME")
Definition: svgfig.py:521
constexpr Vec4< T > z() const
Definition: ExtVec.h:217
Rot3 rotate(Rot3 const &r) const
Definition: ExtVec.h:230
constexpr Vec2< T > y() const
Definition: ExtVec.h:277
constexpr Vec2< T > x() const
Definition: ExtVec.h:276
constexpr Rot2 transpose() const
Definition: ExtVec.h:270
constexpr Rot3< T > operator*(Rot3< T > const &rh, Rot3< T > const &lh)
Definition: ExtVec.h:247
#define N
Definition: blowfish.cc:9
constexpr Rot3(T xx, T xy, T xz, T yx, T yy, T yz, T zx, T zy, T zz)
Definition: ExtVec.h:202
constexpr Vec4< T > rotateBack(Vec4< T > v) const
Definition: ExtVec.h:226
ExtVec< T, 2 > Vec2
Definition: ExtVec.h:63
Definition: ExtVec.h:189
constexpr Rot3(Vec4< T > ix, Vec4< T > iy, Vec4< T > iz)
Definition: ExtVec.h:199
constexpr Vec2< T > rotateBack(Vec2< T > v) const
Definition: ExtVec.h:286
Definition: ExtVec.h:253
Vec2< double > Vec2D
Definition: ExtVec.h:163
auto dot(V x, V y) -> typename std::remove_reference< decltype(x[0])>::type
Definition: ExtVec.h:140
#define VECTOR_EXT(N)
Definition: ExtVec.h:9
auto zw(V v) -> Vec2< typename std::remove_reference< decltype(v[0])>::type >
Definition: ExtVec.h:75
Vec4< double > Vec3D
Definition: ExtVec.h:164
As3D< V > as3D(V const &v)
Definition: ExtVec.h:183
constexpr Vec4< T > rotate(Vec4< T > v) const
Definition: ExtVec.h:220
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:281
Rot3< double > Rot3D
Definition: ExtVec.h:244
long double T
Vec axis[3]
Definition: ExtVec.h:191
constexpr Rot3()
Definition: ExtVec.h:193
auto dot2(V1 x, V2 y) -> typename std::remove_reference< decltype(x[0])>::type
Definition: ExtVec.h:150
#define constexpr
Vec apply(Vec v, F f)
Definition: ExtVec.h:83
constexpr Rot2(Vec2< T > ix, Vec2< T > iy)
Definition: ExtVec.h:262