SSE/PacketMath.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
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_SSE_H
11 #define EIGEN_PACKET_MATH_SSE_H
12 
13 namespace Eigen {
14 
15 namespace internal {
16 
17 #ifndef EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD
18 #define EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD 8
19 #endif
20 
21 #ifndef EIGEN_ARCH_DEFAULT_NUMBER_OF_REGISTERS
22 #define EIGEN_ARCH_DEFAULT_NUMBER_OF_REGISTERS (2*sizeof(void*))
23 #endif
24 
25 typedef __m128 Packet4f;
26 typedef __m128i Packet4i;
27 typedef __m128d Packet2d;
28 
29 template<> struct is_arithmetic<__m128> { enum { value = true }; };
30 template<> struct is_arithmetic<__m128i> { enum { value = true }; };
31 template<> struct is_arithmetic<__m128d> { enum { value = true }; };
32 
33 #define vec4f_swizzle1(v,p,q,r,s) \
34  (_mm_castsi128_ps(_mm_shuffle_epi32( _mm_castps_si128(v), ((s)<<6|(r)<<4|(q)<<2|(p)))))
35 
36 #define vec4i_swizzle1(v,p,q,r,s) \
37  (_mm_shuffle_epi32( v, ((s)<<6|(r)<<4|(q)<<2|(p))))
38 
39 #define vec2d_swizzle1(v,p,q) \
40  (_mm_castsi128_pd(_mm_shuffle_epi32( _mm_castpd_si128(v), ((q*2+1)<<6|(q*2)<<4|(p*2+1)<<2|(p*2)))))
41 
42 #define vec4f_swizzle2(a,b,p,q,r,s) \
43  (_mm_shuffle_ps( (a), (b), ((s)<<6|(r)<<4|(q)<<2|(p))))
44 
45 #define vec4i_swizzle2(a,b,p,q,r,s) \
46  (_mm_castps_si128( (_mm_shuffle_ps( _mm_castsi128_ps(a), _mm_castsi128_ps(b), ((s)<<6|(r)<<4|(q)<<2|(p))))))
47 
48 #define _EIGEN_DECLARE_CONST_Packet4f(NAME,X) \
49  const Packet4f p4f_##NAME = pset1<Packet4f>(X)
50 
51 #define _EIGEN_DECLARE_CONST_Packet2d(NAME,X) \
52  const Packet2d p2d_##NAME = pset1<Packet2d>(X)
53 
54 #define _EIGEN_DECLARE_CONST_Packet4f_FROM_INT(NAME,X) \
55  const Packet4f p4f_##NAME = _mm_castsi128_ps(pset1<Packet4i>(X))
56 
57 #define _EIGEN_DECLARE_CONST_Packet4i(NAME,X) \
58  const Packet4i p4i_##NAME = pset1<Packet4i>(X)
59 
60 
61 template<> struct packet_traits<float> : default_packet_traits
62 {
63  typedef Packet4f type;
64  enum {
65  Vectorizable = 1,
66  AlignedOnScalar = 1,
67  size=4,
68 
69  HasDiv = 1,
70  HasSin = EIGEN_FAST_MATH,
71  HasCos = EIGEN_FAST_MATH,
72  HasLog = 1,
73  HasExp = 1,
74  HasSqrt = 1
75  };
76 };
77 template<> struct packet_traits<double> : default_packet_traits
78 {
79  typedef Packet2d type;
80  enum {
81  Vectorizable = 1,
82  AlignedOnScalar = 1,
83  size=2,
84 
85  HasDiv = 1,
86  HasExp = 1
87  };
88 };
89 template<> struct packet_traits<int> : default_packet_traits
90 {
91  typedef Packet4i type;
92  enum {
93  // FIXME check the Has*
94  Vectorizable = 1,
95  AlignedOnScalar = 1,
96  size=4
97  };
98 };
99 
100 template<> struct unpacket_traits<Packet4f> { typedef float type; enum {size=4}; };
101 template<> struct unpacket_traits<Packet2d> { typedef double type; enum {size=2}; };
102 template<> struct unpacket_traits<Packet4i> { typedef int type; enum {size=4}; };
103 
104 #if defined(_MSC_VER) && (_MSC_VER==1500)
105 // Workaround MSVC 9 internal compiler error.
106 // TODO: It has been detected with win64 builds (amd64), so let's check whether it also happens in 32bits+SSE mode
107 // TODO: let's check whether there does not exist a better fix, like adding a pset0() function. (it crashed on pset1(0)).
108 template<> EIGEN_STRONG_INLINE Packet4f pset1<Packet4f>(const float& from) { return _mm_set_ps(from,from,from,from); }
109 template<> EIGEN_STRONG_INLINE Packet2d pset1<Packet2d>(const double& from) { return _mm_set_pd(from,from); }
110 template<> EIGEN_STRONG_INLINE Packet4i pset1<Packet4i>(const int& from) { return _mm_set_epi32(from,from,from,from); }
111 #else
112 template<> EIGEN_STRONG_INLINE Packet4f pset1<Packet4f>(const float& from) { return _mm_set1_ps(from); }
113 template<> EIGEN_STRONG_INLINE Packet2d pset1<Packet2d>(const double& from) { return _mm_set1_pd(from); }
114 template<> EIGEN_STRONG_INLINE Packet4i pset1<Packet4i>(const int& from) { return _mm_set1_epi32(from); }
115 #endif
116 
117 template<> EIGEN_STRONG_INLINE Packet4f plset<float>(const float& a) { return _mm_add_ps(pset1<Packet4f>(a), _mm_set_ps(3,2,1,0)); }
118 template<> EIGEN_STRONG_INLINE Packet2d plset<double>(const double& a) { return _mm_add_pd(pset1<Packet2d>(a),_mm_set_pd(1,0)); }
119 template<> EIGEN_STRONG_INLINE Packet4i plset<int>(const int& a) { return _mm_add_epi32(pset1<Packet4i>(a),_mm_set_epi32(3,2,1,0)); }
120 
121 template<> EIGEN_STRONG_INLINE Packet4f padd<Packet4f>(const Packet4f& a, const Packet4f& b) { return _mm_add_ps(a,b); }
122 template<> EIGEN_STRONG_INLINE Packet2d padd<Packet2d>(const Packet2d& a, const Packet2d& b) { return _mm_add_pd(a,b); }
123 template<> EIGEN_STRONG_INLINE Packet4i padd<Packet4i>(const Packet4i& a, const Packet4i& b) { return _mm_add_epi32(a,b); }
124 
125 template<> EIGEN_STRONG_INLINE Packet4f psub<Packet4f>(const Packet4f& a, const Packet4f& b) { return _mm_sub_ps(a,b); }
126 template<> EIGEN_STRONG_INLINE Packet2d psub<Packet2d>(const Packet2d& a, const Packet2d& b) { return _mm_sub_pd(a,b); }
127 template<> EIGEN_STRONG_INLINE Packet4i psub<Packet4i>(const Packet4i& a, const Packet4i& b) { return _mm_sub_epi32(a,b); }
128 
129 template<> EIGEN_STRONG_INLINE Packet4f pnegate(const Packet4f& a)
130 {
131  const Packet4f mask = _mm_castsi128_ps(_mm_setr_epi32(0x80000000,0x80000000,0x80000000,0x80000000));
132  return _mm_xor_ps(a,mask);
133 }
134 template<> EIGEN_STRONG_INLINE Packet2d pnegate(const Packet2d& a)
135 {
136  const Packet2d mask = _mm_castsi128_pd(_mm_setr_epi32(0x0,0x80000000,0x0,0x80000000));
137  return _mm_xor_pd(a,mask);
138 }
139 template<> EIGEN_STRONG_INLINE Packet4i pnegate(const Packet4i& a)
140 {
141  return psub(_mm_setr_epi32(0,0,0,0), a);
142 }
143 
144 template<> EIGEN_STRONG_INLINE Packet4f pconj(const Packet4f& a) { return a; }
145 template<> EIGEN_STRONG_INLINE Packet2d pconj(const Packet2d& a) { return a; }
146 template<> EIGEN_STRONG_INLINE Packet4i pconj(const Packet4i& a) { return a; }
147 
148 template<> EIGEN_STRONG_INLINE Packet4f pmul<Packet4f>(const Packet4f& a, const Packet4f& b) { return _mm_mul_ps(a,b); }
149 template<> EIGEN_STRONG_INLINE Packet2d pmul<Packet2d>(const Packet2d& a, const Packet2d& b) { return _mm_mul_pd(a,b); }
150 template<> EIGEN_STRONG_INLINE Packet4i pmul<Packet4i>(const Packet4i& a, const Packet4i& b)
151 {
152 #ifdef EIGEN_VECTORIZE_SSE4_1
153  return _mm_mullo_epi32(a,b);
154 #else
155  // this version is slightly faster than 4 scalar products
156  return vec4i_swizzle1(
158  _mm_mul_epu32(a,b),
159  _mm_mul_epu32(vec4i_swizzle1(a,1,0,3,2),
160  vec4i_swizzle1(b,1,0,3,2)),
161  0,2,0,2),
162  0,2,1,3);
163 #endif
164 }
165 
166 template<> EIGEN_STRONG_INLINE Packet4f pdiv<Packet4f>(const Packet4f& a, const Packet4f& b) { return _mm_div_ps(a,b); }
167 template<> EIGEN_STRONG_INLINE Packet2d pdiv<Packet2d>(const Packet2d& a, const Packet2d& b) { return _mm_div_pd(a,b); }
168 template<> EIGEN_STRONG_INLINE Packet4i pdiv<Packet4i>(const Packet4i& /*a*/, const Packet4i& /*b*/)
169 { eigen_assert(false && "packet integer division are not supported by SSE");
170  return pset1<Packet4i>(0);
171 }
172 
173 // for some weird raisons, it has to be overloaded for packet of integers
174 template<> EIGEN_STRONG_INLINE Packet4i pmadd(const Packet4i& a, const Packet4i& b, const Packet4i& c) { return padd(pmul(a,b), c); }
175 
176 template<> EIGEN_STRONG_INLINE Packet4f pmin<Packet4f>(const Packet4f& a, const Packet4f& b) { return _mm_min_ps(a,b); }
177 template<> EIGEN_STRONG_INLINE Packet2d pmin<Packet2d>(const Packet2d& a, const Packet2d& b) { return _mm_min_pd(a,b); }
178 template<> EIGEN_STRONG_INLINE Packet4i pmin<Packet4i>(const Packet4i& a, const Packet4i& b)
179 {
180 #ifdef EIGEN_VECTORIZE_SSE4_1
181  return _mm_min_epi32(a,b);
182 #else
183  // after some bench, this version *is* faster than a scalar implementation
184  Packet4i mask = _mm_cmplt_epi32(a,b);
185  return _mm_or_si128(_mm_and_si128(mask,a),_mm_andnot_si128(mask,b));
186 #endif
187 }
188 
189 template<> EIGEN_STRONG_INLINE Packet4f pmax<Packet4f>(const Packet4f& a, const Packet4f& b) { return _mm_max_ps(a,b); }
190 template<> EIGEN_STRONG_INLINE Packet2d pmax<Packet2d>(const Packet2d& a, const Packet2d& b) { return _mm_max_pd(a,b); }
191 template<> EIGEN_STRONG_INLINE Packet4i pmax<Packet4i>(const Packet4i& a, const Packet4i& b)
192 {
193 #ifdef EIGEN_VECTORIZE_SSE4_1
194  return _mm_max_epi32(a,b);
195 #else
196  // after some bench, this version *is* faster than a scalar implementation
197  Packet4i mask = _mm_cmpgt_epi32(a,b);
198  return _mm_or_si128(_mm_and_si128(mask,a),_mm_andnot_si128(mask,b));
199 #endif
200 }
201 
202 template<> EIGEN_STRONG_INLINE Packet4f pand<Packet4f>(const Packet4f& a, const Packet4f& b) { return _mm_and_ps(a,b); }
203 template<> EIGEN_STRONG_INLINE Packet2d pand<Packet2d>(const Packet2d& a, const Packet2d& b) { return _mm_and_pd(a,b); }
204 template<> EIGEN_STRONG_INLINE Packet4i pand<Packet4i>(const Packet4i& a, const Packet4i& b) { return _mm_and_si128(a,b); }
205 
206 template<> EIGEN_STRONG_INLINE Packet4f por<Packet4f>(const Packet4f& a, const Packet4f& b) { return _mm_or_ps(a,b); }
207 template<> EIGEN_STRONG_INLINE Packet2d por<Packet2d>(const Packet2d& a, const Packet2d& b) { return _mm_or_pd(a,b); }
208 template<> EIGEN_STRONG_INLINE Packet4i por<Packet4i>(const Packet4i& a, const Packet4i& b) { return _mm_or_si128(a,b); }
209 
210 template<> EIGEN_STRONG_INLINE Packet4f pxor<Packet4f>(const Packet4f& a, const Packet4f& b) { return _mm_xor_ps(a,b); }
211 template<> EIGEN_STRONG_INLINE Packet2d pxor<Packet2d>(const Packet2d& a, const Packet2d& b) { return _mm_xor_pd(a,b); }
212 template<> EIGEN_STRONG_INLINE Packet4i pxor<Packet4i>(const Packet4i& a, const Packet4i& b) { return _mm_xor_si128(a,b); }
213 
214 template<> EIGEN_STRONG_INLINE Packet4f pandnot<Packet4f>(const Packet4f& a, const Packet4f& b) { return _mm_andnot_ps(a,b); }
215 template<> EIGEN_STRONG_INLINE Packet2d pandnot<Packet2d>(const Packet2d& a, const Packet2d& b) { return _mm_andnot_pd(a,b); }
216 template<> EIGEN_STRONG_INLINE Packet4i pandnot<Packet4i>(const Packet4i& a, const Packet4i& b) { return _mm_andnot_si128(a,b); }
217 
218 template<> EIGEN_STRONG_INLINE Packet4f pload<Packet4f>(const float* from) { EIGEN_DEBUG_ALIGNED_LOAD return _mm_load_ps(from); }
219 template<> EIGEN_STRONG_INLINE Packet2d pload<Packet2d>(const double* from) { EIGEN_DEBUG_ALIGNED_LOAD return _mm_load_pd(from); }
220 template<> EIGEN_STRONG_INLINE Packet4i pload<Packet4i>(const int* from) { EIGEN_DEBUG_ALIGNED_LOAD return _mm_load_si128(reinterpret_cast<const Packet4i*>(from)); }
221 
222 #if defined(_MSC_VER)
223  template<> EIGEN_STRONG_INLINE Packet4f ploadu<Packet4f>(const float* from) {
225  #if (_MSC_VER==1600)
226  // NOTE Some version of MSVC10 generates bad code when using _mm_loadu_ps
227  // (i.e., it does not generate an unaligned load!!
228  // TODO On most architectures this version should also be faster than a single _mm_loadu_ps
229  // so we could also enable it for MSVC08 but first we have to make this later does not generate crap when doing so...
230  __m128 res = _mm_loadl_pi(_mm_set1_ps(0.0f), (const __m64*)(from));
231  res = _mm_loadh_pi(res, (const __m64*)(from+2));
232  return res;
233  #else
234  return _mm_loadu_ps(from);
235  #endif
236  }
237  template<> EIGEN_STRONG_INLINE Packet2d ploadu<Packet2d>(const double* from) { EIGEN_DEBUG_UNALIGNED_LOAD return _mm_loadu_pd(from); }
238  template<> EIGEN_STRONG_INLINE Packet4i ploadu<Packet4i>(const int* from) { EIGEN_DEBUG_UNALIGNED_LOAD return _mm_loadu_si128(reinterpret_cast<const Packet4i*>(from)); }
239 #else
240 // Fast unaligned loads. Note that here we cannot directly use intrinsics: this would
241 // require pointer casting to incompatible pointer types and leads to invalid code
242 // because of the strict aliasing rule. The "dummy" stuff are required to enforce
243 // a correct instruction dependency.
244 // TODO: do the same for MSVC (ICC is compatible)
245 // NOTE: with the code below, MSVC's compiler crashes!
246 
247 #if defined(__GNUC__) && defined(__i386__)
248  // bug 195: gcc/i386 emits weird x87 fldl/fstpl instructions for _mm_load_sd
249  #define EIGEN_AVOID_CUSTOM_UNALIGNED_LOADS 1
250 #elif defined(__clang__)
251  // bug 201: Segfaults in __mm_loadh_pd with clang 2.8
252  #define EIGEN_AVOID_CUSTOM_UNALIGNED_LOADS 1
253 #else
254  #define EIGEN_AVOID_CUSTOM_UNALIGNED_LOADS 0
255 #endif
256 
257 template<> EIGEN_STRONG_INLINE Packet4f ploadu<Packet4f>(const float* from)
258 {
260 #if EIGEN_AVOID_CUSTOM_UNALIGNED_LOADS
261  return _mm_loadu_ps(from);
262 #else
263  __m128d res;
264  res = _mm_load_sd((const double*)(from)) ;
265  res = _mm_loadh_pd(res, (const double*)(from+2)) ;
266  return _mm_castpd_ps(res);
267 #endif
268 }
269 template<> EIGEN_STRONG_INLINE Packet2d ploadu<Packet2d>(const double* from)
270 {
272 #if EIGEN_AVOID_CUSTOM_UNALIGNED_LOADS
273  return _mm_loadu_pd(from);
274 #else
275  __m128d res;
276  res = _mm_load_sd(from) ;
277  res = _mm_loadh_pd(res,from+1);
278  return res;
279 #endif
280 }
281 template<> EIGEN_STRONG_INLINE Packet4i ploadu<Packet4i>(const int* from)
282 {
284 #if EIGEN_AVOID_CUSTOM_UNALIGNED_LOADS
285  return _mm_loadu_si128(reinterpret_cast<const Packet4i*>(from));
286 #else
287  __m128d res;
288  res = _mm_load_sd((const double*)(from)) ;
289  res = _mm_loadh_pd(res, (const double*)(from+2)) ;
290  return _mm_castpd_si128(res);
291 #endif
292 }
293 #endif
294 
295 template<> EIGEN_STRONG_INLINE Packet4f ploaddup<Packet4f>(const float* from)
296 {
297  return vec4f_swizzle1(_mm_castpd_ps(_mm_load_sd(reinterpret_cast<const double*>(from))), 0, 0, 1, 1);
298 }
299 template<> EIGEN_STRONG_INLINE Packet2d ploaddup<Packet2d>(const double* from)
300 { return pset1<Packet2d>(from[0]); }
301 template<> EIGEN_STRONG_INLINE Packet4i ploaddup<Packet4i>(const int* from)
302 {
303  Packet4i tmp;
304  tmp = _mm_loadl_epi64(reinterpret_cast<const Packet4i*>(from));
305  return vec4i_swizzle1(tmp, 0, 0, 1, 1);
306 }
307 
308 template<> EIGEN_STRONG_INLINE void pstore<float>(float* to, const Packet4f& from) { EIGEN_DEBUG_ALIGNED_STORE _mm_store_ps(to, from); }
309 template<> EIGEN_STRONG_INLINE void pstore<double>(double* to, const Packet2d& from) { EIGEN_DEBUG_ALIGNED_STORE _mm_store_pd(to, from); }
310 template<> EIGEN_STRONG_INLINE void pstore<int>(int* to, const Packet4i& from) { EIGEN_DEBUG_ALIGNED_STORE _mm_store_si128(reinterpret_cast<Packet4i*>(to), from); }
311 
312 template<> EIGEN_STRONG_INLINE void pstoreu<double>(double* to, const Packet2d& from) {
314  _mm_storel_pd((to), from);
315  _mm_storeh_pd((to+1), from);
316 }
317 template<> EIGEN_STRONG_INLINE void pstoreu<float>(float* to, const Packet4f& from) { EIGEN_DEBUG_UNALIGNED_STORE pstoreu(reinterpret_cast<double*>(to), _mm_castps_pd(from)); }
318 template<> EIGEN_STRONG_INLINE void pstoreu<int>(int* to, const Packet4i& from) { EIGEN_DEBUG_UNALIGNED_STORE pstoreu(reinterpret_cast<double*>(to), _mm_castsi128_pd(from)); }
319 
320 // some compilers might be tempted to perform multiple moves instead of using a vector path.
321 template<> EIGEN_STRONG_INLINE void pstore1<Packet4f>(float* to, const float& a)
322 {
323  Packet4f pa = _mm_set_ss(a);
324  pstore(to, vec4f_swizzle1(pa,0,0,0,0));
325 }
326 // some compilers might be tempted to perform multiple moves instead of using a vector path.
327 template<> EIGEN_STRONG_INLINE void pstore1<Packet2d>(double* to, const double& a)
328 {
329  Packet2d pa = _mm_set_sd(a);
330  pstore(to, vec2d_swizzle1(pa,0,0));
331 }
332 
333 template<> EIGEN_STRONG_INLINE void prefetch<float>(const float* addr) { _mm_prefetch((const char*)(addr), _MM_HINT_T0); }
334 template<> EIGEN_STRONG_INLINE void prefetch<double>(const double* addr) { _mm_prefetch((const char*)(addr), _MM_HINT_T0); }
335 template<> EIGEN_STRONG_INLINE void prefetch<int>(const int* addr) { _mm_prefetch((const char*)(addr), _MM_HINT_T0); }
336 
337 #if defined(_MSC_VER) && defined(_WIN64) && !defined(__INTEL_COMPILER)
338 // The temporary variable fixes an internal compilation error in vs <= 2008 and a wrong-result bug in vs 2010
339 // Direct of the struct members fixed bug #62.
340 template<> EIGEN_STRONG_INLINE float pfirst<Packet4f>(const Packet4f& a) { return a.m128_f32[0]; }
341 template<> EIGEN_STRONG_INLINE double pfirst<Packet2d>(const Packet2d& a) { return a.m128d_f64[0]; }
342 template<> EIGEN_STRONG_INLINE int pfirst<Packet4i>(const Packet4i& a) { int x = _mm_cvtsi128_si32(a); return x; }
343 #elif defined(_MSC_VER) && !defined(__INTEL_COMPILER)
344 // The temporary variable fixes an internal compilation error in vs <= 2008 and a wrong-result bug in vs 2010
345 template<> EIGEN_STRONG_INLINE float pfirst<Packet4f>(const Packet4f& a) { float x = _mm_cvtss_f32(a); return x; }
346 template<> EIGEN_STRONG_INLINE double pfirst<Packet2d>(const Packet2d& a) { double x = _mm_cvtsd_f64(a); return x; }
347 template<> EIGEN_STRONG_INLINE int pfirst<Packet4i>(const Packet4i& a) { int x = _mm_cvtsi128_si32(a); return x; }
348 #else
349 template<> EIGEN_STRONG_INLINE float pfirst<Packet4f>(const Packet4f& a) { return _mm_cvtss_f32(a); }
350 template<> EIGEN_STRONG_INLINE double pfirst<Packet2d>(const Packet2d& a) { return _mm_cvtsd_f64(a); }
351 template<> EIGEN_STRONG_INLINE int pfirst<Packet4i>(const Packet4i& a) { return _mm_cvtsi128_si32(a); }
352 #endif
353 
354 template<> EIGEN_STRONG_INLINE Packet4f preverse(const Packet4f& a)
355 { return _mm_shuffle_ps(a,a,0x1B); }
356 template<> EIGEN_STRONG_INLINE Packet2d preverse(const Packet2d& a)
357 { return _mm_shuffle_pd(a,a,0x1); }
358 template<> EIGEN_STRONG_INLINE Packet4i preverse(const Packet4i& a)
359 { return _mm_shuffle_epi32(a,0x1B); }
360 
361 
362 template<> EIGEN_STRONG_INLINE Packet4f pabs(const Packet4f& a)
363 {
364  const Packet4f mask = _mm_castsi128_ps(_mm_setr_epi32(0x7FFFFFFF,0x7FFFFFFF,0x7FFFFFFF,0x7FFFFFFF));
365  return _mm_and_ps(a,mask);
366 }
367 template<> EIGEN_STRONG_INLINE Packet2d pabs(const Packet2d& a)
368 {
369  const Packet2d mask = _mm_castsi128_pd(_mm_setr_epi32(0xFFFFFFFF,0x7FFFFFFF,0xFFFFFFFF,0x7FFFFFFF));
370  return _mm_and_pd(a,mask);
371 }
372 template<> EIGEN_STRONG_INLINE Packet4i pabs(const Packet4i& a)
373 {
374  #ifdef EIGEN_VECTORIZE_SSSE3
375  return _mm_abs_epi32(a);
376  #else
377  Packet4i aux = _mm_srai_epi32(a,31);
378  return _mm_sub_epi32(_mm_xor_si128(a,aux),aux);
379  #endif
380 }
381 
382 EIGEN_STRONG_INLINE void punpackp(Packet4f* vecs)
383 {
384  vecs[1] = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(vecs[0]), 0x55));
385  vecs[2] = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(vecs[0]), 0xAA));
386  vecs[3] = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(vecs[0]), 0xFF));
387  vecs[0] = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(vecs[0]), 0x00));
388 }
389 
390 #ifdef EIGEN_VECTORIZE_SSE3
391 // TODO implement SSE2 versions as well as integer versions
392 template<> EIGEN_STRONG_INLINE Packet4f preduxp<Packet4f>(const Packet4f* vecs)
393 {
394  return _mm_hadd_ps(_mm_hadd_ps(vecs[0], vecs[1]),_mm_hadd_ps(vecs[2], vecs[3]));
395 }
396 template<> EIGEN_STRONG_INLINE Packet2d preduxp<Packet2d>(const Packet2d* vecs)
397 {
398  return _mm_hadd_pd(vecs[0], vecs[1]);
399 }
400 // SSSE3 version:
401 // EIGEN_STRONG_INLINE Packet4i preduxp(const Packet4i* vecs)
402 // {
403 // return _mm_hadd_epi32(_mm_hadd_epi32(vecs[0], vecs[1]),_mm_hadd_epi32(vecs[2], vecs[3]));
404 // }
405 
406 template<> EIGEN_STRONG_INLINE float predux<Packet4f>(const Packet4f& a)
407 {
408  Packet4f tmp0 = _mm_hadd_ps(a,a);
409  return pfirst(_mm_hadd_ps(tmp0, tmp0));
410 }
411 
412 template<> EIGEN_STRONG_INLINE double predux<Packet2d>(const Packet2d& a) { return pfirst(_mm_hadd_pd(a, a)); }
413 
414 // SSSE3 version:
415 // EIGEN_STRONG_INLINE float predux(const Packet4i& a)
416 // {
417 // Packet4i tmp0 = _mm_hadd_epi32(a,a);
418 // return pfirst(_mm_hadd_epi32(tmp0, tmp0));
419 // }
420 #else
421 // SSE2 versions
422 template<> EIGEN_STRONG_INLINE float predux<Packet4f>(const Packet4f& a)
423 {
424  Packet4f tmp = _mm_add_ps(a, _mm_movehl_ps(a,a));
425  return pfirst(_mm_add_ss(tmp, _mm_shuffle_ps(tmp,tmp, 1)));
426 }
427 template<> EIGEN_STRONG_INLINE double predux<Packet2d>(const Packet2d& a)
428 {
429  return pfirst(_mm_add_sd(a, _mm_unpackhi_pd(a,a)));
430 }
431 
432 template<> EIGEN_STRONG_INLINE Packet4f preduxp<Packet4f>(const Packet4f* vecs)
433 {
434  Packet4f tmp0, tmp1, tmp2;
435  tmp0 = _mm_unpacklo_ps(vecs[0], vecs[1]);
436  tmp1 = _mm_unpackhi_ps(vecs[0], vecs[1]);
437  tmp2 = _mm_unpackhi_ps(vecs[2], vecs[3]);
438  tmp0 = _mm_add_ps(tmp0, tmp1);
439  tmp1 = _mm_unpacklo_ps(vecs[2], vecs[3]);
440  tmp1 = _mm_add_ps(tmp1, tmp2);
441  tmp2 = _mm_movehl_ps(tmp1, tmp0);
442  tmp0 = _mm_movelh_ps(tmp0, tmp1);
443  return _mm_add_ps(tmp0, tmp2);
444 }
445 
446 template<> EIGEN_STRONG_INLINE Packet2d preduxp<Packet2d>(const Packet2d* vecs)
447 {
448  return _mm_add_pd(_mm_unpacklo_pd(vecs[0], vecs[1]), _mm_unpackhi_pd(vecs[0], vecs[1]));
449 }
450 #endif // SSE3
451 
452 template<> EIGEN_STRONG_INLINE int predux<Packet4i>(const Packet4i& a)
453 {
454  Packet4i tmp = _mm_add_epi32(a, _mm_unpackhi_epi64(a,a));
455  return pfirst(tmp) + pfirst(_mm_shuffle_epi32(tmp, 1));
456 }
457 
458 template<> EIGEN_STRONG_INLINE Packet4i preduxp<Packet4i>(const Packet4i* vecs)
459 {
460  Packet4i tmp0, tmp1, tmp2;
461  tmp0 = _mm_unpacklo_epi32(vecs[0], vecs[1]);
462  tmp1 = _mm_unpackhi_epi32(vecs[0], vecs[1]);
463  tmp2 = _mm_unpackhi_epi32(vecs[2], vecs[3]);
464  tmp0 = _mm_add_epi32(tmp0, tmp1);
465  tmp1 = _mm_unpacklo_epi32(vecs[2], vecs[3]);
466  tmp1 = _mm_add_epi32(tmp1, tmp2);
467  tmp2 = _mm_unpacklo_epi64(tmp0, tmp1);
468  tmp0 = _mm_unpackhi_epi64(tmp0, tmp1);
469  return _mm_add_epi32(tmp0, tmp2);
470 }
471 
472 // Other reduction functions:
473 
474 // mul
475 template<> EIGEN_STRONG_INLINE float predux_mul<Packet4f>(const Packet4f& a)
476 {
477  Packet4f tmp = _mm_mul_ps(a, _mm_movehl_ps(a,a));
478  return pfirst(_mm_mul_ss(tmp, _mm_shuffle_ps(tmp,tmp, 1)));
479 }
480 template<> EIGEN_STRONG_INLINE double predux_mul<Packet2d>(const Packet2d& a)
481 {
482  return pfirst(_mm_mul_sd(a, _mm_unpackhi_pd(a,a)));
483 }
484 template<> EIGEN_STRONG_INLINE int predux_mul<Packet4i>(const Packet4i& a)
485 {
486  // after some experiments, it is seems this is the fastest way to implement it
487  // for GCC (eg., reusing pmul is very slow !)
488  // TODO try to call _mm_mul_epu32 directly
489  EIGEN_ALIGN16 int aux[4];
490  pstore(aux, a);
491  return (aux[0] * aux[1]) * (aux[2] * aux[3]);;
492 }
493 
494 // min
495 template<> EIGEN_STRONG_INLINE float predux_min<Packet4f>(const Packet4f& a)
496 {
497  Packet4f tmp = _mm_min_ps(a, _mm_movehl_ps(a,a));
498  return pfirst(_mm_min_ss(tmp, _mm_shuffle_ps(tmp,tmp, 1)));
499 }
500 template<> EIGEN_STRONG_INLINE double predux_min<Packet2d>(const Packet2d& a)
501 {
502  return pfirst(_mm_min_sd(a, _mm_unpackhi_pd(a,a)));
503 }
504 template<> EIGEN_STRONG_INLINE int predux_min<Packet4i>(const Packet4i& a)
505 {
506  // after some experiments, it is seems this is the fastest way to implement it
507  // for GCC (eg., it does not like using std::min after the pstore !!)
508  EIGEN_ALIGN16 int aux[4];
509  pstore(aux, a);
510  register int aux0 = aux[0]<aux[1] ? aux[0] : aux[1];
511  register int aux2 = aux[2]<aux[3] ? aux[2] : aux[3];
512  return aux0<aux2 ? aux0 : aux2;
513 }
514 
515 // max
516 template<> EIGEN_STRONG_INLINE float predux_max<Packet4f>(const Packet4f& a)
517 {
518  Packet4f tmp = _mm_max_ps(a, _mm_movehl_ps(a,a));
519  return pfirst(_mm_max_ss(tmp, _mm_shuffle_ps(tmp,tmp, 1)));
520 }
521 template<> EIGEN_STRONG_INLINE double predux_max<Packet2d>(const Packet2d& a)
522 {
523  return pfirst(_mm_max_sd(a, _mm_unpackhi_pd(a,a)));
524 }
525 template<> EIGEN_STRONG_INLINE int predux_max<Packet4i>(const Packet4i& a)
526 {
527  // after some experiments, it is seems this is the fastest way to implement it
528  // for GCC (eg., it does not like using std::min after the pstore !!)
529  EIGEN_ALIGN16 int aux[4];
530  pstore(aux, a);
531  register int aux0 = aux[0]>aux[1] ? aux[0] : aux[1];
532  register int aux2 = aux[2]>aux[3] ? aux[2] : aux[3];
533  return aux0>aux2 ? aux0 : aux2;
534 }
535 
536 #if (defined __GNUC__)
537 // template <> EIGEN_STRONG_INLINE Packet4f pmadd(const Packet4f& a, const Packet4f& b, const Packet4f& c)
538 // {
539 // Packet4f res = b;
540 // asm("mulps %[a], %[b] \n\taddps %[c], %[b]" : [b] "+x" (res) : [a] "x" (a), [c] "x" (c));
541 // return res;
542 // }
543 // EIGEN_STRONG_INLINE Packet4i _mm_alignr_epi8(const Packet4i& a, const Packet4i& b, const int i)
544 // {
545 // Packet4i res = a;
546 // asm("palignr %[i], %[a], %[b] " : [b] "+x" (res) : [a] "x" (a), [i] "i" (i));
547 // return res;
548 // }
549 #endif
550 
551 #ifdef EIGEN_VECTORIZE_SSSE3
552 // SSSE3 versions
553 template<int Offset>
554 struct palign_impl<Offset,Packet4f>
555 {
556  static EIGEN_STRONG_INLINE void run(Packet4f& first, const Packet4f& second)
557  {
558  if (Offset!=0)
559  first = _mm_castsi128_ps(_mm_alignr_epi8(_mm_castps_si128(second), _mm_castps_si128(first), Offset*4));
560  }
561 };
562 
563 template<int Offset>
564 struct palign_impl<Offset,Packet4i>
565 {
566  static EIGEN_STRONG_INLINE void run(Packet4i& first, const Packet4i& second)
567  {
568  if (Offset!=0)
569  first = _mm_alignr_epi8(second,first, Offset*4);
570  }
571 };
572 
573 template<int Offset>
574 struct palign_impl<Offset,Packet2d>
575 {
576  static EIGEN_STRONG_INLINE void run(Packet2d& first, const Packet2d& second)
577  {
578  if (Offset==1)
579  first = _mm_castsi128_pd(_mm_alignr_epi8(_mm_castpd_si128(second), _mm_castpd_si128(first), 8));
580  }
581 };
582 #else
583 // SSE2 versions
584 template<int Offset>
585 struct palign_impl<Offset,Packet4f>
586 {
587  static EIGEN_STRONG_INLINE void run(Packet4f& first, const Packet4f& second)
588  {
589  if (Offset==1)
590  {
591  first = _mm_move_ss(first,second);
592  first = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(first),0x39));
593  }
594  else if (Offset==2)
595  {
596  first = _mm_movehl_ps(first,first);
597  first = _mm_movelh_ps(first,second);
598  }
599  else if (Offset==3)
600  {
601  first = _mm_move_ss(first,second);
602  first = _mm_shuffle_ps(first,second,0x93);
603  }
604  }
605 };
606 
607 template<int Offset>
608 struct palign_impl<Offset,Packet4i>
609 {
610  static EIGEN_STRONG_INLINE void run(Packet4i& first, const Packet4i& second)
611  {
612  if (Offset==1)
613  {
614  first = _mm_castps_si128(_mm_move_ss(_mm_castsi128_ps(first),_mm_castsi128_ps(second)));
615  first = _mm_shuffle_epi32(first,0x39);
616  }
617  else if (Offset==2)
618  {
619  first = _mm_castps_si128(_mm_movehl_ps(_mm_castsi128_ps(first),_mm_castsi128_ps(first)));
620  first = _mm_castps_si128(_mm_movelh_ps(_mm_castsi128_ps(first),_mm_castsi128_ps(second)));
621  }
622  else if (Offset==3)
623  {
624  first = _mm_castps_si128(_mm_move_ss(_mm_castsi128_ps(first),_mm_castsi128_ps(second)));
625  first = _mm_castps_si128(_mm_shuffle_ps(_mm_castsi128_ps(first),_mm_castsi128_ps(second),0x93));
626  }
627  }
628 };
629 
630 template<int Offset>
631 struct palign_impl<Offset,Packet2d>
632 {
633  static EIGEN_STRONG_INLINE void run(Packet2d& first, const Packet2d& second)
634  {
635  if (Offset==1)
636  {
637  first = _mm_castps_pd(_mm_movehl_ps(_mm_castpd_ps(first),_mm_castpd_ps(first)));
638  first = _mm_castps_pd(_mm_movelh_ps(_mm_castpd_ps(first),_mm_castpd_ps(second)));
639  }
640  }
641 };
642 #endif
643 
644 } // end namespace internal
645 
646 } // end namespace Eigen
647 
648 #endif // EIGEN_PACKET_MATH_SSE_H
EIGEN_STRONG_INLINE Packet4i ploaddup< Packet4i >(const int *from)
#define EIGEN_ALIGN16
EIGEN_STRONG_INLINE Packet4f pxor< Packet4f >(const Packet4f &a, const Packet4f &b)
__vector float Packet4f
EIGEN_STRONG_INLINE int pfirst< Packet4i >(const Packet4i &a)
#define EIGEN_STRONG_INLINE
EIGEN_STRONG_INLINE double predux_min< Packet2d >(const Packet2d &a)
EIGEN_STRONG_INLINE Packet4i pload< Packet4i >(const int *from)
void run(class_loader::ClassLoader *loader)
EIGEN_STRONG_INLINE double predux_max< Packet2d >(const Packet2d &a)
EIGEN_STRONG_INLINE void pstoreu< double >(double *to, const Packet2d &from)
f
#define EIGEN_DEBUG_UNALIGNED_LOAD
EIGEN_STRONG_INLINE float pfirst< Packet4f >(const Packet4f &a)
EIGEN_STRONG_INLINE Packet4f ploaddup< Packet4f >(const float *from)
EIGEN_STRONG_INLINE int predux< Packet4i >(const Packet4i &a)
#define EIGEN_DEBUG_ALIGNED_STORE
Definition: LDLT.h:16
static EIGEN_STRONG_INLINE void run(Packet4i &first, const Packet4i &second)
EIGEN_STRONG_INLINE Packet4i ploadu< Packet4i >(const int *from)
EIGEN_STRONG_INLINE float predux_max< Packet4f >(const Packet4f &a)
EIGEN_STRONG_INLINE void pstore1< Packet2d >(double *to, const double &a)
static EIGEN_STRONG_INLINE void run(Packet4f &first, const Packet4f &second)
#define EIGEN_DEBUG_UNALIGNED_STORE
EIGEN_STRONG_INLINE void prefetch< float >(const float *addr)
EIGEN_STRONG_INLINE Packet4i pdiv< Packet4i >(const Packet4i &, const Packet4i &)
EIGEN_STRONG_INLINE int predux_min< Packet4i >(const Packet4i &a)
#define EIGEN_DEBUG_ALIGNED_LOAD
void pstore(Scalar *to, const Packet &from)
#define vec4i_swizzle1(v, p, q, r, s)
EIGEN_STRONG_INLINE Packet4i plset< int >(const int &a)
EIGEN_STRONG_INLINE Packet4f plset< float >(const float &a)
EIGEN_STRONG_INLINE Packet2d padd< Packet2d >(const Packet2d &a, const Packet2d &b)
EIGEN_STRONG_INLINE Packet2d ploaddup< Packet2d >(const double *from)
EIGEN_STRONG_INLINE double predux_mul< Packet2d >(const Packet2d &a)
EIGEN_STRONG_INLINE Packet2d preduxp< Packet2d >(const Packet2d *vecs)
EIGEN_STRONG_INLINE Packet2d pdiv< Packet2d >(const Packet2d &a, const Packet2d &b)
EIGEN_STRONG_INLINE Packet4i pmul< Packet4i >(const Packet4i &a, const Packet4i &b)
#define vec2d_swizzle1(v, p, q)
EIGEN_STRONG_INLINE Packet2d pandnot< Packet2d >(const Packet2d &a, const Packet2d &b)
void pstoreu(Scalar *to, const Packet &from)
EIGEN_STRONG_INLINE Packet4f pdiv< Packet4f >(const Packet4f &a, const Packet4f &b)
EIGEN_STRONG_INLINE Packet4f pmin< Packet4f >(const Packet4f &a, const Packet4f &b)
EIGEN_STRONG_INLINE Packet2d pmin< Packet2d >(const Packet2d &a, const Packet2d &b)
EIGEN_STRONG_INLINE void prefetch< int >(const int *addr)
EIGEN_STRONG_INLINE Packet4i pand< Packet4i >(const Packet4i &a, const Packet4i &b)
EIGEN_STRONG_INLINE int predux_max< Packet4i >(const Packet4i &a)
EIGEN_STRONG_INLINE Packet4i pmin< Packet4i >(const Packet4i &a, const Packet4i &b)
Packet psub(const Packet &a, const Packet &b)
EIGEN_STRONG_INLINE Packet4f ploadu< Packet4f >(const float *from)
EIGEN_STRONG_INLINE Packet4i pxor< Packet4i >(const Packet4i &a, const Packet4i &b)
EIGEN_STRONG_INLINE void pstoreu< int >(int *to, const Packet4i &from)
#define vec4f_swizzle1(v, p, q, r, s)
EIGEN_STRONG_INLINE void pstore< float >(float *to, const Packet4f &from)
EIGEN_STRONG_INLINE Packet4f por< Packet4f >(const Packet4f &a, const Packet4f &b)
EIGEN_STRONG_INLINE Packet2d pmax< Packet2d >(const Packet2d &a, const Packet2d &b)
unpacket_traits< Packet >::type pfirst(const Packet &a)
EIGEN_STRONG_INLINE Packet4i preduxp< Packet4i >(const Packet4i *vecs)
EIGEN_STRONG_INLINE Packet4f pandnot< Packet4f >(const Packet4f &a, const Packet4f &b)
EIGEN_STRONG_INLINE float predux< Packet4f >(const Packet4f &a)
EIGEN_STRONG_INLINE Packet2d pand< Packet2d >(const Packet2d &a, const Packet2d &b)
EIGEN_STRONG_INLINE Packet2d ploadu< Packet2d >(const double *from)
EIGEN_STRONG_INLINE Packet2cf pconj(const Packet2cf &a)
EIGEN_STRONG_INLINE float predux_min< Packet4f >(const Packet4f &a)
EIGEN_STRONG_INLINE Packet2d pload< Packet2d >(const double *from)
EIGEN_STRONG_INLINE Packet2d psub< Packet2d >(const Packet2d &a, const Packet2d &b)
EIGEN_STRONG_INLINE void pstoreu< float >(float *to, const Packet4f &from)
#define EIGEN_FAST_MATH
EIGEN_STRONG_INLINE int predux_mul< Packet4i >(const Packet4i &a)
EIGEN_STRONG_INLINE Packet4f pmul< Packet4f >(const Packet4f &a, const Packet4f &b)
EIGEN_STRONG_INLINE double predux< Packet2d >(const Packet2d &a)
EIGEN_STRONG_INLINE Packet4f pload< Packet4f >(const float *from)
EIGEN_STRONG_INLINE Packet4i pmax< Packet4i >(const Packet4i &a, const Packet4i &b)
EIGEN_STRONG_INLINE Packet2d pxor< Packet2d >(const Packet2d &a, const Packet2d &b)
EIGEN_STRONG_INLINE Packet2cf pnegate(const Packet2cf &a)
EIGEN_STRONG_INLINE Packet4i por< Packet4i >(const Packet4i &a, const Packet4i &b)
EIGEN_STRONG_INLINE void pstore< double >(double *to, const Packet2d &from)
EIGEN_STRONG_INLINE Packet4i padd< Packet4i >(const Packet4i &a, const Packet4i &b)
EIGEN_STRONG_INLINE void pstore1< Packet4f >(float *to, const float &a)
EIGEN_STRONG_INLINE Packet4f padd< Packet4f >(const Packet4f &a, const Packet4f &b)
EIGEN_STRONG_INLINE void pstore< int >(int *to, const Packet4i &from)
EIGEN_STRONG_INLINE Packet4i pandnot< Packet4i >(const Packet4i &a, const Packet4i &b)
EIGEN_STRONG_INLINE Packet4f pmadd(const Packet4f &a, const Packet4f &b, const Packet4f &c)
Packet pmul(const Packet &a, const Packet &b)
EIGEN_STRONG_INLINE Packet4f preduxp< Packet4f >(const Packet4f *vecs)
EIGEN_STRONG_INLINE Packet2d pset1< Packet2d >(const double &from)
__vector int Packet4i
EIGEN_STRONG_INLINE Packet2d pmul< Packet2d >(const Packet2d &a, const Packet2d &b)
#define eigen_assert(x)
static EIGEN_STRONG_INLINE void run(Packet2d &first, const Packet2d &second)
EIGEN_STRONG_INLINE Packet4f pmax< Packet4f >(const Packet4f &a, const Packet4f &b)
EIGEN_STRONG_INLINE Packet4f pset1< Packet4f >(const float &from)
EIGEN_STRONG_INLINE void punpackp(Packet4f *vecs)
EIGEN_STRONG_INLINE double pfirst< Packet2d >(const Packet2d &a)
EIGEN_STRONG_INLINE void prefetch< double >(const double *addr)
EIGEN_STRONG_INLINE Packet4i psub< Packet4i >(const Packet4i &a, const Packet4i &b)
EIGEN_STRONG_INLINE Packet4f psub< Packet4f >(const Packet4f &a, const Packet4f &b)
EIGEN_STRONG_INLINE Packet2d plset< double >(const double &a)
Packet padd(const Packet &a, const Packet &b)
#define vec4i_swizzle2(a, b, p, q, r, s)
EIGEN_STRONG_INLINE Packet4i pset1< Packet4i >(const int &from)
EIGEN_STRONG_INLINE Packet2cf preverse(const Packet2cf &a)
EIGEN_STRONG_INLINE Packet2d por< Packet2d >(const Packet2d &a, const Packet2d &b)
EIGEN_STRONG_INLINE float predux_mul< Packet4f >(const Packet4f &a)
EIGEN_STRONG_INLINE Packet4f pabs(const Packet4f &a)
EIGEN_STRONG_INLINE Packet4f pand< Packet4f >(const Packet4f &a, const Packet4f &b)


tuw_aruco
Author(s): Lukas Pfeifhofer
autogenerated on Mon Feb 28 2022 23:57:57