Eigen  3.2.91
GenericPacketMath.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 #ifndef EIGEN_GENERIC_PACKET_MATH_H
12 #define EIGEN_GENERIC_PACKET_MATH_H
13 
14 namespace Eigen {
15 
16 namespace internal {
17 
26 #ifndef EIGEN_DEBUG_ALIGNED_LOAD
27 #define EIGEN_DEBUG_ALIGNED_LOAD
28 #endif
29 
30 #ifndef EIGEN_DEBUG_UNALIGNED_LOAD
31 #define EIGEN_DEBUG_UNALIGNED_LOAD
32 #endif
33 
34 #ifndef EIGEN_DEBUG_ALIGNED_STORE
35 #define EIGEN_DEBUG_ALIGNED_STORE
36 #endif
37 
38 #ifndef EIGEN_DEBUG_UNALIGNED_STORE
39 #define EIGEN_DEBUG_UNALIGNED_STORE
40 #endif
41 
42 struct default_packet_traits
43 {
44  enum {
45  HasHalfPacket = 0,
46 
47  HasAdd = 1,
48  HasSub = 1,
49  HasMul = 1,
50  HasNegate = 1,
51  HasAbs = 1,
52  HasArg = 0,
53  HasAbs2 = 1,
54  HasMin = 1,
55  HasMax = 1,
56  HasConj = 1,
57  HasSetLinear = 1,
58  HasBlend = 0,
59 
60  HasDiv = 0,
61  HasSqrt = 0,
62  HasRsqrt = 0,
63  HasExp = 0,
64  HasLog = 0,
65  HasLog10 = 0,
66  HasPow = 0,
67 
68  HasSin = 0,
69  HasCos = 0,
70  HasTan = 0,
71  HasASin = 0,
72  HasACos = 0,
73  HasATan = 0,
74  HasSinh = 0,
75  HasCosh = 0,
76  HasTanh = 0,
77 
78  HasRound = 0,
79  HasFloor = 0,
80  HasCeil = 0
81  };
82 };
83 
84 template<typename T> struct packet_traits : default_packet_traits
85 {
86  typedef T type;
87  typedef T half;
88  enum {
89  Vectorizable = 0,
90  size = 1,
91  AlignedOnScalar = 0,
92  HasHalfPacket = 0
93  };
94  enum {
95  HasAdd = 0,
96  HasSub = 0,
97  HasMul = 0,
98  HasNegate = 0,
99  HasAbs = 0,
100  HasAbs2 = 0,
101  HasMin = 0,
102  HasMax = 0,
103  HasConj = 0,
104  HasSetLinear = 0
105  };
106 };
107 
108 template<typename T> struct packet_traits<const T> : packet_traits<T> { };
109 
110 template <typename Src, typename Tgt> struct type_casting_traits {
111  enum {
112  VectorizedCast = 0,
113  SrcCoeffRatio = 1,
114  TgtCoeffRatio = 1
115  };
116 };
117 
118 
120 template <typename SrcPacket, typename TgtPacket>
121 EIGEN_DEVICE_FUNC inline TgtPacket
122 pcast(const SrcPacket& a) {
123  return static_cast<TgtPacket>(a);
124 }
125 template <typename SrcPacket, typename TgtPacket>
126 EIGEN_DEVICE_FUNC inline TgtPacket
127 pcast(const SrcPacket& a, const SrcPacket& /*b*/) {
128  return static_cast<TgtPacket>(a);
129 }
130 
131 
133 template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
134 padd(const Packet& a,
135  const Packet& b) { return a+b; }
136 
138 template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
139 psub(const Packet& a,
140  const Packet& b) { return a-b; }
141 
143 template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
144 pnegate(const Packet& a) { return -a; }
145 
148 template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
149 pconj(const Packet& a) { return numext::conj(a); }
150 
152 template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
153 pmul(const Packet& a,
154  const Packet& b) { return a*b; }
155 
157 template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
158 pdiv(const Packet& a,
159  const Packet& b) { return a/b; }
160 
162 template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
163 pmin(const Packet& a,
164  const Packet& b) { return numext::mini(a, b); }
165 
167 template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
168 pmax(const Packet& a,
169  const Packet& b) { return numext::maxi(a, b); }
170 
172 template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
173 pabs(const Packet& a) { using std::abs; return abs(a); }
174 
176 template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
177 parg(const Packet& a) { using numext::arg; return arg(a); }
178 
180 template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
181 pand(const Packet& a, const Packet& b) { return a & b; }
182 
184 template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
185 por(const Packet& a, const Packet& b) { return a | b; }
186 
188 template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
189 pxor(const Packet& a, const Packet& b) { return a ^ b; }
190 
192 template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
193 pandnot(const Packet& a, const Packet& b) { return a & (!b); }
194 
196 template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
197 pload(const typename unpacket_traits<Packet>::type* from) { return *from; }
198 
200 template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
201 ploadu(const typename unpacket_traits<Packet>::type* from) { return *from; }
202 
204 template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
205 pset1(const typename unpacket_traits<Packet>::type& a) { return a; }
206 
208 template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
209 pload1(const typename unpacket_traits<Packet>::type *a) { return pset1<Packet>(*a); }
210 
216 template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
217 ploaddup(const typename unpacket_traits<Packet>::type* from) { return *from; }
218 
225 template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
226 ploadquad(const typename unpacket_traits<Packet>::type* from)
227 { return pload1<Packet>(from); }
228 
238 template<typename Packet> EIGEN_DEVICE_FUNC
239 inline void pbroadcast4(const typename unpacket_traits<Packet>::type *a,
240  Packet& a0, Packet& a1, Packet& a2, Packet& a3)
241 {
242  a0 = pload1<Packet>(a+0);
243  a1 = pload1<Packet>(a+1);
244  a2 = pload1<Packet>(a+2);
245  a3 = pload1<Packet>(a+3);
246 }
247 
255 template<typename Packet> EIGEN_DEVICE_FUNC
256 inline void pbroadcast2(const typename unpacket_traits<Packet>::type *a,
257  Packet& a0, Packet& a1)
258 {
259  a0 = pload1<Packet>(a+0);
260  a1 = pload1<Packet>(a+1);
261 }
262 
264 template<typename Packet> inline Packet
265 plset(const typename unpacket_traits<Packet>::type& a) { return a; }
266 
268 template<typename Scalar, typename Packet> EIGEN_DEVICE_FUNC inline void pstore(Scalar* to, const Packet& from)
269 { (*to) = from; }
270 
272 template<typename Scalar, typename Packet> EIGEN_DEVICE_FUNC inline void pstoreu(Scalar* to, const Packet& from)
273 { (*to) = from; }
274 
275  template<typename Scalar, typename Packet> EIGEN_DEVICE_FUNC inline Packet pgather(const Scalar* from, Index /*stride*/)
276  { return ploadu<Packet>(from); }
277 
278  template<typename Scalar, typename Packet> EIGEN_DEVICE_FUNC inline void pscatter(Scalar* to, const Packet& from, Index /*stride*/)
279  { pstore(to, from); }
280 
282 template<typename Scalar> inline void prefetch(const Scalar* addr)
283 {
284 #ifdef __CUDA_ARCH__
285 #if defined(__LP64__)
286  // 64-bit pointer operand constraint for inlined asm
287  asm(" prefetch.L1 [ %1 ];" : "=l"(addr) : "l"(addr));
288 #else
289  // 32-bit pointer operand constraint for inlined asm
290  asm(" prefetch.L1 [ %1 ];" : "=r"(addr) : "r"(addr));
291 #endif
292 #elif !EIGEN_COMP_MSVC
293  __builtin_prefetch(addr);
294 #endif
295 }
296 
298 template<typename Packet> EIGEN_DEVICE_FUNC inline typename unpacket_traits<Packet>::type pfirst(const Packet& a)
299 { return a; }
300 
302 template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
303 preduxp(const Packet* vecs) { return vecs[0]; }
304 
306 template<typename Packet> EIGEN_DEVICE_FUNC inline typename unpacket_traits<Packet>::type predux(const Packet& a)
307 { return a; }
308 
313 template<typename Packet> EIGEN_DEVICE_FUNC inline
314 typename conditional<(unpacket_traits<Packet>::size%8)==0,typename unpacket_traits<Packet>::half,Packet>::type
315 predux4(const Packet& a)
316 { return a; }
317 
319 template<typename Packet> EIGEN_DEVICE_FUNC inline typename unpacket_traits<Packet>::type predux_mul(const Packet& a)
320 { return a; }
321 
323 template<typename Packet> EIGEN_DEVICE_FUNC inline typename unpacket_traits<Packet>::type predux_min(const Packet& a)
324 { return a; }
325 
327 template<typename Packet> EIGEN_DEVICE_FUNC inline typename unpacket_traits<Packet>::type predux_max(const Packet& a)
328 { return a; }
329 
331 template<typename Packet> EIGEN_DEVICE_FUNC inline Packet preverse(const Packet& a)
332 { return a; }
333 
334 template<size_t offset, typename Packet>
335 struct protate_impl
336 {
337  // Empty so attempts to use this unimplemented path will fail to compile.
338  // Only specializations of this template should be used.
339 };
340 
345 template<size_t offset, typename Packet> EIGEN_DEVICE_FUNC inline Packet protate(const Packet& a)
346 {
347  return offset ? protate_impl<offset, Packet>::run(a) : a;
348 }
349 
351 template<typename Packet> EIGEN_DEVICE_FUNC inline Packet pcplxflip(const Packet& a)
352 {
353  // FIXME: uncomment the following in case we drop the internal imag and real functions.
354 // using std::imag;
355 // using std::real;
356  return Packet(imag(a),real(a));
357 }
358 
359 /**************************
360 * Special math functions
361 ***************************/
362 
364 template<typename Packet> EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
365 Packet psin(const Packet& a) { using std::sin; return sin(a); }
366 
368 template<typename Packet> EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
369 Packet pcos(const Packet& a) { using std::cos; return cos(a); }
370 
372 template<typename Packet> EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
373 Packet ptan(const Packet& a) { using std::tan; return tan(a); }
374 
376 template<typename Packet> EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
377 Packet pasin(const Packet& a) { using std::asin; return asin(a); }
378 
380 template<typename Packet> EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
381 Packet pacos(const Packet& a) { using std::acos; return acos(a); }
382 
384 template<typename Packet> EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
385 Packet patan(const Packet& a) { using std::atan; return atan(a); }
386 
388 template<typename Packet> EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
389 Packet psinh(const Packet& a) { using std::sinh; return sinh(a); }
390 
392 template<typename Packet> EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
393 Packet pcosh(const Packet& a) { using std::cosh; return cosh(a); }
394 
396 template<typename Packet> EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
397 Packet ptanh(const Packet& a) { using std::tanh; return tanh(a); }
398 
400 template<typename Packet> EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
401 Packet pexp(const Packet& a) { using std::exp; return exp(a); }
402 
404 template<typename Packet> EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
405 Packet plog(const Packet& a) { using std::log; return log(a); }
406 
408 template<typename Packet> EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
409 Packet plog10(const Packet& a) { using std::log10; return log10(a); }
410 
412 template<typename Packet> EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
413 Packet psqrt(const Packet& a) { using std::sqrt; return sqrt(a); }
414 
416 template<typename Packet> EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
417 Packet prsqrt(const Packet& a) {
418  return pdiv(pset1<Packet>(1), psqrt(a));
419 }
420 
422 template<typename Packet> EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
423 Packet pround(const Packet& a) { using numext::round; return round(a); }
424 
426 template<typename Packet> EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
427 Packet pfloor(const Packet& a) { using numext::floor; return floor(a); }
428 
430 template<typename Packet> EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
431 Packet pceil(const Packet& a) { using numext::ceil; return ceil(a); }
432 
433 /***************************************************************************
434 * The following functions might not have to be overwritten for vectorized types
435 ***************************************************************************/
436 
438 // NOTE: this function must really be templated on the packet type (think about different packet types for the same scalar type)
439 template<typename Packet>
440 inline void pstore1(typename unpacket_traits<Packet>::type* to, const typename unpacket_traits<Packet>::type& a)
441 {
442  pstore(to, pset1<Packet>(a));
443 }
444 
446 template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
447 pmadd(const Packet& a,
448  const Packet& b,
449  const Packet& c)
450 { return padd(pmul(a, b),c); }
451 
454 template<typename Packet, int Alignment>
455 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE Packet ploadt(const typename unpacket_traits<Packet>::type* from)
456 {
457  if(Alignment >= unpacket_traits<Packet>::alignment)
458  return pload<Packet>(from);
459  else
460  return ploadu<Packet>(from);
461 }
462 
465 template<typename Scalar, typename Packet, int Alignment>
466 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE void pstoret(Scalar* to, const Packet& from)
467 {
468  if(Alignment >= unpacket_traits<Packet>::alignment)
469  pstore(to, from);
470  else
471  pstoreu(to, from);
472 }
473 
479 template<typename Packet, int LoadMode>
480 inline Packet ploadt_ro(const typename unpacket_traits<Packet>::type* from)
481 {
482  return ploadt<Packet, LoadMode>(from);
483 }
484 
486 template<int Offset,typename PacketType>
487 struct palign_impl
488 {
489  // by default data are aligned, so there is nothing to be done :)
490  static inline void run(PacketType&, const PacketType&) {}
491 };
492 
508 template<int Offset,typename PacketType>
509 inline void palign(PacketType& first, const PacketType& second)
510 {
511  palign_impl<Offset,PacketType>::run(first,second);
512 }
513 
514 /***************************************************************************
515 * Fast complex products (GCC generates a function call which is very slow)
516 ***************************************************************************/
517 
518 // Eigen+CUDA does not support complexes.
519 #ifndef __CUDACC__
520 
521 template<> inline std::complex<float> pmul(const std::complex<float>& a, const std::complex<float>& b)
522 { return std::complex<float>(real(a)*real(b) - imag(a)*imag(b), imag(a)*real(b) + real(a)*imag(b)); }
523 
524 template<> inline std::complex<double> pmul(const std::complex<double>& a, const std::complex<double>& b)
525 { return std::complex<double>(real(a)*real(b) - imag(a)*imag(b), imag(a)*real(b) + real(a)*imag(b)); }
526 
527 #endif
528 
529 
530 /***************************************************************************
531  * PacketBlock, that is a collection of N packets where the number of words
532  * in the packet is a multiple of N.
533 ***************************************************************************/
534 template <typename Packet,int N=unpacket_traits<Packet>::size> struct PacketBlock {
535  Packet packet[N];
536 };
537 
538 template<typename Packet> EIGEN_DEVICE_FUNC inline void
539 ptranspose(PacketBlock<Packet,1>& /*kernel*/) {
540  // Nothing to do in the scalar case, i.e. a 1x1 matrix.
541 }
542 
543 /***************************************************************************
544  * Selector, i.e. vector of N boolean values used to select (i.e. blend)
545  * words from 2 packets.
546 ***************************************************************************/
547 template <size_t N> struct Selector {
548  bool select[N];
549 };
550 
551 template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
552 pblend(const Selector<unpacket_traits<Packet>::size>& ifPacket, const Packet& thenPacket, const Packet& elsePacket) {
553  return ifPacket.select[0] ? thenPacket : elsePacket;
554 }
555 
556 } // end namespace internal
557 
558 } // end namespace Eigen
559 
560 #endif // EIGEN_GENERIC_PACKET_MATH_H
Definition: LDLT.h:16
Definition: Eigen_Colamd.h:54