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