CMS 3D CMS Logo

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