Eigen  3.3.0
ZVector/PacketMath.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2016 Konstantinos Margaritis <markos@freevec.org>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_PACKET_MATH_ZVECTOR_H
11 #define EIGEN_PACKET_MATH_ZVECTOR_H
12 
13 #include <stdint.h>
14 
15 namespace Eigen {
16 
17 namespace internal {
18 
19 #ifndef EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD
20 #define EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD 4
21 #endif
22 
23 #ifndef EIGEN_HAS_SINGLE_INSTRUCTION_MADD
24 #define EIGEN_HAS_SINGLE_INSTRUCTION_MADD
25 #endif
26 
27 #ifndef EIGEN_HAS_SINGLE_INSTRUCTION_CJMADD
28 #define EIGEN_HAS_SINGLE_INSTRUCTION_CJMADD
29 #endif
30 
31 // NOTE Altivec has 32 registers, but Eigen only accepts a value of 8 or 16
32 #ifndef EIGEN_ARCH_DEFAULT_NUMBER_OF_REGISTERS
33 #define EIGEN_ARCH_DEFAULT_NUMBER_OF_REGISTERS 32
34 #endif
35 
36 typedef __vector int Packet4i;
37 typedef __vector unsigned int Packet4ui;
38 typedef __vector __bool int Packet4bi;
39 typedef __vector short int Packet8i;
40 typedef __vector unsigned char Packet16uc;
41 typedef __vector double Packet2d;
42 typedef __vector unsigned long long Packet2ul;
43 typedef __vector long long Packet2l;
44 
45 typedef union {
46  int32_t i[4];
47  uint32_t ui[4];
48  int64_t l[2];
49  uint64_t ul[2];
50  double d[2];
51  Packet4i v4i;
52  Packet4ui v4ui;
53  Packet2l v2l;
54  Packet2ul v2ul;
55  Packet2d v2d;
56 } Packet;
57 
58 // We don't want to write the same code all the time, but we need to reuse the constants
59 // and it doesn't really work to declare them global, so we define macros instead
60 
61 #define _EIGEN_DECLARE_CONST_FAST_Packet4i(NAME,X) \
62  Packet4i p4i_##NAME = reinterpret_cast<Packet4i>(vec_splat_s32(X))
63 
64 #define _EIGEN_DECLARE_CONST_FAST_Packet2d(NAME,X) \
65  Packet2d p2d_##NAME = reinterpret_cast<Packet2d>(vec_splat_s64(X))
66 
67 #define _EIGEN_DECLARE_CONST_FAST_Packet2l(NAME,X) \
68  Packet2l p2l_##NAME = reinterpret_cast<Packet2l>(vec_splat_s64(X))
69 
70 #define _EIGEN_DECLARE_CONST_Packet4i(NAME,X) \
71  Packet4i p4i_##NAME = pset1<Packet4i>(X)
72 
73 #define _EIGEN_DECLARE_CONST_Packet2d(NAME,X) \
74  Packet2d p2d_##NAME = pset1<Packet2d>(X)
75 
76 #define _EIGEN_DECLARE_CONST_Packet2l(NAME,X) \
77  Packet2l p2l_##NAME = pset1<Packet2l>(X)
78 
79 // These constants are endian-agnostic
80 //static _EIGEN_DECLARE_CONST_FAST_Packet4i(ZERO, 0); //{ 0, 0, 0, 0,}
81 static _EIGEN_DECLARE_CONST_FAST_Packet4i(ONE, 1); //{ 1, 1, 1, 1}
82 
83 static _EIGEN_DECLARE_CONST_FAST_Packet2d(ZERO, 0);
84 static _EIGEN_DECLARE_CONST_FAST_Packet2l(ZERO, 0);
85 static _EIGEN_DECLARE_CONST_FAST_Packet2l(ONE, 1);
86 
87 static Packet2d p2d_ONE = { 1.0, 1.0 };
88 static Packet2d p2d_ZERO_ = { -0.0, -0.0 };
89 
90 static Packet4i p4i_COUNTDOWN = { 0, 1, 2, 3 };
91 static Packet2d p2d_COUNTDOWN = reinterpret_cast<Packet2d>(vec_sld(reinterpret_cast<Packet16uc>(p2d_ZERO), reinterpret_cast<Packet16uc>(p2d_ONE), 8));
92 
93 static Packet16uc p16uc_PSET64_HI = { 0,1,2,3, 4,5,6,7, 0,1,2,3, 4,5,6,7 };
94 static Packet16uc p16uc_DUPLICATE32_HI = { 0,1,2,3, 0,1,2,3, 4,5,6,7, 4,5,6,7 };
95 
96 // Mask alignment
97 #define _EIGEN_MASK_ALIGNMENT 0xfffffffffffffff0
98 
99 #define _EIGEN_ALIGNED_PTR(x) ((ptrdiff_t)(x) & _EIGEN_MASK_ALIGNMENT)
100 
101 // Handle endianness properly while loading constants
102 // Define global static constants:
103 
104 static Packet16uc p16uc_FORWARD = { 0,1,2,3, 4,5,6,7, 8,9,10,11, 12,13,14,15 };
105 static Packet16uc p16uc_REVERSE32 = { 12,13,14,15, 8,9,10,11, 4,5,6,7, 0,1,2,3 };
106 static Packet16uc p16uc_REVERSE64 = { 8,9,10,11, 12,13,14,15, 0,1,2,3, 4,5,6,7 };
107 
108 static Packet16uc p16uc_PSET32_WODD = vec_sld((Packet16uc) vec_splat((Packet4ui)p16uc_FORWARD, 0), (Packet16uc) vec_splat((Packet4ui)p16uc_FORWARD, 2), 8);//{ 0,1,2,3, 0,1,2,3, 8,9,10,11, 8,9,10,11 };
109 static Packet16uc p16uc_PSET32_WEVEN = vec_sld(p16uc_DUPLICATE32_HI, (Packet16uc) vec_splat((Packet4ui)p16uc_FORWARD, 3), 8);//{ 4,5,6,7, 4,5,6,7, 12,13,14,15, 12,13,14,15 };
110 /*static Packet16uc p16uc_HALF64_0_16 = vec_sld((Packet16uc)p4i_ZERO, vec_splat((Packet16uc) vec_abs(p4i_MINUS16), 3), 8); //{ 0,0,0,0, 0,0,0,0, 16,16,16,16, 16,16,16,16};
111 
112 static Packet16uc p16uc_PSET64_HI = (Packet16uc) vec_mergeh((Packet4ui)p16uc_PSET32_WODD, (Packet4ui)p16uc_PSET32_WEVEN); //{ 0,1,2,3, 4,5,6,7, 0,1,2,3, 4,5,6,7 };*/
113 static Packet16uc p16uc_PSET64_LO = (Packet16uc) vec_mergel((Packet4ui)p16uc_PSET32_WODD, (Packet4ui)p16uc_PSET32_WEVEN); //{ 8,9,10,11, 12,13,14,15, 8,9,10,11, 12,13,14,15 };
114 /*static Packet16uc p16uc_TRANSPOSE64_HI = vec_add(p16uc_PSET64_HI, p16uc_HALF64_0_16); //{ 0,1,2,3, 4,5,6,7, 16,17,18,19, 20,21,22,23};
115 static Packet16uc p16uc_TRANSPOSE64_LO = vec_add(p16uc_PSET64_LO, p16uc_HALF64_0_16); //{ 8,9,10,11, 12,13,14,15, 24,25,26,27, 28,29,30,31};*/
116 static Packet16uc p16uc_TRANSPOSE64_HI = { 0,1,2,3, 4,5,6,7, 16,17,18,19, 20,21,22,23};
117 static Packet16uc p16uc_TRANSPOSE64_LO = { 8,9,10,11, 12,13,14,15, 24,25,26,27, 28,29,30,31};
118 
119 //static Packet16uc p16uc_COMPLEX32_REV = vec_sld(p16uc_REVERSE32, p16uc_REVERSE32, 8); //{ 4,5,6,7, 0,1,2,3, 12,13,14,15, 8,9,10,11 };
120 
121 //static Packet16uc p16uc_COMPLEX32_REV2 = vec_sld(p16uc_FORWARD, p16uc_FORWARD, 8); //{ 8,9,10,11, 12,13,14,15, 0,1,2,3, 4,5,6,7 };
122 
123 
124 #if EIGEN_HAS_BUILTIN(__builtin_prefetch) || EIGEN_COMP_GNUC
125  #define EIGEN_ZVECTOR_PREFETCH(ADDR) __builtin_prefetch(ADDR);
126 #else
127  #define EIGEN_ZVECTOR_PREFETCH(ADDR) asm( " pfd [%[addr]]\n" :: [addr] "r" (ADDR) : "cc" );
128 #endif
129 
130 template<> struct packet_traits<int> : default_packet_traits
131 {
132  typedef Packet4i type;
133  typedef Packet4i half;
134  enum {
135  // FIXME check the Has*
136  Vectorizable = 1,
137  AlignedOnScalar = 1,
138  size = 4,
139  HasHalfPacket = 0,
140 
141  // FIXME check the Has*
142  HasAdd = 1,
143  HasSub = 1,
144  HasMul = 1,
145  HasDiv = 1,
146  HasBlend = 1
147  };
148 };
149 
150 template<> struct packet_traits<double> : default_packet_traits
151 {
152  typedef Packet2d type;
153  typedef Packet2d half;
154  enum {
155  Vectorizable = 1,
156  AlignedOnScalar = 1,
157  size=2,
158  HasHalfPacket = 1,
159 
160  // FIXME check the Has*
161  HasAdd = 1,
162  HasSub = 1,
163  HasMul = 1,
164  HasDiv = 1,
165  HasMin = 1,
166  HasMax = 1,
167  HasAbs = 1,
168  HasSin = 0,
169  HasCos = 0,
170  HasLog = 0,
171  HasExp = 1,
172  HasSqrt = 1,
173  HasRsqrt = 1,
174  HasRound = 1,
175  HasFloor = 1,
176  HasCeil = 1,
177  HasNegate = 1,
178  HasBlend = 1
179  };
180 };
181 
182 template<> struct unpacket_traits<Packet4i> { typedef int type; enum {size=4, alignment=Aligned16}; typedef Packet4i half; };
183 template<> struct unpacket_traits<Packet2d> { typedef double type; enum {size=2, alignment=Aligned16}; typedef Packet2d half; };
184 
185 inline std::ostream & operator <<(std::ostream & s, const Packet4i & v)
186 {
187  Packet vt;
188  vt.v4i = v;
189  s << vt.i[0] << ", " << vt.i[1] << ", " << vt.i[2] << ", " << vt.i[3];
190  return s;
191 }
192 
193 inline std::ostream & operator <<(std::ostream & s, const Packet4ui & v)
194 {
195  Packet vt;
196  vt.v4ui = v;
197  s << vt.ui[0] << ", " << vt.ui[1] << ", " << vt.ui[2] << ", " << vt.ui[3];
198  return s;
199 }
200 
201 inline std::ostream & operator <<(std::ostream & s, const Packet2l & v)
202 {
203  Packet vt;
204  vt.v2l = v;
205  s << vt.l[0] << ", " << vt.l[1];
206  return s;
207 }
208 
209 inline std::ostream & operator <<(std::ostream & s, const Packet2ul & v)
210 {
211  Packet vt;
212  vt.v2ul = v;
213  s << vt.ul[0] << ", " << vt.ul[1] ;
214  return s;
215 }
216 
217 inline std::ostream & operator <<(std::ostream & s, const Packet2d & v)
218 {
219  Packet vt;
220  vt.v2d = v;
221  s << vt.d[0] << ", " << vt.d[1];
222  return s;
223 }
224 
225 template<int Offset>
226 struct palign_impl<Offset,Packet4i>
227 {
228  static EIGEN_STRONG_INLINE void run(Packet4i& first, const Packet4i& second)
229  {
230  switch (Offset % 4) {
231  case 1:
232  first = vec_sld(first, second, 4); break;
233  case 2:
234  first = vec_sld(first, second, 8); break;
235  case 3:
236  first = vec_sld(first, second, 12); break;
237  }
238  }
239 };
240 
241 template<int Offset>
242 struct palign_impl<Offset,Packet2d>
243 {
244  static EIGEN_STRONG_INLINE void run(Packet2d& first, const Packet2d& second)
245  {
246  if (Offset == 1)
247  first = reinterpret_cast<Packet2d>(vec_sld(reinterpret_cast<Packet4i>(first), reinterpret_cast<Packet4i>(second), 8));
248  }
249 };
250 
251 template<> EIGEN_STRONG_INLINE Packet4i pload<Packet4i>(const int* from)
252 {
253  // FIXME: No intrinsic yet
254  EIGEN_DEBUG_ALIGNED_LOAD
255  Packet *vfrom;
256  vfrom = (Packet *) from;
257  return vfrom->v4i;
258 }
259 
260 template<> EIGEN_STRONG_INLINE Packet2d pload<Packet2d>(const double* from)
261 {
262  // FIXME: No intrinsic yet
263  EIGEN_DEBUG_ALIGNED_LOAD
264  Packet *vfrom;
265  vfrom = (Packet *) from;
266  return vfrom->v2d;
267 }
268 
269 template<> EIGEN_STRONG_INLINE void pstore<int>(int* to, const Packet4i& from)
270 {
271  // FIXME: No intrinsic yet
272  EIGEN_DEBUG_ALIGNED_STORE
273  Packet *vto;
274  vto = (Packet *) to;
275  vto->v4i = from;
276 }
277 
278 template<> EIGEN_STRONG_INLINE void pstore<double>(double* to, const Packet2d& from)
279 {
280  // FIXME: No intrinsic yet
281  EIGEN_DEBUG_ALIGNED_STORE
282  Packet *vto;
283  vto = (Packet *) to;
284  vto->v2d = from;
285 }
286 
287 template<> EIGEN_STRONG_INLINE Packet4i pset1<Packet4i>(const int& from)
288 {
289  return vec_splats(from);
290 }
291 
292 template<> EIGEN_STRONG_INLINE Packet2d pset1<Packet2d>(const double& from) {
293  return vec_splats(from);
294 }
295 
296 template<> EIGEN_STRONG_INLINE void
297 pbroadcast4<Packet4i>(const int *a,
298  Packet4i& a0, Packet4i& a1, Packet4i& a2, Packet4i& a3)
299 {
300  a3 = pload<Packet4i>(a);
301  a0 = vec_splat(a3, 0);
302  a1 = vec_splat(a3, 1);
303  a2 = vec_splat(a3, 2);
304  a3 = vec_splat(a3, 3);
305 }
306 
307 template<> EIGEN_STRONG_INLINE void
308 pbroadcast4<Packet2d>(const double *a,
309  Packet2d& a0, Packet2d& a1, Packet2d& a2, Packet2d& a3)
310 {
311  a1 = pload<Packet2d>(a);
312  a0 = vec_splat(a1, 0);
313  a1 = vec_splat(a1, 1);
314  a3 = pload<Packet2d>(a+2);
315  a2 = vec_splat(a3, 0);
316  a3 = vec_splat(a3, 1);
317 }
318 
319 template<> EIGEN_DEVICE_FUNC inline Packet4i pgather<int, Packet4i>(const int* from, Index stride)
320 {
321  int EIGEN_ALIGN16 ai[4];
322  ai[0] = from[0*stride];
323  ai[1] = from[1*stride];
324  ai[2] = from[2*stride];
325  ai[3] = from[3*stride];
326  return pload<Packet4i>(ai);
327 }
328 
329 template<> EIGEN_DEVICE_FUNC inline Packet2d pgather<double, Packet2d>(const double* from, Index stride)
330 {
331  double EIGEN_ALIGN16 af[2];
332  af[0] = from[0*stride];
333  af[1] = from[1*stride];
334  return pload<Packet2d>(af);
335 }
336 
337 template<> EIGEN_DEVICE_FUNC inline void pscatter<int, Packet4i>(int* to, const Packet4i& from, Index stride)
338 {
339  int EIGEN_ALIGN16 ai[4];
340  pstore<int>((int *)ai, from);
341  to[0*stride] = ai[0];
342  to[1*stride] = ai[1];
343  to[2*stride] = ai[2];
344  to[3*stride] = ai[3];
345 }
346 
347 template<> EIGEN_DEVICE_FUNC inline void pscatter<double, Packet2d>(double* to, const Packet2d& from, Index stride)
348 {
349  double EIGEN_ALIGN16 af[2];
350  pstore<double>(af, from);
351  to[0*stride] = af[0];
352  to[1*stride] = af[1];
353 }
354 
355 template<> EIGEN_STRONG_INLINE Packet4i padd<Packet4i>(const Packet4i& a, const Packet4i& b) { return (a + b); }
356 template<> EIGEN_STRONG_INLINE Packet2d padd<Packet2d>(const Packet2d& a, const Packet2d& b) { return (a + b); }
357 
358 template<> EIGEN_STRONG_INLINE Packet4i psub<Packet4i>(const Packet4i& a, const Packet4i& b) { return (a - b); }
359 template<> EIGEN_STRONG_INLINE Packet2d psub<Packet2d>(const Packet2d& a, const Packet2d& b) { return (a - b); }
360 
361 template<> EIGEN_STRONG_INLINE Packet4i pmul<Packet4i>(const Packet4i& a, const Packet4i& b) { return (a * b); }
362 template<> EIGEN_STRONG_INLINE Packet2d pmul<Packet2d>(const Packet2d& a, const Packet2d& b) { return (a * b); }
363 
364 template<> EIGEN_STRONG_INLINE Packet4i pdiv<Packet4i>(const Packet4i& a, const Packet4i& b) { return (a / b); }
365 template<> EIGEN_STRONG_INLINE Packet2d pdiv<Packet2d>(const Packet2d& a, const Packet2d& b) { return (a / b); }
366 
367 template<> EIGEN_STRONG_INLINE Packet4i pnegate(const Packet4i& a) { return (-a); }
368 template<> EIGEN_STRONG_INLINE Packet2d pnegate(const Packet2d& a) { return (-a); }
369 
370 template<> EIGEN_STRONG_INLINE Packet4i pconj(const Packet4i& a) { return a; }
371 template<> EIGEN_STRONG_INLINE Packet2d pconj(const Packet2d& a) { return a; }
372 
373 template<> EIGEN_STRONG_INLINE Packet4i pmadd(const Packet4i& a, const Packet4i& b, const Packet4i& c) { return padd<Packet4i>(pmul<Packet4i>(a, b), c); }
374 template<> EIGEN_STRONG_INLINE Packet2d pmadd(const Packet2d& a, const Packet2d& b, const Packet2d& c) { return vec_madd(a, b, c); }
375 
376 template<> EIGEN_STRONG_INLINE Packet4i plset<Packet4i>(const int& a) { return padd<Packet4i>(pset1<Packet4i>(a), p4i_COUNTDOWN); }
377 template<> EIGEN_STRONG_INLINE Packet2d plset<Packet2d>(const double& a) { return padd<Packet2d>(pset1<Packet2d>(a), p2d_COUNTDOWN); }
378 
379 template<> EIGEN_STRONG_INLINE Packet4i pmin<Packet4i>(const Packet4i& a, const Packet4i& b) { return vec_min(a, b); }
380 template<> EIGEN_STRONG_INLINE Packet2d pmin<Packet2d>(const Packet2d& a, const Packet2d& b) { return vec_min(a, b); }
381 
382 template<> EIGEN_STRONG_INLINE Packet4i pmax<Packet4i>(const Packet4i& a, const Packet4i& b) { return vec_max(a, b); }
383 template<> EIGEN_STRONG_INLINE Packet2d pmax<Packet2d>(const Packet2d& a, const Packet2d& b) { return vec_max(a, b); }
384 
385 template<> EIGEN_STRONG_INLINE Packet4i pand<Packet4i>(const Packet4i& a, const Packet4i& b) { return vec_and(a, b); }
386 template<> EIGEN_STRONG_INLINE Packet2d pand<Packet2d>(const Packet2d& a, const Packet2d& b) { return vec_and(a, b); }
387 
388 template<> EIGEN_STRONG_INLINE Packet4i por<Packet4i>(const Packet4i& a, const Packet4i& b) { return vec_or(a, b); }
389 template<> EIGEN_STRONG_INLINE Packet2d por<Packet2d>(const Packet2d& a, const Packet2d& b) { return vec_or(a, b); }
390 
391 template<> EIGEN_STRONG_INLINE Packet4i pxor<Packet4i>(const Packet4i& a, const Packet4i& b) { return vec_xor(a, b); }
392 template<> EIGEN_STRONG_INLINE Packet2d pxor<Packet2d>(const Packet2d& a, const Packet2d& b) { return vec_xor(a, b); }
393 
394 template<> EIGEN_STRONG_INLINE Packet4i pandnot<Packet4i>(const Packet4i& a, const Packet4i& b) { return pand<Packet4i>(a, vec_nor(b, b)); }
395 template<> EIGEN_STRONG_INLINE Packet2d pandnot<Packet2d>(const Packet2d& a, const Packet2d& b) { return vec_and(a, vec_nor(b, b)); }
396 
397 template<> EIGEN_STRONG_INLINE Packet2d pround<Packet2d>(const Packet2d& a) { return vec_round(a); }
398 template<> EIGEN_STRONG_INLINE Packet2d pceil<Packet2d>(const Packet2d& a) { return vec_ceil(a); }
399 template<> EIGEN_STRONG_INLINE Packet2d pfloor<Packet2d>(const Packet2d& a) { return vec_floor(a); }
400 
401 template<> EIGEN_STRONG_INLINE Packet4i ploadu<Packet4i>(const int* from) { return pload<Packet4i>(from); }
402 template<> EIGEN_STRONG_INLINE Packet2d ploadu<Packet2d>(const double* from) { return pload<Packet2d>(from); }
403 
404 
405 template<> EIGEN_STRONG_INLINE Packet4i ploaddup<Packet4i>(const int* from)
406 {
407  Packet4i p = pload<Packet4i>(from);
408  return vec_perm(p, p, p16uc_DUPLICATE32_HI);
409 }
410 
411 template<> EIGEN_STRONG_INLINE Packet2d ploaddup<Packet2d>(const double* from)
412 {
413  Packet2d p = pload<Packet2d>(from);
414  return vec_perm(p, p, p16uc_PSET64_HI);
415 }
416 
417 template<> EIGEN_STRONG_INLINE void pstoreu<int>(int* to, const Packet4i& from) { pstore<int>(to, from); }
418 template<> EIGEN_STRONG_INLINE void pstoreu<double>(double* to, const Packet2d& from) { pstore<double>(to, from); }
419 
420 template<> EIGEN_STRONG_INLINE void prefetch<int>(const int* addr) { EIGEN_ZVECTOR_PREFETCH(addr); }
421 template<> EIGEN_STRONG_INLINE void prefetch<double>(const double* addr) { EIGEN_ZVECTOR_PREFETCH(addr); }
422 
423 template<> EIGEN_STRONG_INLINE int pfirst<Packet4i>(const Packet4i& a) { int EIGEN_ALIGN16 x[4]; pstore(x, a); return x[0]; }
424 template<> EIGEN_STRONG_INLINE double pfirst<Packet2d>(const Packet2d& a) { double EIGEN_ALIGN16 x[2]; pstore(x, a); return x[0]; }
425 
426 template<> EIGEN_STRONG_INLINE Packet4i preverse(const Packet4i& a)
427 {
428  return reinterpret_cast<Packet4i>(vec_perm(reinterpret_cast<Packet16uc>(a), reinterpret_cast<Packet16uc>(a), p16uc_REVERSE32));
429 }
430 
431 template<> EIGEN_STRONG_INLINE Packet2d preverse(const Packet2d& a)
432 {
433  return reinterpret_cast<Packet2d>(vec_perm(reinterpret_cast<Packet16uc>(a), reinterpret_cast<Packet16uc>(a), p16uc_REVERSE64));
434 }
435 
436 template<> EIGEN_STRONG_INLINE Packet4i pabs(const Packet4i& a) { return vec_abs(a); }
437 template<> EIGEN_STRONG_INLINE Packet2d pabs(const Packet2d& a) { return vec_abs(a); }
438 
439 template<> EIGEN_STRONG_INLINE int predux<Packet4i>(const Packet4i& a)
440 {
441  Packet4i b, sum;
442  b = vec_sld(a, a, 8);
443  sum = padd<Packet4i>(a, b);
444  b = vec_sld(sum, sum, 4);
445  sum = padd<Packet4i>(sum, b);
446  return pfirst(sum);
447 }
448 
449 template<> EIGEN_STRONG_INLINE double predux<Packet2d>(const Packet2d& a)
450 {
451  Packet2d b, sum;
452  b = reinterpret_cast<Packet2d>(vec_sld(reinterpret_cast<Packet4i>(a), reinterpret_cast<Packet4i>(a), 8));
453  sum = padd<Packet2d>(a, b);
454  return pfirst(sum);
455 }
456 
457 template<> EIGEN_STRONG_INLINE Packet4i preduxp<Packet4i>(const Packet4i* vecs)
458 {
459  Packet4i v[4], sum[4];
460 
461  // It's easier and faster to transpose then add as columns
462  // Check: http://www.freevec.org/function/matrix_4x4_transpose_floats for explanation
463  // Do the transpose, first set of moves
464  v[0] = vec_mergeh(vecs[0], vecs[2]);
465  v[1] = vec_mergel(vecs[0], vecs[2]);
466  v[2] = vec_mergeh(vecs[1], vecs[3]);
467  v[3] = vec_mergel(vecs[1], vecs[3]);
468  // Get the resulting vectors
469  sum[0] = vec_mergeh(v[0], v[2]);
470  sum[1] = vec_mergel(v[0], v[2]);
471  sum[2] = vec_mergeh(v[1], v[3]);
472  sum[3] = vec_mergel(v[1], v[3]);
473 
474  // Now do the summation:
475  // Lines 0+1
476  sum[0] = padd<Packet4i>(sum[0], sum[1]);
477  // Lines 2+3
478  sum[1] = padd<Packet4i>(sum[2], sum[3]);
479  // Add the results
480  sum[0] = padd<Packet4i>(sum[0], sum[1]);
481 
482  return sum[0];
483 }
484 
485 template<> EIGEN_STRONG_INLINE Packet2d preduxp<Packet2d>(const Packet2d* vecs)
486 {
487  Packet2d v[2], sum;
488  v[0] = padd<Packet2d>(vecs[0], reinterpret_cast<Packet2d>(vec_sld(reinterpret_cast<Packet4ui>(vecs[0]), reinterpret_cast<Packet4ui>(vecs[0]), 8)));
489  v[1] = padd<Packet2d>(vecs[1], reinterpret_cast<Packet2d>(vec_sld(reinterpret_cast<Packet4ui>(vecs[1]), reinterpret_cast<Packet4ui>(vecs[1]), 8)));
490 
491  sum = reinterpret_cast<Packet2d>(vec_sld(reinterpret_cast<Packet4ui>(v[0]), reinterpret_cast<Packet4ui>(v[1]), 8));
492 
493  return sum;
494 }
495 
496 // Other reduction functions:
497 // mul
498 template<> EIGEN_STRONG_INLINE int predux_mul<Packet4i>(const Packet4i& a)
499 {
500  EIGEN_ALIGN16 int aux[4];
501  pstore(aux, a);
502  return aux[0] * aux[1] * aux[2] * aux[3];
503 }
504 
505 template<> EIGEN_STRONG_INLINE double predux_mul<Packet2d>(const Packet2d& a)
506 {
507  return pfirst(pmul(a, reinterpret_cast<Packet2d>(vec_sld(reinterpret_cast<Packet4i>(a), reinterpret_cast<Packet4i>(a), 8))));
508 }
509 
510 // min
511 template<> EIGEN_STRONG_INLINE int predux_min<Packet4i>(const Packet4i& a)
512 {
513  Packet4i b, res;
514  b = pmin<Packet4i>(a, vec_sld(a, a, 8));
515  res = pmin<Packet4i>(b, vec_sld(b, b, 4));
516  return pfirst(res);
517 }
518 
519 template<> EIGEN_STRONG_INLINE double predux_min<Packet2d>(const Packet2d& a)
520 {
521  return pfirst(pmin<Packet2d>(a, reinterpret_cast<Packet2d>(vec_sld(reinterpret_cast<Packet4i>(a), reinterpret_cast<Packet4i>(a), 8))));
522 }
523 
524 // max
525 template<> EIGEN_STRONG_INLINE int predux_max<Packet4i>(const Packet4i& a)
526 {
527  Packet4i b, res;
528  b = pmax<Packet4i>(a, vec_sld(a, a, 8));
529  res = pmax<Packet4i>(b, vec_sld(b, b, 4));
530  return pfirst(res);
531 }
532 
533 // max
534 template<> EIGEN_STRONG_INLINE double predux_max<Packet2d>(const Packet2d& a)
535 {
536  return pfirst(pmax<Packet2d>(a, reinterpret_cast<Packet2d>(vec_sld(reinterpret_cast<Packet4i>(a), reinterpret_cast<Packet4i>(a), 8))));
537 }
538 
539 EIGEN_DEVICE_FUNC inline void
540 ptranspose(PacketBlock<Packet4i,4>& kernel) {
541  Packet4i t0 = vec_mergeh(kernel.packet[0], kernel.packet[2]);
542  Packet4i t1 = vec_mergel(kernel.packet[0], kernel.packet[2]);
543  Packet4i t2 = vec_mergeh(kernel.packet[1], kernel.packet[3]);
544  Packet4i t3 = vec_mergel(kernel.packet[1], kernel.packet[3]);
545  kernel.packet[0] = vec_mergeh(t0, t2);
546  kernel.packet[1] = vec_mergel(t0, t2);
547  kernel.packet[2] = vec_mergeh(t1, t3);
548  kernel.packet[3] = vec_mergel(t1, t3);
549 }
550 
551 EIGEN_DEVICE_FUNC inline void
552 ptranspose(PacketBlock<Packet2d,2>& kernel) {
553  Packet2d t0 = vec_perm(kernel.packet[0], kernel.packet[1], p16uc_TRANSPOSE64_HI);
554  Packet2d t1 = vec_perm(kernel.packet[0], kernel.packet[1], p16uc_TRANSPOSE64_LO);
555  kernel.packet[0] = t0;
556  kernel.packet[1] = t1;
557 }
558 
559 template<> EIGEN_STRONG_INLINE Packet4i pblend(const Selector<4>& ifPacket, const Packet4i& thenPacket, const Packet4i& elsePacket) {
560  Packet4ui select = { ifPacket.select[0], ifPacket.select[1], ifPacket.select[2], ifPacket.select[3] };
561  Packet4ui mask = vec_cmpeq(select, reinterpret_cast<Packet4ui>(p4i_ONE));
562  return vec_sel(elsePacket, thenPacket, mask);
563 }
564 
565 template<> EIGEN_STRONG_INLINE Packet2d pblend(const Selector<2>& ifPacket, const Packet2d& thenPacket, const Packet2d& elsePacket) {
566  Packet2ul select = { ifPacket.select[0], ifPacket.select[1] };
567  Packet2ul mask = vec_cmpeq(select, reinterpret_cast<Packet2ul>(p2l_ONE));
568  return vec_sel(elsePacket, thenPacket, mask);
569 }
570 
571 } // end namespace internal
572 
573 } // end namespace Eigen
574 
575 #endif // EIGEN_PACKET_MATH_ZVECTOR_H
Definition: Constants.h:230
Namespace containing all symbols from the Eigen library.
Definition: Core:287
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
Definition: Eigen_Colamd.h:50