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