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>
62 
63 template <typename T>
65 template <typename T>
67 
68 template <typename V>
71  return Vec2<T>{v[0], v[1]};
72 }
73 
74 template <typename V>
77  return Vec2<T>{v[2], v[3]};
78 }
79 
80 template <typename Vec, typename F>
81 inline Vec apply(Vec v, F f) {
83  constexpr int N = sizeof(Vec) / sizeof(T);
84  Vec ret;
85  for (int i = 0; i != N; ++i)
86  ret[i] = f(v[i]);
87  return ret;
88 }
89 
90 template <typename Vec>
91 inline Vec cross3(Vec x, Vec y) {
92  // typedef Vec4<T> Vec;
93  // yz - zy, zx - xz, xy - yx, 0
94  Vec x1200{x[1], x[2], x[0], x[0]};
95  Vec y2010{y[2], y[0], y[1], y[0]};
96  Vec x2010{x[2], x[0], x[1], x[0]};
97  Vec y1200{y[1], y[2], y[0], y[0]};
98  return x1200 * y2010 - x2010 * y1200;
99 }
100 
101 template <typename V1, typename V2>
102 inline auto cross2(V1 x, V2 y) -> typename std::remove_reference<decltype(x[0])>::type {
103  return x[0] * y[1] - x[1] * y[0];
104 }
105 
106 /*
107 template<typename T>
108 T dot_product(Vec4<T> x, Vec4<T> y) {
109  auto res = x*y;
110  T ret=0;
111  for (int i=0;i!=4;++i) ret+=res[i];
112  return ret;
113 }
114 */
115 
116 /*
117 template<typename V, int K>
118 inline
119 V get1(V v) { return (V){v[K]}; }
120 */
121 
122 /*
123 template<typename T, int N>
124 inline
125 T dot(ExtVec<T,N> x, ExtVec<T,N> y) {
126  T ret=0;
127  for (int i=0;i!=N;++i) ret+=x[i]*y[i];
128  return ret;
129 }
130 */
131 
132 template <typename V>
133 inline auto dot(V x, V y) -> typename std::remove_reference<decltype(x[0])>::type {
135  constexpr int N = sizeof(V) / sizeof(T);
136  T ret = 0;
137  for (int i = 0; i != N; ++i)
138  ret += x[i] * y[i];
139  return ret;
140 }
141 
142 template <typename V1, typename V2>
143 inline auto dot2(V1 x, V2 y) -> typename std::remove_reference<decltype(x[0])>::type {
145  T ret = 0;
146  for (int i = 0; i != 2; ++i)
147  ret += x[i] * y[i];
148  return ret;
149 }
150 
157 
158 /*
159 template<typename T>
160 struct As3D {
161  Vec4<T> const & v;
162  As3D(Vec4<T> const &iv ) : v(iv){}
163 };
164 template<typename T>
165 inline As3D<T> as3D(Vec4<T> const &v ) { return v;}
166 */
167 
168 template <typename V>
169 struct As3D {
170  V const& v;
171  As3D(V const& iv) : v(iv) {}
172 };
173 template <typename V>
174 inline As3D<V> as3D(V const& v) {
175  return v;
176 }
177 
178 // rotations
179 
180 template <typename T>
181 struct Rot3 {
182  typedef Vec4<T> Vec;
183  Vec axis[3];
184 
185  constexpr Rot3() : axis{{(Vec){T(1), 0, 0, 0}}, {(Vec){0, T(1), 0, 0}}, {(Vec){0, 0, T(1), 0}}} {}
186 
187  constexpr Rot3(Vec4<T> ix, Vec4<T> iy, Vec4<T> iz) : 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}}, {(Vec){yx, yy, yz, 0}}, {(Vec){zx, zy, zz, 0}}} {}
191 
193  return Rot3(
194  axis[0][0], axis[1][0], axis[2][0], axis[0][1], axis[1][1], axis[2][1], axis[0][2], axis[1][2], axis[2][2]);
195  }
196 
197  constexpr Vec4<T> x() const { return axis[0]; }
198  constexpr Vec4<T> y() const { return axis[1]; }
199  constexpr Vec4<T> z() const { return axis[2]; }
200 
201  // toLocal...
202  constexpr Vec4<T> rotate(Vec4<T> v) const { return transpose().rotateBack(v); }
203 
204  // toGlobal...
205  constexpr Vec4<T> rotateBack(Vec4<T> v) const { return v[0] * axis[0] + v[1] * axis[1] + v[2] * axis[2]; }
206 
207  Rot3 rotate(Rot3 const& r) const {
208  Rot3 tr = transpose();
209  return Rot3(tr.rotateBack(r.axis[0]), tr.rotateBack(r.axis[1]), tr.rotateBack(r.axis[2]));
210  }
211 
212  constexpr Rot3 rotateBack(Rot3 const& r) const {
213  return Rot3(rotateBack(r.axis[0]), rotateBack(r.axis[1]), rotateBack(r.axis[2]));
214  }
215 };
216 
218 
220 
221 template <typename T>
222 inline constexpr Rot3<T> operator*(Rot3<T> const& rh, Rot3<T> const& lh) {
223  return lh.rotateBack(rh);
224 }
225 
226 template <typename T>
227 struct Rot2 {
228  typedef Vec2<T> Vec;
229  Vec2<T> axis[2];
230 
231  constexpr Rot2() : axis{{(Vec){T(1), 0}}, {(Vec){0, T(1)}}} {}
232 
233  constexpr Rot2(Vec2<T> ix, Vec2<T> iy) : axis{ix, iy} {}
234 
235  constexpr Rot2(T xx, T xy, T yx, T yy) : Rot2((Vec){xx, xy}, (Vec){yx, yy}) {}
236 
237  constexpr Rot2 transpose() const { return Rot2(axis[0][0], axis[1][0], axis[0][1], axis[1][1]); }
238 
239  constexpr Vec2<T> x() const { return axis[0]; }
240  constexpr Vec2<T> y() const { return axis[1]; }
241 
242  // toLocal...
243  constexpr Vec2<T> rotate(Vec2<T> v) const { return transpose().rotateBack(v); }
244 
245  // toGlobal...
246  constexpr Vec2<T> rotateBack(Vec2<T> v) const { return v[0] * axis[0] + v[1] * axis[1]; }
247 
248  Rot2 rotate(Rot2 const& r) const {
249  Rot2 tr = transpose();
250  return Rot2(tr.rotateBack(r.axis[0]), tr.rotateBack(r.axis[1]));
251  }
252 
253  constexpr Rot2 rotateBack(Rot2 const& r) const { return Rot2(rotateBack(r.axis[0]), rotateBack(r.axis[1])); }
254 };
255 
257 
259 
260 template <typename T>
261 inline constexpr Rot2<T> operator*(Rot2<T> const& rh, Rot2<T> const& lh) {
262  return lh.rotateBack(rh);
263 }
264 
265 #include <iosfwd>
266 std::ostream& operator<<(std::ostream& out, Vec2D const& v);
267 std::ostream& operator<<(std::ostream& out, Vec2F const& v);
268 std::ostream& operator<<(std::ostream& out, Vec4F const& v);
269 std::ostream& operator<<(std::ostream& out, Vec4D const& v);
270 
271 std::ostream& operator<<(std::ostream& out, As3D<Vec4F> const& v);
272 std::ostream& operator<<(std::ostream& out, As3D<Vec4D> const& v);
273 
274 std::ostream& operator<<(std::ostream& out, Rot3F const& v);
275 std::ostream& operator<<(std::ostream& out, Rot3D const& v);
276 std::ostream& operator<<(std::ostream& out, Rot2F const& v);
277 std::ostream& operator<<(std::ostream& out, Rot2D const& v);
278 
279 #ifdef USE_INLINE_IO
280 #include <ostream>
281 std::ostream& operator<<(std::ostream& out, ::Vec4F const& v) {
282  return out << '(' << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << ')';
283 }
284 std::ostream& operator<<(std::ostream& out, ::Vec4D const& v) {
285  return out << '(' << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << ')';
286 }
287 std::ostream& operator<<(std::ostream& out, ::Vec2F const& v) { return out << '(' << v[0] << ", " << v[1] << ')'; }
288 std::ostream& operator<<(std::ostream& out, ::Vec2D const& v) { return out << '(' << v[0] << ", " << v[1] << ')'; }
289 
290 std::ostream& operator<<(std::ostream& out, ::As3D<Vec4F> const& v) {
291  return out << '(' << v.v[0] << ", " << v.v[1] << ", " << v.v[2] << ')';
292 }
293 
294 std::ostream& operator<<(std::ostream& out, ::As3D<Vec4D> const& v) {
295  return out << '(' << v.v[0] << ", " << v.v[1] << ", " << v.v[2] << ')';
296 }
297 
298 std::ostream& operator<<(std::ostream& out, ::Rot3F const& r) {
299  return out << as3D(r.axis[0]) << '\n' << as3D(r.axis[1]) << '\n' << as3D(r.axis[2]);
300 }
301 
302 std::ostream& operator<<(std::ostream& out, ::Rot3D const& r) {
303  return out << as3D(r.axis[0]) << '\n' << as3D(r.axis[1]) << '\n' << as3D(r.axis[2]);
304 }
305 
306 std::ostream& operator<<(std::ostream& out, ::Rot2F const& r) { return out << r.axis[0] << '\n' << r.axis[1]; }
307 
308 std::ostream& operator<<(std::ostream& out, ::Rot2D const& r) { return out << r.axis[0] << '\n' << r.axis[1]; }
309 #endif
310 
311 #endif
type
Definition: HCALResponse.h:21
As3D(V const &iv)
Definition: ExtVec.h:171
Vec4< T > Vec
Definition: ExtVec.h:182
ExtVec< T, 2 > Vec2
Definition: ExtVec.h:66
Vec2< T > Vec
Definition: ExtVec.h:228
def transpose(a)
Definition: geometryDiff.py:39
constexpr Vec2< T > rotate(Vec2< T > v) const
Definition: ExtVec.h:243
auto cross2(V1 x, V2 y) -> typename std::remove_reference< decltype(x[0])>::type
Definition: ExtVec.h:102
constexpr Vec4< T > y() const
Definition: ExtVec.h:198
constexpr Rot2(T xx, T xy, T yx, T yy)
Definition: ExtVec.h:235
Rot2< double > Rot2D
Definition: ExtVec.h:258
ret
prodAgent to be discontinued
constexpr Rot2 rotateBack(Rot2 const &r) const
Definition: ExtVec.h:253
auto xy(V v) -> Vec2< typename std::remove_reference< decltype(v[0])>::type >
Definition: ExtVec.h:69
Rot2< float > Rot2F
Definition: ExtVec.h:256
Rot3< float > Rot3F
Definition: ExtVec.h:217
constexpr Rot2()
Definition: ExtVec.h:231
Vec4< float > Vec3F
Definition: ExtVec.h:153
constexpr Rot3 rotateBack(Rot3 const &r) const
Definition: ExtVec.h:212
Vec cross3(Vec x, Vec y)
Definition: ExtVec.h:91
bool int lh
Definition: SIMDVec.h:20
std::ostream & operator<<(std::ostream &out, Vec2D const &v)
Definition: SSEVec.cc:15
Vec4< float > Vec4F
Definition: ExtVec.h:152
constexpr Rot3 transpose() const
Definition: ExtVec.h:192
Vec4< double > Vec4D
Definition: ExtVec.h:156
Rot2 rotate(Rot2 const &r) const
Definition: ExtVec.h:248
constexpr Vec4< T > x() const
Definition: ExtVec.h:197
typename ExtVecTraits< T, N >::type ExtVec
Definition: ExtVec.h:61
Vec2< T > axis[2]
Definition: ExtVec.h:229
Vec2< float > Vec2F
Definition: ExtVec.h:151
V const & v
Definition: ExtVec.h:170
Definition: ExtVec.h:169
double f[11][100]
def template(fileName, svg, replaceme="REPLACEME")
Definition: svgfig.py:521
constexpr Vec4< T > z() const
Definition: ExtVec.h:199
ExtVec< T, 4 > Vec4
Definition: ExtVec.h:64
Rot3 rotate(Rot3 const &r) const
Definition: ExtVec.h:207
constexpr Vec2< T > y() const
Definition: ExtVec.h:240
constexpr Vec2< T > x() const
Definition: ExtVec.h:239
constexpr Rot2 transpose() const
Definition: ExtVec.h:237
constexpr Rot3< T > operator*(Rot3< T > const &rh, Rot3< T > const &lh)
Definition: ExtVec.h:222
#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:189
constexpr Vec4< T > rotateBack(Vec4< T > v) const
Definition: ExtVec.h:205
Definition: ExtVec.h:181
constexpr Rot3(Vec4< T > ix, Vec4< T > iy, Vec4< T > iz)
Definition: ExtVec.h:187
constexpr Vec2< T > rotateBack(Vec2< T > v) const
Definition: ExtVec.h:246
Definition: ExtVec.h:227
Vec2< double > Vec2D
Definition: ExtVec.h:154
auto dot(V x, V y) -> typename std::remove_reference< decltype(x[0])>::type
Definition: ExtVec.h:133
#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:155
As3D< V > as3D(V const &v)
Definition: ExtVec.h:174
constexpr Vec4< T > rotate(Vec4< T > v) const
Definition: ExtVec.h:202
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:163
Rot3< double > Rot3D
Definition: ExtVec.h:219
long double T
Vec axis[3]
Definition: ExtVec.h:183
constexpr Rot3()
Definition: ExtVec.h:185
auto dot2(V1 x, V2 y) -> typename std::remove_reference< decltype(x[0])>::type
Definition: ExtVec.h:143
#define constexpr
Vec apply(Vec v, F f)
Definition: ExtVec.h:81
constexpr Rot2(Vec2< T > ix, Vec2< T > iy)
Definition: ExtVec.h:233