PacketMath.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 #ifndef EIGEN_PACKET_MATH_SSE_H
00011 #define EIGEN_PACKET_MATH_SSE_H
00012 
00013 namespace Eigen {
00014 
00015 namespace internal {
00016 
00017 #ifndef EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD
00018 #define EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD 8
00019 #endif
00020 
00021 #ifndef EIGEN_ARCH_DEFAULT_NUMBER_OF_REGISTERS
00022 #define EIGEN_ARCH_DEFAULT_NUMBER_OF_REGISTERS (2*sizeof(void*))
00023 #endif
00024 
00025 typedef __m128  Packet4f;
00026 typedef __m128i Packet4i;
00027 typedef __m128d Packet2d;
00028 
00029 template<> struct is_arithmetic<__m128>  { enum { value = true }; };
00030 template<> struct is_arithmetic<__m128i> { enum { value = true }; };
00031 template<> struct is_arithmetic<__m128d> { enum { value = true }; };
00032 
00033 #define vec4f_swizzle1(v,p,q,r,s) \
00034   (_mm_castsi128_ps(_mm_shuffle_epi32( _mm_castps_si128(v), ((s)<<6|(r)<<4|(q)<<2|(p)))))
00035 
00036 #define vec4i_swizzle1(v,p,q,r,s) \
00037   (_mm_shuffle_epi32( v, ((s)<<6|(r)<<4|(q)<<2|(p))))
00038 
00039 #define vec2d_swizzle1(v,p,q) \
00040   (_mm_castsi128_pd(_mm_shuffle_epi32( _mm_castpd_si128(v), ((q*2+1)<<6|(q*2)<<4|(p*2+1)<<2|(p*2)))))
00041   
00042 #define vec4f_swizzle2(a,b,p,q,r,s) \
00043   (_mm_shuffle_ps( (a), (b), ((s)<<6|(r)<<4|(q)<<2|(p))))
00044 
00045 #define vec4i_swizzle2(a,b,p,q,r,s) \
00046   (_mm_castps_si128( (_mm_shuffle_ps( _mm_castsi128_ps(a), _mm_castsi128_ps(b), ((s)<<6|(r)<<4|(q)<<2|(p))))))
00047 
00048 #define _EIGEN_DECLARE_CONST_Packet4f(NAME,X) \
00049   const Packet4f p4f_##NAME = pset1<Packet4f>(X)
00050 
00051 #define _EIGEN_DECLARE_CONST_Packet4f_FROM_INT(NAME,X) \
00052   const Packet4f p4f_##NAME = _mm_castsi128_ps(pset1<Packet4i>(X))
00053 
00054 #define _EIGEN_DECLARE_CONST_Packet4i(NAME,X) \
00055   const Packet4i p4i_##NAME = pset1<Packet4i>(X)
00056 
00057 
00058 template<> struct packet_traits<float>  : default_packet_traits
00059 {
00060   typedef Packet4f type;
00061   enum {
00062     Vectorizable = 1,
00063     AlignedOnScalar = 1,
00064     size=4,
00065 
00066     HasDiv    = 1,
00067     HasSin  = EIGEN_FAST_MATH,
00068     HasCos  = EIGEN_FAST_MATH,
00069     HasLog  = 1,
00070     HasExp  = 1,
00071     HasSqrt = 1
00072   };
00073 };
00074 template<> struct packet_traits<double> : default_packet_traits
00075 {
00076   typedef Packet2d type;
00077   enum {
00078     Vectorizable = 1,
00079     AlignedOnScalar = 1,
00080     size=2,
00081 
00082     HasDiv    = 1
00083   };
00084 };
00085 template<> struct packet_traits<int>    : default_packet_traits
00086 {
00087   typedef Packet4i type;
00088   enum {
00089     // FIXME check the Has*
00090     Vectorizable = 1,
00091     AlignedOnScalar = 1,
00092     size=4
00093   };
00094 };
00095 
00096 template<> struct unpacket_traits<Packet4f> { typedef float  type; enum {size=4}; };
00097 template<> struct unpacket_traits<Packet2d> { typedef double type; enum {size=2}; };
00098 template<> struct unpacket_traits<Packet4i> { typedef int    type; enum {size=4}; };
00099 
00100 #if defined(_MSC_VER) && (_MSC_VER==1500)
00101 // Workaround MSVC 9 internal compiler error.
00102 // TODO: It has been detected with win64 builds (amd64), so let's check whether it also happens in 32bits+SSE mode
00103 // TODO: let's check whether there does not exist a better fix, like adding a pset0() function. (it crashed on pset1(0)).
00104 template<> EIGEN_STRONG_INLINE Packet4f pset1<Packet4f>(const float&  from) { return _mm_set_ps(from,from,from,from); }
00105 template<> EIGEN_STRONG_INLINE Packet2d pset1<Packet2d>(const double& from) { return _mm_set_pd(from,from); }
00106 template<> EIGEN_STRONG_INLINE Packet4i pset1<Packet4i>(const int&    from) { return _mm_set_epi32(from,from,from,from); }
00107 #else
00108 template<> EIGEN_STRONG_INLINE Packet4f pset1<Packet4f>(const float&  from) { return _mm_set1_ps(from); }
00109 template<> EIGEN_STRONG_INLINE Packet2d pset1<Packet2d>(const double& from) { return _mm_set1_pd(from); }
00110 template<> EIGEN_STRONG_INLINE Packet4i pset1<Packet4i>(const int&    from) { return _mm_set1_epi32(from); }
00111 #endif
00112 
00113 template<> EIGEN_STRONG_INLINE Packet4f plset<float>(const float& a) { return _mm_add_ps(pset1<Packet4f>(a), _mm_set_ps(3,2,1,0)); }
00114 template<> EIGEN_STRONG_INLINE Packet2d plset<double>(const double& a) { return _mm_add_pd(pset1<Packet2d>(a),_mm_set_pd(1,0)); }
00115 template<> EIGEN_STRONG_INLINE Packet4i plset<int>(const int& a) { return _mm_add_epi32(pset1<Packet4i>(a),_mm_set_epi32(3,2,1,0)); }
00116 
00117 template<> EIGEN_STRONG_INLINE Packet4f padd<Packet4f>(const Packet4f& a, const Packet4f& b) { return _mm_add_ps(a,b); }
00118 template<> EIGEN_STRONG_INLINE Packet2d padd<Packet2d>(const Packet2d& a, const Packet2d& b) { return _mm_add_pd(a,b); }
00119 template<> EIGEN_STRONG_INLINE Packet4i padd<Packet4i>(const Packet4i& a, const Packet4i& b) { return _mm_add_epi32(a,b); }
00120 
00121 template<> EIGEN_STRONG_INLINE Packet4f psub<Packet4f>(const Packet4f& a, const Packet4f& b) { return _mm_sub_ps(a,b); }
00122 template<> EIGEN_STRONG_INLINE Packet2d psub<Packet2d>(const Packet2d& a, const Packet2d& b) { return _mm_sub_pd(a,b); }
00123 template<> EIGEN_STRONG_INLINE Packet4i psub<Packet4i>(const Packet4i& a, const Packet4i& b) { return _mm_sub_epi32(a,b); }
00124 
00125 template<> EIGEN_STRONG_INLINE Packet4f pnegate(const Packet4f& a)
00126 {
00127   const Packet4f mask = _mm_castsi128_ps(_mm_setr_epi32(0x80000000,0x80000000,0x80000000,0x80000000));
00128   return _mm_xor_ps(a,mask);
00129 }
00130 template<> EIGEN_STRONG_INLINE Packet2d pnegate(const Packet2d& a)
00131 {
00132   const Packet2d mask = _mm_castsi128_pd(_mm_setr_epi32(0x0,0x80000000,0x0,0x80000000));
00133   return _mm_xor_pd(a,mask);
00134 }
00135 template<> EIGEN_STRONG_INLINE Packet4i pnegate(const Packet4i& a)
00136 {
00137   return psub(_mm_setr_epi32(0,0,0,0), a);
00138 }
00139 
00140 template<> EIGEN_STRONG_INLINE Packet4f pmul<Packet4f>(const Packet4f& a, const Packet4f& b) { return _mm_mul_ps(a,b); }
00141 template<> EIGEN_STRONG_INLINE Packet2d pmul<Packet2d>(const Packet2d& a, const Packet2d& b) { return _mm_mul_pd(a,b); }
00142 template<> EIGEN_STRONG_INLINE Packet4i pmul<Packet4i>(const Packet4i& a, const Packet4i& b)
00143 {
00144 #ifdef EIGEN_VECTORIZE_SSE4_1
00145   return _mm_mullo_epi32(a,b);
00146 #else
00147   // this version is slightly faster than 4 scalar products
00148   return vec4i_swizzle1(
00149             vec4i_swizzle2(
00150               _mm_mul_epu32(a,b),
00151               _mm_mul_epu32(vec4i_swizzle1(a,1,0,3,2),
00152                             vec4i_swizzle1(b,1,0,3,2)),
00153               0,2,0,2),
00154             0,2,1,3);
00155 #endif
00156 }
00157 
00158 template<> EIGEN_STRONG_INLINE Packet4f pdiv<Packet4f>(const Packet4f& a, const Packet4f& b) { return _mm_div_ps(a,b); }
00159 template<> EIGEN_STRONG_INLINE Packet2d pdiv<Packet2d>(const Packet2d& a, const Packet2d& b) { return _mm_div_pd(a,b); }
00160 template<> EIGEN_STRONG_INLINE Packet4i pdiv<Packet4i>(const Packet4i& /*a*/, const Packet4i& /*b*/)
00161 { eigen_assert(false && "packet integer division are not supported by SSE");
00162   return pset1<Packet4i>(0);
00163 }
00164 
00165 // for some weird raisons, it has to be overloaded for packet of integers
00166 template<> EIGEN_STRONG_INLINE Packet4i pmadd(const Packet4i& a, const Packet4i& b, const Packet4i& c) { return padd(pmul(a,b), c); }
00167 
00168 template<> EIGEN_STRONG_INLINE Packet4f pmin<Packet4f>(const Packet4f& a, const Packet4f& b) { return _mm_min_ps(a,b); }
00169 template<> EIGEN_STRONG_INLINE Packet2d pmin<Packet2d>(const Packet2d& a, const Packet2d& b) { return _mm_min_pd(a,b); }
00170 template<> EIGEN_STRONG_INLINE Packet4i pmin<Packet4i>(const Packet4i& a, const Packet4i& b)
00171 {
00172   // after some bench, this version *is* faster than a scalar implementation
00173   Packet4i mask = _mm_cmplt_epi32(a,b);
00174   return _mm_or_si128(_mm_and_si128(mask,a),_mm_andnot_si128(mask,b));
00175 }
00176 
00177 template<> EIGEN_STRONG_INLINE Packet4f pmax<Packet4f>(const Packet4f& a, const Packet4f& b) { return _mm_max_ps(a,b); }
00178 template<> EIGEN_STRONG_INLINE Packet2d pmax<Packet2d>(const Packet2d& a, const Packet2d& b) { return _mm_max_pd(a,b); }
00179 template<> EIGEN_STRONG_INLINE Packet4i pmax<Packet4i>(const Packet4i& a, const Packet4i& b)
00180 {
00181   // after some bench, this version *is* faster than a scalar implementation
00182   Packet4i mask = _mm_cmpgt_epi32(a,b);
00183   return _mm_or_si128(_mm_and_si128(mask,a),_mm_andnot_si128(mask,b));
00184 }
00185 
00186 template<> EIGEN_STRONG_INLINE Packet4f pand<Packet4f>(const Packet4f& a, const Packet4f& b) { return _mm_and_ps(a,b); }
00187 template<> EIGEN_STRONG_INLINE Packet2d pand<Packet2d>(const Packet2d& a, const Packet2d& b) { return _mm_and_pd(a,b); }
00188 template<> EIGEN_STRONG_INLINE Packet4i pand<Packet4i>(const Packet4i& a, const Packet4i& b) { return _mm_and_si128(a,b); }
00189 
00190 template<> EIGEN_STRONG_INLINE Packet4f por<Packet4f>(const Packet4f& a, const Packet4f& b) { return _mm_or_ps(a,b); }
00191 template<> EIGEN_STRONG_INLINE Packet2d por<Packet2d>(const Packet2d& a, const Packet2d& b) { return _mm_or_pd(a,b); }
00192 template<> EIGEN_STRONG_INLINE Packet4i por<Packet4i>(const Packet4i& a, const Packet4i& b) { return _mm_or_si128(a,b); }
00193 
00194 template<> EIGEN_STRONG_INLINE Packet4f pxor<Packet4f>(const Packet4f& a, const Packet4f& b) { return _mm_xor_ps(a,b); }
00195 template<> EIGEN_STRONG_INLINE Packet2d pxor<Packet2d>(const Packet2d& a, const Packet2d& b) { return _mm_xor_pd(a,b); }
00196 template<> EIGEN_STRONG_INLINE Packet4i pxor<Packet4i>(const Packet4i& a, const Packet4i& b) { return _mm_xor_si128(a,b); }
00197 
00198 template<> EIGEN_STRONG_INLINE Packet4f pandnot<Packet4f>(const Packet4f& a, const Packet4f& b) { return _mm_andnot_ps(a,b); }
00199 template<> EIGEN_STRONG_INLINE Packet2d pandnot<Packet2d>(const Packet2d& a, const Packet2d& b) { return _mm_andnot_pd(a,b); }
00200 template<> EIGEN_STRONG_INLINE Packet4i pandnot<Packet4i>(const Packet4i& a, const Packet4i& b) { return _mm_andnot_si128(a,b); }
00201 
00202 template<> EIGEN_STRONG_INLINE Packet4f pload<Packet4f>(const float*   from) { EIGEN_DEBUG_ALIGNED_LOAD return _mm_load_ps(from); }
00203 template<> EIGEN_STRONG_INLINE Packet2d pload<Packet2d>(const double*  from) { EIGEN_DEBUG_ALIGNED_LOAD return _mm_load_pd(from); }
00204 template<> EIGEN_STRONG_INLINE Packet4i pload<Packet4i>(const int*     from) { EIGEN_DEBUG_ALIGNED_LOAD return _mm_load_si128(reinterpret_cast<const Packet4i*>(from)); }
00205 
00206 #if defined(_MSC_VER)
00207   template<> EIGEN_STRONG_INLINE Packet4f ploadu<Packet4f>(const float*  from) {
00208     EIGEN_DEBUG_UNALIGNED_LOAD
00209     #if (_MSC_VER==1600)
00210     // NOTE Some version of MSVC10 generates bad code when using _mm_loadu_ps
00211     // (i.e., it does not generate an unaligned load!!
00212     // TODO On most architectures this version should also be faster than a single _mm_loadu_ps
00213     // so we could also enable it for MSVC08 but first we have to make this later does not generate crap when doing so...
00214     __m128 res = _mm_loadl_pi(_mm_set1_ps(0.0f), (const __m64*)(from));
00215     res = _mm_loadh_pi(res, (const __m64*)(from+2));
00216     return res;
00217     #else
00218     return _mm_loadu_ps(from);
00219     #endif
00220   }
00221   template<> EIGEN_STRONG_INLINE Packet2d ploadu<Packet2d>(const double* from) { EIGEN_DEBUG_UNALIGNED_LOAD return _mm_loadu_pd(from); }
00222   template<> EIGEN_STRONG_INLINE Packet4i ploadu<Packet4i>(const int*    from) { EIGEN_DEBUG_UNALIGNED_LOAD return _mm_loadu_si128(reinterpret_cast<const Packet4i*>(from)); }
00223 #else
00224 // Fast unaligned loads. Note that here we cannot directly use intrinsics: this would
00225 // require pointer casting to incompatible pointer types and leads to invalid code
00226 // because of the strict aliasing rule. The "dummy" stuff are required to enforce
00227 // a correct instruction dependency.
00228 // TODO: do the same for MSVC (ICC is compatible)
00229 // NOTE: with the code below, MSVC's compiler crashes!
00230 
00231 #if defined(__GNUC__) && defined(__i386__)
00232   // bug 195: gcc/i386 emits weird x87 fldl/fstpl instructions for _mm_load_sd
00233   #define EIGEN_AVOID_CUSTOM_UNALIGNED_LOADS 1
00234 #elif defined(__clang__)
00235   // bug 201: Segfaults in __mm_loadh_pd with clang 2.8
00236   #define EIGEN_AVOID_CUSTOM_UNALIGNED_LOADS 1
00237 #else
00238   #define EIGEN_AVOID_CUSTOM_UNALIGNED_LOADS 0
00239 #endif
00240 
00241 template<> EIGEN_STRONG_INLINE Packet4f ploadu<Packet4f>(const float* from)
00242 {
00243   EIGEN_DEBUG_UNALIGNED_LOAD
00244 #if EIGEN_AVOID_CUSTOM_UNALIGNED_LOADS
00245   return _mm_loadu_ps(from);
00246 #else
00247   __m128d res;
00248   res =  _mm_load_sd((const double*)(from)) ;
00249   res =  _mm_loadh_pd(res, (const double*)(from+2)) ;
00250   return _mm_castpd_ps(res);
00251 #endif
00252 }
00253 template<> EIGEN_STRONG_INLINE Packet2d ploadu<Packet2d>(const double* from)
00254 {
00255   EIGEN_DEBUG_UNALIGNED_LOAD
00256 #if EIGEN_AVOID_CUSTOM_UNALIGNED_LOADS
00257   return _mm_loadu_pd(from);
00258 #else
00259   __m128d res;
00260   res = _mm_load_sd(from) ;
00261   res = _mm_loadh_pd(res,from+1);
00262   return res;
00263 #endif
00264 }
00265 template<> EIGEN_STRONG_INLINE Packet4i ploadu<Packet4i>(const int* from)
00266 {
00267   EIGEN_DEBUG_UNALIGNED_LOAD
00268 #if EIGEN_AVOID_CUSTOM_UNALIGNED_LOADS
00269   return _mm_loadu_si128(reinterpret_cast<const Packet4i*>(from));
00270 #else
00271   __m128d res;
00272   res =  _mm_load_sd((const double*)(from)) ;
00273   res =  _mm_loadh_pd(res, (const double*)(from+2)) ;
00274   return _mm_castpd_si128(res);
00275 #endif
00276 }
00277 #endif
00278 
00279 template<> EIGEN_STRONG_INLINE Packet4f ploaddup<Packet4f>(const float*   from)
00280 {
00281   return vec4f_swizzle1(_mm_castpd_ps(_mm_load_sd(reinterpret_cast<const double*>(from))), 0, 0, 1, 1);
00282 }
00283 template<> EIGEN_STRONG_INLINE Packet2d ploaddup<Packet2d>(const double*  from)
00284 { return pset1<Packet2d>(from[0]); }
00285 template<> EIGEN_STRONG_INLINE Packet4i ploaddup<Packet4i>(const int*     from)
00286 {
00287   Packet4i tmp;
00288   tmp = _mm_loadl_epi64(reinterpret_cast<const Packet4i*>(from));
00289   return vec4i_swizzle1(tmp, 0, 0, 1, 1);
00290 }
00291 
00292 template<> EIGEN_STRONG_INLINE void pstore<float>(float*   to, const Packet4f& from) { EIGEN_DEBUG_ALIGNED_STORE _mm_store_ps(to, from); }
00293 template<> EIGEN_STRONG_INLINE void pstore<double>(double* to, const Packet2d& from) { EIGEN_DEBUG_ALIGNED_STORE _mm_store_pd(to, from); }
00294 template<> EIGEN_STRONG_INLINE void pstore<int>(int*       to, const Packet4i& from) { EIGEN_DEBUG_ALIGNED_STORE _mm_store_si128(reinterpret_cast<Packet4i*>(to), from); }
00295 
00296 template<> EIGEN_STRONG_INLINE void pstoreu<double>(double* to, const Packet2d& from) {
00297   EIGEN_DEBUG_UNALIGNED_STORE
00298   _mm_storel_pd((to), from);
00299   _mm_storeh_pd((to+1), from);
00300 }
00301 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)); }
00302 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)); }
00303 
00304 // some compilers might be tempted to perform multiple moves instead of using a vector path.
00305 template<> EIGEN_STRONG_INLINE void pstore1<Packet4f>(float* to, const float& a)
00306 {
00307   Packet4f pa = _mm_set_ss(a);
00308   pstore(to, vec4f_swizzle1(pa,0,0,0,0));
00309 }
00310 // some compilers might be tempted to perform multiple moves instead of using a vector path.
00311 template<> EIGEN_STRONG_INLINE void pstore1<Packet2d>(double* to, const double& a)
00312 {
00313   Packet2d pa = _mm_set_sd(a);
00314   pstore(to, vec2d_swizzle1(pa,0,0));
00315 }
00316 
00317 template<> EIGEN_STRONG_INLINE void prefetch<float>(const float*   addr) { _mm_prefetch((const char*)(addr), _MM_HINT_T0); }
00318 template<> EIGEN_STRONG_INLINE void prefetch<double>(const double* addr) { _mm_prefetch((const char*)(addr), _MM_HINT_T0); }
00319 template<> EIGEN_STRONG_INLINE void prefetch<int>(const int*       addr) { _mm_prefetch((const char*)(addr), _MM_HINT_T0); }
00320 
00321 #if defined(_MSC_VER) && defined(_WIN64) && !defined(__INTEL_COMPILER)
00322 // The temporary variable fixes an internal compilation error in vs <= 2008 and a wrong-result bug in vs 2010
00323 // Direct of the struct members fixed bug #62.
00324 template<> EIGEN_STRONG_INLINE float  pfirst<Packet4f>(const Packet4f& a) { return a.m128_f32[0]; }
00325 template<> EIGEN_STRONG_INLINE double pfirst<Packet2d>(const Packet2d& a) { return a.m128d_f64[0]; }
00326 template<> EIGEN_STRONG_INLINE int    pfirst<Packet4i>(const Packet4i& a) { int x = _mm_cvtsi128_si32(a); return x; }
00327 #elif defined(_MSC_VER) && !defined(__INTEL_COMPILER)
00328 // The temporary variable fixes an internal compilation error in vs <= 2008 and a wrong-result bug in vs 2010
00329 template<> EIGEN_STRONG_INLINE float  pfirst<Packet4f>(const Packet4f& a) { float x = _mm_cvtss_f32(a); return x; }
00330 template<> EIGEN_STRONG_INLINE double pfirst<Packet2d>(const Packet2d& a) { double x = _mm_cvtsd_f64(a); return x; }
00331 template<> EIGEN_STRONG_INLINE int    pfirst<Packet4i>(const Packet4i& a) { int x = _mm_cvtsi128_si32(a); return x; }
00332 #else
00333 template<> EIGEN_STRONG_INLINE float  pfirst<Packet4f>(const Packet4f& a) { return _mm_cvtss_f32(a); }
00334 template<> EIGEN_STRONG_INLINE double pfirst<Packet2d>(const Packet2d& a) { return _mm_cvtsd_f64(a); }
00335 template<> EIGEN_STRONG_INLINE int    pfirst<Packet4i>(const Packet4i& a) { return _mm_cvtsi128_si32(a); }
00336 #endif
00337 
00338 template<> EIGEN_STRONG_INLINE Packet4f preverse(const Packet4f& a)
00339 { return _mm_shuffle_ps(a,a,0x1B); }
00340 template<> EIGEN_STRONG_INLINE Packet2d preverse(const Packet2d& a)
00341 { return _mm_shuffle_pd(a,a,0x1); }
00342 template<> EIGEN_STRONG_INLINE Packet4i preverse(const Packet4i& a)
00343 { return _mm_shuffle_epi32(a,0x1B); }
00344 
00345 
00346 template<> EIGEN_STRONG_INLINE Packet4f pabs(const Packet4f& a)
00347 {
00348   const Packet4f mask = _mm_castsi128_ps(_mm_setr_epi32(0x7FFFFFFF,0x7FFFFFFF,0x7FFFFFFF,0x7FFFFFFF));
00349   return _mm_and_ps(a,mask);
00350 }
00351 template<> EIGEN_STRONG_INLINE Packet2d pabs(const Packet2d& a)
00352 {
00353   const Packet2d mask = _mm_castsi128_pd(_mm_setr_epi32(0xFFFFFFFF,0x7FFFFFFF,0xFFFFFFFF,0x7FFFFFFF));
00354   return _mm_and_pd(a,mask);
00355 }
00356 template<> EIGEN_STRONG_INLINE Packet4i pabs(const Packet4i& a)
00357 {
00358   #ifdef EIGEN_VECTORIZE_SSSE3
00359   return _mm_abs_epi32(a);
00360   #else
00361   Packet4i aux = _mm_srai_epi32(a,31);
00362   return _mm_sub_epi32(_mm_xor_si128(a,aux),aux);
00363   #endif
00364 }
00365 
00366 EIGEN_STRONG_INLINE void punpackp(Packet4f* vecs)
00367 {
00368   vecs[1] = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(vecs[0]), 0x55));
00369   vecs[2] = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(vecs[0]), 0xAA));
00370   vecs[3] = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(vecs[0]), 0xFF));
00371   vecs[0] = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(vecs[0]), 0x00));
00372 }
00373 
00374 #ifdef EIGEN_VECTORIZE_SSE3
00375 // TODO implement SSE2 versions as well as integer versions
00376 template<> EIGEN_STRONG_INLINE Packet4f preduxp<Packet4f>(const Packet4f* vecs)
00377 {
00378   return _mm_hadd_ps(_mm_hadd_ps(vecs[0], vecs[1]),_mm_hadd_ps(vecs[2], vecs[3]));
00379 }
00380 template<> EIGEN_STRONG_INLINE Packet2d preduxp<Packet2d>(const Packet2d* vecs)
00381 {
00382   return _mm_hadd_pd(vecs[0], vecs[1]);
00383 }
00384 // SSSE3 version:
00385 // EIGEN_STRONG_INLINE Packet4i preduxp(const Packet4i* vecs)
00386 // {
00387 //   return _mm_hadd_epi32(_mm_hadd_epi32(vecs[0], vecs[1]),_mm_hadd_epi32(vecs[2], vecs[3]));
00388 // }
00389 
00390 template<> EIGEN_STRONG_INLINE float predux<Packet4f>(const Packet4f& a)
00391 {
00392   Packet4f tmp0 = _mm_hadd_ps(a,a);
00393   return pfirst(_mm_hadd_ps(tmp0, tmp0));
00394 }
00395 
00396 template<> EIGEN_STRONG_INLINE double predux<Packet2d>(const Packet2d& a) { return pfirst(_mm_hadd_pd(a, a)); }
00397 
00398 // SSSE3 version:
00399 // EIGEN_STRONG_INLINE float predux(const Packet4i& a)
00400 // {
00401 //   Packet4i tmp0 = _mm_hadd_epi32(a,a);
00402 //   return pfirst(_mm_hadd_epi32(tmp0, tmp0));
00403 // }
00404 #else
00405 // SSE2 versions
00406 template<> EIGEN_STRONG_INLINE float predux<Packet4f>(const Packet4f& a)
00407 {
00408   Packet4f tmp = _mm_add_ps(a, _mm_movehl_ps(a,a));
00409   return pfirst(_mm_add_ss(tmp, _mm_shuffle_ps(tmp,tmp, 1)));
00410 }
00411 template<> EIGEN_STRONG_INLINE double predux<Packet2d>(const Packet2d& a)
00412 {
00413   return pfirst(_mm_add_sd(a, _mm_unpackhi_pd(a,a)));
00414 }
00415 
00416 template<> EIGEN_STRONG_INLINE Packet4f preduxp<Packet4f>(const Packet4f* vecs)
00417 {
00418   Packet4f tmp0, tmp1, tmp2;
00419   tmp0 = _mm_unpacklo_ps(vecs[0], vecs[1]);
00420   tmp1 = _mm_unpackhi_ps(vecs[0], vecs[1]);
00421   tmp2 = _mm_unpackhi_ps(vecs[2], vecs[3]);
00422   tmp0 = _mm_add_ps(tmp0, tmp1);
00423   tmp1 = _mm_unpacklo_ps(vecs[2], vecs[3]);
00424   tmp1 = _mm_add_ps(tmp1, tmp2);
00425   tmp2 = _mm_movehl_ps(tmp1, tmp0);
00426   tmp0 = _mm_movelh_ps(tmp0, tmp1);
00427   return _mm_add_ps(tmp0, tmp2);
00428 }
00429 
00430 template<> EIGEN_STRONG_INLINE Packet2d preduxp<Packet2d>(const Packet2d* vecs)
00431 {
00432   return _mm_add_pd(_mm_unpacklo_pd(vecs[0], vecs[1]), _mm_unpackhi_pd(vecs[0], vecs[1]));
00433 }
00434 #endif  // SSE3
00435 
00436 template<> EIGEN_STRONG_INLINE int predux<Packet4i>(const Packet4i& a)
00437 {
00438   Packet4i tmp = _mm_add_epi32(a, _mm_unpackhi_epi64(a,a));
00439   return pfirst(tmp) + pfirst(_mm_shuffle_epi32(tmp, 1));
00440 }
00441 
00442 template<> EIGEN_STRONG_INLINE Packet4i preduxp<Packet4i>(const Packet4i* vecs)
00443 {
00444   Packet4i tmp0, tmp1, tmp2;
00445   tmp0 = _mm_unpacklo_epi32(vecs[0], vecs[1]);
00446   tmp1 = _mm_unpackhi_epi32(vecs[0], vecs[1]);
00447   tmp2 = _mm_unpackhi_epi32(vecs[2], vecs[3]);
00448   tmp0 = _mm_add_epi32(tmp0, tmp1);
00449   tmp1 = _mm_unpacklo_epi32(vecs[2], vecs[3]);
00450   tmp1 = _mm_add_epi32(tmp1, tmp2);
00451   tmp2 = _mm_unpacklo_epi64(tmp0, tmp1);
00452   tmp0 = _mm_unpackhi_epi64(tmp0, tmp1);
00453   return _mm_add_epi32(tmp0, tmp2);
00454 }
00455 
00456 // Other reduction functions:
00457 
00458 // mul
00459 template<> EIGEN_STRONG_INLINE float predux_mul<Packet4f>(const Packet4f& a)
00460 {
00461   Packet4f tmp = _mm_mul_ps(a, _mm_movehl_ps(a,a));
00462   return pfirst(_mm_mul_ss(tmp, _mm_shuffle_ps(tmp,tmp, 1)));
00463 }
00464 template<> EIGEN_STRONG_INLINE double predux_mul<Packet2d>(const Packet2d& a)
00465 {
00466   return pfirst(_mm_mul_sd(a, _mm_unpackhi_pd(a,a)));
00467 }
00468 template<> EIGEN_STRONG_INLINE int predux_mul<Packet4i>(const Packet4i& a)
00469 {
00470   // after some experiments, it is seems this is the fastest way to implement it
00471   // for GCC (eg., reusing pmul is very slow !)
00472   // TODO try to call _mm_mul_epu32 directly
00473   EIGEN_ALIGN16 int aux[4];
00474   pstore(aux, a);
00475   return  (aux[0] * aux[1]) * (aux[2] * aux[3]);;
00476 }
00477 
00478 // min
00479 template<> EIGEN_STRONG_INLINE float predux_min<Packet4f>(const Packet4f& a)
00480 {
00481   Packet4f tmp = _mm_min_ps(a, _mm_movehl_ps(a,a));
00482   return pfirst(_mm_min_ss(tmp, _mm_shuffle_ps(tmp,tmp, 1)));
00483 }
00484 template<> EIGEN_STRONG_INLINE double predux_min<Packet2d>(const Packet2d& a)
00485 {
00486   return pfirst(_mm_min_sd(a, _mm_unpackhi_pd(a,a)));
00487 }
00488 template<> EIGEN_STRONG_INLINE int predux_min<Packet4i>(const Packet4i& a)
00489 {
00490   // after some experiments, it is seems this is the fastest way to implement it
00491   // for GCC (eg., it does not like using std::min after the pstore !!)
00492   EIGEN_ALIGN16 int aux[4];
00493   pstore(aux, a);
00494   register int aux0 = aux[0]<aux[1] ? aux[0] : aux[1];
00495   register int aux2 = aux[2]<aux[3] ? aux[2] : aux[3];
00496   return aux0<aux2 ? aux0 : aux2;
00497 }
00498 
00499 // max
00500 template<> EIGEN_STRONG_INLINE float predux_max<Packet4f>(const Packet4f& a)
00501 {
00502   Packet4f tmp = _mm_max_ps(a, _mm_movehl_ps(a,a));
00503   return pfirst(_mm_max_ss(tmp, _mm_shuffle_ps(tmp,tmp, 1)));
00504 }
00505 template<> EIGEN_STRONG_INLINE double predux_max<Packet2d>(const Packet2d& a)
00506 {
00507   return pfirst(_mm_max_sd(a, _mm_unpackhi_pd(a,a)));
00508 }
00509 template<> EIGEN_STRONG_INLINE int predux_max<Packet4i>(const Packet4i& a)
00510 {
00511   // after some experiments, it is seems this is the fastest way to implement it
00512   // for GCC (eg., it does not like using std::min after the pstore !!)
00513   EIGEN_ALIGN16 int aux[4];
00514   pstore(aux, a);
00515   register int aux0 = aux[0]>aux[1] ? aux[0] : aux[1];
00516   register int aux2 = aux[2]>aux[3] ? aux[2] : aux[3];
00517   return aux0>aux2 ? aux0 : aux2;
00518 }
00519 
00520 #if (defined __GNUC__)
00521 // template <> EIGEN_STRONG_INLINE Packet4f pmadd(const Packet4f&  a, const Packet4f&  b, const Packet4f&  c)
00522 // {
00523 //   Packet4f res = b;
00524 //   asm("mulps %[a], %[b] \n\taddps %[c], %[b]" : [b] "+x" (res) : [a] "x" (a), [c] "x" (c));
00525 //   return res;
00526 // }
00527 // EIGEN_STRONG_INLINE Packet4i _mm_alignr_epi8(const Packet4i&  a, const Packet4i&  b, const int i)
00528 // {
00529 //   Packet4i res = a;
00530 //   asm("palignr %[i], %[a], %[b] " : [b] "+x" (res) : [a] "x" (a), [i] "i" (i));
00531 //   return res;
00532 // }
00533 #endif
00534 
00535 #ifdef EIGEN_VECTORIZE_SSSE3
00536 // SSSE3 versions
00537 template<int Offset>
00538 struct palign_impl<Offset,Packet4f>
00539 {
00540   static EIGEN_STRONG_INLINE void run(Packet4f& first, const Packet4f& second)
00541   {
00542     if (Offset!=0)
00543       first = _mm_castsi128_ps(_mm_alignr_epi8(_mm_castps_si128(second), _mm_castps_si128(first), Offset*4));
00544   }
00545 };
00546 
00547 template<int Offset>
00548 struct palign_impl<Offset,Packet4i>
00549 {
00550   static EIGEN_STRONG_INLINE void run(Packet4i& first, const Packet4i& second)
00551   {
00552     if (Offset!=0)
00553       first = _mm_alignr_epi8(second,first, Offset*4);
00554   }
00555 };
00556 
00557 template<int Offset>
00558 struct palign_impl<Offset,Packet2d>
00559 {
00560   static EIGEN_STRONG_INLINE void run(Packet2d& first, const Packet2d& second)
00561   {
00562     if (Offset==1)
00563       first = _mm_castsi128_pd(_mm_alignr_epi8(_mm_castpd_si128(second), _mm_castpd_si128(first), 8));
00564   }
00565 };
00566 #else
00567 // SSE2 versions
00568 template<int Offset>
00569 struct palign_impl<Offset,Packet4f>
00570 {
00571   static EIGEN_STRONG_INLINE void run(Packet4f& first, const Packet4f& second)
00572   {
00573     if (Offset==1)
00574     {
00575       first = _mm_move_ss(first,second);
00576       first = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(first),0x39));
00577     }
00578     else if (Offset==2)
00579     {
00580       first = _mm_movehl_ps(first,first);
00581       first = _mm_movelh_ps(first,second);
00582     }
00583     else if (Offset==3)
00584     {
00585       first = _mm_move_ss(first,second);
00586       first = _mm_shuffle_ps(first,second,0x93);
00587     }
00588   }
00589 };
00590 
00591 template<int Offset>
00592 struct palign_impl<Offset,Packet4i>
00593 {
00594   static EIGEN_STRONG_INLINE void run(Packet4i& first, const Packet4i& second)
00595   {
00596     if (Offset==1)
00597     {
00598       first = _mm_castps_si128(_mm_move_ss(_mm_castsi128_ps(first),_mm_castsi128_ps(second)));
00599       first = _mm_shuffle_epi32(first,0x39);
00600     }
00601     else if (Offset==2)
00602     {
00603       first = _mm_castps_si128(_mm_movehl_ps(_mm_castsi128_ps(first),_mm_castsi128_ps(first)));
00604       first = _mm_castps_si128(_mm_movelh_ps(_mm_castsi128_ps(first),_mm_castsi128_ps(second)));
00605     }
00606     else if (Offset==3)
00607     {
00608       first = _mm_castps_si128(_mm_move_ss(_mm_castsi128_ps(first),_mm_castsi128_ps(second)));
00609       first = _mm_castps_si128(_mm_shuffle_ps(_mm_castsi128_ps(first),_mm_castsi128_ps(second),0x93));
00610     }
00611   }
00612 };
00613 
00614 template<int Offset>
00615 struct palign_impl<Offset,Packet2d>
00616 {
00617   static EIGEN_STRONG_INLINE void run(Packet2d& first, const Packet2d& second)
00618   {
00619     if (Offset==1)
00620     {
00621       first = _mm_castps_pd(_mm_movehl_ps(_mm_castpd_ps(first),_mm_castpd_ps(first)));
00622       first = _mm_castps_pd(_mm_movelh_ps(_mm_castpd_ps(first),_mm_castpd_ps(second)));
00623     }
00624   }
00625 };
00626 #endif
00627 
00628 } // end namespace internal
00629 
00630 } // end namespace Eigen
00631 
00632 #endif // EIGEN_PACKET_MATH_SSE_H


win_eigen
Author(s): Daniel Stonier
autogenerated on Mon Oct 6 2014 12:25:22