Eigen  3.4.90 (git rev 5a9f66fb35d03a4da9ef8976e67a61b30aa16dcf)
 
Loading...
Searching...
No Matches
SSE/PacketMath.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008-2009 Gael Guennebaud <[email protected]>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10#ifndef EIGEN_PACKET_MATH_SSE_H
11#define EIGEN_PACKET_MATH_SSE_H
12
13#include <cstdint>
14// IWYU pragma: private
15#include "../../InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20
21#ifndef EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD
22#define EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD 8
23#endif
24
25#if !defined(EIGEN_VECTORIZE_AVX) && !defined(EIGEN_ARCH_DEFAULT_NUMBER_OF_REGISTERS)
26// 32 bits => 8 registers
27// 64 bits => 16 registers
28#define EIGEN_ARCH_DEFAULT_NUMBER_OF_REGISTERS (2 * sizeof(void*))
29#endif
30
31#ifdef EIGEN_VECTORIZE_FMA
32#ifndef EIGEN_HAS_SINGLE_INSTRUCTION_MADD
33#define EIGEN_HAS_SINGLE_INSTRUCTION_MADD
34#endif
35#endif
36
37#if ((defined EIGEN_VECTORIZE_AVX) && (EIGEN_COMP_GNUC_STRICT || EIGEN_COMP_MINGW || EIGEN_COMP_LCC) && \
38 (__GXX_ABI_VERSION < 1004)) || \
39 EIGEN_OS_QNX
40// With GCC's default ABI version, a __m128 or __m256 are the same types and therefore we cannot
41// have overloads for both types without linking error.
42// One solution is to increase ABI version using -fabi-version=4 (or greater).
43// Otherwise, we workaround this inconvenience by wrapping 128bit types into the following helper
44// structure:
45typedef eigen_packet_wrapper<__m128> Packet4f;
46typedef eigen_packet_wrapper<__m128d> Packet2d;
47#else
48typedef __m128 Packet4f;
49typedef __m128d Packet2d;
50#endif
51
52typedef eigen_packet_wrapper<__m128i, 0> Packet4i;
53typedef eigen_packet_wrapper<__m128i, 1> Packet16b;
54typedef eigen_packet_wrapper<__m128i, 4> Packet4ui;
55typedef eigen_packet_wrapper<__m128i, 5> Packet2l;
56
57template <>
58struct is_arithmetic<__m128> {
59 enum { value = true };
60};
61template <>
62struct is_arithmetic<__m128i> {
63 enum { value = true };
64};
65template <>
66struct is_arithmetic<__m128d> {
67 enum { value = true };
68};
69template <>
70struct is_arithmetic<Packet4i> {
71 enum { value = true };
72};
73template <>
74struct is_arithmetic<Packet2l> {
75 enum { value = true };
76};
77// Note that `Packet4ui` uses the underlying type `__m128i`, which is
78// interpreted as a vector of _signed_ `int32`s, which breaks some arithmetic
79// operations used in `GenericPacketMath.h`.
80template <>
81struct is_arithmetic<Packet4ui> {
82 enum { value = false };
83};
84template <>
85struct is_arithmetic<Packet16b> {
86 enum { value = true };
87};
88
89template <int p, int q, int r, int s>
90struct shuffle_mask {
91 enum { mask = (s) << 6 | (r) << 4 | (q) << 2 | (p) };
92};
93
94// TODO: change the implementation of all swizzle* ops from macro to template,
95#define vec4f_swizzle1(v, p, q, r, s) \
96 Packet4f(_mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(v), (shuffle_mask<p, q, r, s>::mask))))
97
98#define vec4i_swizzle1(v, p, q, r, s) Packet4i(_mm_shuffle_epi32(v, (shuffle_mask<p, q, r, s>::mask)))
99
100#define vec4ui_swizzle1(v, p, q, r, s) Packet4ui(vec4i_swizzle1(v, p, q, r, s))
101
102#define vec2d_swizzle1(v, p, q) \
103 Packet2d(_mm_castsi128_pd( \
104 _mm_shuffle_epi32(_mm_castpd_si128(v), (shuffle_mask<2 * p, 2 * p + 1, 2 * q, 2 * q + 1>::mask))))
105
106#define vec4f_swizzle2(a, b, p, q, r, s) Packet4f(_mm_shuffle_ps((a), (b), (shuffle_mask<p, q, r, s>::mask)))
107
108#define vec4i_swizzle2(a, b, p, q, r, s) \
109 Packet4i( \
110 _mm_castps_si128((_mm_shuffle_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b), (shuffle_mask<p, q, r, s>::mask)))))
111
112#define vec4ui_swizzle2(a, b, p, q, r, s) Packet4i(vec4i_swizzle2(a, b, p, q, r, s))
113
114EIGEN_STRONG_INLINE Packet4f vec4f_movelh(const Packet4f& a, const Packet4f& b) {
115 return Packet4f(_mm_movelh_ps(a, b));
116}
117EIGEN_STRONG_INLINE Packet4f vec4f_movehl(const Packet4f& a, const Packet4f& b) {
118 return Packet4f(_mm_movehl_ps(a, b));
119}
120EIGEN_STRONG_INLINE Packet4f vec4f_unpacklo(const Packet4f& a, const Packet4f& b) {
121 return Packet4f(_mm_unpacklo_ps(a, b));
122}
123EIGEN_STRONG_INLINE Packet4f vec4f_unpackhi(const Packet4f& a, const Packet4f& b) {
124 return Packet4f(_mm_unpackhi_ps(a, b));
125}
126#define vec4f_duplane(a, p) vec4f_swizzle2(a, a, p, p, p, p)
127
128#define vec2d_swizzle2(a, b, mask) Packet2d(_mm_shuffle_pd(a, b, mask))
129
130EIGEN_STRONG_INLINE Packet2d vec2d_unpacklo(const Packet2d& a, const Packet2d& b) {
131 return Packet2d(_mm_unpacklo_pd(a, b));
132}
133EIGEN_STRONG_INLINE Packet2d vec2d_unpackhi(const Packet2d& a, const Packet2d& b) {
134 return Packet2d(_mm_unpackhi_pd(a, b));
135}
136#define vec2d_duplane(a, p) vec2d_swizzle2(a, a, (p << 1) | p)
137
138#define EIGEN_DECLARE_CONST_Packet4f(NAME, X) const Packet4f p4f_##NAME = pset1<Packet4f>(X)
139
140#define EIGEN_DECLARE_CONST_Packet2d(NAME, X) const Packet2d p2d_##NAME = pset1<Packet2d>(X)
141
142#define EIGEN_DECLARE_CONST_Packet4f_FROM_INT(NAME, X) const Packet4f p4f_##NAME = pset1frombits<Packet4f>(X)
143
144#define EIGEN_DECLARE_CONST_Packet4i(NAME, X) const Packet4i p4i_##NAME = pset1<Packet4i>(X)
145
146#define EIGEN_DECLARE_CONST_Packet4ui(NAME, X) const Packet4ui p4ui_##NAME = pset1<Packet4ui>(X)
147
148// Work around lack of extract/cvt for epi64 when compiling for 32-bit.
149#if EIGEN_ARCH_x86_64
150EIGEN_ALWAYS_INLINE int64_t _mm_extract_epi64_0(const __m128i& a) { return _mm_cvtsi128_si64(a); }
151#ifdef EIGEN_VECTORIZE_SSE4_1
152EIGEN_ALWAYS_INLINE int64_t _mm_extract_epi64_1(const __m128i& a) { return _mm_extract_epi64(a, 1); }
153#else
154EIGEN_ALWAYS_INLINE int64_t _mm_extract_epi64_1(const __m128i& a) {
155 return _mm_cvtsi128_si64(_mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(a), _mm_castsi128_pd(a), 0x1)));
156}
157#endif
158#else
159// epi64 instructions are not available. The following seems to generate the same instructions
160// with -O2 in GCC/Clang.
161EIGEN_ALWAYS_INLINE int64_t _mm_extract_epi64_0(const __m128i& a) {
162 return numext::bit_cast<int64_t>(_mm_cvtsd_f64(_mm_castsi128_pd(a)));
163}
164EIGEN_ALWAYS_INLINE int64_t _mm_extract_epi64_1(const __m128i& a) {
165 return numext::bit_cast<int64_t>(_mm_cvtsd_f64(_mm_shuffle_pd(_mm_castsi128_pd(a), _mm_castsi128_pd(a), 0x1)));
166}
167#endif
168
169// Use the packet_traits defined in AVX/PacketMath.h instead if we're going
170// to leverage AVX instructions.
171#ifndef EIGEN_VECTORIZE_AVX
172template <>
173struct packet_traits<float> : default_packet_traits {
174 typedef Packet4f type;
175 typedef Packet4f half;
176 enum {
177 Vectorizable = 1,
178 AlignedOnScalar = 1,
179 size = 4,
180
181 HasCmp = 1,
182 HasDiv = 1,
183 HasReciprocal = EIGEN_FAST_MATH,
184 HasSin = EIGEN_FAST_MATH,
185 HasCos = EIGEN_FAST_MATH,
186 HasACos = 1,
187 HasASin = 1,
188 HasATan = 1,
189 HasATanh = 1,
190 HasLog = 1,
191 HasLog1p = 1,
192 HasExpm1 = 1,
193 HasNdtri = 1,
194 HasExp = 1,
195 HasBessel = 1,
196 HasSqrt = 1,
197 HasRsqrt = 1,
198 HasTanh = EIGEN_FAST_MATH,
199 HasErf = EIGEN_FAST_MATH,
200 HasBlend = 1,
201 HasSign = 0 // The manually vectorized version is slightly slower for SSE.
202 };
203};
204template <>
205struct packet_traits<double> : default_packet_traits {
206 typedef Packet2d type;
207 typedef Packet2d half;
208 enum {
209 Vectorizable = 1,
210 AlignedOnScalar = 1,
211 size = 2,
212
213 HasCmp = 1,
214 HasDiv = 1,
215 HasSin = EIGEN_FAST_MATH,
216 HasCos = EIGEN_FAST_MATH,
217 HasLog = 1,
218 HasExp = 1,
219 HasSqrt = 1,
220 HasRsqrt = 1,
221 HasATan = 1,
222 HasBlend = 1
223 };
224};
225template <>
226struct packet_traits<int> : default_packet_traits {
227 typedef Packet4i type;
228 typedef Packet4i half;
229 enum {
230 Vectorizable = 1,
231 AlignedOnScalar = 1,
232 size = 4,
233
234 HasCmp = 1,
235 HasDiv = 1,
236 HasShift = 1,
237 HasBlend = 1
238 };
239};
240template <>
241struct packet_traits<uint32_t> : default_packet_traits {
242 typedef Packet4ui type;
243 typedef Packet4ui half;
244 enum {
245 Vectorizable = 1,
246 AlignedOnScalar = 1,
247 size = 4,
248
249 HasDiv = 0,
250 HasNegate = 0,
251 HasCmp = 1,
252 HasShift = 1,
253 HasBlend = 1
254 };
255};
256template <>
257struct packet_traits<int64_t> : default_packet_traits {
258 typedef Packet2l type;
259 typedef Packet2l half;
260 enum {
261 Vectorizable = 1,
262 AlignedOnScalar = 1,
263 size = 2,
264
265 HasDiv = 0,
266 HasCmp = 1,
267 HasShift = 1,
268 HasBlend = 1
269 };
270};
271#endif
272template <>
273struct packet_traits<bool> : default_packet_traits {
274 typedef Packet16b type;
275 typedef Packet16b half;
276 enum {
277 Vectorizable = 1,
278 AlignedOnScalar = 1,
279 size = 16,
280
281 HasCmp = 1, // note -- only pcmp_eq is defined
282 HasShift = 0,
283 HasAbs = 0,
284 HasAbs2 = 0,
285 HasMin = 0,
286 HasMax = 0,
287 HasConj = 0,
288 HasSqrt = 1,
289 HasNegate = 0,
290 HasSign = 0 // Don't try to vectorize psign<bool> = identity.
291 };
292};
293
294template <>
295struct unpacket_traits<Packet4f> {
296 typedef float type;
297 typedef Packet4f half;
298 typedef Packet4i integer_packet;
299 enum {
300 size = 4,
301 alignment = Aligned16,
302 vectorizable = true,
303 masked_load_available = false,
304 masked_store_available = false
305 };
306};
307template <>
308struct unpacket_traits<Packet2d> {
309 typedef double type;
310 typedef Packet2d half;
311 typedef Packet2l integer_packet;
312 enum {
313 size = 2,
314 alignment = Aligned16,
315 vectorizable = true,
316 masked_load_available = false,
317 masked_store_available = false
318 };
319};
320template <>
321struct unpacket_traits<Packet2l> {
322 typedef int64_t type;
323 typedef Packet2l half;
324 enum {
325 size = 2,
326 alignment = Aligned16,
327 vectorizable = true,
328 masked_load_available = false,
329 masked_store_available = false
330 };
331};
332template <>
333struct unpacket_traits<Packet4i> {
334 typedef int type;
335 typedef Packet4i half;
336 enum {
337 size = 4,
338 alignment = Aligned16,
339 vectorizable = true,
340 masked_load_available = false,
341 masked_store_available = false
342 };
343};
344template <>
345struct unpacket_traits<Packet4ui> {
346 typedef uint32_t type;
347 typedef Packet4ui half;
348 enum {
349 size = 4,
350 alignment = Aligned16,
351 vectorizable = true,
352 masked_load_available = false,
353 masked_store_available = false
354 };
355};
356template <>
357struct unpacket_traits<Packet16b> {
358 typedef bool type;
359 typedef Packet16b half;
360 enum {
361 size = 16,
362 alignment = Aligned16,
363 vectorizable = true,
364 masked_load_available = false,
365 masked_store_available = false
366 };
367};
368
369#ifndef EIGEN_VECTORIZE_AVX
370template <>
371struct scalar_div_cost<float, true> {
372 enum { value = 7 };
373};
374template <>
375struct scalar_div_cost<double, true> {
376 enum { value = 8 };
377};
378#endif
379
380template <>
381EIGEN_STRONG_INLINE Packet4f pset1<Packet4f>(const float& from) {
382 return _mm_set_ps1(from);
383}
384template <>
385EIGEN_STRONG_INLINE Packet2d pset1<Packet2d>(const double& from) {
386 return _mm_set1_pd(from);
387}
388template <>
389EIGEN_STRONG_INLINE Packet2l pset1<Packet2l>(const int64_t& from) {
390 return _mm_set1_epi64x(from);
391}
392template <>
393EIGEN_STRONG_INLINE Packet4i pset1<Packet4i>(const int& from) {
394 return _mm_set1_epi32(from);
395}
396template <>
397EIGEN_STRONG_INLINE Packet4ui pset1<Packet4ui>(const uint32_t& from) {
398 return _mm_set1_epi32(numext::bit_cast<int32_t>(from));
399}
400template <>
401EIGEN_STRONG_INLINE Packet16b pset1<Packet16b>(const bool& from) {
402 return _mm_set1_epi8(static_cast<char>(from));
403}
404
405template <>
406EIGEN_STRONG_INLINE Packet4f pset1frombits<Packet4f>(unsigned int from) {
407 return _mm_castsi128_ps(pset1<Packet4i>(from));
408}
409template <>
410EIGEN_STRONG_INLINE Packet2d pset1frombits<Packet2d>(uint64_t from) {
411 return _mm_castsi128_pd(_mm_set1_epi64x(from));
412}
413
414template <>
415EIGEN_STRONG_INLINE Packet4f peven_mask(const Packet4f& /*a*/) {
416 return _mm_castsi128_ps(_mm_set_epi32(0, -1, 0, -1));
417}
418template <>
419EIGEN_STRONG_INLINE Packet2l peven_mask(const Packet2l& /*a*/) {
420 return _mm_set_epi32(0, 0, -1, -1);
421}
422template <>
423EIGEN_STRONG_INLINE Packet4i peven_mask(const Packet4i& /*a*/) {
424 return _mm_set_epi32(0, -1, 0, -1);
425}
426template <>
427EIGEN_STRONG_INLINE Packet4ui peven_mask(const Packet4ui& /*a*/) {
428 return _mm_set_epi32(0, -1, 0, -1);
429}
430template <>
431EIGEN_STRONG_INLINE Packet2d peven_mask(const Packet2d& /*a*/) {
432 return _mm_castsi128_pd(_mm_set_epi32(0, 0, -1, -1));
433}
434
435template <>
436EIGEN_STRONG_INLINE Packet4f pzero(const Packet4f& /*a*/) {
437 return _mm_setzero_ps();
438}
439template <>
440EIGEN_STRONG_INLINE Packet2d pzero(const Packet2d& /*a*/) {
441 return _mm_setzero_pd();
442}
443template <>
444EIGEN_STRONG_INLINE Packet2l pzero(const Packet2l& /*a*/) {
445 return _mm_setzero_si128();
446}
447template <>
448EIGEN_STRONG_INLINE Packet4i pzero(const Packet4i& /*a*/) {
449 return _mm_setzero_si128();
450}
451template <>
452EIGEN_STRONG_INLINE Packet4ui pzero(const Packet4ui& /*a*/) {
453 return _mm_setzero_si128();
454}
455
456// GCC generates a shufps instruction for _mm_set1_ps/_mm_load1_ps instead of the more efficient pshufd instruction.
457// However, using inrinsics for pset1 makes gcc to generate crappy code in some cases (see bug 203)
458// Using inline assembly is also not an option because then gcc fails to reorder properly the instructions.
459// Therefore, we introduced the pload1 functions to be used in product kernels for which bug 203 does not apply.
460// Also note that with AVX, we want it to generate a vbroadcastss.
461#if EIGEN_COMP_GNUC_STRICT && (!defined __AVX__)
462template <>
463EIGEN_STRONG_INLINE Packet4f pload1<Packet4f>(const float* from) {
464 return vec4f_swizzle1(_mm_load_ss(from), 0, 0, 0, 0);
465}
466#endif
467
468template <>
469EIGEN_STRONG_INLINE Packet4f plset<Packet4f>(const float& a) {
470 return _mm_add_ps(pset1<Packet4f>(a), _mm_set_ps(3, 2, 1, 0));
471}
472template <>
473EIGEN_STRONG_INLINE Packet2d plset<Packet2d>(const double& a) {
474 return _mm_add_pd(pset1<Packet2d>(a), _mm_set_pd(1, 0));
475}
476template <>
477EIGEN_STRONG_INLINE Packet2l plset<Packet2l>(const int64_t& a) {
478 return _mm_add_epi32(pset1<Packet2l>(a), _mm_set_epi64x(1, 0));
479}
480template <>
481EIGEN_STRONG_INLINE Packet4i plset<Packet4i>(const int& a) {
482 return _mm_add_epi32(pset1<Packet4i>(a), _mm_set_epi32(3, 2, 1, 0));
483}
484template <>
485EIGEN_STRONG_INLINE Packet4ui plset<Packet4ui>(const uint32_t& a) {
486 return _mm_add_epi32(pset1<Packet4ui>(a), _mm_set_epi32(3, 2, 1, 0));
487}
488
489template <>
490EIGEN_STRONG_INLINE Packet4f padd<Packet4f>(const Packet4f& a, const Packet4f& b) {
491 return _mm_add_ps(a, b);
492}
493template <>
494EIGEN_STRONG_INLINE Packet2d padd<Packet2d>(const Packet2d& a, const Packet2d& b) {
495 return _mm_add_pd(a, b);
496}
497template <>
498EIGEN_STRONG_INLINE Packet2l padd<Packet2l>(const Packet2l& a, const Packet2l& b) {
499 return _mm_add_epi64(a, b);
500}
501template <>
502EIGEN_STRONG_INLINE Packet4i padd<Packet4i>(const Packet4i& a, const Packet4i& b) {
503 return _mm_add_epi32(a, b);
504}
505template <>
506EIGEN_STRONG_INLINE Packet4ui padd<Packet4ui>(const Packet4ui& a, const Packet4ui& b) {
507 return _mm_add_epi32(a, b);
508}
509
510template <>
511EIGEN_STRONG_INLINE Packet16b padd<Packet16b>(const Packet16b& a, const Packet16b& b) {
512 return _mm_or_si128(a, b);
513}
514
515template <typename Packet>
516EIGEN_STRONG_INLINE Packet padds(const Packet& a, const Packet& b);
517template <>
518EIGEN_STRONG_INLINE Packet4f padds<Packet4f>(const Packet4f& a, const Packet4f& b) {
519 return _mm_add_ss(a, b);
520}
521template <>
522EIGEN_STRONG_INLINE Packet2d padds<Packet2d>(const Packet2d& a, const Packet2d& b) {
523 return _mm_add_sd(a, b);
524}
525
526template <>
527EIGEN_STRONG_INLINE Packet4f psub<Packet4f>(const Packet4f& a, const Packet4f& b) {
528 return _mm_sub_ps(a, b);
529}
530template <>
531EIGEN_STRONG_INLINE Packet2d psub<Packet2d>(const Packet2d& a, const Packet2d& b) {
532 return _mm_sub_pd(a, b);
533}
534template <>
535EIGEN_STRONG_INLINE Packet2l psub<Packet2l>(const Packet2l& a, const Packet2l& b) {
536 return _mm_sub_epi64(a, b);
537}
538template <>
539EIGEN_STRONG_INLINE Packet4i psub<Packet4i>(const Packet4i& a, const Packet4i& b) {
540 return _mm_sub_epi32(a, b);
541}
542template <>
543EIGEN_STRONG_INLINE Packet4ui psub<Packet4ui>(const Packet4ui& a, const Packet4ui& b) {
544 return _mm_sub_epi32(a, b);
545}
546template <>
547EIGEN_STRONG_INLINE Packet16b psub<Packet16b>(const Packet16b& a, const Packet16b& b) {
548 return _mm_xor_si128(a, b);
549}
550
551template <>
552EIGEN_STRONG_INLINE Packet4f pxor<Packet4f>(const Packet4f& a, const Packet4f& b);
553template <>
554EIGEN_STRONG_INLINE Packet4f paddsub<Packet4f>(const Packet4f& a, const Packet4f& b) {
555#ifdef EIGEN_VECTORIZE_SSE3
556 return _mm_addsub_ps(a, b);
557#else
558 const Packet4f mask = _mm_castsi128_ps(_mm_setr_epi32(0x80000000, 0x0, 0x80000000, 0x0));
559 return padd(a, pxor(mask, b));
560#endif
561}
562
563template <>
564EIGEN_STRONG_INLINE Packet2d pxor<Packet2d>(const Packet2d&, const Packet2d&);
565template <>
566EIGEN_STRONG_INLINE Packet2d paddsub<Packet2d>(const Packet2d& a, const Packet2d& b) {
567#ifdef EIGEN_VECTORIZE_SSE3
568 return _mm_addsub_pd(a, b);
569#else
570 const Packet2d mask = _mm_castsi128_pd(_mm_setr_epi32(0x0, 0x80000000, 0x0, 0x0));
571 return padd(a, pxor(mask, b));
572#endif
573}
574
575template <>
576EIGEN_STRONG_INLINE Packet4f pnegate(const Packet4f& a) {
577 const Packet4f mask = _mm_castsi128_ps(_mm_setr_epi32(0x80000000, 0x80000000, 0x80000000, 0x80000000));
578 return _mm_xor_ps(a, mask);
579}
580template <>
581EIGEN_STRONG_INLINE Packet2d pnegate(const Packet2d& a) {
582 const Packet2d mask = _mm_castsi128_pd(_mm_setr_epi32(0x0, 0x80000000, 0x0, 0x80000000));
583 return _mm_xor_pd(a, mask);
584}
585template <>
586EIGEN_STRONG_INLINE Packet2l pnegate(const Packet2l& a) {
587 return psub(pzero(a), a);
588}
589
590template <>
591EIGEN_STRONG_INLINE Packet4i pnegate(const Packet4i& a) {
592 return psub(pzero(a), a);
593}
594
595template <>
596EIGEN_STRONG_INLINE Packet4f pconj(const Packet4f& a) {
597 return a;
598}
599template <>
600EIGEN_STRONG_INLINE Packet2d pconj(const Packet2d& a) {
601 return a;
602}
603template <>
604EIGEN_STRONG_INLINE Packet2l pconj(const Packet2l& a) {
605 return a;
606}
607template <>
608EIGEN_STRONG_INLINE Packet4i pconj(const Packet4i& a) {
609 return a;
610}
611
612template <>
613EIGEN_STRONG_INLINE Packet4f pmul<Packet4f>(const Packet4f& a, const Packet4f& b) {
614 return _mm_mul_ps(a, b);
615}
616template <>
617EIGEN_STRONG_INLINE Packet2d pmul<Packet2d>(const Packet2d& a, const Packet2d& b) {
618 return _mm_mul_pd(a, b);
619}
620template <>
621EIGEN_STRONG_INLINE Packet2l pmul<Packet2l>(const Packet2l& a, const Packet2l& b) {
622 // 64-bit mul requires avx512, so do this with 32-bit multiplication
623 __m128i upper32_a = _mm_srli_epi64(a, 32);
624 __m128i upper32_b = _mm_srli_epi64(b, 32);
625
626 // upper * lower
627 __m128i mul1 = _mm_mul_epu32(upper32_a, b);
628 __m128i mul2 = _mm_mul_epu32(upper32_b, a);
629 // Gives us both upper*upper and lower*lower
630 __m128i mul3 = _mm_mul_epu32(a, b);
631
632 __m128i high = _mm_slli_epi64(_mm_add_epi64(mul1, mul2), 32);
633 return _mm_add_epi64(high, mul3);
634}
635template <>
636EIGEN_STRONG_INLINE Packet4i pmul<Packet4i>(const Packet4i& a, const Packet4i& b) {
637#ifdef EIGEN_VECTORIZE_SSE4_1
638 return _mm_mullo_epi32(a, b);
639#else
640 // this version is slightly faster than 4 scalar products
641 return vec4i_swizzle1(
642 vec4i_swizzle2(_mm_mul_epu32(a, b), _mm_mul_epu32(vec4i_swizzle1(a, 1, 0, 3, 2), vec4i_swizzle1(b, 1, 0, 3, 2)),
643 0, 2, 0, 2),
644 0, 2, 1, 3);
645#endif
646}
647template <>
648EIGEN_STRONG_INLINE Packet4ui pmul<Packet4ui>(const Packet4ui& a, const Packet4ui& b) {
649#ifdef EIGEN_VECTORIZE_SSE4_1
650 return _mm_mullo_epi32(a, b);
651#else
652 // this version is slightly faster than 4 scalar products
653 return vec4ui_swizzle1(
654 vec4ui_swizzle2(_mm_mul_epu32(a, b),
655 _mm_mul_epu32(vec4ui_swizzle1(a, 1, 0, 3, 2), vec4ui_swizzle1(b, 1, 0, 3, 2)), 0, 2, 0, 2),
656 0, 2, 1, 3);
657#endif
658}
659
660template <>
661EIGEN_STRONG_INLINE Packet16b pmul<Packet16b>(const Packet16b& a, const Packet16b& b) {
662 return _mm_and_si128(a, b);
663}
664
665template <>
666EIGEN_STRONG_INLINE Packet4f pdiv<Packet4f>(const Packet4f& a, const Packet4f& b) {
667 return _mm_div_ps(a, b);
668}
669template <>
670EIGEN_STRONG_INLINE Packet2d pdiv<Packet2d>(const Packet2d& a, const Packet2d& b) {
671 return _mm_div_pd(a, b);
672}
673
674template <>
675EIGEN_STRONG_INLINE Packet4i pdiv<Packet4i>(const Packet4i& a, const Packet4i& b) {
676#ifdef EIGEN_VECTORIZE_AVX
677 return _mm256_cvttpd_epi32(_mm256_div_pd(_mm256_cvtepi32_pd(a), _mm256_cvtepi32_pd(b)));
678#else
679 __m128i q_lo = _mm_cvttpd_epi32(_mm_div_pd(_mm_cvtepi32_pd(a), _mm_cvtepi32_pd(b)));
680 __m128i q_hi = _mm_cvttpd_epi32(
681 _mm_div_pd(_mm_cvtepi32_pd(vec4i_swizzle1(a, 2, 3, 0, 1)), _mm_cvtepi32_pd(vec4i_swizzle1(b, 2, 3, 0, 1))));
682 return vec4i_swizzle1(_mm_unpacklo_epi32(q_lo, q_hi), 0, 2, 1, 3);
683#endif
684}
685
686#ifdef EIGEN_VECTORIZE_FMA
687template <>
688EIGEN_STRONG_INLINE Packet4f pmadd(const Packet4f& a, const Packet4f& b, const Packet4f& c) {
689 return _mm_fmadd_ps(a, b, c);
690}
691template <>
692EIGEN_STRONG_INLINE Packet2d pmadd(const Packet2d& a, const Packet2d& b, const Packet2d& c) {
693 return _mm_fmadd_pd(a, b, c);
694}
695template <>
696EIGEN_STRONG_INLINE Packet4f pmsub(const Packet4f& a, const Packet4f& b, const Packet4f& c) {
697 return _mm_fmsub_ps(a, b, c);
698}
699template <>
700EIGEN_STRONG_INLINE Packet2d pmsub(const Packet2d& a, const Packet2d& b, const Packet2d& c) {
701 return _mm_fmsub_pd(a, b, c);
702}
703template <>
704EIGEN_STRONG_INLINE Packet4f pnmadd(const Packet4f& a, const Packet4f& b, const Packet4f& c) {
705 return _mm_fnmadd_ps(a, b, c);
706}
707template <>
708EIGEN_STRONG_INLINE Packet2d pnmadd(const Packet2d& a, const Packet2d& b, const Packet2d& c) {
709 return _mm_fnmadd_pd(a, b, c);
710}
711template <>
712EIGEN_STRONG_INLINE Packet4f pnmsub(const Packet4f& a, const Packet4f& b, const Packet4f& c) {
713 return _mm_fnmsub_ps(a, b, c);
714}
715template <>
716EIGEN_STRONG_INLINE Packet2d pnmsub(const Packet2d& a, const Packet2d& b, const Packet2d& c) {
717 return _mm_fnmsub_pd(a, b, c);
718}
719
720template <typename Packet>
721EIGEN_STRONG_INLINE Packet pmadds(const Packet& a, const Packet& b, const Packet& c);
722template <>
723EIGEN_STRONG_INLINE Packet4f pmadds<Packet4f>(const Packet4f& a, const Packet4f& b, const Packet4f& c) {
724 return _mm_fmadd_ss(a, b, c);
725}
726template <>
727EIGEN_STRONG_INLINE Packet2d pmadds<Packet2d>(const Packet2d& a, const Packet2d& b, const Packet2d& c) {
728 return _mm_fmadd_sd(a, b, c);
729}
730#endif
731
732#ifdef EIGEN_VECTORIZE_SSE4_1
733template <>
734EIGEN_STRONG_INLINE Packet4f pselect(const Packet4f& mask, const Packet4f& a, const Packet4f& b) {
735 return _mm_blendv_ps(b, a, mask);
736}
737
738template <>
739EIGEN_STRONG_INLINE Packet2l pselect(const Packet2l& mask, const Packet2l& a, const Packet2l& b) {
740 return _mm_castpd_si128(_mm_blendv_pd(_mm_castsi128_pd(b), _mm_castsi128_pd(a), _mm_castsi128_pd(mask)));
741}
742
743template <>
744EIGEN_STRONG_INLINE Packet4i pselect(const Packet4i& mask, const Packet4i& a, const Packet4i& b) {
745 return _mm_castps_si128(_mm_blendv_ps(_mm_castsi128_ps(b), _mm_castsi128_ps(a), _mm_castsi128_ps(mask)));
746}
747
748template <>
749EIGEN_STRONG_INLINE Packet4ui pselect(const Packet4ui& mask, const Packet4ui& a, const Packet4ui& b) {
750 return _mm_castps_si128(_mm_blendv_ps(_mm_castsi128_ps(b), _mm_castsi128_ps(a), _mm_castsi128_ps(mask)));
751}
752
753template <>
754EIGEN_STRONG_INLINE Packet2d pselect(const Packet2d& mask, const Packet2d& a, const Packet2d& b) {
755 return _mm_blendv_pd(b, a, mask);
756}
757#endif
758
759template <>
760EIGEN_STRONG_INLINE Packet2l ptrue<Packet2l>(const Packet2l& a) {
761 return _mm_cmpeq_epi32(a, a);
762}
763template <>
764EIGEN_STRONG_INLINE Packet4i ptrue<Packet4i>(const Packet4i& a) {
765 return _mm_cmpeq_epi32(a, a);
766}
767template <>
768EIGEN_STRONG_INLINE Packet16b ptrue<Packet16b>(const Packet16b& /*a*/) {
769 return pset1<Packet16b>(true);
770}
771template <>
772EIGEN_STRONG_INLINE Packet4f ptrue<Packet4f>(const Packet4f& a) {
773 Packet4i b = _mm_castps_si128(a);
774 return _mm_castsi128_ps(_mm_cmpeq_epi32(b, b));
775}
776template <>
777EIGEN_STRONG_INLINE Packet2d ptrue<Packet2d>(const Packet2d& a) {
778 Packet4i b = _mm_castpd_si128(a);
779 return _mm_castsi128_pd(_mm_cmpeq_epi32(b, b));
780}
781
782template <>
783EIGEN_STRONG_INLINE Packet4f pand<Packet4f>(const Packet4f& a, const Packet4f& b) {
784 return _mm_and_ps(a, b);
785}
786template <>
787EIGEN_STRONG_INLINE Packet2d pand<Packet2d>(const Packet2d& a, const Packet2d& b) {
788 return _mm_and_pd(a, b);
789}
790template <>
791EIGEN_STRONG_INLINE Packet2l pand<Packet2l>(const Packet2l& a, const Packet2l& b) {
792 return _mm_and_si128(a, b);
793}
794template <>
795EIGEN_STRONG_INLINE Packet4i pand<Packet4i>(const Packet4i& a, const Packet4i& b) {
796 return _mm_and_si128(a, b);
797}
798template <>
799EIGEN_STRONG_INLINE Packet4ui pand<Packet4ui>(const Packet4ui& a, const Packet4ui& b) {
800 return _mm_and_si128(a, b);
801}
802template <>
803EIGEN_STRONG_INLINE Packet16b pand<Packet16b>(const Packet16b& a, const Packet16b& b) {
804 return _mm_and_si128(a, b);
805}
806
807template <>
808EIGEN_STRONG_INLINE Packet4f por<Packet4f>(const Packet4f& a, const Packet4f& b) {
809 return _mm_or_ps(a, b);
810}
811template <>
812EIGEN_STRONG_INLINE Packet2d por<Packet2d>(const Packet2d& a, const Packet2d& b) {
813 return _mm_or_pd(a, b);
814}
815template <>
816EIGEN_STRONG_INLINE Packet2l por<Packet2l>(const Packet2l& a, const Packet2l& b) {
817 return _mm_or_si128(a, b);
818}
819template <>
820EIGEN_STRONG_INLINE Packet4i por<Packet4i>(const Packet4i& a, const Packet4i& b) {
821 return _mm_or_si128(a, b);
822}
823template <>
824EIGEN_STRONG_INLINE Packet4ui por<Packet4ui>(const Packet4ui& a, const Packet4ui& b) {
825 return _mm_or_si128(a, b);
826}
827template <>
828EIGEN_STRONG_INLINE Packet16b por<Packet16b>(const Packet16b& a, const Packet16b& b) {
829 return _mm_or_si128(a, b);
830}
831
832template <>
833EIGEN_STRONG_INLINE Packet4f pxor<Packet4f>(const Packet4f& a, const Packet4f& b) {
834 return _mm_xor_ps(a, b);
835}
836template <>
837EIGEN_STRONG_INLINE Packet2d pxor<Packet2d>(const Packet2d& a, const Packet2d& b) {
838 return _mm_xor_pd(a, b);
839}
840template <>
841EIGEN_STRONG_INLINE Packet2l pxor<Packet2l>(const Packet2l& a, const Packet2l& b) {
842 return _mm_xor_si128(a, b);
843}
844template <>
845EIGEN_STRONG_INLINE Packet4i pxor<Packet4i>(const Packet4i& a, const Packet4i& b) {
846 return _mm_xor_si128(a, b);
847}
848template <>
849EIGEN_STRONG_INLINE Packet4ui pxor<Packet4ui>(const Packet4ui& a, const Packet4ui& b) {
850 return _mm_xor_si128(a, b);
851}
852template <>
853EIGEN_STRONG_INLINE Packet16b pxor<Packet16b>(const Packet16b& a, const Packet16b& b) {
854 return _mm_xor_si128(a, b);
855}
856
857template <>
858EIGEN_STRONG_INLINE Packet4f pandnot<Packet4f>(const Packet4f& a, const Packet4f& b) {
859 return _mm_andnot_ps(b, a);
860}
861template <>
862EIGEN_STRONG_INLINE Packet2d pandnot<Packet2d>(const Packet2d& a, const Packet2d& b) {
863 return _mm_andnot_pd(b, a);
864}
865template <>
866EIGEN_STRONG_INLINE Packet2l pandnot<Packet2l>(const Packet2l& a, const Packet2l& b) {
867 return _mm_andnot_si128(b, a);
868}
869template <>
870EIGEN_STRONG_INLINE Packet4i pandnot<Packet4i>(const Packet4i& a, const Packet4i& b) {
871 return _mm_andnot_si128(b, a);
872}
873template <>
874EIGEN_STRONG_INLINE Packet4ui pandnot<Packet4ui>(const Packet4ui& a, const Packet4ui& b) {
875 return _mm_andnot_si128(b, a);
876}
877
878template <>
879EIGEN_STRONG_INLINE Packet4f pcmp_le(const Packet4f& a, const Packet4f& b) {
880 return _mm_cmple_ps(a, b);
881}
882template <>
883EIGEN_STRONG_INLINE Packet4f pcmp_lt(const Packet4f& a, const Packet4f& b) {
884 return _mm_cmplt_ps(a, b);
885}
886template <>
887EIGEN_STRONG_INLINE Packet4f pcmp_lt_or_nan(const Packet4f& a, const Packet4f& b) {
888 return _mm_cmpnge_ps(a, b);
889}
890template <>
891EIGEN_STRONG_INLINE Packet4f pcmp_eq(const Packet4f& a, const Packet4f& b) {
892 return _mm_cmpeq_ps(a, b);
893}
894
895template <>
896EIGEN_STRONG_INLINE Packet2d pcmp_le(const Packet2d& a, const Packet2d& b) {
897 return _mm_cmple_pd(a, b);
898}
899template <>
900EIGEN_STRONG_INLINE Packet2d pcmp_lt(const Packet2d& a, const Packet2d& b) {
901 return _mm_cmplt_pd(a, b);
902}
903template <>
904EIGEN_STRONG_INLINE Packet2d pcmp_lt_or_nan(const Packet2d& a, const Packet2d& b) {
905 return _mm_cmpnge_pd(a, b);
906}
907template <>
908EIGEN_STRONG_INLINE Packet2d pcmp_eq(const Packet2d& a, const Packet2d& b) {
909 return _mm_cmpeq_pd(a, b);
910}
911template <>
912EIGEN_STRONG_INLINE Packet4i pcmp_lt(const Packet4i& a, const Packet4i& b) {
913 return _mm_cmplt_epi32(a, b);
914}
915template <>
916EIGEN_STRONG_INLINE Packet4i pcmp_eq(const Packet4i& a, const Packet4i& b) {
917 return _mm_cmpeq_epi32(a, b);
918}
919template <>
920EIGEN_STRONG_INLINE Packet4i pcmp_le(const Packet4i& a, const Packet4i& b) {
921 return por(pcmp_lt(a, b), pcmp_eq(a, b));
922}
923template <>
924EIGEN_STRONG_INLINE Packet2l pcmp_lt(const Packet2l& a, const Packet2l& b) {
925#ifdef EIGEN_VECTORIZE_SSE4_2
926 return _mm_cmpgt_epi64(b, a);
927#else
928 Packet4i eq = pcmp_eq<Packet4i>(Packet4i(a), Packet4i(b));
929 Packet2l hi_eq = Packet2l(_mm_shuffle_epi32(eq, (shuffle_mask<1, 1, 3, 3>::mask)));
930 Packet4i lt = pcmp_lt<Packet4i>(Packet4i(a), Packet4i(b));
931 Packet2l hi_lt = Packet2l(_mm_shuffle_epi32(lt, (shuffle_mask<1, 1, 3, 3>::mask)));
932 Packet2l lo_lt = Packet2l(_mm_shuffle_epi32(lt, (shuffle_mask<0, 0, 2, 2>::mask)));
933 // return hi(a) < hi(b) || (hi(a) == hi(b) && lo(a) < lo(b))
934 return por(hi_lt, pand(hi_eq, lo_lt));
935#endif
936}
937template <>
938EIGEN_STRONG_INLINE Packet2l pcmp_eq(const Packet2l& a, const Packet2l& b) {
939#ifdef EIGEN_VECTORIZE_SSE4_1
940 return _mm_cmpeq_epi64(a, b);
941#else
942 Packet4i tmp = pcmp_eq<Packet4i>(Packet4i(a), Packet4i(b));
943 return Packet2l(pand<Packet4i>(tmp, _mm_shuffle_epi32(tmp, (shuffle_mask<1, 0, 3, 2>::mask))));
944#endif
945}
946template <>
947EIGEN_STRONG_INLINE Packet2l pcmp_le(const Packet2l& a, const Packet2l& b) {
948 return por(pcmp_lt(a, b), pcmp_eq(a, b));
949}
950template <>
951EIGEN_STRONG_INLINE Packet16b pcmp_eq(const Packet16b& a, const Packet16b& b) {
952 // Mask out invalid bool bits to avoid UB.
953 const Packet16b kBoolMask = pset1<Packet16b>(true);
954 return _mm_and_si128(_mm_cmpeq_epi8(a, b), kBoolMask);
955}
956template <>
957EIGEN_STRONG_INLINE Packet4ui pcmp_eq(const Packet4ui& a, const Packet4ui& b) {
958 return _mm_cmpeq_epi32(a, b);
959}
960
961template <>
962EIGEN_STRONG_INLINE Packet4f pmin<Packet4f>(const Packet4f& a, const Packet4f& b) {
963#if EIGEN_GNUC_STRICT_LESS_THAN(6, 3, 0)
964// There appears to be a bug in GCC, by which the optimizer may
965// flip the argument order in calls to _mm_min_ps, so we have to
966// resort to inline ASM here. This is supposed to be fixed in gcc6.3,
967// see also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=72867
968#ifdef EIGEN_VECTORIZE_AVX
969 Packet4f res;
970 asm("vminps %[a], %[b], %[res]" : [res] "=x"(res) : [a] "x"(a), [b] "x"(b));
971#else
972 Packet4f res = b;
973 asm("minps %[a], %[res]" : [res] "+x"(res) : [a] "x"(a));
974#endif
975 return res;
976#else
977 // Arguments are reversed to match NaN propagation behavior of std::min.
978 return _mm_min_ps(b, a);
979#endif
980}
981template <>
982EIGEN_STRONG_INLINE Packet2d pmin<Packet2d>(const Packet2d& a, const Packet2d& b) {
983#if EIGEN_GNUC_STRICT_LESS_THAN(6, 3, 0)
984// There appears to be a bug in GCC, by which the optimizer may
985// flip the argument order in calls to _mm_min_pd, so we have to
986// resort to inline ASM here. This is supposed to be fixed in gcc6.3,
987// see also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=72867
988#ifdef EIGEN_VECTORIZE_AVX
989 Packet2d res;
990 asm("vminpd %[a], %[b], %[res]" : [res] "=x"(res) : [a] "x"(a), [b] "x"(b));
991#else
992 Packet2d res = b;
993 asm("minpd %[a], %[res]" : [res] "+x"(res) : [a] "x"(a));
994#endif
995 return res;
996#else
997 // Arguments are reversed to match NaN propagation behavior of std::min.
998 return _mm_min_pd(b, a);
999#endif
1000}
1001template <>
1002EIGEN_STRONG_INLINE Packet2l pmin<Packet2l>(const Packet2l& a, const Packet2l& b) {
1003 Packet2l a_lt_mask = pcmp_lt(a, b);
1004 return por(pandnot(b, a_lt_mask), pand(a, a_lt_mask));
1005}
1006template <>
1007EIGEN_STRONG_INLINE Packet4i pmin<Packet4i>(const Packet4i& a, const Packet4i& b) {
1008#ifdef EIGEN_VECTORIZE_SSE4_1
1009 return _mm_min_epi32(a, b);
1010#else
1011 // after some bench, this version *is* faster than a scalar implementation
1012 Packet4i mask = _mm_cmplt_epi32(a, b);
1013 return _mm_or_si128(_mm_and_si128(mask, a), _mm_andnot_si128(mask, b));
1014#endif
1015}
1016template <>
1017EIGEN_STRONG_INLINE Packet4ui pmin<Packet4ui>(const Packet4ui& a, const Packet4ui& b) {
1018#ifdef EIGEN_VECTORIZE_SSE4_1
1019 return _mm_min_epu32(a, b);
1020#else
1021 return padd((Packet4ui)pmin((Packet4i)psub(a, pset1<Packet4ui>(0x80000000UL)),
1022 (Packet4i)psub(b, pset1<Packet4ui>(0x80000000UL))),
1023 pset1<Packet4ui>(0x80000000UL));
1024#endif
1025}
1026
1027template <>
1028EIGEN_STRONG_INLINE Packet4f pmax<Packet4f>(const Packet4f& a, const Packet4f& b) {
1029#if EIGEN_GNUC_STRICT_LESS_THAN(6, 3, 0)
1030// There appears to be a bug in GCC, by which the optimizer may
1031// flip the argument order in calls to _mm_max_ps, so we have to
1032// resort to inline ASM here. This is supposed to be fixed in gcc6.3,
1033// see also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=72867
1034#ifdef EIGEN_VECTORIZE_AVX
1035 Packet4f res;
1036 asm("vmaxps %[a], %[b], %[res]" : [res] "=x"(res) : [a] "x"(a), [b] "x"(b));
1037#else
1038 Packet4f res = b;
1039 asm("maxps %[a], %[res]" : [res] "+x"(res) : [a] "x"(a));
1040#endif
1041 return res;
1042#else
1043 // Arguments are reversed to match NaN propagation behavior of std::max.
1044 return _mm_max_ps(b, a);
1045#endif
1046}
1047template <>
1048EIGEN_STRONG_INLINE Packet2d pmax<Packet2d>(const Packet2d& a, const Packet2d& b) {
1049#if EIGEN_GNUC_STRICT_LESS_THAN(6, 3, 0)
1050// There appears to be a bug in GCC, by which the optimizer may
1051// flip the argument order in calls to _mm_max_pd, so we have to
1052// resort to inline ASM here. This is supposed to be fixed in gcc6.3,
1053// see also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=72867
1054#ifdef EIGEN_VECTORIZE_AVX
1055 Packet2d res;
1056 asm("vmaxpd %[a], %[b], %[res]" : [res] "=x"(res) : [a] "x"(a), [b] "x"(b));
1057#else
1058 Packet2d res = b;
1059 asm("maxpd %[a], %[res]" : [res] "+x"(res) : [a] "x"(a));
1060#endif
1061 return res;
1062#else
1063 // Arguments are reversed to match NaN propagation behavior of std::max.
1064 return _mm_max_pd(b, a);
1065#endif
1066}
1067template <>
1068EIGEN_STRONG_INLINE Packet2l pmax<Packet2l>(const Packet2l& a, const Packet2l& b) {
1069 Packet2l a_lt_mask = pcmp_lt(a, b);
1070 return por(pandnot(a, a_lt_mask), pand(b, a_lt_mask));
1071}
1072template <>
1073EIGEN_STRONG_INLINE Packet4i pmax<Packet4i>(const Packet4i& a, const Packet4i& b) {
1074#ifdef EIGEN_VECTORIZE_SSE4_1
1075 return _mm_max_epi32(a, b);
1076#else
1077 // after some bench, this version *is* faster than a scalar implementation
1078 Packet4i mask = _mm_cmpgt_epi32(a, b);
1079 return _mm_or_si128(_mm_and_si128(mask, a), _mm_andnot_si128(mask, b));
1080#endif
1081}
1082template <>
1083EIGEN_STRONG_INLINE Packet4ui pmax<Packet4ui>(const Packet4ui& a, const Packet4ui& b) {
1084#ifdef EIGEN_VECTORIZE_SSE4_1
1085 return _mm_max_epu32(a, b);
1086#else
1087 return padd((Packet4ui)pmax((Packet4i)psub(a, pset1<Packet4ui>(0x80000000UL)),
1088 (Packet4i)psub(b, pset1<Packet4ui>(0x80000000UL))),
1089 pset1<Packet4ui>(0x80000000UL));
1090#endif
1091}
1092
1093template <>
1094EIGEN_STRONG_INLINE Packet4ui pcmp_lt(const Packet4ui& a, const Packet4ui& b) {
1095#ifdef EIGEN_VECTORIZE_SSE4_1
1096 return pxor(pcmp_eq(a, pmax(a, b)), ptrue(a));
1097#else
1098 return (Packet4ui)pcmp_lt((Packet4i)psub(a, pset1<Packet4ui>(0x80000000UL)),
1099 (Packet4i)psub(b, pset1<Packet4ui>(0x80000000UL)));
1100#endif
1101}
1102template <>
1103EIGEN_STRONG_INLINE Packet4ui pcmp_le(const Packet4ui& a, const Packet4ui& b) {
1104#ifdef EIGEN_VECTORIZE_SSE4_1
1105 return pcmp_eq(a, pmin(a, b));
1106#else
1107 return (Packet4ui)pcmp_le((Packet4i)psub(a, pset1<Packet4ui>(0x80000000UL)),
1108 (Packet4i)psub(b, pset1<Packet4ui>(0x80000000UL)));
1109#endif
1110}
1111
1112template <typename Packet, typename Op>
1113EIGEN_STRONG_INLINE Packet pminmax_propagate_numbers(const Packet& a, const Packet& b, Op op) {
1114 // In this implementation, we take advantage of the fact that pmin/pmax for SSE
1115 // always return a if either a or b is NaN.
1116 Packet not_nan_mask_a = pcmp_eq(a, a);
1117 Packet m = op(a, b);
1118 return pselect<Packet>(not_nan_mask_a, m, b);
1119}
1120
1121template <typename Packet, typename Op>
1122EIGEN_STRONG_INLINE Packet pminmax_propagate_nan(const Packet& a, const Packet& b, Op op) {
1123 // In this implementation, we take advantage of the fact that pmin/pmax for SSE
1124 // always return a if either a or b is NaN.
1125 Packet not_nan_mask_a = pcmp_eq(a, a);
1126 Packet m = op(b, a);
1127 return pselect<Packet>(not_nan_mask_a, m, a);
1128}
1129
1130// Add specializations for min/max with prescribed NaN progation.
1131template <>
1132EIGEN_STRONG_INLINE Packet4f pmin<PropagateNumbers, Packet4f>(const Packet4f& a, const Packet4f& b) {
1133 return pminmax_propagate_numbers(a, b, pmin<Packet4f>);
1134}
1135template <>
1136EIGEN_STRONG_INLINE Packet2d pmin<PropagateNumbers, Packet2d>(const Packet2d& a, const Packet2d& b) {
1137 return pminmax_propagate_numbers(a, b, pmin<Packet2d>);
1138}
1139template <>
1140EIGEN_STRONG_INLINE Packet4f pmax<PropagateNumbers, Packet4f>(const Packet4f& a, const Packet4f& b) {
1141 return pminmax_propagate_numbers(a, b, pmax<Packet4f>);
1142}
1143template <>
1144EIGEN_STRONG_INLINE Packet2d pmax<PropagateNumbers, Packet2d>(const Packet2d& a, const Packet2d& b) {
1145 return pminmax_propagate_numbers(a, b, pmax<Packet2d>);
1146}
1147template <>
1148EIGEN_STRONG_INLINE Packet4f pmin<PropagateNaN, Packet4f>(const Packet4f& a, const Packet4f& b) {
1149 return pminmax_propagate_nan(a, b, pmin<Packet4f>);
1150}
1151template <>
1152EIGEN_STRONG_INLINE Packet2d pmin<PropagateNaN, Packet2d>(const Packet2d& a, const Packet2d& b) {
1153 return pminmax_propagate_nan(a, b, pmin<Packet2d>);
1154}
1155template <>
1156EIGEN_STRONG_INLINE Packet4f pmax<PropagateNaN, Packet4f>(const Packet4f& a, const Packet4f& b) {
1157 return pminmax_propagate_nan(a, b, pmax<Packet4f>);
1158}
1159template <>
1160EIGEN_STRONG_INLINE Packet2d pmax<PropagateNaN, Packet2d>(const Packet2d& a, const Packet2d& b) {
1161 return pminmax_propagate_nan(a, b, pmax<Packet2d>);
1162}
1163
1164template <>
1165EIGEN_STRONG_INLINE Packet4f psignbit(const Packet4f& a) {
1166 return _mm_castsi128_ps(_mm_srai_epi32(_mm_castps_si128(a), 31));
1167}
1168template <>
1169EIGEN_STRONG_INLINE Packet2d psignbit(const Packet2d& a) {
1170 Packet4f tmp = psignbit<Packet4f>(_mm_castpd_ps(a));
1171#ifdef EIGEN_VECTORIZE_AVX
1172 return _mm_castps_pd(_mm_permute_ps(tmp, (shuffle_mask<1, 1, 3, 3>::mask)));
1173#else
1174 return _mm_castps_pd(_mm_shuffle_ps(tmp, tmp, (shuffle_mask<1, 1, 3, 3>::mask)));
1175#endif // EIGEN_VECTORIZE_AVX
1176}
1177template <>
1178EIGEN_STRONG_INLINE Packet4i psignbit(const Packet4i& a) {
1179 return _mm_srai_epi32(a, 31);
1180}
1181template <>
1182EIGEN_STRONG_INLINE Packet4ui psignbit(const Packet4ui& a) {
1183 return pzero(a);
1184}
1185template <>
1186EIGEN_STRONG_INLINE Packet2l psignbit(const Packet2l& a) {
1187 Packet4i tmp = psignbit<Packet4i>(Packet4i(a));
1188 return Packet2l(_mm_shuffle_epi32(tmp, (shuffle_mask<1, 1, 3, 3>::mask)));
1189}
1190
1191template <int N>
1192EIGEN_STRONG_INLINE Packet2l parithmetic_shift_right(const Packet2l& a) {
1193 Packet2l signbit = psignbit(a);
1194 return por(_mm_slli_epi64(signbit, 64 - N), _mm_srli_epi64(a, N));
1195}
1196template <int N>
1197EIGEN_STRONG_INLINE Packet2l plogical_shift_right(const Packet2l& a) {
1198 return _mm_srli_epi64(a, N);
1199}
1200template <int N>
1201EIGEN_STRONG_INLINE Packet2l plogical_shift_left(const Packet2l& a) {
1202 return _mm_slli_epi64(a, N);
1203}
1204template <int N>
1205EIGEN_STRONG_INLINE Packet4i parithmetic_shift_right(const Packet4i& a) {
1206 return _mm_srai_epi32(a, N);
1207}
1208template <int N>
1209EIGEN_STRONG_INLINE Packet4i plogical_shift_right(const Packet4i& a) {
1210 return _mm_srli_epi32(a, N);
1211}
1212template <int N>
1213EIGEN_STRONG_INLINE Packet4i plogical_shift_left(const Packet4i& a) {
1214 return _mm_slli_epi32(a, N);
1215}
1216template <int N>
1217EIGEN_STRONG_INLINE Packet4ui parithmetic_shift_right(const Packet4ui& a) {
1218 return _mm_srli_epi32(a, N);
1219}
1220template <int N>
1221EIGEN_STRONG_INLINE Packet4ui plogical_shift_right(const Packet4ui& a) {
1222 return _mm_srli_epi32(a, N);
1223}
1224template <int N>
1225EIGEN_STRONG_INLINE Packet4ui plogical_shift_left(const Packet4ui& a) {
1226 return _mm_slli_epi32(a, N);
1227}
1228
1229template <>
1230EIGEN_STRONG_INLINE Packet4f pabs(const Packet4f& a) {
1231 const Packet4f mask = _mm_castsi128_ps(_mm_setr_epi32(0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF));
1232 return _mm_and_ps(a, mask);
1233}
1234template <>
1235EIGEN_STRONG_INLINE Packet2d pabs(const Packet2d& a) {
1236 const Packet2d mask = _mm_castsi128_pd(_mm_setr_epi32(0xFFFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF, 0x7FFFFFFF));
1237 return _mm_and_pd(a, mask);
1238}
1239template <>
1240EIGEN_STRONG_INLINE Packet2l pabs(const Packet2l& a) {
1241 Packet2l signbit = psignbit(a);
1242 return _mm_sub_epi64(_mm_xor_si128(a, signbit), signbit);
1243}
1244template <>
1245EIGEN_STRONG_INLINE Packet4i pabs(const Packet4i& a) {
1246#ifdef EIGEN_VECTORIZE_SSSE3
1247 return _mm_abs_epi32(a);
1248#else
1249 Packet4i signbit = psignbit(a);
1250 return _mm_sub_epi32(_mm_xor_si128(a, signbit), signbit);
1251#endif
1252}
1253template <>
1254EIGEN_STRONG_INLINE Packet4ui pabs(const Packet4ui& a) {
1255 return a;
1256}
1257
1258#ifdef EIGEN_VECTORIZE_SSE4_1
1259template <>
1260EIGEN_STRONG_INLINE Packet4f pround<Packet4f>(const Packet4f& a) {
1261 // Unfortunately _mm_round_ps doesn't have a rounding mode to implement numext::round.
1262 const Packet4f mask = pset1frombits<Packet4f>(0x80000000u);
1263 const Packet4f prev0dot5 = pset1frombits<Packet4f>(0x3EFFFFFFu);
1264 return _mm_round_ps(padd(por(pand(a, mask), prev0dot5), a), _MM_FROUND_TO_ZERO);
1265}
1266
1267template <>
1268EIGEN_STRONG_INLINE Packet2d pround<Packet2d>(const Packet2d& a) {
1269 const Packet2d mask = _mm_castsi128_pd(_mm_set_epi64x(0x8000000000000000ull, 0x8000000000000000ull));
1270 const Packet2d prev0dot5 = _mm_castsi128_pd(_mm_set_epi64x(0x3FDFFFFFFFFFFFFFull, 0x3FDFFFFFFFFFFFFFull));
1271 return _mm_round_pd(padd(por(pand(a, mask), prev0dot5), a), _MM_FROUND_TO_ZERO);
1272}
1273
1274template <>
1275EIGEN_STRONG_INLINE Packet4f print<Packet4f>(const Packet4f& a) {
1276 return _mm_round_ps(a, _MM_FROUND_CUR_DIRECTION);
1277}
1278template <>
1279EIGEN_STRONG_INLINE Packet2d print<Packet2d>(const Packet2d& a) {
1280 return _mm_round_pd(a, _MM_FROUND_CUR_DIRECTION);
1281}
1282
1283template <>
1284EIGEN_STRONG_INLINE Packet4f pceil<Packet4f>(const Packet4f& a) {
1285 return _mm_ceil_ps(a);
1286}
1287template <>
1288EIGEN_STRONG_INLINE Packet2d pceil<Packet2d>(const Packet2d& a) {
1289 return _mm_ceil_pd(a);
1290}
1291
1292template <>
1293EIGEN_STRONG_INLINE Packet4f pfloor<Packet4f>(const Packet4f& a) {
1294 return _mm_floor_ps(a);
1295}
1296template <>
1297EIGEN_STRONG_INLINE Packet2d pfloor<Packet2d>(const Packet2d& a) {
1298 return _mm_floor_pd(a);
1299}
1300
1301template <>
1302EIGEN_STRONG_INLINE Packet4f ptrunc<Packet4f>(const Packet4f& a) {
1303 return _mm_round_ps(a, _MM_FROUND_TRUNC);
1304}
1305template <>
1306EIGEN_STRONG_INLINE Packet2d ptrunc<Packet2d>(const Packet2d& a) {
1307 return _mm_round_pd(a, _MM_FROUND_TRUNC);
1308}
1309#endif
1310
1311template <>
1312EIGEN_STRONG_INLINE Packet4f pload<Packet4f>(const float* from) {
1313 EIGEN_DEBUG_ALIGNED_LOAD return _mm_load_ps(from);
1314}
1315template <>
1316EIGEN_STRONG_INLINE Packet2d pload<Packet2d>(const double* from) {
1317 EIGEN_DEBUG_ALIGNED_LOAD return _mm_load_pd(from);
1318}
1319template <>
1320EIGEN_STRONG_INLINE Packet2l pload<Packet2l>(const int64_t* from) {
1321 EIGEN_DEBUG_ALIGNED_LOAD return _mm_load_si128(reinterpret_cast<const __m128i*>(from));
1322}
1323template <>
1324EIGEN_STRONG_INLINE Packet4i pload<Packet4i>(const int* from) {
1325 EIGEN_DEBUG_ALIGNED_LOAD return _mm_load_si128(reinterpret_cast<const __m128i*>(from));
1326}
1327template <>
1328EIGEN_STRONG_INLINE Packet4ui pload<Packet4ui>(const uint32_t* from) {
1329 EIGEN_DEBUG_ALIGNED_LOAD return _mm_load_si128(reinterpret_cast<const __m128i*>(from));
1330}
1331template <>
1332EIGEN_STRONG_INLINE Packet16b pload<Packet16b>(const bool* from) {
1333 EIGEN_DEBUG_ALIGNED_LOAD return _mm_load_si128(reinterpret_cast<const __m128i*>(from));
1334}
1335
1336#if EIGEN_COMP_MSVC
1337template <>
1338EIGEN_STRONG_INLINE Packet4f ploadu<Packet4f>(const float* from) {
1339 EIGEN_DEBUG_UNALIGNED_LOAD
1340 return _mm_loadu_ps(from);
1341}
1342#else
1343// NOTE: with the code below, MSVC's compiler crashes!
1344
1345template <>
1346EIGEN_STRONG_INLINE Packet4f ploadu<Packet4f>(const float* from) {
1347 EIGEN_DEBUG_UNALIGNED_LOAD
1348 return _mm_loadu_ps(from);
1349}
1350#endif
1351
1352template <>
1353EIGEN_STRONG_INLINE Packet2d ploadu<Packet2d>(const double* from) {
1354 EIGEN_DEBUG_UNALIGNED_LOAD
1355 return _mm_loadu_pd(from);
1356}
1357template <>
1358EIGEN_STRONG_INLINE Packet2l ploadu<Packet2l>(const int64_t* from) {
1359 EIGEN_DEBUG_UNALIGNED_LOAD
1360 return _mm_loadu_si128(reinterpret_cast<const __m128i*>(from));
1361}
1362template <>
1363EIGEN_STRONG_INLINE Packet4i ploadu<Packet4i>(const int* from) {
1364 EIGEN_DEBUG_UNALIGNED_LOAD
1365 return _mm_loadu_si128(reinterpret_cast<const __m128i*>(from));
1366}
1367template <>
1368EIGEN_STRONG_INLINE Packet4ui ploadu<Packet4ui>(const uint32_t* from) {
1369 EIGEN_DEBUG_UNALIGNED_LOAD
1370 return _mm_loadu_si128(reinterpret_cast<const __m128i*>(from));
1371}
1372template <>
1373EIGEN_STRONG_INLINE Packet16b ploadu<Packet16b>(const bool* from) {
1374 EIGEN_DEBUG_UNALIGNED_LOAD
1375 return _mm_loadu_si128(reinterpret_cast<const __m128i*>(from));
1376}
1377
1378// Load lower part of packet zero extending.
1379template <typename Packet>
1380EIGEN_STRONG_INLINE Packet ploadl(const typename unpacket_traits<Packet>::type* from);
1381template <>
1382EIGEN_STRONG_INLINE Packet4f ploadl<Packet4f>(const float* from) {
1383 EIGEN_DEBUG_UNALIGNED_LOAD return _mm_castpd_ps(_mm_load_sd(reinterpret_cast<const double*>(from)));
1384}
1385template <>
1386EIGEN_STRONG_INLINE Packet2d ploadl<Packet2d>(const double* from) {
1387 EIGEN_DEBUG_UNALIGNED_LOAD return _mm_load_sd(from);
1388}
1389
1390// Load scalar
1391template <typename Packet>
1392EIGEN_STRONG_INLINE Packet ploads(const typename unpacket_traits<Packet>::type* from);
1393template <>
1394EIGEN_STRONG_INLINE Packet4f ploads<Packet4f>(const float* from) {
1395 EIGEN_DEBUG_UNALIGNED_LOAD return _mm_load_ss(from);
1396}
1397template <>
1398EIGEN_STRONG_INLINE Packet2d ploads<Packet2d>(const double* from) {
1399 EIGEN_DEBUG_UNALIGNED_LOAD return _mm_load_sd(from);
1400}
1401
1402template <>
1403EIGEN_STRONG_INLINE Packet4f ploaddup<Packet4f>(const float* from) {
1404 return vec4f_swizzle1(_mm_castpd_ps(_mm_load_sd(reinterpret_cast<const double*>(from))), 0, 0, 1, 1);
1405}
1406template <>
1407EIGEN_STRONG_INLINE Packet2d ploaddup<Packet2d>(const double* from) {
1408 return pset1<Packet2d>(from[0]);
1409}
1410template <>
1411EIGEN_STRONG_INLINE Packet2l ploaddup<Packet2l>(const int64_t* from) {
1412 return pset1<Packet2l>(from[0]);
1413}
1414template <>
1415EIGEN_STRONG_INLINE Packet4i ploaddup<Packet4i>(const int* from) {
1416 Packet4i tmp;
1417 tmp = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(from));
1418 return vec4i_swizzle1(tmp, 0, 0, 1, 1);
1419}
1420template <>
1421EIGEN_STRONG_INLINE Packet4ui ploaddup<Packet4ui>(const uint32_t* from) {
1422 Packet4ui tmp;
1423 tmp = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(from));
1424 return vec4ui_swizzle1(tmp, 0, 0, 1, 1);
1425}
1426
1427// Loads 8 bools from memory and returns the packet
1428// {b0, b0, b1, b1, b2, b2, b3, b3, b4, b4, b5, b5, b6, b6, b7, b7}
1429template <>
1430EIGEN_STRONG_INLINE Packet16b ploaddup<Packet16b>(const bool* from) {
1431 __m128i tmp = _mm_castpd_si128(pload1<Packet2d>(reinterpret_cast<const double*>(from)));
1432 return _mm_unpacklo_epi8(tmp, tmp);
1433}
1434
1435// Loads 4 bools from memory and returns the packet
1436// {b0, b0 b0, b0, b1, b1, b1, b1, b2, b2, b2, b2, b3, b3, b3, b3}
1437template <>
1438EIGEN_STRONG_INLINE Packet16b ploadquad<Packet16b>(const bool* from) {
1439 __m128i tmp = _mm_castps_si128(pload1<Packet4f>(reinterpret_cast<const float*>(from)));
1440 tmp = _mm_unpacklo_epi8(tmp, tmp);
1441 return _mm_unpacklo_epi16(tmp, tmp);
1442}
1443
1444template <>
1445EIGEN_STRONG_INLINE void pstore<float>(float* to, const Packet4f& from) {
1446 EIGEN_DEBUG_ALIGNED_STORE _mm_store_ps(to, from);
1447}
1448template <>
1449EIGEN_STRONG_INLINE void pstore<double>(double* to, const Packet2d& from) {
1450 EIGEN_DEBUG_ALIGNED_STORE _mm_store_pd(to, from);
1451}
1452template <>
1453EIGEN_STRONG_INLINE void pstore<int64_t>(int64_t* to, const Packet2l& from) {
1454 EIGEN_DEBUG_ALIGNED_STORE _mm_store_si128(reinterpret_cast<__m128i*>(to), from);
1455}
1456template <>
1457EIGEN_STRONG_INLINE void pstore<int>(int* to, const Packet4i& from) {
1458 EIGEN_DEBUG_ALIGNED_STORE _mm_store_si128(reinterpret_cast<__m128i*>(to), from);
1459}
1460template <>
1461EIGEN_STRONG_INLINE void pstore<uint32_t>(uint32_t* to, const Packet4ui& from) {
1462 EIGEN_DEBUG_ALIGNED_STORE _mm_store_si128(reinterpret_cast<__m128i*>(to), from);
1463}
1464template <>
1465EIGEN_STRONG_INLINE void pstore<bool>(bool* to, const Packet16b& from) {
1466 EIGEN_DEBUG_ALIGNED_STORE _mm_store_si128(reinterpret_cast<__m128i*>(to), from);
1467}
1468
1469template <>
1470EIGEN_STRONG_INLINE void pstoreu<double>(double* to, const Packet2d& from) {
1471 EIGEN_DEBUG_UNALIGNED_STORE _mm_storeu_pd(to, from);
1472}
1473template <>
1474EIGEN_STRONG_INLINE void pstoreu<float>(float* to, const Packet4f& from) {
1475 EIGEN_DEBUG_UNALIGNED_STORE _mm_storeu_ps(to, from);
1476}
1477template <>
1478EIGEN_STRONG_INLINE void pstoreu<int64_t>(int64_t* to, const Packet2l& from) {
1479 EIGEN_DEBUG_UNALIGNED_STORE _mm_storeu_si128(reinterpret_cast<__m128i*>(to), from);
1480}
1481template <>
1482EIGEN_STRONG_INLINE void pstoreu<int>(int* to, const Packet4i& from) {
1483 EIGEN_DEBUG_UNALIGNED_STORE _mm_storeu_si128(reinterpret_cast<__m128i*>(to), from);
1484}
1485template <>
1486EIGEN_STRONG_INLINE void pstoreu<uint32_t>(uint32_t* to, const Packet4ui& from) {
1487 EIGEN_DEBUG_UNALIGNED_STORE _mm_storeu_si128(reinterpret_cast<__m128i*>(to), from);
1488}
1489template <>
1490EIGEN_STRONG_INLINE void pstoreu<bool>(bool* to, const Packet16b& from) {
1491 EIGEN_DEBUG_UNALIGNED_STORE _mm_storeu_si128(reinterpret_cast<__m128i*>(to), from);
1492}
1493
1494template <typename Scalar, typename Packet>
1495EIGEN_STRONG_INLINE void pstorel(Scalar* to, const Packet& from);
1496template <>
1497EIGEN_STRONG_INLINE void pstorel(float* to, const Packet4f& from) {
1498 EIGEN_DEBUG_UNALIGNED_STORE _mm_storel_pi(reinterpret_cast<__m64*>(to), from);
1499}
1500template <>
1501EIGEN_STRONG_INLINE void pstorel(double* to, const Packet2d& from) {
1502 EIGEN_DEBUG_UNALIGNED_STORE _mm_storel_pd(to, from);
1503}
1504
1505template <typename Scalar, typename Packet>
1506EIGEN_STRONG_INLINE void pstores(Scalar* to, const Packet& from);
1507template <>
1508EIGEN_STRONG_INLINE void pstores(float* to, const Packet4f& from) {
1509 EIGEN_DEBUG_UNALIGNED_STORE _mm_store_ss(to, from);
1510}
1511template <>
1512EIGEN_STRONG_INLINE void pstores(double* to, const Packet2d& from) {
1513 EIGEN_DEBUG_UNALIGNED_STORE _mm_store_sd(to, from);
1514}
1515
1516template <>
1517EIGEN_STRONG_INLINE Packet4f preverse(const Packet4f& a) {
1518 return _mm_shuffle_ps(a, a, 0x1B);
1519}
1520template <>
1521EIGEN_STRONG_INLINE Packet2d preverse(const Packet2d& a) {
1522 return _mm_shuffle_pd(a, a, 0x1);
1523}
1524template <>
1525EIGEN_STRONG_INLINE Packet2l preverse(const Packet2l& a) {
1526 return _mm_castpd_si128(preverse(_mm_castsi128_pd(a)));
1527}
1528template <>
1529EIGEN_STRONG_INLINE Packet4i preverse(const Packet4i& a) {
1530 return _mm_shuffle_epi32(a, 0x1B);
1531}
1532template <>
1533EIGEN_STRONG_INLINE Packet4ui preverse(const Packet4ui& a) {
1534 return _mm_shuffle_epi32(a, 0x1B);
1535}
1536template <>
1537EIGEN_STRONG_INLINE Packet16b preverse(const Packet16b& a) {
1538#ifdef EIGEN_VECTORIZE_SSSE3
1539 __m128i mask = _mm_set_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
1540 return _mm_shuffle_epi8(a, mask);
1541#else
1542 Packet16b tmp = _mm_shuffle_epi32(a, _MM_SHUFFLE(0, 1, 2, 3));
1543 tmp = _mm_shufflehi_epi16(_mm_shufflelo_epi16(tmp, _MM_SHUFFLE(2, 3, 0, 1)), _MM_SHUFFLE(2, 3, 0, 1));
1544 return _mm_or_si128(_mm_slli_epi16(tmp, 8), _mm_srli_epi16(tmp, 8));
1545#endif
1546}
1547
1548#if EIGEN_COMP_MSVC_STRICT && EIGEN_OS_WIN64
1549// The temporary variable fixes an internal compilation error in vs <= 2008 and a wrong-result bug in vs 2010
1550// Direct of the struct members fixed bug #62.
1551template <>
1552EIGEN_STRONG_INLINE float pfirst<Packet4f>(const Packet4f& a) {
1553 return a.m128_f32[0];
1554}
1555template <>
1556EIGEN_STRONG_INLINE double pfirst<Packet2d>(const Packet2d& a) {
1557 return a.m128d_f64[0];
1558}
1559template <>
1560EIGEN_STRONG_INLINE int64_t pfirst<Packet2l>(const Packet2l& a) {
1561 int64_t x = _mm_extract_epi64_0(a);
1562 return x;
1563}
1564template <>
1565EIGEN_STRONG_INLINE int pfirst<Packet4i>(const Packet4i& a) {
1566 int x = _mm_cvtsi128_si32(a);
1567 return x;
1568}
1569template <>
1570EIGEN_STRONG_INLINE uint32_t pfirst<Packet4ui>(const Packet4ui& a) {
1571 uint32_t x = numext::bit_cast<uint32_t>(_mm_cvtsi128_si32(a));
1572 return x;
1573}
1574#elif EIGEN_COMP_MSVC_STRICT
1575// The temporary variable fixes an internal compilation error in vs <= 2008 and a wrong-result bug in vs 2010
1576template <>
1577EIGEN_STRONG_INLINE float pfirst<Packet4f>(const Packet4f& a) {
1578 float x = _mm_cvtss_f32(a);
1579 return x;
1580}
1581template <>
1582EIGEN_STRONG_INLINE double pfirst<Packet2d>(const Packet2d& a) {
1583 double x = _mm_cvtsd_f64(a);
1584 return x;
1585}
1586template <>
1587EIGEN_STRONG_INLINE int64_t pfirst<Packet2l>(const Packet2l& a) {
1588 int64_t x = _mm_extract_epi64_0(a);
1589 return x;
1590}
1591template <>
1592EIGEN_STRONG_INLINE int pfirst<Packet4i>(const Packet4i& a) {
1593 int x = _mm_cvtsi128_si32(a);
1594 return x;
1595}
1596template <>
1597EIGEN_STRONG_INLINE uint32_t pfirst<Packet4ui>(const Packet4ui& a) {
1598 uint32_t x = numext::bit_cast<uint32_t>(_mm_cvtsi128_si32(a));
1599 return x;
1600}
1601#else
1602template <>
1603EIGEN_STRONG_INLINE float pfirst<Packet4f>(const Packet4f& a) {
1604 return _mm_cvtss_f32(a);
1605}
1606template <>
1607EIGEN_STRONG_INLINE double pfirst<Packet2d>(const Packet2d& a) {
1608 return _mm_cvtsd_f64(a);
1609}
1610template <>
1611EIGEN_STRONG_INLINE int64_t pfirst<Packet2l>(const Packet2l& a) {
1612 return _mm_extract_epi64_0(a);
1613}
1614template <>
1615EIGEN_STRONG_INLINE int pfirst<Packet4i>(const Packet4i& a) {
1616 return _mm_cvtsi128_si32(a);
1617}
1618template <>
1619EIGEN_STRONG_INLINE uint32_t pfirst<Packet4ui>(const Packet4ui& a) {
1620 return numext::bit_cast<uint32_t>(_mm_cvtsi128_si32(a));
1621}
1622#endif
1623template <>
1624EIGEN_STRONG_INLINE bool pfirst<Packet16b>(const Packet16b& a) {
1625 int x = _mm_cvtsi128_si32(a);
1626 return static_cast<bool>(x & 1);
1627}
1628
1629template <>
1630EIGEN_STRONG_INLINE Packet4f pgather<float, Packet4f>(const float* from, Index stride) {
1631 return _mm_set_ps(from[3 * stride], from[2 * stride], from[1 * stride], from[0 * stride]);
1632}
1633template <>
1634EIGEN_STRONG_INLINE Packet2d pgather<double, Packet2d>(const double* from, Index stride) {
1635 return _mm_set_pd(from[1 * stride], from[0 * stride]);
1636}
1637template <>
1638EIGEN_STRONG_INLINE Packet2l pgather<int64_t, Packet2l>(const int64_t* from, Index stride) {
1639 return _mm_set_epi64x(from[1 * stride], from[0 * stride]);
1640}
1641template <>
1642EIGEN_STRONG_INLINE Packet4i pgather<int, Packet4i>(const int* from, Index stride) {
1643 return _mm_set_epi32(from[3 * stride], from[2 * stride], from[1 * stride], from[0 * stride]);
1644}
1645template <>
1646EIGEN_STRONG_INLINE Packet4ui pgather<uint32_t, Packet4ui>(const uint32_t* from, Index stride) {
1647 return _mm_set_epi32(numext::bit_cast<int32_t>(from[3 * stride]), numext::bit_cast<int32_t>(from[2 * stride]),
1648 numext::bit_cast<int32_t>(from[1 * stride]), numext::bit_cast<int32_t>(from[0 * stride]));
1649}
1650
1651template <>
1652EIGEN_STRONG_INLINE Packet16b pgather<bool, Packet16b>(const bool* from, Index stride) {
1653 return _mm_set_epi8(from[15 * stride], from[14 * stride], from[13 * stride], from[12 * stride], from[11 * stride],
1654 from[10 * stride], from[9 * stride], from[8 * stride], from[7 * stride], from[6 * stride],
1655 from[5 * stride], from[4 * stride], from[3 * stride], from[2 * stride], from[1 * stride],
1656 from[0 * stride]);
1657}
1658
1659template <>
1660EIGEN_STRONG_INLINE void pscatter<float, Packet4f>(float* to, const Packet4f& from, Index stride) {
1661 to[stride * 0] = pfirst(from);
1662 to[stride * 1] = pfirst(_mm_shuffle_ps(from, from, 1));
1663 to[stride * 2] = pfirst(_mm_shuffle_ps(from, from, 2));
1664 to[stride * 3] = pfirst(_mm_shuffle_ps(from, from, 3));
1665}
1666template <>
1667EIGEN_STRONG_INLINE void pscatter<double, Packet2d>(double* to, const Packet2d& from, Index stride) {
1668 to[stride * 0] = pfirst(from);
1669 to[stride * 1] = pfirst(preverse(from));
1670}
1671template <>
1672EIGEN_STRONG_INLINE void pscatter<int64_t, Packet2l>(int64_t* to, const Packet2l& from, Index stride) {
1673 to[stride * 0] = pfirst(from);
1674 to[stride * 1] = pfirst(preverse(from));
1675}
1676template <>
1677EIGEN_STRONG_INLINE void pscatter<int, Packet4i>(int* to, const Packet4i& from, Index stride) {
1678 to[stride * 0] = _mm_cvtsi128_si32(from);
1679 to[stride * 1] = _mm_cvtsi128_si32(_mm_shuffle_epi32(from, 1));
1680 to[stride * 2] = _mm_cvtsi128_si32(_mm_shuffle_epi32(from, 2));
1681 to[stride * 3] = _mm_cvtsi128_si32(_mm_shuffle_epi32(from, 3));
1682}
1683template <>
1684EIGEN_STRONG_INLINE void pscatter<uint32_t, Packet4ui>(uint32_t* to, const Packet4ui& from, Index stride) {
1685 to[stride * 0] = numext::bit_cast<uint32_t>(_mm_cvtsi128_si32(from));
1686 to[stride * 1] = numext::bit_cast<uint32_t>(_mm_cvtsi128_si32(_mm_shuffle_epi32(from, 1)));
1687 to[stride * 2] = numext::bit_cast<uint32_t>(_mm_cvtsi128_si32(_mm_shuffle_epi32(from, 2)));
1688 to[stride * 3] = numext::bit_cast<uint32_t>(_mm_cvtsi128_si32(_mm_shuffle_epi32(from, 3)));
1689}
1690template <>
1691EIGEN_STRONG_INLINE void pscatter<bool, Packet16b>(bool* to, const Packet16b& from, Index stride) {
1692 to[4 * stride * 0] = _mm_cvtsi128_si32(from);
1693 to[4 * stride * 1] = _mm_cvtsi128_si32(_mm_shuffle_epi32(from, 1));
1694 to[4 * stride * 2] = _mm_cvtsi128_si32(_mm_shuffle_epi32(from, 2));
1695 to[4 * stride * 3] = _mm_cvtsi128_si32(_mm_shuffle_epi32(from, 3));
1696}
1697
1698// some compilers might be tempted to perform multiple moves instead of using a vector path.
1699template <>
1700EIGEN_STRONG_INLINE void pstore1<Packet4f>(float* to, const float& a) {
1701 Packet4f pa = _mm_set_ss(a);
1702 pstore(to, Packet4f(vec4f_swizzle1(pa, 0, 0, 0, 0)));
1703}
1704// some compilers might be tempted to perform multiple moves instead of using a vector path.
1705template <>
1706EIGEN_STRONG_INLINE void pstore1<Packet2d>(double* to, const double& a) {
1707 Packet2d pa = _mm_set_sd(a);
1708 pstore(to, Packet2d(vec2d_swizzle1(pa, 0, 0)));
1709}
1710
1711#if EIGEN_COMP_PGI && EIGEN_COMP_PGI < 1900
1712typedef const void* SsePrefetchPtrType;
1713#else
1714typedef const char* SsePrefetchPtrType;
1715#endif
1716
1717#ifndef EIGEN_VECTORIZE_AVX
1718template <>
1719EIGEN_STRONG_INLINE void prefetch<float>(const float* addr) {
1720 _mm_prefetch((SsePrefetchPtrType)(addr), _MM_HINT_T0);
1721}
1722template <>
1723EIGEN_STRONG_INLINE void prefetch<double>(const double* addr) {
1724 _mm_prefetch((SsePrefetchPtrType)(addr), _MM_HINT_T0);
1725}
1726template <>
1727EIGEN_STRONG_INLINE void prefetch<int>(const int* addr) {
1728 _mm_prefetch((SsePrefetchPtrType)(addr), _MM_HINT_T0);
1729}
1730template <>
1731EIGEN_STRONG_INLINE void prefetch<int64_t>(const int64_t* addr) {
1732 _mm_prefetch((SsePrefetchPtrType)(addr), _MM_HINT_T0);
1733}
1734template <>
1735EIGEN_STRONG_INLINE void prefetch<uint32_t>(const uint32_t* addr) {
1736 _mm_prefetch((SsePrefetchPtrType)(addr), _MM_HINT_T0);
1737}
1738#endif
1739
1740template <>
1741EIGEN_STRONG_INLINE Packet4f pfrexp<Packet4f>(const Packet4f& a, Packet4f& exponent) {
1742 return pfrexp_generic(a, exponent);
1743}
1744
1745// Extract exponent without existence of Packet2l.
1746template <>
1747EIGEN_STRONG_INLINE Packet2d pfrexp_generic_get_biased_exponent(const Packet2d& a) {
1748 const Packet2d cst_exp_mask = pset1frombits<Packet2d>(static_cast<uint64_t>(0x7ff0000000000000ull));
1749 __m128i a_expo = _mm_srli_epi64(_mm_castpd_si128(pand(a, cst_exp_mask)), 52);
1750 return _mm_cvtepi32_pd(vec4i_swizzle1(a_expo, 0, 2, 1, 3));
1751}
1752
1753template <>
1754EIGEN_STRONG_INLINE Packet2d pfrexp<Packet2d>(const Packet2d& a, Packet2d& exponent) {
1755 return pfrexp_generic(a, exponent);
1756}
1757
1758template <>
1759EIGEN_STRONG_INLINE Packet4f pldexp<Packet4f>(const Packet4f& a, const Packet4f& exponent) {
1760 return pldexp_generic(a, exponent);
1761}
1762
1763// We specialize pldexp here, since the generic implementation uses Packet2l, which is not well
1764// supported by SSE, and has more range than is needed for exponents.
1765// TODO(rmlarsen): Remove this specialization once Packet2l has support or casting.
1766template <>
1767EIGEN_STRONG_INLINE Packet2d pldexp<Packet2d>(const Packet2d& a, const Packet2d& exponent) {
1768 // Clamp exponent to [-2099, 2099]
1769 const Packet2d max_exponent = pset1<Packet2d>(2099.0);
1770 const Packet2d e = pmin(pmax(exponent, pnegate(max_exponent)), max_exponent);
1771
1772 // Convert e to integer and swizzle to low-order bits.
1773 const Packet4i ei = vec4i_swizzle1(_mm_cvtpd_epi32(e), 0, 3, 1, 3);
1774
1775 // Split 2^e into four factors and multiply:
1776 const Packet4i bias = _mm_set_epi32(0, 1023, 0, 1023);
1777 Packet4i b = parithmetic_shift_right<2>(ei); // floor(e/4)
1778 Packet2d c = _mm_castsi128_pd(_mm_slli_epi64(padd(b, bias), 52)); // 2^b
1779 Packet2d out = pmul(pmul(pmul(a, c), c), c); // a * 2^(3b)
1780 b = psub(psub(psub(ei, b), b), b); // e - 3b
1781 c = _mm_castsi128_pd(_mm_slli_epi64(padd(b, bias), 52)); // 2^(e - 3b)
1782 out = pmul(out, c); // a * 2^e
1783 return out;
1784}
1785
1786// with AVX, the default implementations based on pload1 are faster
1787#ifndef __AVX__
1788template <>
1789EIGEN_STRONG_INLINE void pbroadcast4<Packet4f>(const float* a, Packet4f& a0, Packet4f& a1, Packet4f& a2, Packet4f& a3) {
1790 a3 = pload<Packet4f>(a);
1791 a0 = vec4f_swizzle1(a3, 0, 0, 0, 0);
1792 a1 = vec4f_swizzle1(a3, 1, 1, 1, 1);
1793 a2 = vec4f_swizzle1(a3, 2, 2, 2, 2);
1794 a3 = vec4f_swizzle1(a3, 3, 3, 3, 3);
1795}
1796template <>
1797EIGEN_STRONG_INLINE void pbroadcast4<Packet2d>(const double* a, Packet2d& a0, Packet2d& a1, Packet2d& a2,
1798 Packet2d& a3) {
1799#ifdef EIGEN_VECTORIZE_SSE3
1800 a0 = _mm_loaddup_pd(a + 0);
1801 a1 = _mm_loaddup_pd(a + 1);
1802 a2 = _mm_loaddup_pd(a + 2);
1803 a3 = _mm_loaddup_pd(a + 3);
1804#else
1805 a1 = pload<Packet2d>(a);
1806 a0 = vec2d_swizzle1(a1, 0, 0);
1807 a1 = vec2d_swizzle1(a1, 1, 1);
1808 a3 = pload<Packet2d>(a + 2);
1809 a2 = vec2d_swizzle1(a3, 0, 0);
1810 a3 = vec2d_swizzle1(a3, 1, 1);
1811#endif
1812}
1813#endif
1814
1815EIGEN_STRONG_INLINE void punpackp(Packet4f* vecs) {
1816 vecs[1] = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(vecs[0]), 0x55));
1817 vecs[2] = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(vecs[0]), 0xAA));
1818 vecs[3] = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(vecs[0]), 0xFF));
1819 vecs[0] = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(vecs[0]), 0x00));
1820}
1821
1822template <>
1823EIGEN_STRONG_INLINE float predux<Packet4f>(const Packet4f& a) {
1824 // Disable SSE3 _mm_hadd_pd that is extremely slow on all existing Intel's architectures
1825 // (from Nehalem to Haswell)
1826 // #ifdef EIGEN_VECTORIZE_SSE3
1827 // Packet4f tmp = _mm_add_ps(a, vec4f_swizzle1(a,2,3,2,3));
1828 // return pfirst<Packet4f>(_mm_hadd_ps(tmp, tmp));
1829 // #else
1830 Packet4f tmp = _mm_add_ps(a, _mm_movehl_ps(a, a));
1831 return pfirst<Packet4f>(_mm_add_ss(tmp, _mm_shuffle_ps(tmp, tmp, 1)));
1832 // #endif
1833}
1834
1835template <>
1836EIGEN_STRONG_INLINE double predux<Packet2d>(const Packet2d& a) {
1837 // Disable SSE3 _mm_hadd_pd that is extremely slow on all existing Intel's architectures
1838 // (from Nehalem to Haswell)
1839 // #ifdef EIGEN_VECTORIZE_SSE3
1840 // return pfirst<Packet2d>(_mm_hadd_pd(a, a));
1841 // #else
1842 return pfirst<Packet2d>(_mm_add_sd(a, _mm_unpackhi_pd(a, a)));
1843 // #endif
1844}
1845
1846template <>
1847EIGEN_STRONG_INLINE int64_t predux<Packet2l>(const Packet2l& a) {
1848 return pfirst<Packet2l>(_mm_add_epi64(a, _mm_unpackhi_epi64(a, a)));
1849}
1850
1851#ifdef EIGEN_VECTORIZE_SSSE3
1852template <>
1853EIGEN_STRONG_INLINE int predux<Packet4i>(const Packet4i& a) {
1854 Packet4i tmp0 = _mm_hadd_epi32(a, a);
1855 return pfirst<Packet4i>(_mm_hadd_epi32(tmp0, tmp0));
1856}
1857template <>
1858EIGEN_STRONG_INLINE uint32_t predux<Packet4ui>(const Packet4ui& a) {
1859 Packet4ui tmp0 = _mm_hadd_epi32(a, a);
1860 return pfirst<Packet4ui>(_mm_hadd_epi32(tmp0, tmp0));
1861}
1862#else
1863template <>
1864EIGEN_STRONG_INLINE int predux<Packet4i>(const Packet4i& a) {
1865 Packet4i tmp = _mm_add_epi32(a, _mm_unpackhi_epi64(a, a));
1866 return pfirst(tmp) + pfirst<Packet4i>(_mm_shuffle_epi32(tmp, 1));
1867}
1868template <>
1869EIGEN_STRONG_INLINE uint32_t predux<Packet4ui>(const Packet4ui& a) {
1870 Packet4ui tmp = _mm_add_epi32(a, _mm_unpackhi_epi64(a, a));
1871 return pfirst(tmp) + pfirst<Packet4ui>(_mm_shuffle_epi32(tmp, 1));
1872}
1873#endif
1874
1875template <>
1876EIGEN_STRONG_INLINE bool predux<Packet16b>(const Packet16b& a) {
1877 Packet4i tmp = _mm_or_si128(a, _mm_unpackhi_epi64(a, a));
1878 return (pfirst(tmp) != 0) || (pfirst<Packet4i>(_mm_shuffle_epi32(tmp, 1)) != 0);
1879}
1880
1881// Other reduction functions:
1882
1883// mul
1884template <>
1885EIGEN_STRONG_INLINE float predux_mul<Packet4f>(const Packet4f& a) {
1886 Packet4f tmp = _mm_mul_ps(a, _mm_movehl_ps(a, a));
1887 return pfirst<Packet4f>(_mm_mul_ss(tmp, _mm_shuffle_ps(tmp, tmp, 1)));
1888}
1889template <>
1890EIGEN_STRONG_INLINE double predux_mul<Packet2d>(const Packet2d& a) {
1891 return pfirst<Packet2d>(_mm_mul_sd(a, _mm_unpackhi_pd(a, a)));
1892}
1893template <>
1894EIGEN_STRONG_INLINE int64_t predux_mul<Packet2l>(const Packet2l& a) {
1895 EIGEN_ALIGN16 int64_t aux[2];
1896 pstore(aux, a);
1897 return aux[0] * aux[1];
1898}
1899template <>
1900EIGEN_STRONG_INLINE int predux_mul<Packet4i>(const Packet4i& a) {
1901 // after some experiments, it is seems this is the fastest way to implement it
1902 // for GCC (e.g., reusing pmul is very slow!)
1903 // TODO try to call _mm_mul_epu32 directly
1904 EIGEN_ALIGN16 int aux[4];
1905 pstore(aux, a);
1906 return (aux[0] * aux[1]) * (aux[2] * aux[3]);
1907}
1908template <>
1909EIGEN_STRONG_INLINE uint32_t predux_mul<Packet4ui>(const Packet4ui& a) {
1910 // after some experiments, it is seems this is the fastest way to implement it
1911 // for GCC (eg., reusing pmul is very slow !)
1912 // TODO try to call _mm_mul_epu32 directly
1913 EIGEN_ALIGN16 uint32_t aux[4];
1914 pstore(aux, a);
1915 return (aux[0] * aux[1]) * (aux[2] * aux[3]);
1916}
1917
1918template <>
1919EIGEN_STRONG_INLINE bool predux_mul<Packet16b>(const Packet16b& a) {
1920 Packet4i tmp = _mm_and_si128(a, _mm_unpackhi_epi64(a, a));
1921 return ((pfirst<Packet4i>(tmp) == 0x01010101) && (pfirst<Packet4i>(_mm_shuffle_epi32(tmp, 1)) == 0x01010101));
1922}
1923
1924// min
1925template <>
1926EIGEN_STRONG_INLINE float predux_min<Packet4f>(const Packet4f& a) {
1927 Packet4f tmp = _mm_min_ps(a, _mm_movehl_ps(a, a));
1928 return pfirst<Packet4f>(_mm_min_ss(tmp, _mm_shuffle_ps(tmp, tmp, 1)));
1929}
1930template <>
1931EIGEN_STRONG_INLINE double predux_min<Packet2d>(const Packet2d& a) {
1932 return pfirst<Packet2d>(_mm_min_sd(a, _mm_unpackhi_pd(a, a)));
1933}
1934template <>
1935EIGEN_STRONG_INLINE int predux_min<Packet4i>(const Packet4i& a) {
1936#ifdef EIGEN_VECTORIZE_SSE4_1
1937 Packet4i tmp = _mm_min_epi32(a, _mm_shuffle_epi32(a, _MM_SHUFFLE(0, 0, 3, 2)));
1938 return pfirst<Packet4i>(_mm_min_epi32(tmp, _mm_shuffle_epi32(tmp, 1)));
1939#else
1940 // after some experiments, it is seems this is the fastest way to implement it
1941 // for GCC (eg., it does not like using std::min after the pstore !!)
1942 EIGEN_ALIGN16 int aux[4];
1943 pstore(aux, a);
1944 int aux0 = aux[0] < aux[1] ? aux[0] : aux[1];
1945 int aux2 = aux[2] < aux[3] ? aux[2] : aux[3];
1946 return aux0 < aux2 ? aux0 : aux2;
1947#endif // EIGEN_VECTORIZE_SSE4_1
1948}
1949template <>
1950EIGEN_STRONG_INLINE uint32_t predux_min<Packet4ui>(const Packet4ui& a) {
1951#ifdef EIGEN_VECTORIZE_SSE4_1
1952 Packet4ui tmp = _mm_min_epu32(a, _mm_shuffle_epi32(a, _MM_SHUFFLE(0, 0, 3, 2)));
1953 return pfirst<Packet4ui>(_mm_min_epu32(tmp, _mm_shuffle_epi32(tmp, 1)));
1954#else
1955 // after some experiments, it is seems this is the fastest way to implement it
1956 // for GCC (eg., it does not like using std::min after the pstore !!)
1957 EIGEN_ALIGN16 uint32_t aux[4];
1958 pstore(aux, a);
1959 uint32_t aux0 = aux[0] < aux[1] ? aux[0] : aux[1];
1960 uint32_t aux2 = aux[2] < aux[3] ? aux[2] : aux[3];
1961 return aux0 < aux2 ? aux0 : aux2;
1962#endif // EIGEN_VECTORIZE_SSE4_1
1963}
1964
1965// max
1966template <>
1967EIGEN_STRONG_INLINE float predux_max<Packet4f>(const Packet4f& a) {
1968 Packet4f tmp = _mm_max_ps(a, _mm_movehl_ps(a, a));
1969 return pfirst<Packet4f>(_mm_max_ss(tmp, _mm_shuffle_ps(tmp, tmp, 1)));
1970}
1971template <>
1972EIGEN_STRONG_INLINE double predux_max<Packet2d>(const Packet2d& a) {
1973 return pfirst<Packet2d>(_mm_max_sd(a, _mm_unpackhi_pd(a, a)));
1974}
1975template <>
1976EIGEN_STRONG_INLINE int predux_max<Packet4i>(const Packet4i& a) {
1977#ifdef EIGEN_VECTORIZE_SSE4_1
1978 Packet4i tmp = _mm_max_epi32(a, _mm_shuffle_epi32(a, _MM_SHUFFLE(0, 0, 3, 2)));
1979 return pfirst<Packet4i>(_mm_max_epi32(tmp, _mm_shuffle_epi32(tmp, 1)));
1980#else
1981 // after some experiments, it is seems this is the fastest way to implement it
1982 // for GCC (eg., it does not like using std::min after the pstore !!)
1983 EIGEN_ALIGN16 int aux[4];
1984 pstore(aux, a);
1985 int aux0 = aux[0] > aux[1] ? aux[0] : aux[1];
1986 int aux2 = aux[2] > aux[3] ? aux[2] : aux[3];
1987 return aux0 > aux2 ? aux0 : aux2;
1988#endif // EIGEN_VECTORIZE_SSE4_1
1989}
1990template <>
1991EIGEN_STRONG_INLINE uint32_t predux_max<Packet4ui>(const Packet4ui& a) {
1992#ifdef EIGEN_VECTORIZE_SSE4_1
1993 Packet4ui tmp = _mm_max_epu32(a, _mm_shuffle_epi32(a, _MM_SHUFFLE(0, 0, 3, 2)));
1994 return pfirst<Packet4ui>(_mm_max_epu32(tmp, _mm_shuffle_epi32(tmp, 1)));
1995#else
1996 // after some experiments, it is seems this is the fastest way to implement it
1997 // for GCC (eg., it does not like using std::min after the pstore !!)
1998 EIGEN_ALIGN16 uint32_t aux[4];
1999 pstore(aux, a);
2000 uint32_t aux0 = aux[0] > aux[1] ? aux[0] : aux[1];
2001 uint32_t aux2 = aux[2] > aux[3] ? aux[2] : aux[3];
2002 return aux0 > aux2 ? aux0 : aux2;
2003#endif // EIGEN_VECTORIZE_SSE4_1
2004}
2005
2006// not needed yet
2007// template<> EIGEN_STRONG_INLINE bool predux_all(const Packet4f& x)
2008// {
2009// return _mm_movemask_ps(x) == 0xF;
2010// }
2011
2012template <>
2013EIGEN_STRONG_INLINE bool predux_any(const Packet2d& x) {
2014 return _mm_movemask_pd(x) != 0x0;
2015}
2016
2017template <>
2018EIGEN_STRONG_INLINE bool predux_any(const Packet4f& x) {
2019 return _mm_movemask_ps(x) != 0x0;
2020}
2021
2022template <>
2023EIGEN_STRONG_INLINE bool predux_any(const Packet2l& x) {
2024 return _mm_movemask_pd(_mm_castsi128_pd(x)) != 0x0;
2025}
2026
2027template <>
2028EIGEN_STRONG_INLINE bool predux_any(const Packet4i& x) {
2029 return _mm_movemask_ps(_mm_castsi128_ps(x)) != 0x0;
2030}
2031template <>
2032EIGEN_STRONG_INLINE bool predux_any(const Packet4ui& x) {
2033 return _mm_movemask_ps(_mm_castsi128_ps(x)) != 0x0;
2034}
2035
2036EIGEN_STRONG_INLINE void ptranspose(PacketBlock<Packet4f, 4>& kernel) {
2037 _MM_TRANSPOSE4_PS(kernel.packet[0], kernel.packet[1], kernel.packet[2], kernel.packet[3]);
2038}
2039
2040EIGEN_STRONG_INLINE void ptranspose(PacketBlock<Packet2d, 2>& kernel) {
2041 __m128d tmp = _mm_unpackhi_pd(kernel.packet[0], kernel.packet[1]);
2042 kernel.packet[0] = _mm_unpacklo_pd(kernel.packet[0], kernel.packet[1]);
2043 kernel.packet[1] = tmp;
2044}
2045
2046EIGEN_STRONG_INLINE void ptranspose(PacketBlock<Packet2l, 2>& kernel) {
2047 __m128i tmp = _mm_unpackhi_epi64(kernel.packet[0], kernel.packet[1]);
2048 kernel.packet[0] = _mm_unpacklo_epi64(kernel.packet[0], kernel.packet[1]);
2049 kernel.packet[1] = tmp;
2050}
2051
2052EIGEN_STRONG_INLINE void ptranspose(PacketBlock<Packet4i, 4>& kernel) {
2053 __m128i T0 = _mm_unpacklo_epi32(kernel.packet[0], kernel.packet[1]);
2054 __m128i T1 = _mm_unpacklo_epi32(kernel.packet[2], kernel.packet[3]);
2055 __m128i T2 = _mm_unpackhi_epi32(kernel.packet[0], kernel.packet[1]);
2056 __m128i T3 = _mm_unpackhi_epi32(kernel.packet[2], kernel.packet[3]);
2057
2058 kernel.packet[0] = _mm_unpacklo_epi64(T0, T1);
2059 kernel.packet[1] = _mm_unpackhi_epi64(T0, T1);
2060 kernel.packet[2] = _mm_unpacklo_epi64(T2, T3);
2061 kernel.packet[3] = _mm_unpackhi_epi64(T2, T3);
2062}
2063EIGEN_STRONG_INLINE void ptranspose(PacketBlock<Packet4ui, 4>& kernel) {
2064 ptranspose((PacketBlock<Packet4i, 4>&)kernel);
2065}
2066
2067EIGEN_STRONG_INLINE void ptranspose(PacketBlock<Packet16b, 4>& kernel) {
2068 __m128i T0 = _mm_unpacklo_epi8(kernel.packet[0], kernel.packet[1]);
2069 __m128i T1 = _mm_unpackhi_epi8(kernel.packet[0], kernel.packet[1]);
2070 __m128i T2 = _mm_unpacklo_epi8(kernel.packet[2], kernel.packet[3]);
2071 __m128i T3 = _mm_unpackhi_epi8(kernel.packet[2], kernel.packet[3]);
2072 kernel.packet[0] = _mm_unpacklo_epi16(T0, T2);
2073 kernel.packet[1] = _mm_unpackhi_epi16(T0, T2);
2074 kernel.packet[2] = _mm_unpacklo_epi16(T1, T3);
2075 kernel.packet[3] = _mm_unpackhi_epi16(T1, T3);
2076}
2077
2078EIGEN_STRONG_INLINE void ptranspose(PacketBlock<Packet16b, 16>& kernel) {
2079 // If we number the elements in the input thus:
2080 // kernel.packet[ 0] = {00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 0a, 0b, 0c, 0d, 0e, 0f}
2081 // kernel.packet[ 1] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 1a, 1b, 1c, 1d, 1e, 1f}
2082 // ...
2083 // kernel.packet[15] = {f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, fa, fb, fc, fd, fe, ff},
2084 //
2085 // the desired output is:
2086 // kernel.packet[ 0] = {00, 10, 20, 30, 40, 50, 60, 70, 80, 90, a0, b0, c0, d0, e0, f0}
2087 // kernel.packet[ 1] = {01, 11, 21, 31, 41, 51, 61, 71, 81, 91, a1, b1, c1, d1, e1, f1}
2088 // ...
2089 // kernel.packet[15] = {0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f, 9f, af, bf, cf, df, ef, ff},
2090 __m128i t0 =
2091 _mm_unpacklo_epi8(kernel.packet[0], kernel.packet[1]); // 00 10 01 11 02 12 03 13 04 14 05 15 06 16 07 17
2092 __m128i t1 =
2093 _mm_unpackhi_epi8(kernel.packet[0], kernel.packet[1]); // 08 18 09 19 0a 1a 0b 1b 0c 1c 0d 1d 0e 1e 0f 1f
2094 __m128i t2 =
2095 _mm_unpacklo_epi8(kernel.packet[2], kernel.packet[3]); // 20 30 21 31 22 32 ... 27 37
2096 __m128i t3 =
2097 _mm_unpackhi_epi8(kernel.packet[2], kernel.packet[3]); // 28 38 29 39 2a 3a ... 2f 3f
2098 __m128i t4 =
2099 _mm_unpacklo_epi8(kernel.packet[4], kernel.packet[5]); // 40 50 41 51 42 52 47 57
2100 __m128i t5 = _mm_unpackhi_epi8(kernel.packet[4], kernel.packet[5]); // 48 58 49 59 4a 5a
2101 __m128i t6 = _mm_unpacklo_epi8(kernel.packet[6], kernel.packet[7]);
2102 __m128i t7 = _mm_unpackhi_epi8(kernel.packet[6], kernel.packet[7]);
2103 __m128i t8 = _mm_unpacklo_epi8(kernel.packet[8], kernel.packet[9]);
2104 __m128i t9 = _mm_unpackhi_epi8(kernel.packet[8], kernel.packet[9]);
2105 __m128i ta = _mm_unpacklo_epi8(kernel.packet[10], kernel.packet[11]);
2106 __m128i tb = _mm_unpackhi_epi8(kernel.packet[10], kernel.packet[11]);
2107 __m128i tc = _mm_unpacklo_epi8(kernel.packet[12], kernel.packet[13]);
2108 __m128i td = _mm_unpackhi_epi8(kernel.packet[12], kernel.packet[13]);
2109 __m128i te = _mm_unpacklo_epi8(kernel.packet[14], kernel.packet[15]);
2110 __m128i tf = _mm_unpackhi_epi8(kernel.packet[14], kernel.packet[15]);
2111
2112 __m128i s0 = _mm_unpacklo_epi16(t0, t2); // 00 10 20 30 01 11 21 31 02 12 22 32 03 13 23 33
2113 __m128i s1 = _mm_unpackhi_epi16(t0, t2); // 04 14 24 34
2114 __m128i s2 = _mm_unpacklo_epi16(t1, t3); // 08 18 28 38 ...
2115 __m128i s3 = _mm_unpackhi_epi16(t1, t3); // 0c 1c 2c 3c ...
2116 __m128i s4 = _mm_unpacklo_epi16(t4, t6); // 40 50 60 70 41 51 61 71 42 52 62 72 43 53 63 73
2117 __m128i s5 = _mm_unpackhi_epi16(t4, t6); // 44 54 64 74 ...
2118 __m128i s6 = _mm_unpacklo_epi16(t5, t7);
2119 __m128i s7 = _mm_unpackhi_epi16(t5, t7);
2120 __m128i s8 = _mm_unpacklo_epi16(t8, ta);
2121 __m128i s9 = _mm_unpackhi_epi16(t8, ta);
2122 __m128i sa = _mm_unpacklo_epi16(t9, tb);
2123 __m128i sb = _mm_unpackhi_epi16(t9, tb);
2124 __m128i sc = _mm_unpacklo_epi16(tc, te);
2125 __m128i sd = _mm_unpackhi_epi16(tc, te);
2126 __m128i se = _mm_unpacklo_epi16(td, tf);
2127 __m128i sf = _mm_unpackhi_epi16(td, tf);
2128
2129 __m128i u0 = _mm_unpacklo_epi32(s0, s4); // 00 10 20 30 40 50 60 70 01 11 21 31 41 51 61 71
2130 __m128i u1 = _mm_unpackhi_epi32(s0, s4); // 02 12 22 32 42 52 62 72 03 13 23 33 43 53 63 73
2131 __m128i u2 = _mm_unpacklo_epi32(s1, s5);
2132 __m128i u3 = _mm_unpackhi_epi32(s1, s5);
2133 __m128i u4 = _mm_unpacklo_epi32(s2, s6);
2134 __m128i u5 = _mm_unpackhi_epi32(s2, s6);
2135 __m128i u6 = _mm_unpacklo_epi32(s3, s7);
2136 __m128i u7 = _mm_unpackhi_epi32(s3, s7);
2137 __m128i u8 = _mm_unpacklo_epi32(s8, sc);
2138 __m128i u9 = _mm_unpackhi_epi32(s8, sc);
2139 __m128i ua = _mm_unpacklo_epi32(s9, sd);
2140 __m128i ub = _mm_unpackhi_epi32(s9, sd);
2141 __m128i uc = _mm_unpacklo_epi32(sa, se);
2142 __m128i ud = _mm_unpackhi_epi32(sa, se);
2143 __m128i ue = _mm_unpacklo_epi32(sb, sf);
2144 __m128i uf = _mm_unpackhi_epi32(sb, sf);
2145
2146 kernel.packet[0] = _mm_unpacklo_epi64(u0, u8);
2147 kernel.packet[1] = _mm_unpackhi_epi64(u0, u8);
2148 kernel.packet[2] = _mm_unpacklo_epi64(u1, u9);
2149 kernel.packet[3] = _mm_unpackhi_epi64(u1, u9);
2150 kernel.packet[4] = _mm_unpacklo_epi64(u2, ua);
2151 kernel.packet[5] = _mm_unpackhi_epi64(u2, ua);
2152 kernel.packet[6] = _mm_unpacklo_epi64(u3, ub);
2153 kernel.packet[7] = _mm_unpackhi_epi64(u3, ub);
2154 kernel.packet[8] = _mm_unpacklo_epi64(u4, uc);
2155 kernel.packet[9] = _mm_unpackhi_epi64(u4, uc);
2156 kernel.packet[10] = _mm_unpacklo_epi64(u5, ud);
2157 kernel.packet[11] = _mm_unpackhi_epi64(u5, ud);
2158 kernel.packet[12] = _mm_unpacklo_epi64(u6, ue);
2159 kernel.packet[13] = _mm_unpackhi_epi64(u6, ue);
2160 kernel.packet[14] = _mm_unpacklo_epi64(u7, uf);
2161 kernel.packet[15] = _mm_unpackhi_epi64(u7, uf);
2162}
2163
2164EIGEN_STRONG_INLINE __m128i sse_blend_mask(const Selector<2>& ifPacket) {
2165 return _mm_set_epi64x(0 - ifPacket.select[1], 0 - ifPacket.select[0]);
2166}
2167
2168EIGEN_STRONG_INLINE __m128i sse_blend_mask(const Selector<4>& ifPacket) {
2169 return _mm_set_epi32(0 - ifPacket.select[3], 0 - ifPacket.select[2], 0 - ifPacket.select[1], 0 - ifPacket.select[0]);
2170}
2171
2172template <>
2173EIGEN_STRONG_INLINE Packet2l pblend(const Selector<2>& ifPacket, const Packet2l& thenPacket,
2174 const Packet2l& elsePacket) {
2175 const __m128i true_mask = sse_blend_mask(ifPacket);
2176 return pselect<Packet2l>(true_mask, thenPacket, elsePacket);
2177}
2178template <>
2179EIGEN_STRONG_INLINE Packet4i pblend(const Selector<4>& ifPacket, const Packet4i& thenPacket,
2180 const Packet4i& elsePacket) {
2181 const __m128i true_mask = sse_blend_mask(ifPacket);
2182 return pselect<Packet4i>(true_mask, thenPacket, elsePacket);
2183}
2184template <>
2185EIGEN_STRONG_INLINE Packet4ui pblend(const Selector<4>& ifPacket, const Packet4ui& thenPacket,
2186 const Packet4ui& elsePacket) {
2187 return (Packet4ui)pblend(ifPacket, (Packet4i)thenPacket, (Packet4i)elsePacket);
2188}
2189template <>
2190EIGEN_STRONG_INLINE Packet4f pblend(const Selector<4>& ifPacket, const Packet4f& thenPacket,
2191 const Packet4f& elsePacket) {
2192 const __m128i true_mask = sse_blend_mask(ifPacket);
2193 return pselect<Packet4f>(_mm_castsi128_ps(true_mask), thenPacket, elsePacket);
2194}
2195template <>
2196EIGEN_STRONG_INLINE Packet2d pblend(const Selector<2>& ifPacket, const Packet2d& thenPacket,
2197 const Packet2d& elsePacket) {
2198 const __m128i true_mask = sse_blend_mask(ifPacket);
2199 return pselect<Packet2d>(_mm_castsi128_pd(true_mask), thenPacket, elsePacket);
2200}
2201
2202// Scalar path for pmadd with FMA to ensure consistency with vectorized path.
2203#ifdef EIGEN_VECTORIZE_FMA
2204template <>
2205EIGEN_STRONG_INLINE float pmadd(const float& a, const float& b, const float& c) {
2206 return ::fmaf(a, b, c);
2207}
2208template <>
2209EIGEN_STRONG_INLINE double pmadd(const double& a, const double& b, const double& c) {
2210 return ::fma(a, b, c);
2211}
2212template <>
2213EIGEN_STRONG_INLINE float pmsub(const float& a, const float& b, const float& c) {
2214 return ::fmaf(a, b, -c);
2215}
2216template <>
2217EIGEN_STRONG_INLINE double pmsub(const double& a, const double& b, const double& c) {
2218 return ::fma(a, b, -c);
2219}
2220template <>
2221EIGEN_STRONG_INLINE float pnmadd(const float& a, const float& b, const float& c) {
2222 return ::fmaf(-a, b, c);
2223}
2224template <>
2225EIGEN_STRONG_INLINE double pnmadd(const double& a, const double& b, const double& c) {
2226 return ::fma(-a, b, c);
2227}
2228template <>
2229EIGEN_STRONG_INLINE float pnmsub(const float& a, const float& b, const float& c) {
2230 return ::fmaf(-a, b, -c);
2231}
2232template <>
2233EIGEN_STRONG_INLINE double pnmsub(const double& a, const double& b, const double& c) {
2234 return ::fma(-a, b, -c);
2235}
2236#endif
2237
2238#ifdef EIGEN_VECTORIZE_SSE4_1
2239// Helpers for half->float and float->half conversions.
2240// Currently only used by the AVX code.
2241EIGEN_STRONG_INLINE __m128i half2floatsse(__m128i h) {
2242 __m128i input = _mm_cvtepu16_epi32(h);
2243
2244 // Direct vectorization of half_to_float, C parts in the comments.
2245 __m128i shifted_exp = _mm_set1_epi32(0x7c00 << 13);
2246 // o.u = (h.x & 0x7fff) << 13; // exponent/mantissa bits
2247 __m128i ou = _mm_slli_epi32(_mm_and_si128(input, _mm_set1_epi32(0x7fff)), 13);
2248 // exp = shifted_exp & o.u; // just the exponent
2249 __m128i exp = _mm_and_si128(ou, shifted_exp);
2250 // o.u += (127 - 15) << 23;
2251 ou = _mm_add_epi32(ou, _mm_set1_epi32((127 - 15) << 23));
2252
2253 // Inf/NaN?
2254 __m128i naninf_mask = _mm_cmpeq_epi32(exp, shifted_exp);
2255 // Inf/NaN adjust
2256 __m128i naninf_adj = _mm_and_si128(_mm_set1_epi32((128 - 16) << 23), naninf_mask);
2257 // extra exp adjust for Inf/NaN
2258 ou = _mm_add_epi32(ou, naninf_adj);
2259
2260 // Zero/Denormal?
2261 __m128i zeroden_mask = _mm_cmpeq_epi32(exp, _mm_setzero_si128());
2262 __m128i zeroden_adj = _mm_and_si128(zeroden_mask, _mm_set1_epi32(1 << 23));
2263 // o.u += 1 << 23;
2264 ou = _mm_add_epi32(ou, zeroden_adj);
2265 // magic.u = 113 << 23
2266 __m128i magic = _mm_and_si128(zeroden_mask, _mm_set1_epi32(113 << 23));
2267 // o.f -= magic.f
2268 ou = _mm_castps_si128(_mm_sub_ps(_mm_castsi128_ps(ou), _mm_castsi128_ps(magic)));
2269
2270 __m128i sign = _mm_slli_epi32(_mm_and_si128(input, _mm_set1_epi32(0x8000)), 16);
2271 // o.u |= (h.x & 0x8000) << 16; // sign bit
2272 ou = _mm_or_si128(ou, sign);
2273 // return o.f;
2274 // We are actually returning uint version, to make
2275 // _mm256_insertf128_si256 work.
2276 return ou;
2277}
2278
2279EIGEN_STRONG_INLINE __m128i float2half(__m128 f) {
2280 __m128i o = _mm_setzero_si128();
2281
2282 // unsigned int sign_mask = 0x80000000u;
2283 __m128i sign = _mm_set1_epi32(0x80000000u);
2284 // unsigned int sign = f.u & sign_mask;
2285 sign = _mm_and_si128(sign, _mm_castps_si128(f));
2286 // f.u ^= sign;
2287 f = _mm_xor_ps(f, _mm_castsi128_ps(sign));
2288
2289 __m128i fu = _mm_castps_si128(f);
2290
2291 __m128i f16max = _mm_set1_epi32((127 + 16) << 23);
2292 __m128i f32infty = _mm_set1_epi32(255 << 23);
2293 // if (f.u >= f16max.u) // result is Inf or NaN (all exponent bits set)
2294 // there is no _mm_cmpge_epi32, so use lt and swap operands
2295 __m128i infnan_mask = _mm_cmplt_epi32(f16max, _mm_castps_si128(f));
2296 __m128i inf_mask = _mm_cmpgt_epi32(_mm_castps_si128(f), f32infty);
2297 __m128i nan_mask = _mm_andnot_si128(inf_mask, infnan_mask);
2298 __m128i inf_value = _mm_and_si128(inf_mask, _mm_set1_epi32(0x7e00));
2299 __m128i nan_value = _mm_and_si128(nan_mask, _mm_set1_epi32(0x7c00));
2300 // o.x = (f.u > f32infty.u) ? 0x7e00 : 0x7c00; // NaN->qNaN and Inf->Inf
2301 __m128i naninf_value = _mm_or_si128(inf_value, nan_value);
2302
2303 __m128i denorm_magic = _mm_set1_epi32(((127 - 15) + (23 - 10) + 1) << 23);
2304 __m128i subnorm_mask = _mm_cmplt_epi32(_mm_castps_si128(f), _mm_set1_epi32(113 << 23));
2305 // f.f += denorm_magic.f;
2306 f = _mm_add_ps(f, _mm_castsi128_ps(denorm_magic));
2307 // f.u - denorm_magic.u
2308 o = _mm_sub_epi32(_mm_castps_si128(f), denorm_magic);
2309 o = _mm_and_si128(o, subnorm_mask);
2310 // Correct result for inf/nan/zero/subnormal, 0 otherwise
2311 o = _mm_or_si128(o, naninf_value);
2312
2313 __m128i mask = _mm_or_si128(infnan_mask, subnorm_mask);
2314 o = _mm_and_si128(o, mask);
2315
2316 // mant_odd = (f.u >> 13) & 1;
2317 __m128i mand_odd = _mm_and_si128(_mm_srli_epi32(fu, 13), _mm_set1_epi32(0x1));
2318 // f.u += 0xc8000fffU;
2319 fu = _mm_add_epi32(fu, _mm_set1_epi32(0xc8000fffU));
2320 // f.u += mant_odd;
2321 fu = _mm_add_epi32(fu, mand_odd);
2322 fu = _mm_andnot_si128(mask, fu);
2323 // f.u >> 13
2324 fu = _mm_srli_epi32(fu, 13);
2325 o = _mm_or_si128(fu, o);
2326
2327 // o.x |= static_cast<numext::uint16_t>(sign >> 16);
2328 o = _mm_or_si128(o, _mm_srli_epi32(sign, 16));
2329
2330 // 16 bit values
2331 return _mm_and_si128(o, _mm_set1_epi32(0xffff));
2332}
2333#endif
2334
2335// Packet math for Eigen::half
2336// Disable the following code since it's broken on too many platforms / compilers.
2337// #elif defined(EIGEN_VECTORIZE_SSE) && (!EIGEN_ARCH_x86_64) && (!EIGEN_COMP_MSVC)
2338#if 0
2339
2340typedef struct {
2341 __m64 x;
2342} Packet4h;
2343
2344
2345template<> struct is_arithmetic<Packet4h> { enum { value = true }; };
2346
2347template <>
2348struct packet_traits<Eigen::half> : default_packet_traits {
2349 typedef Packet4h type;
2350 // There is no half-size packet for Packet4h.
2351 typedef Packet4h half;
2352 enum {
2353 Vectorizable = 1,
2354 AlignedOnScalar = 1,
2355 size = 4,
2356 HasAdd = 1,
2357 HasSub = 1,
2358 HasMul = 1,
2359 HasDiv = 1,
2360 HasNegate = 0,
2361 HasAbs = 0,
2362 HasAbs2 = 0,
2363 HasMin = 0,
2364 HasMax = 0,
2365 HasConj = 0,
2366 HasSetLinear = 0,
2367 };
2368};
2369
2370
2371template<> struct unpacket_traits<Packet4h> { typedef Eigen::half type; enum {size=4, alignment=Aligned16, vectorizable=true, masked_load_available=false, masked_store_available=false}; typedef Packet4h half; };
2372
2373template<> EIGEN_STRONG_INLINE Packet4h pset1<Packet4h>(const Eigen::half& from) {
2374 Packet4h result;
2375 result.x = _mm_set1_pi16(from.x);
2376 return result;
2377}
2378
2379template<> EIGEN_STRONG_INLINE Eigen::half pfirst<Packet4h>(const Packet4h& from) {
2380 return half_impl::raw_uint16_to_half(static_cast<unsigned short>(_mm_cvtsi64_si32(from.x)));
2381}
2382
2383template<> EIGEN_STRONG_INLINE Packet4h pconj(const Packet4h& a) { return a; }
2384
2385template<> EIGEN_STRONG_INLINE Packet4h padd<Packet4h>(const Packet4h& a, const Packet4h& b) {
2386 __int64_t a64 = _mm_cvtm64_si64(a.x);
2387 __int64_t b64 = _mm_cvtm64_si64(b.x);
2388
2389 Eigen::half h[4];
2390
2391 Eigen::half ha = half_impl::raw_uint16_to_half(static_cast<unsigned short>(a64));
2392 Eigen::half hb = half_impl::raw_uint16_to_half(static_cast<unsigned short>(b64));
2393 h[0] = ha + hb;
2394 ha = half_impl::raw_uint16_to_half(static_cast<unsigned short>(a64 >> 16));
2395 hb = half_impl::raw_uint16_to_half(static_cast<unsigned short>(b64 >> 16));
2396 h[1] = ha + hb;
2397 ha = half_impl::raw_uint16_to_half(static_cast<unsigned short>(a64 >> 32));
2398 hb = half_impl::raw_uint16_to_half(static_cast<unsigned short>(b64 >> 32));
2399 h[2] = ha + hb;
2400 ha = half_impl::raw_uint16_to_half(static_cast<unsigned short>(a64 >> 48));
2401 hb = half_impl::raw_uint16_to_half(static_cast<unsigned short>(b64 >> 48));
2402 h[3] = ha + hb;
2403 Packet4h result;
2404 result.x = _mm_set_pi16(h[3].x, h[2].x, h[1].x, h[0].x);
2405 return result;
2406}
2407
2408template<> EIGEN_STRONG_INLINE Packet4h psub<Packet4h>(const Packet4h& a, const Packet4h& b) {
2409 __int64_t a64 = _mm_cvtm64_si64(a.x);
2410 __int64_t b64 = _mm_cvtm64_si64(b.x);
2411
2412 Eigen::half h[4];
2413
2414 Eigen::half ha = half_impl::raw_uint16_to_half(static_cast<unsigned short>(a64));
2415 Eigen::half hb = half_impl::raw_uint16_to_half(static_cast<unsigned short>(b64));
2416 h[0] = ha - hb;
2417 ha = half_impl::raw_uint16_to_half(static_cast<unsigned short>(a64 >> 16));
2418 hb = half_impl::raw_uint16_to_half(static_cast<unsigned short>(b64 >> 16));
2419 h[1] = ha - hb;
2420 ha = half_impl::raw_uint16_to_half(static_cast<unsigned short>(a64 >> 32));
2421 hb = half_impl::raw_uint16_to_half(static_cast<unsigned short>(b64 >> 32));
2422 h[2] = ha - hb;
2423 ha = half_impl::raw_uint16_to_half(static_cast<unsigned short>(a64 >> 48));
2424 hb = half_impl::raw_uint16_to_half(static_cast<unsigned short>(b64 >> 48));
2425 h[3] = ha - hb;
2426 Packet4h result;
2427 result.x = _mm_set_pi16(h[3].x, h[2].x, h[1].x, h[0].x);
2428 return result;
2429}
2430
2431template<> EIGEN_STRONG_INLINE Packet4h pmul<Packet4h>(const Packet4h& a, const Packet4h& b) {
2432 __int64_t a64 = _mm_cvtm64_si64(a.x);
2433 __int64_t b64 = _mm_cvtm64_si64(b.x);
2434
2435 Eigen::half h[4];
2436
2437 Eigen::half ha = half_impl::raw_uint16_to_half(static_cast<unsigned short>(a64));
2438 Eigen::half hb = half_impl::raw_uint16_to_half(static_cast<unsigned short>(b64));
2439 h[0] = ha * hb;
2440 ha = half_impl::raw_uint16_to_half(static_cast<unsigned short>(a64 >> 16));
2441 hb = half_impl::raw_uint16_to_half(static_cast<unsigned short>(b64 >> 16));
2442 h[1] = ha * hb;
2443 ha = half_impl::raw_uint16_to_half(static_cast<unsigned short>(a64 >> 32));
2444 hb = half_impl::raw_uint16_to_half(static_cast<unsigned short>(b64 >> 32));
2445 h[2] = ha * hb;
2446 ha = half_impl::raw_uint16_to_half(static_cast<unsigned short>(a64 >> 48));
2447 hb = half_impl::raw_uint16_to_half(static_cast<unsigned short>(b64 >> 48));
2448 h[3] = ha * hb;
2449 Packet4h result;
2450 result.x = _mm_set_pi16(h[3].x, h[2].x, h[1].x, h[0].x);
2451 return result;
2452}
2453
2454template<> EIGEN_STRONG_INLINE Packet4h pdiv<Packet4h>(const Packet4h& a, const Packet4h& b) {
2455 __int64_t a64 = _mm_cvtm64_si64(a.x);
2456 __int64_t b64 = _mm_cvtm64_si64(b.x);
2457
2458 Eigen::half h[4];
2459
2460 Eigen::half ha = half_impl::raw_uint16_to_half(static_cast<unsigned short>(a64));
2461 Eigen::half hb = half_impl::raw_uint16_to_half(static_cast<unsigned short>(b64));
2462 h[0] = ha / hb;
2463 ha = half_impl::raw_uint16_to_half(static_cast<unsigned short>(a64 >> 16));
2464 hb = half_impl::raw_uint16_to_half(static_cast<unsigned short>(b64 >> 16));
2465 h[1] = ha / hb;
2466 ha = half_impl::raw_uint16_to_half(static_cast<unsigned short>(a64 >> 32));
2467 hb = half_impl::raw_uint16_to_half(static_cast<unsigned short>(b64 >> 32));
2468 h[2] = ha / hb;
2469 ha = half_impl::raw_uint16_to_half(static_cast<unsigned short>(a64 >> 48));
2470 hb = half_impl::raw_uint16_to_half(static_cast<unsigned short>(b64 >> 48));
2471 h[3] = ha / hb;
2472 Packet4h result;
2473 result.x = _mm_set_pi16(h[3].x, h[2].x, h[1].x, h[0].x);
2474 return result;
2475}
2476
2477template<> EIGEN_STRONG_INLINE Packet4h pload<Packet4h>(const Eigen::half* from) {
2478 Packet4h result;
2479 result.x = _mm_cvtsi64_m64(*reinterpret_cast<const __int64_t*>(from));
2480 return result;
2481}
2482
2483template<> EIGEN_STRONG_INLINE Packet4h ploadu<Packet4h>(const Eigen::half* from) {
2484 Packet4h result;
2485 result.x = _mm_cvtsi64_m64(*reinterpret_cast<const __int64_t*>(from));
2486 return result;
2487}
2488
2489template<> EIGEN_STRONG_INLINE void pstore<Eigen::half>(Eigen::half* to, const Packet4h& from) {
2490 __int64_t r = _mm_cvtm64_si64(from.x);
2491 *(reinterpret_cast<__int64_t*>(to)) = r;
2492}
2493
2494template<> EIGEN_STRONG_INLINE void pstoreu<Eigen::half>(Eigen::half* to, const Packet4h& from) {
2495 __int64_t r = _mm_cvtm64_si64(from.x);
2496 *(reinterpret_cast<__int64_t*>(to)) = r;
2497}
2498
2499template<> EIGEN_STRONG_INLINE Packet4h
2500ploadquad<Packet4h>(const Eigen::half* from) {
2501 return pset1<Packet4h>(*from);
2502}
2503
2504template<> EIGEN_STRONG_INLINE Packet4h pgather<Eigen::half, Packet4h>(const Eigen::half* from, Index stride)
2505{
2506 Packet4h result;
2507 result.x = _mm_set_pi16(from[3*stride].x, from[2*stride].x, from[1*stride].x, from[0*stride].x);
2508 return result;
2509}
2510
2511template<> EIGEN_STRONG_INLINE void pscatter<Eigen::half, Packet4h>(Eigen::half* to, const Packet4h& from, Index stride)
2512{
2513 __int64_t a = _mm_cvtm64_si64(from.x);
2514 to[stride*0].x = static_cast<unsigned short>(a);
2515 to[stride*1].x = static_cast<unsigned short>(a >> 16);
2516 to[stride*2].x = static_cast<unsigned short>(a >> 32);
2517 to[stride*3].x = static_cast<unsigned short>(a >> 48);
2518}
2519
2520EIGEN_STRONG_INLINE void
2521ptranspose(PacketBlock<Packet4h,4>& kernel) {
2522 __m64 T0 = _mm_unpacklo_pi16(kernel.packet[0].x, kernel.packet[1].x);
2523 __m64 T1 = _mm_unpacklo_pi16(kernel.packet[2].x, kernel.packet[3].x);
2524 __m64 T2 = _mm_unpackhi_pi16(kernel.packet[0].x, kernel.packet[1].x);
2525 __m64 T3 = _mm_unpackhi_pi16(kernel.packet[2].x, kernel.packet[3].x);
2526
2527 kernel.packet[0].x = _mm_unpacklo_pi32(T0, T1);
2528 kernel.packet[1].x = _mm_unpackhi_pi32(T0, T1);
2529 kernel.packet[2].x = _mm_unpacklo_pi32(T2, T3);
2530 kernel.packet[3].x = _mm_unpackhi_pi32(T2, T3);
2531}
2532
2533#endif
2534
2535} // end namespace internal
2536
2537} // end namespace Eigen
2538
2539#if EIGEN_COMP_PGI && EIGEN_COMP_PGI < 1900
2540// PGI++ does not define the following intrinsics in C++ mode.
2541static inline __m128 _mm_castpd_ps(__m128d x) { return reinterpret_cast<__m128&>(x); }
2542static inline __m128i _mm_castpd_si128(__m128d x) { return reinterpret_cast<__m128i&>(x); }
2543static inline __m128d _mm_castps_pd(__m128 x) { return reinterpret_cast<__m128d&>(x); }
2544static inline __m128i _mm_castps_si128(__m128 x) { return reinterpret_cast<__m128i&>(x); }
2545static inline __m128 _mm_castsi128_ps(__m128i x) { return reinterpret_cast<__m128&>(x); }
2546static inline __m128d _mm_castsi128_pd(__m128i x) { return reinterpret_cast<__m128d&>(x); }
2547#endif
2548
2549#endif // EIGEN_PACKET_MATH_SSE_H
@ Aligned16
Definition Constants.h:237
Namespace containing all symbols from the Eigen library.
Definition Core:137
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_exp_op< typename Derived::Scalar >, const Derived > exp(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sign_op< typename Derived::Scalar >, const Derived > sign(const Eigen::ArrayBase< Derived > &x)