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