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