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 #define VECTOR_EXT(N) __attribute__((vector_size(N)))
7 
8 typedef float VECTOR_EXT(8) cms_float32x2_t;
9 typedef float VECTOR_EXT(16) cms_float32x4_t;
10 typedef float VECTOR_EXT(32) cms_float32x8_t;
11 typedef double VECTOR_EXT(16) cms_float64x2_t;
12 typedef double VECTOR_EXT(32) cms_float64x4_t;
13 typedef double VECTOR_EXT(64) cms_float64x8_t;
14 
15 typedef long double VECTOR_EXT(32) cms_float128x2_t;
16 typedef long double VECTOR_EXT(64) cms_float128x4_t;
17 typedef long double VECTOR_EXT(128) cms_float128x8_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 template <>
47 struct ExtVecTraits<long double, 2> {
48  typedef long double VECTOR_EXT(2 * sizeof(long double)) type;
49 };
50 
51 template <>
52 struct ExtVecTraits<long double, 4> {
53  typedef long double VECTOR_EXT(4 * sizeof(long double)) type;
54 };
55 
56 template <typename T, int N>
58 
59 template <typename T>
61 template <typename T>
63 
64 // convert V in W
65 template <typename W, typename V>
66 inline W convert(V v) {
67  // see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114943
68  // return __builtin_convertvector(v,W); // nope inefficient in gcc
70  constexpr int N = sizeof(V) / sizeof(T);
71  W w;
72  for (int i = 0; i != N; ++i)
73  w[i] = v[i];
74  return w;
75 }
76 
77 template <typename V>
80  return Vec2<T>{v[0], v[1]};
81 }
82 
83 template <typename V>
86  return Vec2<T>{v[2], v[3]};
87 }
88 
89 template <typename Vec, typename F>
90 inline Vec apply(Vec v, F f) {
92  constexpr int N = sizeof(Vec) / sizeof(T);
93  Vec ret;
94  for (int i = 0; i != N; ++i)
95  ret[i] = f(v[i]);
96  return ret;
97 }
98 
99 template <typename Vec>
100 inline Vec cross3(Vec x, Vec y) {
101  // typedef Vec4<T> Vec;
102  // yz - zy, zx - xz, xy - yx, 0
103  Vec x1200{x[1], x[2], x[0], x[0]};
104  Vec y2010{y[2], y[0], y[1], y[0]};
105  Vec x2010{x[2], x[0], x[1], x[0]};
106  Vec y1200{y[1], y[2], y[0], y[0]};
107  return x1200 * y2010 - x2010 * y1200;
108 }
109 
110 template <typename V1, typename V2>
111 inline auto cross2(V1 x, V2 y) -> typename std::remove_reference<decltype(x[0])>::type {
112  return x[0] * y[1] - x[1] * y[0];
113 }
114 
115 /*
116 template<typename T>
117 T dot_product(Vec4<T> x, Vec4<T> y) {
118  auto res = x*y;
119  T ret=0;
120  for (int i=0;i!=4;++i) ret+=res[i];
121  return ret;
122 }
123 */
124 
125 /*
126 template<typename V, int K>
127 inline
128 V get1(V v) { return (V){v[K]}; }
129 */
130 
131 /*
132 template<typename T, int N>
133 inline
134 T dot(ExtVec<T,N> x, ExtVec<T,N> y) {
135  T ret=0;
136  for (int i=0;i!=N;++i) ret+=x[i]*y[i];
137  return ret;
138 }
139 */
140 
141 template <typename V>
144  constexpr int N = sizeof(V) / sizeof(T);
145  T ret = 0;
146  for (int i = 0; i != N; ++i)
147  ret += x[i] * y[i];
148  return ret;
149 }
150 
151 template <typename V1, typename V2>
152 inline auto dot2(V1 x, V2 y) -> typename std::remove_reference<decltype(x[0])>::type {
153  return x[0] * y[0] + x[1] * y[1];
154 }
155 
156 template <typename V1, typename V2>
157 inline auto dot3(V1 x, V2 y) -> typename std::remove_reference<decltype(x[0])>::type {
158  auto z = x * y;
159  return z[0] + z[1] + z[2];
160 }
161 
168 
169 /*
170 template<typename T>
171 struct As3D {
172  Vec4<T> const & v;
173  As3D(Vec4<T> const &iv ) : v(iv){}
174 };
175 template<typename T>
176 inline As3D<T> as3D(Vec4<T> const &v ) { return v;}
177 */
178 
179 template <typename V>
180 struct As3D {
181  V const& v;
182  As3D(V const& iv) : v(iv) {}
183 };
184 template <typename V>
185 inline As3D<V> as3D(V const& v) {
186  return v;
187 }
188 
189 // rotations
190 
191 template <typename T>
192 struct Rot3 {
193  typedef Vec4<T> Vec;
194  Vec axis[3];
195 
196  constexpr Rot3() : axis{{(Vec){T(1), 0, 0, 0}}, {(Vec){0, T(1), 0, 0}}, {(Vec){0, 0, T(1), 0}}} {}
197 
199 
200  constexpr Rot3(T xx, T xy, T xz, T yx, T yy, T yz, T zx, T zy, T zz)
201  : axis{{(Vec){xx, xy, xz, 0}}, {(Vec){yx, yy, yz, 0}}, {(Vec){zx, zy, zz, 0}}} {}
202 
204  return Rot3(
205  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]);
206  }
207 
208  constexpr Vec4<T> x() const { return axis[0]; }
209  constexpr Vec4<T> y() const { return axis[1]; }
210  constexpr Vec4<T> z() const { return axis[2]; }
211 
212  // toLocal...
214 
215  // toGlobal...
216  constexpr Vec4<T> rotateBack(Vec4<T> v) const { return v[0] * axis[0] + v[1] * axis[1] + v[2] * axis[2]; }
217 
218  Rot3 rotate(Rot3 const& r) const {
219  Rot3 tr = transpose();
220  return Rot3(tr.rotateBack(r.axis[0]), tr.rotateBack(r.axis[1]), tr.rotateBack(r.axis[2]));
221  }
222 
223  constexpr Rot3 rotateBack(Rot3 const& r) const {
224  return Rot3(rotateBack(r.axis[0]), rotateBack(r.axis[1]), rotateBack(r.axis[2]));
225  }
226 };
227 
229 
231 
232 template <typename T>
233 inline constexpr Rot3<T> operator*(Rot3<T> const& rh, Rot3<T> const& lh) {
234  return lh.rotateBack(rh);
235 }
236 
237 template <typename T>
238 struct Rot2 {
239  typedef Vec2<T> Vec;
241 
242  constexpr Rot2() : axis{{(Vec){T(1), 0}}, {(Vec){0, T(1)}}} {}
243 
245 
246  constexpr Rot2(T xx, T xy, T yx, T yy) : Rot2((Vec){xx, xy}, (Vec){yx, yy}) {}
247 
248  constexpr Rot2 transpose() const { return Rot2(axis[0][0], axis[1][0], axis[0][1], axis[1][1]); }
249 
250  constexpr Vec2<T> x() const { return axis[0]; }
251  constexpr Vec2<T> y() const { return axis[1]; }
252 
253  // toLocal...
255 
256  // toGlobal...
257  constexpr Vec2<T> rotateBack(Vec2<T> v) const { return v[0] * axis[0] + v[1] * axis[1]; }
258 
259  Rot2 rotate(Rot2 const& r) const {
260  Rot2 tr = transpose();
261  return Rot2(tr.rotateBack(r.axis[0]), tr.rotateBack(r.axis[1]));
262  }
263 
264  constexpr Rot2 rotateBack(Rot2 const& r) const { return Rot2(rotateBack(r.axis[0]), rotateBack(r.axis[1])); }
265 };
266 
268 
270 
271 template <typename T>
272 inline constexpr Rot2<T> operator*(Rot2<T> const& rh, Rot2<T> const& lh) {
273  return lh.rotateBack(rh);
274 }
275 
276 #include <iosfwd>
277 std::ostream& operator<<(std::ostream& out, Vec2D const& v);
278 std::ostream& operator<<(std::ostream& out, Vec2F const& v);
279 std::ostream& operator<<(std::ostream& out, Vec4F const& v);
280 std::ostream& operator<<(std::ostream& out, Vec4D const& v);
281 
282 std::ostream& operator<<(std::ostream& out, As3D<Vec4F> const& v);
283 std::ostream& operator<<(std::ostream& out, As3D<Vec4D> const& v);
284 
285 std::ostream& operator<<(std::ostream& out, Rot3F const& v);
286 std::ostream& operator<<(std::ostream& out, Rot3D const& v);
287 std::ostream& operator<<(std::ostream& out, Rot2F const& v);
288 std::ostream& operator<<(std::ostream& out, Rot2D const& v);
289 
290 #ifdef USE_INLINE_IO
291 #include <ostream>
292 std::ostream& operator<<(std::ostream& out, ::Vec4F const& v) {
293  return out << '(' << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << ')';
294 }
295 std::ostream& operator<<(std::ostream& out, ::Vec4D const& v) {
296  return out << '(' << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << ')';
297 }
298 std::ostream& operator<<(std::ostream& out, ::Vec2F const& v) { return out << '(' << v[0] << ", " << v[1] << ')'; }
299 std::ostream& operator<<(std::ostream& out, ::Vec2D const& v) { return out << '(' << v[0] << ", " << v[1] << ')'; }
300 
301 std::ostream& operator<<(std::ostream& out, ::As3D<Vec4F> const& v) {
302  return out << '(' << v.v[0] << ", " << v.v[1] << ", " << v.v[2] << ')';
303 }
304 
305 std::ostream& operator<<(std::ostream& out, ::As3D<Vec4D> const& v) {
306  return out << '(' << v.v[0] << ", " << v.v[1] << ", " << v.v[2] << ')';
307 }
308 
309 std::ostream& operator<<(std::ostream& out, ::Rot3F const& r) {
310  return out << as3D(r.axis[0]) << '\n' << as3D(r.axis[1]) << '\n' << as3D(r.axis[2]);
311 }
312 
313 std::ostream& operator<<(std::ostream& out, ::Rot3D const& r) {
314  return out << as3D(r.axis[0]) << '\n' << as3D(r.axis[1]) << '\n' << as3D(r.axis[2]);
315 }
316 
317 std::ostream& operator<<(std::ostream& out, ::Rot2F const& r) { return out << r.axis[0] << '\n' << r.axis[1]; }
318 
319 std::ostream& operator<<(std::ostream& out, ::Rot2D const& r) { return out << r.axis[0] << '\n' << r.axis[1]; }
320 #endif
321 
322 #endif
As3D(V const &iv)
Definition: ExtVec.h:182
Vec4< T > Vec
Definition: ExtVec.h:193
ExtVec< T, 2 > Vec2
Definition: ExtVec.h:62
Vec2< T > Vec
Definition: ExtVec.h:239
constexpr Vec2< T > x() const
Definition: ExtVec.h:250
auto cross2(V1 x, V2 y) -> typename std::remove_reference< decltype(x[0])>::type
Definition: ExtVec.h:111
constexpr Rot2(T xx, T xy, T yx, T yy)
Definition: ExtVec.h:246
T w() const
Rot2< double > Rot2D
Definition: ExtVec.h:269
ret
prodAgent to be discontinued
constexpr Rot2 rotateBack(Rot2 const &r) const
Definition: ExtVec.h:264
constexpr Rot3 rotateBack(Rot3 const &r) const
Definition: ExtVec.h:223
auto xy(V v) -> Vec2< typename std::remove_reference< decltype(v[0])>::type >
Definition: ExtVec.h:78
auto dot3(V1 x, V2 y) -> typename std::remove_reference< decltype(x[0])>::type
Definition: ExtVec.h:157
Rot2< float > Rot2F
Definition: ExtVec.h:267
Rot3< float > Rot3F
Definition: ExtVec.h:228
constexpr Rot2()
Definition: ExtVec.h:242
Vec4< float > Vec3F
Definition: ExtVec.h:164
Vec cross3(Vec x, Vec y)
Definition: ExtVec.h:100
bool int lh
Definition: SIMDVec.h:27
std::ostream & operator<<(std::ostream &out, Vec2D const &v)
Definition: SSEVec.cc:15
Vec4< float > Vec4F
Definition: ExtVec.h:163
constexpr Vec2< T > rotate(Vec2< T > v) const
Definition: ExtVec.h:254
Vec4< double > Vec4D
Definition: ExtVec.h:167
typename ExtVecTraits< T, N >::type ExtVec
Definition: ExtVec.h:57
Vec2< T > axis[2]
Definition: ExtVec.h:240
Vec2< float > Vec2F
Definition: ExtVec.h:162
uint32_t T const *__restrict__ uint32_t const *__restrict__ int32_t int Histo::index_type cudaStream_t V
V const & v
Definition: ExtVec.h:181
Rot3 rotate(Rot3 const &r) const
Definition: ExtVec.h:218
constexpr Vec4< T > rotate(Vec4< T > v) const
Definition: ExtVec.h:213
Definition: ExtVec.h:180
double f[11][100]
def template(fileName, svg, replaceme="REPLACEME")
Definition: svgfig.py:521
ExtVec< T, 4 > Vec4
Definition: ExtVec.h:60
constexpr Vec4< T > z() const
Definition: ExtVec.h:210
constexpr Rot2 transpose() const
Definition: ExtVec.h:248
constexpr Vec4< T > y() const
Definition: ExtVec.h:209
W convert(V v)
Definition: ExtVec.h:66
ALPAKA_FN_ACC ALPAKA_FN_INLINE uint32_t ix(uint32_t id)
constexpr Rot3< T > operator*(Rot3< T > const &rh, Rot3< T > const &lh)
Definition: ExtVec.h:233
#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:200
Rot2 rotate(Rot2 const &r) const
Definition: ExtVec.h:259
Definition: ExtVec.h:192
alpaka::Vec< TDim, Idx > Vec
Definition: config.h:24
constexpr Rot3(Vec4< T > ix, Vec4< T > iy, Vec4< T > iz)
Definition: ExtVec.h:198
Definition: ExtVec.h:238
Vec2< double > Vec2D
Definition: ExtVec.h:165
auto dot(V x, V y) -> typename std::remove_reference< decltype(x[0])>::type
Definition: ExtVec.h:142
float x
#define VECTOR_EXT(N)
Definition: ExtVec.h:6
ALPAKA_FN_ACC ALPAKA_FN_INLINE uint32_t iy(uint32_t id)
auto zw(V v) -> Vec2< typename std::remove_reference< decltype(v[0])>::type >
Definition: ExtVec.h:84
Vec4< double > Vec3D
Definition: ExtVec.h:166
As3D< V > as3D(V const &v)
Definition: ExtVec.h:185
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:163
Rot3< double > Rot3D
Definition: ExtVec.h:230
long double T
Vec axis[3]
Definition: ExtVec.h:194
constexpr Rot3()
Definition: ExtVec.h:196
constexpr Vec4< T > rotateBack(Vec4< T > v) const
Definition: ExtVec.h:216
constexpr Vec2< T > y() const
Definition: ExtVec.h:251
auto dot2(V1 x, V2 y) -> typename std::remove_reference< decltype(x[0])>::type
Definition: ExtVec.h:152
Vec apply(Vec v, F f)
Definition: ExtVec.h:90
constexpr Rot3 transpose() const
Definition: ExtVec.h:203
constexpr Rot2(Vec2< T > ix, Vec2< T > iy)
Definition: ExtVec.h:244
constexpr Vec4< T > x() const
Definition: ExtVec.h:208
constexpr Vec2< T > rotateBack(Vec2< T > v) const
Definition: ExtVec.h:257