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  HasAdd = 1,
46  HasSub = 1,
47  HasMul = 1,
48  HasNegate = 1,
49  HasAbs = 1,
50  HasAbs2 = 1,
51  HasMin = 1,
52  HasMax = 1,
53  HasConj = 1,
54  HasSetLinear = 1,
55 
56  HasDiv = 0,
57  HasSqrt = 0,
58  HasExp = 0,
59  HasLog = 0,
60  HasPow = 0,
61 
62  HasSin = 0,
63  HasCos = 0,
64  HasTan = 0,
65  HasASin = 0,
66  HasACos = 0,
67  HasATan = 0
68  };
69 };
70 
71 template<typename T> struct packet_traits : default_packet_traits
72 {
73  typedef T type;
74  enum {
75  Vectorizable = 0,
76  size = 1,
77  AlignedOnScalar = 0
78  };
79  enum {
80  HasAdd = 0,
81  HasSub = 0,
82  HasMul = 0,
83  HasNegate = 0,
84  HasAbs = 0,
85  HasAbs2 = 0,
86  HasMin = 0,
87  HasMax = 0,
88  HasConj = 0,
89  HasSetLinear = 0
90  };
91 };
92 
94 template<typename Packet> inline Packet
95 padd(const Packet& a,
96  const Packet& b) { return a+b; }
97 
99 template<typename Packet> inline Packet
100 psub(const Packet& a,
101  const Packet& b) { return a-b; }
102 
104 template<typename Packet> inline Packet
105 pnegate(const Packet& a) { return -a; }
106 
108 template<typename Packet> inline Packet
109 pconj(const Packet& a) { return conj(a); }
110 
112 template<typename Packet> inline Packet
113 pmul(const Packet& a,
114  const Packet& b) { return a*b; }
115 
117 template<typename Packet> inline Packet
118 pdiv(const Packet& a,
119  const Packet& b) { return a/b; }
120 
122 template<typename Packet> inline Packet
123 pmin(const Packet& a,
124  const Packet& b) { using std::min; return (min)(a, b); }
125 
127 template<typename Packet> inline Packet
128 pmax(const Packet& a,
129  const Packet& b) { using std::max; return (max)(a, b); }
130 
132 template<typename Packet> inline Packet
133 pabs(const Packet& a) { return abs(a); }
134 
136 template<typename Packet> inline Packet
137 pand(const Packet& a, const Packet& b) { return a & b; }
138 
140 template<typename Packet> inline Packet
141 por(const Packet& a, const Packet& b) { return a | b; }
142 
144 template<typename Packet> inline Packet
145 pxor(const Packet& a, const Packet& b) { return a ^ b; }
146 
148 template<typename Packet> inline Packet
149 pandnot(const Packet& a, const Packet& b) { return a & (!b); }
150 
152 template<typename Packet> inline Packet
153 pload(const typename unpacket_traits<Packet>::type* from) { return *from; }
154 
156 template<typename Packet> inline Packet
157 ploadu(const typename unpacket_traits<Packet>::type* from) { return *from; }
158 
160 template<typename Packet> inline Packet
161 ploaddup(const typename unpacket_traits<Packet>::type* from) { return *from; }
162 
164 template<typename Packet> inline Packet
165 pset1(const typename unpacket_traits<Packet>::type& a) { return a; }
166 
168 template<typename Scalar> inline typename packet_traits<Scalar>::type
169 plset(const Scalar& a) { return a; }
170 
172 template<typename Scalar, typename Packet> inline void pstore(Scalar* to, const Packet& from)
173 { (*to) = from; }
174 
176 template<typename Scalar, typename Packet> inline void pstoreu(Scalar* to, const Packet& from)
177 { (*to) = from; }
178 
180 template<typename Scalar> inline void prefetch(const Scalar* addr)
181 {
182 #if !defined(_MSC_VER)
183 __builtin_prefetch(addr);
184 #endif
185 }
186 
188 template<typename Packet> inline typename unpacket_traits<Packet>::type pfirst(const Packet& a)
189 { return a; }
190 
192 template<typename Packet> inline Packet
193 preduxp(const Packet* vecs) { return vecs[0]; }
194 
196 template<typename Packet> inline typename unpacket_traits<Packet>::type predux(const Packet& a)
197 { return a; }
198 
200 template<typename Packet> inline typename unpacket_traits<Packet>::type predux_mul(const Packet& a)
201 { return a; }
202 
204 template<typename Packet> inline typename unpacket_traits<Packet>::type predux_min(const Packet& a)
205 { return a; }
206 
208 template<typename Packet> inline typename unpacket_traits<Packet>::type predux_max(const Packet& a)
209 { return a; }
210 
212 template<typename Packet> inline Packet preverse(const Packet& a)
213 { return a; }
214 
215 
217 template<typename Packet> inline Packet pcplxflip(const Packet& a)
218 { return Packet(imag(a),real(a)); }
219 
220 /**************************
221 * Special math functions
222 ***************************/
223 
225 template<typename Packet> EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
226 Packet psin(const Packet& a) { return sin(a); }
227 
229 template<typename Packet> EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
230 Packet pcos(const Packet& a) { return cos(a); }
231 
233 template<typename Packet> EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
234 Packet ptan(const Packet& a) { return tan(a); }
235 
237 template<typename Packet> EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
238 Packet pasin(const Packet& a) { return asin(a); }
239 
241 template<typename Packet> EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
242 Packet pacos(const Packet& a) { return acos(a); }
243 
245 template<typename Packet> EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
246 Packet pexp(const Packet& a) { return exp(a); }
247 
249 template<typename Packet> EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
250 Packet plog(const Packet& a) { return log(a); }
251 
253 template<typename Packet> EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
254 Packet psqrt(const Packet& a) { return sqrt(a); }
255 
256 /***************************************************************************
257 * The following functions might not have to be overwritten for vectorized types
258 ***************************************************************************/
259 
261 // NOTE: this function must really be templated on the packet type (think about different packet types for the same scalar type)
262 template<typename Packet>
263 inline void pstore1(typename unpacket_traits<Packet>::type* to, const typename unpacket_traits<Packet>::type& a)
264 {
265  pstore(to, pset1<Packet>(a));
266 }
267 
269 template<typename Packet> inline Packet
270 pmadd(const Packet& a,
271  const Packet& b,
272  const Packet& c)
273 { return padd(pmul(a, b),c); }
274 
277 template<typename Packet, int LoadMode>
278 inline Packet ploadt(const typename unpacket_traits<Packet>::type* from)
279 {
280  if(LoadMode == Aligned)
281  return pload<Packet>(from);
282  else
283  return ploadu<Packet>(from);
284 }
285 
288 template<typename Scalar, typename Packet, int LoadMode>
289 inline void pstoret(Scalar* to, const Packet& from)
290 {
291  if(LoadMode == Aligned)
292  pstore(to, from);
293  else
294  pstoreu(to, from);
295 }
296 
298 template<int Offset,typename PacketType>
299 struct palign_impl
300 {
301  // by default data are aligned, so there is nothing to be done :)
302  static inline void run(PacketType&, const PacketType&) {}
303 };
304 
307 template<int Offset,typename PacketType>
308 inline void palign(PacketType& first, const PacketType& second)
309 {
310  palign_impl<Offset,PacketType>::run(first,second);
311 }
312 
313 /***************************************************************************
314 * Fast complex products (GCC generates a function call which is very slow)
315 ***************************************************************************/
316 
317 template<> inline std::complex<float> pmul(const std::complex<float>& a, const std::complex<float>& b)
318 { return std::complex<float>(real(a)*real(b) - imag(a)*imag(b), imag(a)*real(b) + real(a)*imag(b)); }
319 
320 template<> inline std::complex<double> pmul(const std::complex<double>& a, const std::complex<double>& b)
321 { return std::complex<double>(real(a)*real(b) - imag(a)*imag(b), imag(a)*real(b) + real(a)*imag(b)); }
322 
323 } // end namespace internal
324 
325 } // end namespace Eigen
326 
327 #endif // EIGEN_GENERIC_PACKET_MATH_H
328