10 #ifndef EIGEN_PACKET_MATH_SSE_H
11 #define EIGEN_PACKET_MATH_SSE_H
17 #ifndef EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD
18 #define EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD 8
21 #ifndef EIGEN_ARCH_DEFAULT_NUMBER_OF_REGISTERS
22 #define EIGEN_ARCH_DEFAULT_NUMBER_OF_REGISTERS (2*sizeof(void*))
26 #ifndef EIGEN_HAS_SINGLE_INSTRUCTION_MADD
27 #define EIGEN_HAS_SINGLE_INSTRUCTION_MADD 1
31 #if ((defined EIGEN_VECTORIZE_AVX) && (EIGEN_COMP_GNUC_STRICT || EIGEN_COMP_MINGW) && (__GXX_ABI_VERSION < 1004)) || EIGEN_OS_QNX
38 struct eigen_packet_wrapper
51 typedef eigen_packet_wrapper<__m128>
Packet4f;
52 typedef eigen_packet_wrapper<__m128i>
Packet4i;
53 typedef eigen_packet_wrapper<__m128d>
Packet2d;
64 #define vec4f_swizzle1(v,p,q,r,s) \
65 (_mm_castsi128_ps(_mm_shuffle_epi32( _mm_castps_si128(v), ((s)<<6|(r)<<4|(q)<<2|(p)))))
67 #define vec4i_swizzle1(v,p,q,r,s) \
68 (_mm_shuffle_epi32( v, ((s)<<6|(r)<<4|(q)<<2|(p))))
70 #define vec2d_swizzle1(v,p,q) \
71 (_mm_castsi128_pd(_mm_shuffle_epi32( _mm_castpd_si128(v), ((q*2+1)<<6|(q*2)<<4|(p*2+1)<<2|(p*2)))))
73 #define vec4f_swizzle2(a,b,p,q,r,s) \
74 (_mm_shuffle_ps( (a), (b), ((s)<<6|(r)<<4|(q)<<2|(p))))
76 #define vec4i_swizzle2(a,b,p,q,r,s) \
77 (_mm_castps_si128( (_mm_shuffle_ps( _mm_castsi128_ps(a), _mm_castsi128_ps(b), ((s)<<6|(r)<<4|(q)<<2|(p))))))
79 #define _EIGEN_DECLARE_CONST_Packet4f(NAME,X) \
80 const Packet4f p4f_##NAME = pset1<Packet4f>(X)
82 #define _EIGEN_DECLARE_CONST_Packet2d(NAME,X) \
83 const Packet2d p2d_##NAME = pset1<Packet2d>(X)
85 #define _EIGEN_DECLARE_CONST_Packet4f_FROM_INT(NAME,X) \
86 const Packet4f p4f_##NAME = _mm_castsi128_ps(pset1<Packet4i>(X))
88 #define _EIGEN_DECLARE_CONST_Packet4i(NAME,X) \
89 const Packet4i p4i_##NAME = pset1<Packet4i>(X)
94 #ifndef EIGEN_VECTORIZE_AVX
95 template<>
struct packet_traits<float> : default_packet_traits
115 #ifdef EIGEN_VECTORIZE_SSE4_1
123 template<>
struct packet_traits<double> : default_packet_traits
139 #ifdef EIGEN_VECTORIZE_SSE4_1
148 template<>
struct packet_traits<
int> : default_packet_traits
165 #ifndef EIGEN_VECTORIZE_AVX
167 template<>
struct scalar_div_cost<double,true> {
enum {
value = 8 }; };
170 #if EIGEN_COMP_MSVC==1500
188 #if EIGEN_COMP_GNUC_STRICT && (!defined __AVX__)
208 const Packet4f mask = _mm_castsi128_ps(_mm_setr_epi32(0x80000000,0x80000000,0x80000000,0x80000000));
209 return _mm_xor_ps(
a,mask);
213 const Packet2d mask = _mm_castsi128_pd(_mm_setr_epi32(0x0,0x80000000,0x0,0x80000000));
214 return _mm_xor_pd(
a,mask);
229 #ifdef EIGEN_VECTORIZE_SSE4_1
230 return _mm_mullo_epi32(
a,
b);
257 #ifdef EIGEN_VECTORIZE_SSE4_1
258 return _mm_min_epi32(
a,
b);
262 return _mm_or_si128(_mm_and_si128(mask,
a),_mm_andnot_si128(mask,
b));
270 #ifdef EIGEN_VECTORIZE_SSE4_1
271 return _mm_max_epi32(
a,
b);
275 return _mm_or_si128(_mm_and_si128(mask,
a),_mm_andnot_si128(mask,
b));
279 #ifdef EIGEN_VECTORIZE_SSE4_1
313 #if (EIGEN_COMP_MSVC==1600)
316 __m128 res = _mm_loadl_pi(_mm_set1_ps(0.0f), (
const __m64*)(from));
317 res = _mm_loadh_pi(res, (
const __m64*)(from+2));
320 return _mm_loadu_ps(from);
329 return _mm_loadu_ps(from);
336 return _mm_loadu_pd(from);
341 return _mm_loadu_si128(
reinterpret_cast<const __m128i*
>(from));
347 return vec4f_swizzle1(_mm_castpd_ps(_mm_load_sd(
reinterpret_cast<const double*
>(from))), 0, 0, 1, 1);
354 tmp = _mm_loadl_epi64(
reinterpret_cast<const __m128i*
>(from));
368 return _mm_set_ps(from[3*stride], from[2*stride], from[1*stride], from[0*stride]);
372 return _mm_set_pd(from[1*stride], from[0*stride]);
376 return _mm_set_epi32(from[3*stride], from[2*stride], from[1*stride], from[0*stride]);
381 to[stride*0] = _mm_cvtss_f32(from);
382 to[stride*1] = _mm_cvtss_f32(_mm_shuffle_ps(from, from, 1));
383 to[stride*2] = _mm_cvtss_f32(_mm_shuffle_ps(from, from, 2));
384 to[stride*3] = _mm_cvtss_f32(_mm_shuffle_ps(from, from, 3));
388 to[stride*0] = _mm_cvtsd_f64(from);
389 to[stride*1] = _mm_cvtsd_f64(_mm_shuffle_pd(from, from, 1));
393 to[stride*0] = _mm_cvtsi128_si32(from);
394 to[stride*1] = _mm_cvtsi128_si32(_mm_shuffle_epi32(from, 1));
395 to[stride*2] = _mm_cvtsi128_si32(_mm_shuffle_epi32(from, 2));
396 to[stride*3] = _mm_cvtsi128_si32(_mm_shuffle_epi32(from, 3));
418 #ifndef EIGEN_VECTORIZE_AVX
424 #if EIGEN_COMP_MSVC_STRICT && EIGEN_OS_WIN64
430 #elif EIGEN_COMP_MSVC_STRICT
442 {
return _mm_shuffle_ps(
a,
a,0x1B); }
444 {
return _mm_shuffle_pd(
a,
a,0x1); }
446 {
return _mm_shuffle_epi32(
a,0x1B); }
450 const Packet4f mask = _mm_castsi128_ps(_mm_setr_epi32(0x7FFFFFFF,0x7FFFFFFF,0x7FFFFFFF,0x7FFFFFFF));
451 return _mm_and_ps(
a,mask);
455 const Packet2d mask = _mm_castsi128_pd(_mm_setr_epi32(0xFFFFFFFF,0x7FFFFFFF,0xFFFFFFFF,0x7FFFFFFF));
456 return _mm_and_pd(
a,mask);
460 #ifdef EIGEN_VECTORIZE_SSSE3
461 return _mm_abs_epi32(
a);
464 return _mm_sub_epi32(_mm_xor_si128(
a,aux),aux);
484 #ifdef EIGEN_VECTORIZE_SSE3
485 a0 = _mm_loaddup_pd(
a+0);
486 a1 = _mm_loaddup_pd(
a+1);
487 a2 = _mm_loaddup_pd(
a+2);
488 a3 = _mm_loaddup_pd(
a+3);
502 vecs[1] = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(vecs[0]), 0x55));
503 vecs[2] = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(vecs[0]), 0xAA));
504 vecs[3] = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(vecs[0]), 0xFF));
505 vecs[0] = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(vecs[0]), 0x00));
508 #ifdef EIGEN_VECTORIZE_SSE3
511 return _mm_hadd_ps(_mm_hadd_ps(vecs[0], vecs[1]),_mm_hadd_ps(vecs[2], vecs[3]));
516 return _mm_hadd_pd(vecs[0], vecs[1]);
523 tmp0 = _mm_unpacklo_ps(vecs[0], vecs[1]);
524 tmp1 = _mm_unpackhi_ps(vecs[0], vecs[1]);
525 tmp2 = _mm_unpackhi_ps(vecs[2], vecs[3]);
526 tmp0 = _mm_add_ps(tmp0, tmp1);
527 tmp1 = _mm_unpacklo_ps(vecs[2], vecs[3]);
528 tmp1 = _mm_add_ps(tmp1, tmp2);
529 tmp2 = _mm_movehl_ps(tmp1, tmp0);
530 tmp0 = _mm_movelh_ps(tmp0, tmp1);
531 return _mm_add_ps(tmp0, tmp2);
536 return _mm_add_pd(_mm_unpacklo_pd(vecs[0], vecs[1]), _mm_unpackhi_pd(vecs[0], vecs[1]));
564 #ifdef EIGEN_VECTORIZE_SSSE3
567 return _mm_hadd_epi32(_mm_hadd_epi32(vecs[0], vecs[1]),_mm_hadd_epi32(vecs[2], vecs[3]));
577 Packet4i tmp = _mm_add_epi32(
a, _mm_unpackhi_epi64(
a,
a));
584 tmp0 = _mm_unpacklo_epi32(vecs[0], vecs[1]);
585 tmp1 = _mm_unpackhi_epi32(vecs[0], vecs[1]);
586 tmp2 = _mm_unpackhi_epi32(vecs[2], vecs[3]);
587 tmp0 = _mm_add_epi32(tmp0, tmp1);
588 tmp1 = _mm_unpacklo_epi32(vecs[2], vecs[3]);
589 tmp1 = _mm_add_epi32(tmp1, tmp2);
590 tmp2 = _mm_unpacklo_epi64(tmp0, tmp1);
591 tmp0 = _mm_unpackhi_epi64(tmp0, tmp1);
592 return _mm_add_epi32(tmp0, tmp2);
614 return (aux[0] * aux[1]) * (aux[2] * aux[3]);;
629 #ifdef EIGEN_VECTORIZE_SSE4_1
630 Packet4i tmp = _mm_min_epi32(
a, _mm_shuffle_epi32(
a, _MM_SHUFFLE(0,0,3,2)));
637 int aux0 = aux[0]<aux[1] ? aux[0] : aux[1];
638 int aux2 = aux[2]<aux[3] ? aux[2] : aux[3];
639 return aux0<aux2 ? aux0 : aux2;
640 #endif // EIGEN_VECTORIZE_SSE4_1
655 #ifdef EIGEN_VECTORIZE_SSE4_1
656 Packet4i tmp = _mm_max_epi32(
a, _mm_shuffle_epi32(
a, _MM_SHUFFLE(0,0,3,2)));
663 int aux0 = aux[0]>aux[1] ? aux[0] : aux[1];
664 int aux2 = aux[2]>aux[3] ? aux[2] : aux[3];
665 return aux0>aux2 ? aux0 : aux2;
666 #endif // EIGEN_VECTORIZE_SSE4_1
684 #ifdef EIGEN_VECTORIZE_SSSE3
692 first = _mm_castsi128_ps(_mm_alignr_epi8(_mm_castps_si128(second), _mm_castps_si128(first), Offset*4));
702 first = _mm_alignr_epi8(second,first, Offset*4);
712 first = _mm_castsi128_pd(_mm_alignr_epi8(_mm_castpd_si128(second), _mm_castpd_si128(first), 8));
724 first = _mm_move_ss(first,second);
725 first = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(first),0x39));
729 first = _mm_movehl_ps(first,first);
730 first = _mm_movelh_ps(first,second);
734 first = _mm_move_ss(first,second);
735 first = _mm_shuffle_ps(first,second,0x93);
747 first = _mm_castps_si128(_mm_move_ss(_mm_castsi128_ps(first),_mm_castsi128_ps(second)));
748 first = _mm_shuffle_epi32(first,0x39);
752 first = _mm_castps_si128(_mm_movehl_ps(_mm_castsi128_ps(first),_mm_castsi128_ps(first)));
753 first = _mm_castps_si128(_mm_movelh_ps(_mm_castsi128_ps(first),_mm_castsi128_ps(second)));
757 first = _mm_castps_si128(_mm_move_ss(_mm_castsi128_ps(first),_mm_castsi128_ps(second)));
758 first = _mm_castps_si128(_mm_shuffle_ps(_mm_castsi128_ps(first),_mm_castsi128_ps(second),0x93));
770 first = _mm_castps_pd(_mm_movehl_ps(_mm_castpd_ps(first),_mm_castpd_ps(first)));
771 first = _mm_castps_pd(_mm_movelh_ps(_mm_castpd_ps(first),_mm_castpd_ps(second)));
777 EIGEN_DEVICE_FUNC
inline void
779 _MM_TRANSPOSE4_PS(kernel.packet[0], kernel.packet[1], kernel.packet[2], kernel.packet[3]);
782 EIGEN_DEVICE_FUNC
inline void
784 __m128d tmp = _mm_unpackhi_pd(kernel.
packet[0], kernel.
packet[1]);
789 EIGEN_DEVICE_FUNC
inline void
791 __m128i T0 = _mm_unpacklo_epi32(kernel.packet[0], kernel.packet[1]);
792 __m128i T1 = _mm_unpacklo_epi32(kernel.packet[2], kernel.packet[3]);
793 __m128i T2 = _mm_unpackhi_epi32(kernel.packet[0], kernel.packet[1]);
794 __m128i T3 = _mm_unpackhi_epi32(kernel.packet[2], kernel.packet[3]);
796 kernel.packet[0] = _mm_unpacklo_epi64(T0, T1);
797 kernel.packet[1] = _mm_unpackhi_epi64(T0, T1);
798 kernel.packet[2] = _mm_unpacklo_epi64(T2, T3);
799 kernel.packet[3] = _mm_unpackhi_epi64(T2, T3);
803 const __m128i zero = _mm_setzero_si128();
804 const __m128i select = _mm_set_epi32(ifPacket.select[3], ifPacket.select[2], ifPacket.select[1], ifPacket.select[0]);
805 __m128i false_mask = _mm_cmpeq_epi32(select, zero);
806 #ifdef EIGEN_VECTORIZE_SSE4_1
807 return _mm_blendv_epi8(thenPacket, elsePacket, false_mask);
809 return _mm_or_si128(_mm_andnot_si128(false_mask, thenPacket), _mm_and_si128(false_mask, elsePacket));
813 const __m128 zero = _mm_setzero_ps();
814 const __m128 select = _mm_set_ps(ifPacket.select[3], ifPacket.select[2], ifPacket.select[1], ifPacket.select[0]);
815 __m128 false_mask = _mm_cmpeq_ps(select, zero);
816 #ifdef EIGEN_VECTORIZE_SSE4_1
817 return _mm_blendv_ps(thenPacket, elsePacket, false_mask);
819 return _mm_or_ps(_mm_andnot_ps(false_mask, thenPacket), _mm_and_ps(false_mask, elsePacket));
823 const __m128d zero = _mm_setzero_pd();
824 const __m128d select = _mm_set_pd(ifPacket.
select[1], ifPacket.
select[0]);
825 __m128d false_mask = _mm_cmpeq_pd(select, zero);
826 #ifdef EIGEN_VECTORIZE_SSE4_1
827 return _mm_blendv_pd(thenPacket, elsePacket, false_mask);
829 return _mm_or_pd(_mm_andnot_pd(false_mask, thenPacket), _mm_and_pd(false_mask, elsePacket));
835 #ifdef EIGEN_VECTORIZE_SSE4_1
838 return _mm_move_ss(
a, _mm_load_ss(&
b));
844 #ifdef EIGEN_VECTORIZE_SSE4_1
847 return _mm_move_sd(
a, _mm_load_sd(&
b));
853 #ifdef EIGEN_VECTORIZE_SSE4_1
856 const Packet4f mask = _mm_castsi128_ps(_mm_setr_epi32(0x0,0x0,0x0,0xFFFFFFFF));
863 #ifdef EIGEN_VECTORIZE_SSE4_1
866 const Packet2d mask = _mm_castsi128_pd(_mm_setr_epi32(0x0,0x0,0xFFFFFFFF,0xFFFFFFFF));
874 return ::fmaf(
a,
b,
c);
887 static inline __m128 _mm_castpd_ps (__m128d
x) {
return reinterpret_cast<__m128&
>(
x); }
888 static inline __m128i _mm_castpd_si128(__m128d
x) {
return reinterpret_cast<__m128i&
>(
x); }
889 static inline __m128d _mm_castps_pd (__m128
x) {
return reinterpret_cast<__m128d&
>(
x); }
890 static inline __m128i _mm_castps_si128(__m128
x) {
return reinterpret_cast<__m128i&
>(
x); }
891 static inline __m128 _mm_castsi128_ps(__m128i
x) {
return reinterpret_cast<__m128&
>(
x); }
892 static inline __m128d _mm_castsi128_pd(__m128i
x) {
return reinterpret_cast<__m128d&
>(
x); }
895 #endif // EIGEN_PACKET_MATH_SSE_H