Eigen  5.0.1
 
Loading...
Searching...
No Matches
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 <gael.guennebaud@inria.fr>
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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_PACKET_MATH_SSE_H
12#define EIGEN_PACKET_MATH_SSE_H
13
14#include <cstdint>
15// IWYU pragma: private
16#include "../../InternalHeaderCheck.h"
17
18namespace Eigen {
19
20namespace internal {
21
22#ifndef EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD
23#define EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD 8
24#endif
25
26#if !defined(EIGEN_VECTORIZE_AVX) && !defined(EIGEN_ARCH_DEFAULT_NUMBER_OF_REGISTERS)
27// 32 bits => 8 registers
28// 64 bits => 16 registers
29#define EIGEN_ARCH_DEFAULT_NUMBER_OF_REGISTERS (2 * sizeof(void*))
30#endif
31
32#ifdef EIGEN_VECTORIZE_FMA
33#ifndef EIGEN_HAS_SINGLE_INSTRUCTION_MADD
34#define EIGEN_HAS_SINGLE_INSTRUCTION_MADD
35#endif
36#endif
37
38#if ((defined EIGEN_VECTORIZE_AVX) && (EIGEN_COMP_GNUC_STRICT || EIGEN_COMP_MINGW || EIGEN_COMP_LCC) && \
39 (__GXX_ABI_VERSION < 1004)) || \
40 EIGEN_OS_QNX
41// With GCC's default ABI version, a __m128 or __m256 are the same types and therefore we cannot
42// have overloads for both types without linking error.
43// One solution is to increase ABI version using -fabi-version=4 (or greater).
44// Otherwise, we workaround this inconvenience by wrapping 128bit types into the following helper
45// structure:
46using Packet4f = eigen_packet_wrapper<__m128>;
47using Packet2d = eigen_packet_wrapper<__m128d>;
48#else
49using Packet4f = __m128;
50using Packet2d = __m128d;
51#endif
52
53using Packet4i = eigen_packet_wrapper<__m128i, 0>;
54using Packet16b = eigen_packet_wrapper<__m128i, 1>;
55using Packet4ui = eigen_packet_wrapper<__m128i, 4>;
56using Packet2l = eigen_packet_wrapper<__m128i, 5>;
57
58template <>
59struct is_arithmetic<__m128> : std::true_type {};
60template <>
61struct is_arithmetic<__m128i> : std::true_type {};
62template <>
63struct is_arithmetic<__m128d> : std::true_type {};
64template <>
65struct is_arithmetic<Packet4i> : std::true_type {};
66template <>
67struct is_arithmetic<Packet2l> : std::true_type {};
68// Note that `Packet4ui` uses the underlying type `__m128i`, which is
69// interpreted as a vector of _signed_ `int32`s, which breaks some arithmetic
70// operations used in `GenericPacketMath.h`.
71template <>
72struct is_arithmetic<Packet4ui> : std::false_type {};
73template <>
74struct is_arithmetic<Packet16b> : std::true_type {};
75
76template <int p, int q, int r, int s>
77struct shuffle_mask {
78 enum { mask = (s) << 6 | (r) << 4 | (q) << 2 | (p) };
79};
80
81#define SIGN_MASK_I32 static_cast<int32_t>(0x80000000)
82
83// TODO: change the implementation of all swizzle* ops from macro to template,
84#define vec4f_swizzle1(v, p, q, r, s) \
85 Packet4f(_mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(v), (shuffle_mask<p, q, r, s>::mask))))
86
87#define vec4i_swizzle1(v, p, q, r, s) Packet4i(_mm_shuffle_epi32(v, (shuffle_mask<p, q, r, s>::mask)))
88
89#define vec4ui_swizzle1(v, p, q, r, s) Packet4ui(vec4i_swizzle1(v, p, q, r, s))
90
91#define vec2d_swizzle1(v, p, q) \
92 Packet2d(_mm_castsi128_pd( \
93 _mm_shuffle_epi32(_mm_castpd_si128(v), (shuffle_mask<2 * p, 2 * p + 1, 2 * q, 2 * q + 1>::mask))))
94
95#define vec4f_swizzle2(a, b, p, q, r, s) Packet4f(_mm_shuffle_ps((a), (b), (shuffle_mask<p, q, r, s>::mask)))
96
97#define vec4i_swizzle2(a, b, p, q, r, s) \
98 Packet4i( \
99 _mm_castps_si128((_mm_shuffle_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b), (shuffle_mask<p, q, r, s>::mask)))))
100
101#define vec4ui_swizzle2(a, b, p, q, r, s) Packet4i(vec4i_swizzle2(a, b, p, q, r, s))
102
103EIGEN_STRONG_INLINE Packet4f vec4f_movelh(const Packet4f& a, const Packet4f& b) {
104 return Packet4f(_mm_movelh_ps(a, b));
105}
106EIGEN_STRONG_INLINE Packet4f vec4f_movehl(const Packet4f& a, const Packet4f& b) {
107 return Packet4f(_mm_movehl_ps(a, b));
108}
109EIGEN_STRONG_INLINE Packet4f vec4f_unpacklo(const Packet4f& a, const Packet4f& b) {
110 return Packet4f(_mm_unpacklo_ps(a, b));
111}
112EIGEN_STRONG_INLINE Packet4f vec4f_unpackhi(const Packet4f& a, const Packet4f& b) {
113 return Packet4f(_mm_unpackhi_ps(a, b));
114}
115#define vec4f_duplane(a, p) vec4f_swizzle2(a, a, p, p, p, p)
116
117#define vec2d_swizzle2(a, b, mask) Packet2d(_mm_shuffle_pd(a, b, mask))
118
119EIGEN_STRONG_INLINE Packet2d vec2d_unpacklo(const Packet2d& a, const Packet2d& b) {
120 return Packet2d(_mm_unpacklo_pd(a, b));
121}
122EIGEN_STRONG_INLINE Packet2d vec2d_unpackhi(const Packet2d& a, const Packet2d& b) {
123 return Packet2d(_mm_unpackhi_pd(a, b));
124}
125#define vec2d_duplane(a, p) vec2d_swizzle2(a, a, (p << 1) | p)
126
127#define EIGEN_DECLARE_CONST_Packet4f(NAME, X) const Packet4f p4f_##NAME = pset1<Packet4f>(X)
128
129#define EIGEN_DECLARE_CONST_Packet2d(NAME, X) const Packet2d p2d_##NAME = pset1<Packet2d>(X)
130
131#define EIGEN_DECLARE_CONST_Packet4f_FROM_INT(NAME, X) const Packet4f p4f_##NAME = pset1frombits<Packet4f>(X)
132
133#define EIGEN_DECLARE_CONST_Packet4i(NAME, X) const Packet4i p4i_##NAME = pset1<Packet4i>(X)
134
135#define EIGEN_DECLARE_CONST_Packet4ui(NAME, X) const Packet4ui p4ui_##NAME = pset1<Packet4ui>(X)
136
137// Work around lack of extract/cvt for epi64 when compiling for 32-bit.
138#if EIGEN_ARCH_x86_64
139EIGEN_ALWAYS_INLINE int64_t _mm_extract_epi64_0(const __m128i& a) { return _mm_cvtsi128_si64(a); }
140#ifdef EIGEN_VECTORIZE_SSE4_1
141EIGEN_ALWAYS_INLINE int64_t _mm_extract_epi64_1(const __m128i& a) { return _mm_extract_epi64(a, 1); }
142#else
143EIGEN_ALWAYS_INLINE int64_t _mm_extract_epi64_1(const __m128i& a) {
144 return _mm_cvtsi128_si64(_mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(a), _mm_castsi128_pd(a), 0x1)));
145}
146#endif
147#else
148// epi64 instructions are not available. The following seems to generate the same instructions
149// with -O2 in GCC/Clang.
150EIGEN_ALWAYS_INLINE int64_t _mm_extract_epi64_0(const __m128i& a) {
151 return numext::bit_cast<int64_t>(_mm_cvtsd_f64(_mm_castsi128_pd(a)));
152}
153EIGEN_ALWAYS_INLINE int64_t _mm_extract_epi64_1(const __m128i& a) {
154 return numext::bit_cast<int64_t>(_mm_cvtsd_f64(_mm_shuffle_pd(_mm_castsi128_pd(a), _mm_castsi128_pd(a), 0x1)));
155}
156#endif
157
158// Use the packet_traits defined in AVX/PacketMath.h instead if we're going
159// to leverage AVX instructions.
160#ifndef EIGEN_VECTORIZE_AVX
161template <>
162struct packet_traits<float> : default_packet_traits {
163 using type = Packet4f;
164 using half = Packet4f;
165 enum {
166 Vectorizable = 1,
167 AlignedOnScalar = 1,
168 size = 4,
169
170 HasCmp = 1,
171 HasDiv = 1,
172 HasReciprocal = EIGEN_FAST_MATH,
173 HasSin = EIGEN_FAST_MATH,
174 HasCos = EIGEN_FAST_MATH,
175 HasTan = EIGEN_FAST_MATH,
176 HasACos = 1,
177 HasASin = 1,
178 HasATan = 1,
179 HasATanh = 1,
180 HasSinh = 1,
181 HasCosh = 1,
182 HasASinh = 1,
183 HasACosh = 1,
184 HasLog = 1,
185 HasLog1p = 1,
186 HasLog10 = 1,
187 HasExpm1 = 1,
188 HasNdtri = 1,
189 HasExp = 1,
190 HasPow = 1,
191 HasBessel = 1,
192 HasSqrt = 1,
193 HasRsqrt = 1,
194 HasCbrt = 1,
195 HasTanh = EIGEN_FAST_MATH,
196 HasErf = EIGEN_FAST_MATH,
197 HasErfc = EIGEN_FAST_MATH,
198 HasSign = 0 // The manually vectorized version is slightly slower for SSE.
199 };
200};
201template <>
202struct packet_traits<double> : default_packet_traits {
203 using type = Packet2d;
204 using half = Packet2d;
205 enum {
206 Vectorizable = 1,
207 AlignedOnScalar = 1,
208 size = 2,
209
210 HasCmp = 1,
211 HasDiv = 1,
212 HasSin = EIGEN_FAST_MATH,
213 HasCos = EIGEN_FAST_MATH,
214 HasTan = EIGEN_FAST_MATH,
215 HasSinh = 1,
216 HasCosh = 1,
217 HasASinh = 1,
218 HasACosh = 1,
219 HasTanh = EIGEN_FAST_MATH,
220 HasErf = EIGEN_FAST_MATH,
221 HasErfc = EIGEN_FAST_MATH,
222 HasLog = 1,
223 HasLog10 = 1,
224 HasExp = 1,
225 HasLog1p = 1,
226 HasExpm1 = 1,
227 HasPow = 1,
228 HasSqrt = 1,
229 HasRsqrt = 1,
230 HasCbrt = 1,
231 HasATan = 1,
232 HasATanh = 1,
233 };
234};
235template <>
236struct packet_traits<int> : default_packet_traits {
237 using type = Packet4i;
238 using half = Packet4i;
239 enum {
240 Vectorizable = 1,
241 AlignedOnScalar = 1,
242 size = 4,
243
244 HasCmp = 1,
245 HasDiv = 1,
246 HasShift = 1,
247 };
248};
249template <>
250struct packet_traits<uint32_t> : default_packet_traits {
251 using type = Packet4ui;
252 using half = Packet4ui;
253 enum {
254 Vectorizable = 1,
255 AlignedOnScalar = 1,
256 size = 4,
257
258 HasNegate = 0,
259 HasCmp = 1,
260 HasShift = 1,
261 };
262};
263template <>
264struct packet_traits<int64_t> : default_packet_traits {
265 using type = Packet2l;
266 using half = Packet2l;
267 enum {
268 Vectorizable = 1,
269 AlignedOnScalar = 1,
270 size = 2,
271
272 HasCmp = 1,
273 HasShift = 1,
274 };
275};
276#endif
277template <>
278struct packet_traits<bool> : default_packet_traits {
279 using type = Packet16b;
280 using half = Packet16b;
281 enum {
282 Vectorizable = 1,
283 AlignedOnScalar = 1,
284 size = 16,
285
286 HasCmp = 1,
287 HasShift = 0,
288 HasAbs = 0,
289 HasMin = 0,
290 HasMax = 0,
291 HasConj = 0,
292 HasSqrt = 1,
293 HasNegate = 0,
294 HasSign = 0 // Don't try to vectorize psign<bool> = identity.
295 };
296};
297
298template <>
299struct unpacket_traits<Packet4f> {
300 using type = float;
301 using half = Packet4f;
302 using integer_packet = Packet4i;
303 enum {
304 size = 4,
305 alignment = Aligned16,
306 vectorizable = true,
307 masked_load_available = false,
308 masked_store_available = false
309 };
310};
311template <>
312struct unpacket_traits<Packet2d> {
313 using type = double;
314 using half = Packet2d;
315 using integer_packet = Packet2l;
316 enum {
317 size = 2,
318 alignment = Aligned16,
319 vectorizable = true,
320 masked_load_available = false,
321 masked_store_available = false
322 };
323};
324template <>
325struct unpacket_traits<Packet2l> {
326 using type = int64_t;
327 using half = Packet2l;
328 enum {
329 size = 2,
330 alignment = Aligned16,
331 vectorizable = true,
332 masked_load_available = false,
333 masked_store_available = false
334 };
335};
336template <>
337struct unpacket_traits<Packet4i> {
338 using type = int;
339 using half = Packet4i;
340 enum {
341 size = 4,
342 alignment = Aligned16,
343 vectorizable = true,
344 masked_load_available = false,
345 masked_store_available = false
346 };
347};
348template <>
349struct unpacket_traits<Packet4ui> {
350 using type = uint32_t;
351 using half = Packet4ui;
352 enum {
353 size = 4,
354 alignment = Aligned16,
355 vectorizable = true,
356 masked_load_available = false,
357 masked_store_available = false
358 };
359};
360template <>
361struct unpacket_traits<Packet16b> {
362 using type = bool;
363 using half = Packet16b;
364 enum {
365 size = 16,
366 alignment = Aligned16,
367 vectorizable = true,
368 masked_load_available = false,
369 masked_store_available = false
370 };
371};
372
373#ifndef EIGEN_VECTORIZE_AVX
374template <>
375struct scalar_div_cost<float, true> : std::integral_constant<int, 7> {};
376template <>
377struct scalar_div_cost<double, true> : std::integral_constant<int, 8> {};
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 Packet4f r = _mm_castsi128_ps(_mm_set_epi32(0, -1, 0, -1));
417 EIGEN_FAST_MATH_CONSTANT_BARRIER(r);
418 return r;
419}
420template <>
421EIGEN_STRONG_INLINE Packet2l peven_mask(const Packet2l& /*a*/) {
422 return _mm_set_epi32(0, 0, -1, -1);
423}
424template <>
425EIGEN_STRONG_INLINE Packet4i peven_mask(const Packet4i& /*a*/) {
426 return _mm_set_epi32(0, -1, 0, -1);
427}
428template <>
429EIGEN_STRONG_INLINE Packet4ui peven_mask(const Packet4ui& /*a*/) {
430 return _mm_set_epi32(0, -1, 0, -1);
431}
432template <>
433EIGEN_STRONG_INLINE Packet2d peven_mask(const Packet2d& /*a*/) {
434 Packet2d r = _mm_castsi128_pd(_mm_set_epi32(0, 0, -1, -1));
435 EIGEN_FAST_MATH_CONSTANT_BARRIER(r);
436 return r;
437}
438
439template <>
440EIGEN_STRONG_INLINE Packet4f pzero(const Packet4f& /*a*/) {
441 return _mm_setzero_ps();
442}
443template <>
444EIGEN_STRONG_INLINE Packet2d pzero(const Packet2d& /*a*/) {
445 return _mm_setzero_pd();
446}
447template <>
448EIGEN_STRONG_INLINE Packet2l pzero(const Packet2l& /*a*/) {
449 return _mm_setzero_si128();
450}
451template <>
452EIGEN_STRONG_INLINE Packet4i pzero(const Packet4i& /*a*/) {
453 return _mm_setzero_si128();
454}
455template <>
456EIGEN_STRONG_INLINE Packet4ui pzero(const Packet4ui& /*a*/) {
457 return _mm_setzero_si128();
458}
459
460// GCC generates a shufps instruction for _mm_set1_ps/_mm_load1_ps instead of the more efficient pshufd instruction.
461// However, using intrinsics for pset1 makes gcc to generate crappy code in some cases (see bug 203)
462// Using inline assembly is also not an option because then gcc fails to reorder properly the instructions.
463// Therefore, we introduced the pload1 functions to be used in product kernels for which bug 203 does not apply.
464// Also note that with AVX, we want it to generate a vbroadcastss.
465#if EIGEN_COMP_GNUC_STRICT && (!defined __AVX__)
466template <>
467EIGEN_STRONG_INLINE Packet4f pload1<Packet4f>(const float* from) {
468 return vec4f_swizzle1(_mm_load_ss(from), 0, 0, 0, 0);
469}
470#endif
471
472template <>
473EIGEN_STRONG_INLINE Packet4f plset<Packet4f>(const float& a) {
474 return _mm_add_ps(pset1<Packet4f>(a), _mm_set_ps(3, 2, 1, 0));
475}
476template <>
477EIGEN_STRONG_INLINE Packet2d plset<Packet2d>(const double& a) {
478 return _mm_add_pd(pset1<Packet2d>(a), _mm_set_pd(1, 0));
479}
480template <>
481EIGEN_STRONG_INLINE Packet2l plset<Packet2l>(const int64_t& a) {
482 return _mm_add_epi32(pset1<Packet2l>(a), _mm_set_epi64x(1, 0));
483}
484template <>
485EIGEN_STRONG_INLINE Packet4i plset<Packet4i>(const int& a) {
486 return _mm_add_epi32(pset1<Packet4i>(a), _mm_set_epi32(3, 2, 1, 0));
487}
488template <>
489EIGEN_STRONG_INLINE Packet4ui plset<Packet4ui>(const uint32_t& a) {
490 return _mm_add_epi32(pset1<Packet4ui>(a), _mm_set_epi32(3, 2, 1, 0));
491}
492
493template <>
494EIGEN_STRONG_INLINE Packet4f padd<Packet4f>(const Packet4f& a, const Packet4f& b) {
495 return _mm_add_ps(a, b);
496}
497template <>
498EIGEN_STRONG_INLINE Packet2d padd<Packet2d>(const Packet2d& a, const Packet2d& b) {
499 return _mm_add_pd(a, b);
500}
501template <>
502EIGEN_STRONG_INLINE Packet2l padd<Packet2l>(const Packet2l& a, const Packet2l& b) {
503 return _mm_add_epi64(a, b);
504}
505template <>
506EIGEN_STRONG_INLINE Packet4i padd<Packet4i>(const Packet4i& a, const Packet4i& b) {
507 return _mm_add_epi32(a, b);
508}
509template <>
510EIGEN_STRONG_INLINE Packet4ui padd<Packet4ui>(const Packet4ui& a, const Packet4ui& b) {
511 return _mm_add_epi32(a, b);
512}
513
514template <>
515EIGEN_STRONG_INLINE Packet16b padd<Packet16b>(const Packet16b& a, const Packet16b& b) {
516 return _mm_or_si128(a, b);
517}
518
519template <typename Packet>
520EIGEN_STRONG_INLINE Packet padds(const Packet& a, const Packet& b);
521template <>
522EIGEN_STRONG_INLINE Packet4f padds<Packet4f>(const Packet4f& a, const Packet4f& b) {
523 return _mm_add_ss(a, b);
524}
525template <>
526EIGEN_STRONG_INLINE Packet2d padds<Packet2d>(const Packet2d& a, const Packet2d& b) {
527 return _mm_add_sd(a, b);
528}
529
530template <>
531EIGEN_STRONG_INLINE Packet4f psub<Packet4f>(const Packet4f& a, const Packet4f& b) {
532 return _mm_sub_ps(a, b);
533}
534template <>
535EIGEN_STRONG_INLINE Packet2d psub<Packet2d>(const Packet2d& a, const Packet2d& b) {
536 return _mm_sub_pd(a, b);
537}
538template <>
539EIGEN_STRONG_INLINE Packet2l psub<Packet2l>(const Packet2l& a, const Packet2l& b) {
540 return _mm_sub_epi64(a, b);
541}
542template <>
543EIGEN_STRONG_INLINE Packet4i psub<Packet4i>(const Packet4i& a, const Packet4i& b) {
544 return _mm_sub_epi32(a, b);
545}
546template <>
547EIGEN_STRONG_INLINE Packet4ui psub<Packet4ui>(const Packet4ui& a, const Packet4ui& b) {
548 return _mm_sub_epi32(a, b);
549}
550template <>
551EIGEN_STRONG_INLINE Packet16b psub<Packet16b>(const Packet16b& a, const Packet16b& b) {
552 return _mm_xor_si128(a, b);
553}
554
555template <>
556EIGEN_STRONG_INLINE Packet4f pxor<Packet4f>(const Packet4f& a, const Packet4f& b);
557template <>
558EIGEN_STRONG_INLINE Packet4f paddsub<Packet4f>(const Packet4f& a, const Packet4f& b) {
559#ifdef EIGEN_VECTORIZE_SSE3
560 return _mm_addsub_ps(a, b);
561#else
562 const Packet4f mask = _mm_castsi128_ps(_mm_setr_epi32(SIGN_MASK_I32, 0x0, SIGN_MASK_I32, 0x0));
563 return padd(a, pxor(mask, b));
564#endif
565}
566
567template <>
568EIGEN_STRONG_INLINE Packet2d pxor<Packet2d>(const Packet2d&, const Packet2d&);
569template <>
570EIGEN_STRONG_INLINE Packet2d paddsub<Packet2d>(const Packet2d& a, const Packet2d& b) {
571#ifdef EIGEN_VECTORIZE_SSE3
572 return _mm_addsub_pd(a, b);
573#else
574 const Packet2d mask = _mm_castsi128_pd(_mm_setr_epi32(0x0, SIGN_MASK_I32, 0x0, 0x0));
575 return padd(a, pxor(mask, b));
576#endif
577}
578
579template <>
580EIGEN_STRONG_INLINE Packet4f pnegate(const Packet4f& a) {
581 const Packet4f mask = _mm_castsi128_ps(_mm_setr_epi32(SIGN_MASK_I32, SIGN_MASK_I32, SIGN_MASK_I32, SIGN_MASK_I32));
582 return _mm_xor_ps(a, mask);
583}
584template <>
585EIGEN_STRONG_INLINE Packet2d pnegate(const Packet2d& a) {
586 const Packet2d mask = _mm_castsi128_pd(_mm_setr_epi32(0x0, SIGN_MASK_I32, 0x0, SIGN_MASK_I32));
587 return _mm_xor_pd(a, mask);
588}
589template <>
590EIGEN_STRONG_INLINE Packet2l pnegate(const Packet2l& a) {
591 return psub(pzero(a), a);
592}
593
594template <>
595EIGEN_STRONG_INLINE Packet4i pnegate(const Packet4i& a) {
596 return psub(pzero(a), a);
597}
598
599template <>
600EIGEN_STRONG_INLINE Packet4f pmul<Packet4f>(const Packet4f& a, const Packet4f& b) {
601 return _mm_mul_ps(a, b);
602}
603template <>
604EIGEN_STRONG_INLINE Packet2d pmul<Packet2d>(const Packet2d& a, const Packet2d& b) {
605 return _mm_mul_pd(a, b);
606}
607template <>
608EIGEN_STRONG_INLINE Packet2l pmul<Packet2l>(const Packet2l& a, const Packet2l& b) {
609 // 64-bit mul requires avx512, so do this with 32-bit multiplication
610 __m128i upper32_a = _mm_srli_epi64(a, 32);
611 __m128i upper32_b = _mm_srli_epi64(b, 32);
612
613 // upper * lower
614 __m128i mul1 = _mm_mul_epu32(upper32_a, b);
615 __m128i mul2 = _mm_mul_epu32(upper32_b, a);
616 // Gives us both upper*upper and lower*lower
617 __m128i mul3 = _mm_mul_epu32(a, b);
618
619 __m128i high = _mm_slli_epi64(_mm_add_epi64(mul1, mul2), 32);
620 return _mm_add_epi64(high, mul3);
621}
622template <>
623EIGEN_STRONG_INLINE Packet4i pmul<Packet4i>(const Packet4i& a, const Packet4i& b) {
624#ifdef EIGEN_VECTORIZE_SSE4_1
625 return _mm_mullo_epi32(a, b);
626#else
627 // this version is slightly faster than 4 scalar products
628 return vec4i_swizzle1(
629 vec4i_swizzle2(_mm_mul_epu32(a, b), _mm_mul_epu32(vec4i_swizzle1(a, 1, 0, 3, 2), vec4i_swizzle1(b, 1, 0, 3, 2)),
630 0, 2, 0, 2),
631 0, 2, 1, 3);
632#endif
633}
634template <>
635EIGEN_STRONG_INLINE Packet4ui pmul<Packet4ui>(const Packet4ui& a, const Packet4ui& b) {
636#ifdef EIGEN_VECTORIZE_SSE4_1
637 return _mm_mullo_epi32(a, b);
638#else
639 // this version is slightly faster than 4 scalar products
640 return vec4ui_swizzle1(
641 vec4ui_swizzle2(_mm_mul_epu32(a, b),
642 _mm_mul_epu32(vec4ui_swizzle1(a, 1, 0, 3, 2), vec4ui_swizzle1(b, 1, 0, 3, 2)), 0, 2, 0, 2),
643 0, 2, 1, 3);
644#endif
645}
646
647template <>
648EIGEN_STRONG_INLINE Packet16b pmul<Packet16b>(const Packet16b& a, const Packet16b& b) {
649 return _mm_and_si128(a, b);
650}
651
652template <>
653EIGEN_STRONG_INLINE Packet4f pdiv<Packet4f>(const Packet4f& a, const Packet4f& b) {
654 return _mm_div_ps(a, b);
655}
656template <>
657EIGEN_STRONG_INLINE Packet2d pdiv<Packet2d>(const Packet2d& a, const Packet2d& b) {
658 return _mm_div_pd(a, b);
659}
660
661template <>
662EIGEN_STRONG_INLINE Packet4i pdiv<Packet4i>(const Packet4i& a, const Packet4i& b) {
663#ifdef EIGEN_VECTORIZE_AVX
664 return _mm256_cvttpd_epi32(_mm256_div_pd(_mm256_cvtepi32_pd(a), _mm256_cvtepi32_pd(b)));
665#else
666 __m128i q_lo = _mm_cvttpd_epi32(_mm_div_pd(_mm_cvtepi32_pd(a), _mm_cvtepi32_pd(b)));
667 __m128i q_hi = _mm_cvttpd_epi32(
668 _mm_div_pd(_mm_cvtepi32_pd(vec4i_swizzle1(a, 2, 3, 0, 1)), _mm_cvtepi32_pd(vec4i_swizzle1(b, 2, 3, 0, 1))));
669 return vec4i_swizzle1(_mm_unpacklo_epi32(q_lo, q_hi), 0, 2, 1, 3);
670#endif
671}
672
673#ifdef EIGEN_VECTORIZE_FMA
674template <>
675EIGEN_STRONG_INLINE Packet4f pmadd(const Packet4f& a, const Packet4f& b, const Packet4f& c) {
676 return _mm_fmadd_ps(a, b, c);
677}
678template <>
679EIGEN_STRONG_INLINE Packet2d pmadd(const Packet2d& a, const Packet2d& b, const Packet2d& c) {
680 return _mm_fmadd_pd(a, b, c);
681}
682template <>
683EIGEN_STRONG_INLINE Packet4f pmsub(const Packet4f& a, const Packet4f& b, const Packet4f& c) {
684 return _mm_fmsub_ps(a, b, c);
685}
686template <>
687EIGEN_STRONG_INLINE Packet2d pmsub(const Packet2d& a, const Packet2d& b, const Packet2d& c) {
688 return _mm_fmsub_pd(a, b, c);
689}
690template <>
691EIGEN_STRONG_INLINE Packet4f pnmadd(const Packet4f& a, const Packet4f& b, const Packet4f& c) {
692 return _mm_fnmadd_ps(a, b, c);
693}
694template <>
695EIGEN_STRONG_INLINE Packet2d pnmadd(const Packet2d& a, const Packet2d& b, const Packet2d& c) {
696 return _mm_fnmadd_pd(a, b, c);
697}
698template <>
699EIGEN_STRONG_INLINE Packet4f pnmsub(const Packet4f& a, const Packet4f& b, const Packet4f& c) {
700 return _mm_fnmsub_ps(a, b, c);
701}
702template <>
703EIGEN_STRONG_INLINE Packet2d pnmsub(const Packet2d& a, const Packet2d& b, const Packet2d& c) {
704 return _mm_fnmsub_pd(a, b, c);
705}
706
707template <typename Packet>
708EIGEN_STRONG_INLINE Packet pmadds(const Packet& a, const Packet& b, const Packet& c);
709template <>
710EIGEN_STRONG_INLINE Packet4f pmadds<Packet4f>(const Packet4f& a, const Packet4f& b, const Packet4f& c) {
711 return _mm_fmadd_ss(a, b, c);
712}
713template <>
714EIGEN_STRONG_INLINE Packet2d pmadds<Packet2d>(const Packet2d& a, const Packet2d& b, const Packet2d& c) {
715 return _mm_fmadd_sd(a, b, c);
716}
717#endif
718
719#ifdef EIGEN_VECTORIZE_SSE4_1
720template <>
721EIGEN_STRONG_INLINE Packet4f pselect(const Packet4f& mask, const Packet4f& a, const Packet4f& b) {
722 return _mm_blendv_ps(b, a, mask);
723}
724
725template <>
726EIGEN_STRONG_INLINE Packet2l pselect(const Packet2l& mask, const Packet2l& a, const Packet2l& b) {
727 return _mm_castpd_si128(_mm_blendv_pd(_mm_castsi128_pd(b), _mm_castsi128_pd(a), _mm_castsi128_pd(mask)));
728}
729
730template <>
731EIGEN_STRONG_INLINE Packet4i pselect(const Packet4i& mask, const Packet4i& a, const Packet4i& b) {
732 return _mm_castps_si128(_mm_blendv_ps(_mm_castsi128_ps(b), _mm_castsi128_ps(a), _mm_castsi128_ps(mask)));
733}
734
735template <>
736EIGEN_STRONG_INLINE Packet4ui pselect(const Packet4ui& mask, const Packet4ui& a, const Packet4ui& b) {
737 return _mm_castps_si128(_mm_blendv_ps(_mm_castsi128_ps(b), _mm_castsi128_ps(a), _mm_castsi128_ps(mask)));
738}
739
740template <>
741EIGEN_STRONG_INLINE Packet2d pselect(const Packet2d& mask, const Packet2d& a, const Packet2d& b) {
742 return _mm_blendv_pd(b, a, mask);
743}
744#endif
745
746template <>
747EIGEN_STRONG_INLINE Packet2l ptrue<Packet2l>(const Packet2l& a) {
748 return _mm_cmpeq_epi32(a, a);
749}
750template <>
751EIGEN_STRONG_INLINE Packet4i ptrue<Packet4i>(const Packet4i& a) {
752 return _mm_cmpeq_epi32(a, a);
753}
754template <>
755EIGEN_STRONG_INLINE Packet16b ptrue<Packet16b>(const Packet16b& /*a*/) {
756 return pset1<Packet16b>(true);
757}
758template <>
759EIGEN_STRONG_INLINE Packet4f ptrue<Packet4f>(const Packet4f& a) {
760 Packet4i b = _mm_castps_si128(a);
761 Packet4f r = _mm_castsi128_ps(_mm_cmpeq_epi32(b, b));
762 EIGEN_FAST_MATH_CONSTANT_BARRIER(r);
763 return r;
764}
765template <>
766EIGEN_STRONG_INLINE Packet2d ptrue<Packet2d>(const Packet2d& a) {
767 Packet4i b = _mm_castpd_si128(a);
768 Packet2d r = _mm_castsi128_pd(_mm_cmpeq_epi32(b, b));
769 EIGEN_FAST_MATH_CONSTANT_BARRIER(r);
770 return r;
771}
772
773template <>
774EIGEN_STRONG_INLINE Packet4f pand<Packet4f>(const Packet4f& a, const Packet4f& b) {
775 return _mm_and_ps(a, b);
776}
777template <>
778EIGEN_STRONG_INLINE Packet2d pand<Packet2d>(const Packet2d& a, const Packet2d& b) {
779 return _mm_and_pd(a, b);
780}
781template <>
782EIGEN_STRONG_INLINE Packet2l pand<Packet2l>(const Packet2l& a, const Packet2l& b) {
783 return _mm_and_si128(a, b);
784}
785template <>
786EIGEN_STRONG_INLINE Packet4i pand<Packet4i>(const Packet4i& a, const Packet4i& b) {
787 return _mm_and_si128(a, b);
788}
789template <>
790EIGEN_STRONG_INLINE Packet4ui pand<Packet4ui>(const Packet4ui& a, const Packet4ui& b) {
791 return _mm_and_si128(a, b);
792}
793template <>
794EIGEN_STRONG_INLINE Packet16b pand<Packet16b>(const Packet16b& a, const Packet16b& b) {
795 return _mm_and_si128(a, b);
796}
797
798template <>
799EIGEN_STRONG_INLINE Packet4f por<Packet4f>(const Packet4f& a, const Packet4f& b) {
800 return _mm_or_ps(a, b);
801}
802template <>
803EIGEN_STRONG_INLINE Packet2d por<Packet2d>(const Packet2d& a, const Packet2d& b) {
804 return _mm_or_pd(a, b);
805}
806template <>
807EIGEN_STRONG_INLINE Packet2l por<Packet2l>(const Packet2l& a, const Packet2l& b) {
808 return _mm_or_si128(a, b);
809}
810template <>
811EIGEN_STRONG_INLINE Packet4i por<Packet4i>(const Packet4i& a, const Packet4i& b) {
812 return _mm_or_si128(a, b);
813}
814template <>
815EIGEN_STRONG_INLINE Packet4ui por<Packet4ui>(const Packet4ui& a, const Packet4ui& b) {
816 return _mm_or_si128(a, b);
817}
818template <>
819EIGEN_STRONG_INLINE Packet16b por<Packet16b>(const Packet16b& a, const Packet16b& b) {
820 return _mm_or_si128(a, b);
821}
822
823template <>
824EIGEN_STRONG_INLINE Packet4f pxor<Packet4f>(const Packet4f& a, const Packet4f& b) {
825 return _mm_xor_ps(a, b);
826}
827template <>
828EIGEN_STRONG_INLINE Packet2d pxor<Packet2d>(const Packet2d& a, const Packet2d& b) {
829 return _mm_xor_pd(a, b);
830}
831template <>
832EIGEN_STRONG_INLINE Packet2l pxor<Packet2l>(const Packet2l& a, const Packet2l& b) {
833 return _mm_xor_si128(a, b);
834}
835template <>
836EIGEN_STRONG_INLINE Packet4i pxor<Packet4i>(const Packet4i& a, const Packet4i& b) {
837 return _mm_xor_si128(a, b);
838}
839template <>
840EIGEN_STRONG_INLINE Packet4ui pxor<Packet4ui>(const Packet4ui& a, const Packet4ui& b) {
841 return _mm_xor_si128(a, b);
842}
843template <>
844EIGEN_STRONG_INLINE Packet16b pxor<Packet16b>(const Packet16b& a, const Packet16b& b) {
845 return _mm_xor_si128(a, b);
846}
847
848template <>
849EIGEN_STRONG_INLINE Packet4f pandnot<Packet4f>(const Packet4f& a, const Packet4f& b) {
850 return _mm_andnot_ps(b, a);
851}
852template <>
853EIGEN_STRONG_INLINE Packet2d pandnot<Packet2d>(const Packet2d& a, const Packet2d& b) {
854 return _mm_andnot_pd(b, a);
855}
856template <>
857EIGEN_STRONG_INLINE Packet2l pandnot<Packet2l>(const Packet2l& a, const Packet2l& b) {
858 return _mm_andnot_si128(b, a);
859}
860template <>
861EIGEN_STRONG_INLINE Packet4i pandnot<Packet4i>(const Packet4i& a, const Packet4i& b) {
862 return _mm_andnot_si128(b, a);
863}
864template <>
865EIGEN_STRONG_INLINE Packet4ui pandnot<Packet4ui>(const Packet4ui& a, const Packet4ui& b) {
866 return _mm_andnot_si128(b, a);
867}
868template <>
869EIGEN_STRONG_INLINE Packet16b pandnot<Packet16b>(const Packet16b& a, const Packet16b& b) {
870 return _mm_andnot_si128(b, a);
871}
872template <>
873EIGEN_STRONG_INLINE Packet16b pcmp_lt(const Packet16b& a, const Packet16b& b) {
874 return _mm_andnot_si128(a, b);
875}
876template <>
877EIGEN_STRONG_INLINE Packet4f pcmp_le(const Packet4f& a, const Packet4f& b) {
878 return _mm_cmple_ps(a, b);
879}
880template <>
881EIGEN_STRONG_INLINE Packet4f pcmp_lt(const Packet4f& a, const Packet4f& b) {
882 return _mm_cmplt_ps(a, b);
883}
884template <>
885EIGEN_STRONG_INLINE Packet4f pcmp_lt_or_nan(const Packet4f& a, const Packet4f& b) {
886 return _mm_cmpnge_ps(a, b);
887}
888template <>
889EIGEN_STRONG_INLINE Packet4f pcmp_eq(const Packet4f& a, const Packet4f& b) {
890 return _mm_cmpeq_ps(a, b);
891}
892
893template <>
894EIGEN_STRONG_INLINE Packet2d pcmp_le(const Packet2d& a, const Packet2d& b) {
895 return _mm_cmple_pd(a, b);
896}
897template <>
898EIGEN_STRONG_INLINE Packet2d pcmp_lt(const Packet2d& a, const Packet2d& b) {
899 return _mm_cmplt_pd(a, b);
900}
901template <>
902EIGEN_STRONG_INLINE Packet2d pcmp_lt_or_nan(const Packet2d& a, const Packet2d& b) {
903 return _mm_cmpnge_pd(a, b);
904}
905template <>
906EIGEN_STRONG_INLINE Packet2d pcmp_eq(const Packet2d& a, const Packet2d& b) {
907 return _mm_cmpeq_pd(a, b);
908}
909template <>
910EIGEN_STRONG_INLINE Packet4i pcmp_lt(const Packet4i& a, const Packet4i& b) {
911 return _mm_cmplt_epi32(a, b);
912}
913template <>
914EIGEN_STRONG_INLINE Packet4i pcmp_eq(const Packet4i& a, const Packet4i& b) {
915 return _mm_cmpeq_epi32(a, b);
916}
917template <>
918EIGEN_STRONG_INLINE Packet4i pcmp_le(const Packet4i& a, const Packet4i& b) {
919#ifdef EIGEN_VECTORIZE_SSE4_1
920 return _mm_cmpeq_epi32(a, _mm_min_epi32(a, b));
921#else
922 return por(pcmp_lt(a, b), pcmp_eq(a, b));
923#endif
924}
925template <>
926EIGEN_STRONG_INLINE Packet2l pcmp_lt(const Packet2l& a, const Packet2l& b) {
927#ifdef EIGEN_VECTORIZE_SSE4_2
928 return _mm_cmpgt_epi64(b, a);
929#else
930 Packet4i eq = pcmp_eq<Packet4i>(Packet4i(a), Packet4i(b));
931 Packet2l hi_eq = Packet2l(_mm_shuffle_epi32(eq, (shuffle_mask<1, 1, 3, 3>::mask)));
932 // The low halves carry magnitude only and must be ordered as unsigned, while the high halves carry
933 // the sign. Biasing just the low lanes by 2^31 makes one signed 32-bit compare serve both.
934 const Packet4i kLowSignFlip = _mm_setr_epi32(SIGN_MASK_I32, 0x0, SIGN_MASK_I32, 0x0);
935 Packet4i lt = pcmp_lt<Packet4i>(pxor(Packet4i(a), kLowSignFlip), pxor(Packet4i(b), kLowSignFlip));
936 Packet2l hi_lt = Packet2l(_mm_shuffle_epi32(lt, (shuffle_mask<1, 1, 3, 3>::mask)));
937 Packet2l lo_lt = Packet2l(_mm_shuffle_epi32(lt, (shuffle_mask<0, 0, 2, 2>::mask)));
938 // return hi(a) < hi(b) || (hi(a) == hi(b) && lo(a) < lo(b))
939 return por(hi_lt, pand(hi_eq, lo_lt));
940#endif
941}
942template <>
943EIGEN_STRONG_INLINE Packet2l pcmp_eq(const Packet2l& a, const Packet2l& b) {
944#ifdef EIGEN_VECTORIZE_SSE4_1
945 return _mm_cmpeq_epi64(a, b);
946#else
947 Packet4i tmp = pcmp_eq<Packet4i>(Packet4i(a), Packet4i(b));
948 return Packet2l(pand<Packet4i>(tmp, _mm_shuffle_epi32(tmp, (shuffle_mask<1, 0, 3, 2>::mask))));
949#endif
950}
951template <>
952EIGEN_STRONG_INLINE Packet2l pcmp_le(const Packet2l& a, const Packet2l& b) {
953 return por(pcmp_lt(a, b), pcmp_eq(a, b));
954}
955template <>
956EIGEN_STRONG_INLINE Packet16b pcmp_eq(const Packet16b& a, const Packet16b& b) {
957 // Mask out invalid bool bits to avoid UB.
958 const Packet16b kBoolMask = pset1<Packet16b>(true);
959 return _mm_and_si128(_mm_cmpeq_epi8(a, b), kBoolMask);
960}
961template <>
962EIGEN_STRONG_INLINE Packet4ui pcmp_eq(const Packet4ui& a, const Packet4ui& b) {
963 return _mm_cmpeq_epi32(a, b);
964}
965
966template <>
967EIGEN_STRONG_INLINE Packet4f pmin<Packet4f>(const Packet4f& a, const Packet4f& b) {
968#if EIGEN_GNUC_STRICT_LESS_THAN(6, 3, 0)
969// There appears to be a bug in GCC, by which the optimizer may
970// flip the argument order in calls to _mm_min_ps, so we have to
971// resort to inline ASM here. This is supposed to be fixed in gcc6.3,
972// see also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=72867
973#ifdef EIGEN_VECTORIZE_AVX
974 Packet4f res;
975 asm("vminps %[a], %[b], %[res]" : [res] "=x"(res) : [a] "x"(a), [b] "x"(b));
976#else
977 Packet4f res = b;
978 asm("minps %[a], %[res]" : [res] "+x"(res) : [a] "x"(a));
979#endif
980 return res;
981#else
982 // Arguments are reversed to match NaN propagation behavior of std::min.
983 return _mm_min_ps(b, a);
984#endif
985}
986template <>
987EIGEN_STRONG_INLINE Packet2d pmin<Packet2d>(const Packet2d& a, const Packet2d& b) {
988#if EIGEN_GNUC_STRICT_LESS_THAN(6, 3, 0)
989// There appears to be a bug in GCC, by which the optimizer may
990// flip the argument order in calls to _mm_min_pd, so we have to
991// resort to inline ASM here. This is supposed to be fixed in gcc6.3,
992// see also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=72867
993#ifdef EIGEN_VECTORIZE_AVX
994 Packet2d res;
995 asm("vminpd %[a], %[b], %[res]" : [res] "=x"(res) : [a] "x"(a), [b] "x"(b));
996#else
997 Packet2d res = b;
998 asm("minpd %[a], %[res]" : [res] "+x"(res) : [a] "x"(a));
999#endif
1000 return res;
1001#else
1002 // Arguments are reversed to match NaN propagation behavior of std::min.
1003 return _mm_min_pd(b, a);
1004#endif
1005}
1006template <>
1007EIGEN_STRONG_INLINE Packet2l pmin<Packet2l>(const Packet2l& a, const Packet2l& b) {
1008 Packet2l a_lt_mask = pcmp_lt(a, b);
1009 return por(pandnot(b, a_lt_mask), pand(a, a_lt_mask));
1010}
1011template <>
1012EIGEN_STRONG_INLINE Packet4i pmin<Packet4i>(const Packet4i& a, const Packet4i& b) {
1013#ifdef EIGEN_VECTORIZE_SSE4_1
1014 return _mm_min_epi32(a, b);
1015#else
1016 // after some bench, this version *is* faster than a scalar implementation
1017 Packet4i mask = _mm_cmplt_epi32(a, b);
1018 return _mm_or_si128(_mm_and_si128(mask, a), _mm_andnot_si128(mask, b));
1019#endif
1020}
1021template <>
1022EIGEN_STRONG_INLINE Packet4ui pmin<Packet4ui>(const Packet4ui& a, const Packet4ui& b) {
1023#ifdef EIGEN_VECTORIZE_SSE4_1
1024 return _mm_min_epu32(a, b);
1025#else
1026 return padd((Packet4ui)pmin((Packet4i)psub(a, pset1<Packet4ui>(0x80000000UL)),
1027 (Packet4i)psub(b, pset1<Packet4ui>(0x80000000UL))),
1028 pset1<Packet4ui>(0x80000000UL));
1029#endif
1030}
1031
1032template <>
1033EIGEN_STRONG_INLINE Packet4f pmax<Packet4f>(const Packet4f& a, const Packet4f& b) {
1034#if EIGEN_GNUC_STRICT_LESS_THAN(6, 3, 0)
1035// There appears to be a bug in GCC, by which the optimizer may
1036// flip the argument order in calls to _mm_max_ps, so we have to
1037// resort to inline ASM here. This is supposed to be fixed in gcc6.3,
1038// see also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=72867
1039#ifdef EIGEN_VECTORIZE_AVX
1040 Packet4f res;
1041 asm("vmaxps %[a], %[b], %[res]" : [res] "=x"(res) : [a] "x"(a), [b] "x"(b));
1042#else
1043 Packet4f res = b;
1044 asm("maxps %[a], %[res]" : [res] "+x"(res) : [a] "x"(a));
1045#endif
1046 return res;
1047#else
1048 // Arguments are reversed to match NaN propagation behavior of std::max.
1049 return _mm_max_ps(b, a);
1050#endif
1051}
1052template <>
1053EIGEN_STRONG_INLINE Packet2d pmax<Packet2d>(const Packet2d& a, const Packet2d& b) {
1054#if EIGEN_GNUC_STRICT_LESS_THAN(6, 3, 0)
1055// There appears to be a bug in GCC, by which the optimizer may
1056// flip the argument order in calls to _mm_max_pd, so we have to
1057// resort to inline ASM here. This is supposed to be fixed in gcc6.3,
1058// see also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=72867
1059#ifdef EIGEN_VECTORIZE_AVX
1060 Packet2d res;
1061 asm("vmaxpd %[a], %[b], %[res]" : [res] "=x"(res) : [a] "x"(a), [b] "x"(b));
1062#else
1063 Packet2d res = b;
1064 asm("maxpd %[a], %[res]" : [res] "+x"(res) : [a] "x"(a));
1065#endif
1066 return res;
1067#else
1068 // Arguments are reversed to match NaN propagation behavior of std::max.
1069 return _mm_max_pd(b, a);
1070#endif
1071}
1072template <>
1073EIGEN_STRONG_INLINE Packet2l pmax<Packet2l>(const Packet2l& a, const Packet2l& b) {
1074 Packet2l a_lt_mask = pcmp_lt(a, b);
1075 return por(pandnot(a, a_lt_mask), pand(b, a_lt_mask));
1076}
1077template <>
1078EIGEN_STRONG_INLINE Packet4i pmax<Packet4i>(const Packet4i& a, const Packet4i& b) {
1079#ifdef EIGEN_VECTORIZE_SSE4_1
1080 return _mm_max_epi32(a, b);
1081#else
1082 // after some bench, this version *is* faster than a scalar implementation
1083 Packet4i mask = _mm_cmpgt_epi32(a, b);
1084 return _mm_or_si128(_mm_and_si128(mask, a), _mm_andnot_si128(mask, b));
1085#endif
1086}
1087template <>
1088EIGEN_STRONG_INLINE Packet4ui pmax<Packet4ui>(const Packet4ui& a, const Packet4ui& b) {
1089#ifdef EIGEN_VECTORIZE_SSE4_1
1090 return _mm_max_epu32(a, b);
1091#else
1092 return padd((Packet4ui)pmax((Packet4i)psub(a, pset1<Packet4ui>(0x80000000UL)),
1093 (Packet4i)psub(b, pset1<Packet4ui>(0x80000000UL))),
1094 pset1<Packet4ui>(0x80000000UL));
1095#endif
1096}
1097
1098template <>
1099EIGEN_STRONG_INLINE Packet4ui pcmp_lt(const Packet4ui& a, const Packet4ui& b) {
1100#ifdef EIGEN_VECTORIZE_SSE4_1
1101 return pxor(pcmp_eq(a, pmax(a, b)), ptrue(a));
1102#else
1103 return (Packet4ui)pcmp_lt((Packet4i)psub(a, pset1<Packet4ui>(0x80000000UL)),
1104 (Packet4i)psub(b, pset1<Packet4ui>(0x80000000UL)));
1105#endif
1106}
1107template <>
1108EIGEN_STRONG_INLINE Packet4ui pcmp_le(const Packet4ui& a, const Packet4ui& b) {
1109#ifdef EIGEN_VECTORIZE_SSE4_1
1110 return pcmp_eq(a, pmin(a, b));
1111#else
1112 return (Packet4ui)pcmp_le((Packet4i)psub(a, pset1<Packet4ui>(0x80000000UL)),
1113 (Packet4i)psub(b, pset1<Packet4ui>(0x80000000UL)));
1114#endif
1115}
1116
1117template <typename Packet, typename Op>
1118EIGEN_STRONG_INLINE Packet pminmax_propagate_numbers(const Packet& a, const Packet& b, Op op) {
1119 // In this implementation, we take advantage of the fact that pmin/pmax for SSE
1120 // always return a if either a or b is NaN.
1121 Packet not_nan_mask_a = pcmp_eq(a, a);
1122 Packet m = op(a, b);
1123 return pselect<Packet>(not_nan_mask_a, m, b);
1124}
1125
1126template <typename Packet, typename Op>
1127EIGEN_STRONG_INLINE Packet pminmax_propagate_nan(const Packet& a, const Packet& b, Op op) {
1128 // In this implementation, we take advantage of the fact that pmin/pmax for SSE
1129 // always return a if either a or b is NaN. Testing b rather than a is what supplies the
1130 // missing case, and it keeps op's operand order, hence its choice on a signed-zero tie.
1131 Packet not_nan_mask_b = pcmp_eq(b, b);
1132 Packet m = op(a, b);
1133 return pselect<Packet>(not_nan_mask_b, m, b);
1134}
1135
1136// Add specializations for min/max with prescribed NaN propagation.
1137template <>
1138EIGEN_STRONG_INLINE Packet4f pmin<PropagateNumbers, Packet4f>(const Packet4f& a, const Packet4f& b) {
1139 return pminmax_propagate_numbers(a, b, pmin<Packet4f>);
1140}
1141template <>
1142EIGEN_STRONG_INLINE Packet2d pmin<PropagateNumbers, Packet2d>(const Packet2d& a, const Packet2d& b) {
1143 return pminmax_propagate_numbers(a, b, pmin<Packet2d>);
1144}
1145template <>
1146EIGEN_STRONG_INLINE Packet4f pmax<PropagateNumbers, Packet4f>(const Packet4f& a, const Packet4f& b) {
1147 return pminmax_propagate_numbers(a, b, pmax<Packet4f>);
1148}
1149template <>
1150EIGEN_STRONG_INLINE Packet2d pmax<PropagateNumbers, Packet2d>(const Packet2d& a, const Packet2d& b) {
1151 return pminmax_propagate_numbers(a, b, pmax<Packet2d>);
1152}
1153template <>
1154EIGEN_STRONG_INLINE Packet4f pmin<PropagateNaN, Packet4f>(const Packet4f& a, const Packet4f& b) {
1155 return pminmax_propagate_nan(a, b, pmin<Packet4f>);
1156}
1157template <>
1158EIGEN_STRONG_INLINE Packet2d pmin<PropagateNaN, Packet2d>(const Packet2d& a, const Packet2d& b) {
1159 return pminmax_propagate_nan(a, b, pmin<Packet2d>);
1160}
1161template <>
1162EIGEN_STRONG_INLINE Packet4f pmax<PropagateNaN, Packet4f>(const Packet4f& a, const Packet4f& b) {
1163 return pminmax_propagate_nan(a, b, pmax<Packet4f>);
1164}
1165template <>
1166EIGEN_STRONG_INLINE Packet2d pmax<PropagateNaN, Packet2d>(const Packet2d& a, const Packet2d& b) {
1167 return pminmax_propagate_nan(a, b, pmax<Packet2d>);
1168}
1169
1170template <>
1171EIGEN_STRONG_INLINE Packet4f psignbit(const Packet4f& a) {
1172 return _mm_castsi128_ps(_mm_srai_epi32(_mm_castps_si128(a), 31));
1173}
1174template <>
1175EIGEN_STRONG_INLINE Packet2d psignbit(const Packet2d& a) {
1176 Packet4f tmp = psignbit<Packet4f>(_mm_castpd_ps(a));
1177#ifdef EIGEN_VECTORIZE_AVX
1178 return _mm_castps_pd(_mm_permute_ps(tmp, (shuffle_mask<1, 1, 3, 3>::mask)));
1179#else
1180 return _mm_castps_pd(_mm_shuffle_ps(tmp, tmp, (shuffle_mask<1, 1, 3, 3>::mask)));
1181#endif // EIGEN_VECTORIZE_AVX
1182}
1183template <>
1184EIGEN_STRONG_INLINE Packet4i psignbit(const Packet4i& a) {
1185 return _mm_srai_epi32(a, 31);
1186}
1187template <>
1188EIGEN_STRONG_INLINE Packet2l psignbit(const Packet2l& a) {
1189 Packet4i tmp = psignbit<Packet4i>(Packet4i(a));
1190 return Packet2l(_mm_shuffle_epi32(tmp, (shuffle_mask<1, 1, 3, 3>::mask)));
1191}
1192
1193template <int N>
1194EIGEN_STRONG_INLINE Packet2l parithmetic_shift_right(const Packet2l& a) {
1195 Packet2l signbit = psignbit(a);
1196 return por(_mm_slli_epi64(signbit, 64 - N), _mm_srli_epi64(a, N));
1197}
1198template <int N>
1199EIGEN_STRONG_INLINE Packet2l plogical_shift_right(const Packet2l& a) {
1200 return _mm_srli_epi64(a, N);
1201}
1202template <int N>
1203EIGEN_STRONG_INLINE Packet2l plogical_shift_left(const Packet2l& a) {
1204 return _mm_slli_epi64(a, N);
1205}
1206template <int N>
1207EIGEN_STRONG_INLINE Packet4i parithmetic_shift_right(const Packet4i& a) {
1208 return _mm_srai_epi32(a, N);
1209}
1210template <int N>
1211EIGEN_STRONG_INLINE Packet4i plogical_shift_right(const Packet4i& a) {
1212 return _mm_srli_epi32(a, N);
1213}
1214template <int N>
1215EIGEN_STRONG_INLINE Packet4i plogical_shift_left(const Packet4i& a) {
1216 return _mm_slli_epi32(a, N);
1217}
1218template <int N>
1219EIGEN_STRONG_INLINE Packet4ui parithmetic_shift_right(const Packet4ui& a) {
1220 return _mm_srli_epi32(a, N);
1221}
1222template <int N>
1223EIGEN_STRONG_INLINE Packet4ui plogical_shift_right(const Packet4ui& a) {
1224 return _mm_srli_epi32(a, N);
1225}
1226template <int N>
1227EIGEN_STRONG_INLINE Packet4ui plogical_shift_left(const Packet4ui& a) {
1228 return _mm_slli_epi32(a, N);
1229}
1230
1231template <>
1232EIGEN_STRONG_INLINE Packet4f pabs(const Packet4f& a) {
1233 const __m128i mask = _mm_setr_epi32(0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF);
1234 return _mm_castsi128_ps(_mm_and_si128(mask, _mm_castps_si128(a)));
1235}
1236template <>
1237EIGEN_STRONG_INLINE Packet2d pabs(const Packet2d& a) {
1238 const __m128i mask = _mm_setr_epi32(-1, 0x7FFFFFFF, -1, 0x7FFFFFFF);
1239 return _mm_castsi128_pd(_mm_and_si128(mask, _mm_castpd_si128(a)));
1240}
1241template <>
1242EIGEN_STRONG_INLINE Packet2l pabs(const Packet2l& a) {
1243 Packet2l signbit = psignbit(a);
1244 return _mm_sub_epi64(_mm_xor_si128(a, signbit), signbit);
1245}
1246template <>
1247EIGEN_STRONG_INLINE Packet4i pabs(const Packet4i& a) {
1248#ifdef EIGEN_VECTORIZE_SSSE3
1249 return _mm_abs_epi32(a);
1250#else
1251 Packet4i signbit = psignbit(a);
1252 return _mm_sub_epi32(_mm_xor_si128(a, signbit), signbit);
1253#endif
1254}
1255#ifdef EIGEN_VECTORIZE_SSE4_1
1256template <>
1257EIGEN_STRONG_INLINE Packet4f pround<Packet4f>(const Packet4f& a) {
1258 // Unfortunately _mm_round_ps doesn't have a rounding mode to implement numext::round.
1259 const Packet4f mask = pset1frombits<Packet4f>(0x80000000u);
1260 const Packet4f prev0dot5 = pset1frombits<Packet4f>(0x3EFFFFFFu);
1261 return _mm_round_ps(padd(por(pand(a, mask), prev0dot5), a), _MM_FROUND_TO_ZERO);
1262}
1263
1264template <>
1265EIGEN_STRONG_INLINE Packet2d pround<Packet2d>(const Packet2d& a) {
1266 const Packet2d mask = _mm_castsi128_pd(_mm_set_epi64x(0x8000000000000000ull, 0x8000000000000000ull));
1267 const Packet2d prev0dot5 = _mm_castsi128_pd(_mm_set_epi64x(0x3FDFFFFFFFFFFFFFull, 0x3FDFFFFFFFFFFFFFull));
1268 return _mm_round_pd(padd(por(pand(a, mask), prev0dot5), a), _MM_FROUND_TO_ZERO);
1269}
1270
1271template <>
1272EIGEN_STRONG_INLINE Packet4f print<Packet4f>(const Packet4f& a) {
1273 return _mm_round_ps(a, _MM_FROUND_CUR_DIRECTION);
1274}
1275template <>
1276EIGEN_STRONG_INLINE Packet2d print<Packet2d>(const Packet2d& a) {
1277 return _mm_round_pd(a, _MM_FROUND_CUR_DIRECTION);
1278}
1279
1280template <>
1281EIGEN_STRONG_INLINE Packet4f pceil<Packet4f>(const Packet4f& a) {
1282 return _mm_ceil_ps(a);
1283}
1284template <>
1285EIGEN_STRONG_INLINE Packet2d pceil<Packet2d>(const Packet2d& a) {
1286 return _mm_ceil_pd(a);
1287}
1288
1289template <>
1290EIGEN_STRONG_INLINE Packet4f pfloor<Packet4f>(const Packet4f& a) {
1291 return _mm_floor_ps(a);
1292}
1293template <>
1294EIGEN_STRONG_INLINE Packet2d pfloor<Packet2d>(const Packet2d& a) {
1295 return _mm_floor_pd(a);
1296}
1297
1298template <>
1299EIGEN_STRONG_INLINE Packet4f ptrunc<Packet4f>(const Packet4f& a) {
1300 return _mm_round_ps(a, _MM_FROUND_TRUNC);
1301}
1302template <>
1303EIGEN_STRONG_INLINE Packet2d ptrunc<Packet2d>(const Packet2d& a) {
1304 return _mm_round_pd(a, _MM_FROUND_TRUNC);
1305}
1306#endif
1307
1308template <>
1309EIGEN_STRONG_INLINE Packet4f pload<Packet4f>(const float* from) {
1310 EIGEN_DEBUG_ALIGNED_LOAD return _mm_load_ps(from);
1311}
1312template <>
1313EIGEN_STRONG_INLINE Packet2d pload<Packet2d>(const double* from) {
1314 EIGEN_DEBUG_ALIGNED_LOAD return _mm_load_pd(from);
1315}
1316template <>
1317EIGEN_STRONG_INLINE Packet2l pload<Packet2l>(const int64_t* from) {
1318 EIGEN_DEBUG_ALIGNED_LOAD return _mm_load_si128(reinterpret_cast<const __m128i*>(from));
1319}
1320template <>
1321EIGEN_STRONG_INLINE Packet4i pload<Packet4i>(const int* from) {
1322 EIGEN_DEBUG_ALIGNED_LOAD return _mm_load_si128(reinterpret_cast<const __m128i*>(from));
1323}
1324template <>
1325EIGEN_STRONG_INLINE Packet4ui pload<Packet4ui>(const uint32_t* from) {
1326 EIGEN_DEBUG_ALIGNED_LOAD return _mm_load_si128(reinterpret_cast<const __m128i*>(from));
1327}
1328template <>
1329EIGEN_STRONG_INLINE Packet16b pload<Packet16b>(const bool* from) {
1330 EIGEN_DEBUG_ALIGNED_LOAD return _mm_load_si128(reinterpret_cast<const __m128i*>(from));
1331}
1332
1333template <>
1334EIGEN_STRONG_INLINE Packet4f ploadu<Packet4f>(const float* from) {
1335 EIGEN_DEBUG_UNALIGNED_LOAD
1336 return _mm_loadu_ps(from);
1337}
1338
1339template <>
1340EIGEN_STRONG_INLINE Packet2d ploadu<Packet2d>(const double* from) {
1341 EIGEN_DEBUG_UNALIGNED_LOAD
1342 return _mm_loadu_pd(from);
1343}
1344template <>
1345EIGEN_STRONG_INLINE Packet2l ploadu<Packet2l>(const int64_t* from) {
1346 EIGEN_DEBUG_UNALIGNED_LOAD
1347 return _mm_loadu_si128(reinterpret_cast<const __m128i*>(from));
1348}
1349template <>
1350EIGEN_STRONG_INLINE Packet4i ploadu<Packet4i>(const int* from) {
1351 EIGEN_DEBUG_UNALIGNED_LOAD
1352 return _mm_loadu_si128(reinterpret_cast<const __m128i*>(from));
1353}
1354template <>
1355EIGEN_STRONG_INLINE Packet4ui ploadu<Packet4ui>(const uint32_t* from) {
1356 EIGEN_DEBUG_UNALIGNED_LOAD
1357 return _mm_loadu_si128(reinterpret_cast<const __m128i*>(from));
1358}
1359template <>
1360EIGEN_STRONG_INLINE Packet16b ploadu<Packet16b>(const bool* from) {
1361 EIGEN_DEBUG_UNALIGNED_LOAD
1362 return _mm_loadu_si128(reinterpret_cast<const __m128i*>(from));
1363}
1364
1365EIGEN_STRONG_INLINE __m128i ploadu_si64(const void* from) {
1366#if EIGEN_GNUC_STRICT_LESS_THAN(9, 1, 0) || (EIGEN_COMP_MINGW && !EIGEN_COMP_CLANG && EIGEN_COMP_GNUC < 910)
1367 // GCC added _mm_loadu_si64 in 9.1. Copy through __m64 to avoid relying on _mm_loadl_epi64's type-punned load.
1368 EIGEN_USING_STD(memcpy);
1369 __m64 lo;
1370 memcpy(&lo, from, sizeof(lo));
1371 return _mm_set_epi64((__m64)0LL, lo);
1372#else
1373 return _mm_loadu_si64(from);
1374#endif
1375}
1376
1377// Load lower part of packet zero extending.
1378template <typename Packet>
1379EIGEN_STRONG_INLINE Packet ploadl(const typename unpacket_traits<Packet>::type* from);
1380template <>
1381EIGEN_STRONG_INLINE Packet4f ploadl<Packet4f>(const float* from) {
1382 EIGEN_DEBUG_UNALIGNED_LOAD return _mm_castsi128_ps(ploadu_si64(reinterpret_cast<const void*>(from)));
1383}
1384template <>
1385EIGEN_STRONG_INLINE Packet2d ploadl<Packet2d>(const double* from) {
1386 EIGEN_DEBUG_UNALIGNED_LOAD return _mm_load_sd(from);
1387}
1388
1389// Load scalar
1390template <typename Packet>
1391EIGEN_STRONG_INLINE Packet ploads(const typename unpacket_traits<Packet>::type* from);
1392template <>
1393EIGEN_STRONG_INLINE Packet4f ploads<Packet4f>(const float* from) {
1394 EIGEN_DEBUG_UNALIGNED_LOAD return _mm_load_ss(from);
1395}
1396template <>
1397EIGEN_STRONG_INLINE Packet2d ploads<Packet2d>(const double* from) {
1398 EIGEN_DEBUG_UNALIGNED_LOAD return _mm_load_sd(from);
1399}
1400
1401template <>
1402EIGEN_STRONG_INLINE Packet4f ploaddup<Packet4f>(const float* from) {
1403 return vec4f_swizzle1(_mm_castsi128_ps(ploadu_si64(reinterpret_cast<const void*>(from))), 0, 0, 1, 1);
1404}
1405template <>
1406EIGEN_STRONG_INLINE Packet4i ploaddup<Packet4i>(const int* from) {
1407 Packet4i tmp;
1408 tmp = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(from));
1409 return vec4i_swizzle1(tmp, 0, 0, 1, 1);
1410}
1411template <>
1412EIGEN_STRONG_INLINE Packet4ui ploaddup<Packet4ui>(const uint32_t* from) {
1413 Packet4ui tmp;
1414 tmp = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(from));
1415 return vec4ui_swizzle1(tmp, 0, 0, 1, 1);
1416}
1417
1418// Loads 8 bools from memory and returns the packet
1419// {b0, b0, b1, b1, b2, b2, b3, b3, b4, b4, b5, b5, b6, b6, b7, b7}
1420template <>
1421EIGEN_STRONG_INLINE Packet16b ploaddup<Packet16b>(const bool* from) {
1422 __m128i tmp = ploadu_si64(reinterpret_cast<const void*>(from));
1423 return _mm_unpacklo_epi8(tmp, tmp);
1424}
1425
1426// Loads 4 bools from memory and returns the packet
1427// {b0, b0, b0, b0, b1, b1, b1, b1, b2, b2, b2, b2, b3, b3, b3, b3}
1428template <>
1429EIGEN_STRONG_INLINE Packet16b ploadquad<Packet16b>(const bool* from) {
1430 EIGEN_USING_STD(memcpy);
1431 int val;
1432 memcpy(&val, from, sizeof(int));
1433 __m128i tmp = _mm_cvtsi32_si128(val);
1434 tmp = _mm_unpacklo_epi8(tmp, tmp);
1435 return _mm_unpacklo_epi16(tmp, tmp);
1436}
1437
1438template <>
1439EIGEN_STRONG_INLINE void pstore<float>(float* to, const Packet4f& from) {
1440 EIGEN_DEBUG_ALIGNED_STORE _mm_store_ps(to, from);
1441}
1442template <>
1443EIGEN_STRONG_INLINE void pstore<double>(double* to, const Packet2d& from) {
1444 EIGEN_DEBUG_ALIGNED_STORE _mm_store_pd(to, from);
1445}
1446template <>
1447EIGEN_STRONG_INLINE void pstore<int64_t>(int64_t* to, const Packet2l& from) {
1448 EIGEN_DEBUG_ALIGNED_STORE _mm_store_si128(reinterpret_cast<__m128i*>(to), from);
1449}
1450template <>
1451EIGEN_STRONG_INLINE void pstore<int>(int* to, const Packet4i& from) {
1452 EIGEN_DEBUG_ALIGNED_STORE _mm_store_si128(reinterpret_cast<__m128i*>(to), from);
1453}
1454template <>
1455EIGEN_STRONG_INLINE void pstore<uint32_t>(uint32_t* to, const Packet4ui& from) {
1456 EIGEN_DEBUG_ALIGNED_STORE _mm_store_si128(reinterpret_cast<__m128i*>(to), from);
1457}
1458template <>
1459EIGEN_STRONG_INLINE void pstore<bool>(bool* to, const Packet16b& from) {
1460 EIGEN_DEBUG_ALIGNED_STORE _mm_store_si128(reinterpret_cast<__m128i*>(to), from);
1461}
1462
1463template <>
1464EIGEN_STRONG_INLINE void pstoreu<double>(double* to, const Packet2d& from) {
1465 EIGEN_DEBUG_UNALIGNED_STORE _mm_storeu_pd(to, from);
1466}
1467template <>
1468EIGEN_STRONG_INLINE void pstoreu<float>(float* to, const Packet4f& from) {
1469 EIGEN_DEBUG_UNALIGNED_STORE _mm_storeu_ps(to, from);
1470}
1471template <>
1472EIGEN_STRONG_INLINE void pstoreu<int64_t>(int64_t* to, const Packet2l& from) {
1473 EIGEN_DEBUG_UNALIGNED_STORE _mm_storeu_si128(reinterpret_cast<__m128i*>(to), from);
1474}
1475template <>
1476EIGEN_STRONG_INLINE void pstoreu<int>(int* to, const Packet4i& from) {
1477 EIGEN_DEBUG_UNALIGNED_STORE _mm_storeu_si128(reinterpret_cast<__m128i*>(to), from);
1478}
1479template <>
1480EIGEN_STRONG_INLINE void pstoreu<uint32_t>(uint32_t* to, const Packet4ui& from) {
1481 EIGEN_DEBUG_UNALIGNED_STORE _mm_storeu_si128(reinterpret_cast<__m128i*>(to), from);
1482}
1483template <>
1484EIGEN_STRONG_INLINE void pstoreu<bool>(bool* to, const Packet16b& from) {
1485 EIGEN_DEBUG_UNALIGNED_STORE _mm_storeu_si128(reinterpret_cast<__m128i*>(to), from);
1486}
1487
1488template <typename Scalar, typename Packet>
1489EIGEN_STRONG_INLINE void pstorel(Scalar* to, const Packet& from);
1490template <>
1491EIGEN_STRONG_INLINE void pstorel(float* to, const Packet4f& from) {
1492 EIGEN_DEBUG_UNALIGNED_STORE _mm_storel_pi(reinterpret_cast<__m64*>(to), from);
1493}
1494template <>
1495EIGEN_STRONG_INLINE void pstorel(double* to, const Packet2d& from) {
1496 EIGEN_DEBUG_UNALIGNED_STORE _mm_storel_pd(to, from);
1497}
1498
1499template <typename Scalar, typename Packet>
1500EIGEN_STRONG_INLINE void pstores(Scalar* to, const Packet& from);
1501template <>
1502EIGEN_STRONG_INLINE void pstores(float* to, const Packet4f& from) {
1503 EIGEN_DEBUG_UNALIGNED_STORE _mm_store_ss(to, from);
1504}
1505template <>
1506EIGEN_STRONG_INLINE void pstores(double* to, const Packet2d& from) {
1507 EIGEN_DEBUG_UNALIGNED_STORE _mm_store_sd(to, from);
1508}
1509
1510template <>
1511EIGEN_STRONG_INLINE Packet4f preverse(const Packet4f& a) {
1512 return _mm_shuffle_ps(a, a, 0x1B);
1513}
1514template <>
1515EIGEN_STRONG_INLINE Packet2d preverse(const Packet2d& a) {
1516 return _mm_shuffle_pd(a, a, 0x1);
1517}
1518template <>
1519EIGEN_STRONG_INLINE Packet2l preverse(const Packet2l& a) {
1520 return _mm_castpd_si128(preverse(_mm_castsi128_pd(a)));
1521}
1522template <>
1523EIGEN_STRONG_INLINE Packet4i preverse(const Packet4i& a) {
1524 return _mm_shuffle_epi32(a, 0x1B);
1525}
1526template <>
1527EIGEN_STRONG_INLINE Packet4ui preverse(const Packet4ui& a) {
1528 return _mm_shuffle_epi32(a, 0x1B);
1529}
1530template <>
1531EIGEN_STRONG_INLINE Packet16b preverse(const Packet16b& a) {
1532#ifdef EIGEN_VECTORIZE_SSSE3
1533 __m128i mask = _mm_set_epi8(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
1534 return _mm_shuffle_epi8(a, mask);
1535#else
1536 Packet16b tmp = _mm_shuffle_epi32(a, _MM_SHUFFLE(0, 1, 2, 3));
1537 tmp = _mm_shufflehi_epi16(_mm_shufflelo_epi16(tmp, _MM_SHUFFLE(2, 3, 0, 1)), _MM_SHUFFLE(2, 3, 0, 1));
1538 return _mm_or_si128(_mm_slli_epi16(tmp, 8), _mm_srli_epi16(tmp, 8));
1539#endif
1540}
1541
1542template <>
1543EIGEN_STRONG_INLINE float pfirst<Packet4f>(const Packet4f& a) {
1544 return _mm_cvtss_f32(a);
1545}
1546template <>
1547EIGEN_STRONG_INLINE double pfirst<Packet2d>(const Packet2d& a) {
1548 return _mm_cvtsd_f64(a);
1549}
1550template <>
1551EIGEN_STRONG_INLINE int64_t pfirst<Packet2l>(const Packet2l& a) {
1552 return _mm_extract_epi64_0(a);
1553}
1554template <>
1555EIGEN_STRONG_INLINE int pfirst<Packet4i>(const Packet4i& a) {
1556 return _mm_cvtsi128_si32(a);
1557}
1558template <>
1559EIGEN_STRONG_INLINE uint32_t pfirst<Packet4ui>(const Packet4ui& a) {
1560 return numext::bit_cast<uint32_t>(_mm_cvtsi128_si32(a));
1561}
1562template <>
1563EIGEN_STRONG_INLINE bool pfirst<Packet16b>(const Packet16b& a) {
1564 int x = _mm_cvtsi128_si32(a);
1565 return static_cast<bool>(x & 1);
1566}
1567
1568template <>
1569EIGEN_STRONG_INLINE Packet4f pgather<float, Packet4f>(const float* from, Index stride) {
1570 if (stride == 2) {
1571 // Overlap the loads so the second ends at the last gathered coefficient, from[6].
1572 return _mm_shuffle_ps(_mm_loadu_ps(from), _mm_loadu_ps(from + 3), _MM_SHUFFLE(3, 1, 2, 0));
1573 }
1574 return _mm_set_ps(from[3 * stride], from[2 * stride], from[1 * stride], from[0 * stride]);
1575}
1576template <>
1577EIGEN_STRONG_INLINE Packet2d pgather<double, Packet2d>(const double* from, Index stride) {
1578 return _mm_set_pd(from[1 * stride], from[0 * stride]);
1579}
1580template <>
1581EIGEN_STRONG_INLINE Packet2l pgather<int64_t, Packet2l>(const int64_t* from, Index stride) {
1582 return _mm_set_epi64x(from[1 * stride], from[0 * stride]);
1583}
1584template <>
1585EIGEN_STRONG_INLINE Packet4i pgather<int, Packet4i>(const int* from, Index stride) {
1586 return _mm_set_epi32(from[3 * stride], from[2 * stride], from[1 * stride], from[0 * stride]);
1587}
1588template <>
1589EIGEN_STRONG_INLINE Packet4ui pgather<uint32_t, Packet4ui>(const uint32_t* from, Index stride) {
1590 return _mm_set_epi32(numext::bit_cast<int32_t>(from[3 * stride]), numext::bit_cast<int32_t>(from[2 * stride]),
1591 numext::bit_cast<int32_t>(from[1 * stride]), numext::bit_cast<int32_t>(from[0 * stride]));
1592}
1593
1594template <>
1595EIGEN_STRONG_INLINE Packet16b pgather<bool, Packet16b>(const bool* from, Index stride) {
1596 return _mm_set_epi8(from[15 * stride], from[14 * stride], from[13 * stride], from[12 * stride], from[11 * stride],
1597 from[10 * stride], from[9 * stride], from[8 * stride], from[7 * stride], from[6 * stride],
1598 from[5 * stride], from[4 * stride], from[3 * stride], from[2 * stride], from[1 * stride],
1599 from[0 * stride]);
1600}
1601
1602template <>
1603EIGEN_STRONG_INLINE void pscatter<float, Packet4f>(float* to, const Packet4f& from, Index stride) {
1604 to[stride * 0] = pfirst(from);
1605 to[stride * 1] = pfirst(Packet4f(_mm_shuffle_ps(from, from, 1)));
1606 to[stride * 2] = pfirst(Packet4f(_mm_shuffle_ps(from, from, 2)));
1607 to[stride * 3] = pfirst(Packet4f(_mm_shuffle_ps(from, from, 3)));
1608}
1609template <>
1610EIGEN_STRONG_INLINE void pscatter<double, Packet2d>(double* to, const Packet2d& from, Index stride) {
1611 to[stride * 0] = pfirst(from);
1612 to[stride * 1] = pfirst(preverse(from));
1613}
1614template <>
1615EIGEN_STRONG_INLINE void pscatter<int64_t, Packet2l>(int64_t* to, const Packet2l& from, Index stride) {
1616 to[stride * 0] = pfirst(from);
1617 to[stride * 1] = pfirst(preverse(from));
1618}
1619template <>
1620EIGEN_STRONG_INLINE void pscatter<int, Packet4i>(int* to, const Packet4i& from, Index stride) {
1621 to[stride * 0] = _mm_cvtsi128_si32(from);
1622 to[stride * 1] = _mm_cvtsi128_si32(_mm_shuffle_epi32(from, 1));
1623 to[stride * 2] = _mm_cvtsi128_si32(_mm_shuffle_epi32(from, 2));
1624 to[stride * 3] = _mm_cvtsi128_si32(_mm_shuffle_epi32(from, 3));
1625}
1626template <>
1627EIGEN_STRONG_INLINE void pscatter<uint32_t, Packet4ui>(uint32_t* to, const Packet4ui& from, Index stride) {
1628 to[stride * 0] = numext::bit_cast<uint32_t>(_mm_cvtsi128_si32(from));
1629 to[stride * 1] = numext::bit_cast<uint32_t>(_mm_cvtsi128_si32(_mm_shuffle_epi32(from, 1)));
1630 to[stride * 2] = numext::bit_cast<uint32_t>(_mm_cvtsi128_si32(_mm_shuffle_epi32(from, 2)));
1631 to[stride * 3] = numext::bit_cast<uint32_t>(_mm_cvtsi128_si32(_mm_shuffle_epi32(from, 3)));
1632}
1633template <>
1634EIGEN_STRONG_INLINE void pscatter<bool, Packet16b>(bool* to, const Packet16b& from, Index stride) {
1635 EIGEN_ALIGN16 bool tmp[16];
1636 pstore(tmp, from);
1637 to[stride * 0] = tmp[0];
1638 to[stride * 1] = tmp[1];
1639 to[stride * 2] = tmp[2];
1640 to[stride * 3] = tmp[3];
1641 to[stride * 4] = tmp[4];
1642 to[stride * 5] = tmp[5];
1643 to[stride * 6] = tmp[6];
1644 to[stride * 7] = tmp[7];
1645 to[stride * 8] = tmp[8];
1646 to[stride * 9] = tmp[9];
1647 to[stride * 10] = tmp[10];
1648 to[stride * 11] = tmp[11];
1649 to[stride * 12] = tmp[12];
1650 to[stride * 13] = tmp[13];
1651 to[stride * 14] = tmp[14];
1652 to[stride * 15] = tmp[15];
1653}
1654
1655// some compilers might be tempted to perform multiple moves instead of using a vector path.
1656template <>
1657EIGEN_STRONG_INLINE void pstore1<Packet4f>(float* to, const float& a) {
1658 Packet4f pa = _mm_set_ss(a);
1659 pstore(to, Packet4f(vec4f_swizzle1(pa, 0, 0, 0, 0)));
1660}
1661// some compilers might be tempted to perform multiple moves instead of using a vector path.
1662template <>
1663EIGEN_STRONG_INLINE void pstore1<Packet2d>(double* to, const double& a) {
1664 Packet2d pa = _mm_set_sd(a);
1665 pstore(to, Packet2d(vec2d_swizzle1(pa, 0, 0)));
1666}
1667
1668#if EIGEN_COMP_PGI && EIGEN_COMP_PGI < 1900
1669using SsePrefetchPtrType = const void*;
1670#else
1671using SsePrefetchPtrType = const char*;
1672#endif
1673
1674#ifndef EIGEN_VECTORIZE_AVX
1675template <>
1676EIGEN_STRONG_INLINE void prefetch<float>(const float* addr) {
1677 _mm_prefetch((SsePrefetchPtrType)(addr), _MM_HINT_T0);
1678}
1679template <>
1680EIGEN_STRONG_INLINE void prefetch<double>(const double* addr) {
1681 _mm_prefetch((SsePrefetchPtrType)(addr), _MM_HINT_T0);
1682}
1683template <>
1684EIGEN_STRONG_INLINE void prefetch<int>(const int* addr) {
1685 _mm_prefetch((SsePrefetchPtrType)(addr), _MM_HINT_T0);
1686}
1687template <>
1688EIGEN_STRONG_INLINE void prefetch<int64_t>(const int64_t* addr) {
1689 _mm_prefetch((SsePrefetchPtrType)(addr), _MM_HINT_T0);
1690}
1691template <>
1692EIGEN_STRONG_INLINE void prefetch<uint32_t>(const uint32_t* addr) {
1693 _mm_prefetch((SsePrefetchPtrType)(addr), _MM_HINT_T0);
1694}
1695#endif
1696
1697template <>
1698EIGEN_STRONG_INLINE Packet4f pfrexp<Packet4f>(const Packet4f& a, Packet4f& exponent) {
1699 return pfrexp_generic(a, exponent);
1700}
1701
1702// Extract exponent without existence of Packet2l.
1703template <>
1704EIGEN_STRONG_INLINE Packet2d pfrexp_generic_get_biased_exponent(const Packet2d& a) {
1705 const Packet2d cst_exp_mask = pset1frombits<Packet2d>(static_cast<uint64_t>(0x7ff0000000000000ull));
1706 __m128i a_expo = _mm_srli_epi64(_mm_castpd_si128(pand(a, cst_exp_mask)), 52);
1707 return _mm_cvtepi32_pd(vec4i_swizzle1(a_expo, 0, 2, 1, 3));
1708}
1709
1710template <>
1711EIGEN_STRONG_INLINE Packet2d pfrexp<Packet2d>(const Packet2d& a, Packet2d& exponent) {
1712 return pfrexp_generic(a, exponent);
1713}
1714
1715template <>
1716EIGEN_STRONG_INLINE Packet4f pldexp<Packet4f>(const Packet4f& a, const Packet4f& exponent) {
1717 return pldexp_generic(a, exponent);
1718}
1719
1720// We specialize pldexp here, since the generic implementation uses Packet2l, which is not well
1721// supported by SSE, and has more range than is needed for exponents.
1722template <>
1723EIGEN_STRONG_INLINE Packet2d pldexp<Packet2d>(const Packet2d& a, const Packet2d& exponent) {
1724 // The single-rounding split of pldexp_generic on the two low int32 lanes. Interleaving t and e - t puts each
1725 // biased pair in one 64-bit lane, t + 2 * bias low: shifting left by 51 drops the high half, and shifting right by
1726 // 32 first selects it.
1727 const Packet2d max_exponent = pset1<Packet2d>(2099.0);
1728 const Packet2d last_max = pset1<Packet2d>(1022.0);
1729 const Packet4i e = _mm_cvtpd_epi32(pmin(pmax(exponent, pnegate(max_exponent)), max_exponent));
1730 const Packet4i b = _mm_cvtpd_epi32(pmin(pmax(exponent, pnegate(last_max)), last_max));
1731 const Packet4i t = pandnot(psub(e, b), pset1<Packet4i>(1)); // even
1732 const Packet4i biased =
1733 padd(Packet4i(_mm_unpacklo_epi32(t, psub(e, t))), Packet4i(_mm_set_epi32(1023, 2046, 1023, 2046)));
1734 const Packet2d c1 = _mm_castsi128_pd(_mm_slli_epi64(biased, 51)); // 2^(t/2)
1735 const Packet2d c2 = _mm_castsi128_pd(_mm_slli_epi64(_mm_srli_epi64(biased, 32), 52)); // 2^(e - t)
1736 return pldexp_apply_factors(a, c1, c2); // a * 2^e
1737}
1738
1739// We specialize pldexp here, since the generic implementation uses Packet2l, which is not well
1740// supported by SSE, and has more range than is needed for exponents.
1741template <>
1742EIGEN_STRONG_INLINE Packet2d pldexp_fast<Packet2d>(const Packet2d& a, const Packet2d& exponent) {
1743 // Clamp exponent to [-1023, 1024]
1744 const Packet2d min_exponent = pset1<Packet2d>(-1023.0);
1745 const Packet2d max_exponent = pset1<Packet2d>(1024.0);
1746 const Packet2d e = pmin(pmax(exponent, min_exponent), max_exponent);
1747
1748 // Convert e to integer and swizzle to low-order bits.
1749 const Packet4i ei = vec4i_swizzle1(_mm_cvtpd_epi32(e), 0, 3, 1, 3);
1750
1751 // Compute 2^e multiply:
1752 const Packet4i bias = _mm_set_epi32(0, 1023, 0, 1023);
1753 const Packet2d c = _mm_castsi128_pd(_mm_slli_epi64(padd(ei, bias), 52)); // 2^e
1754 return pmul(a, c);
1755}
1756
1757// with AVX, the default implementations based on pload1 are faster
1758#ifndef __AVX__
1759template <>
1760EIGEN_STRONG_INLINE void pbroadcast4<Packet4f>(const float* a, Packet4f& a0, Packet4f& a1, Packet4f& a2, Packet4f& a3) {
1761 a3 = pload<Packet4f>(a);
1762 a0 = vec4f_swizzle1(a3, 0, 0, 0, 0);
1763 a1 = vec4f_swizzle1(a3, 1, 1, 1, 1);
1764 a2 = vec4f_swizzle1(a3, 2, 2, 2, 2);
1765 a3 = vec4f_swizzle1(a3, 3, 3, 3, 3);
1766}
1767template <>
1768EIGEN_STRONG_INLINE void pbroadcast4<Packet2d>(const double* a, Packet2d& a0, Packet2d& a1, Packet2d& a2,
1769 Packet2d& a3) {
1770#ifdef EIGEN_VECTORIZE_SSE3
1771 a0 = _mm_loaddup_pd(a + 0);
1772 a1 = _mm_loaddup_pd(a + 1);
1773 a2 = _mm_loaddup_pd(a + 2);
1774 a3 = _mm_loaddup_pd(a + 3);
1775#else
1776 a1 = pload<Packet2d>(a);
1777 a0 = vec2d_swizzle1(a1, 0, 0);
1778 a1 = vec2d_swizzle1(a1, 1, 1);
1779 a3 = pload<Packet2d>(a + 2);
1780 a2 = vec2d_swizzle1(a3, 0, 0);
1781 a3 = vec2d_swizzle1(a3, 1, 1);
1782#endif
1783}
1784#endif
1785
1786EIGEN_STRONG_INLINE void punpackp(Packet4f* vecs) {
1787 vecs[1] = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(vecs[0]), 0x55));
1788 vecs[2] = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(vecs[0]), 0xAA));
1789 vecs[3] = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(vecs[0]), 0xFF));
1790 vecs[0] = _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(vecs[0]), 0x00));
1791}
1792
1793EIGEN_STRONG_INLINE void ptranspose(PacketBlock<Packet4f, 4>& kernel) {
1794 _MM_TRANSPOSE4_PS(kernel.packet[0], kernel.packet[1], kernel.packet[2], kernel.packet[3]);
1795}
1796
1797EIGEN_STRONG_INLINE void ptranspose(PacketBlock<Packet2d, 2>& kernel) {
1798 __m128d tmp = _mm_unpackhi_pd(kernel.packet[0], kernel.packet[1]);
1799 kernel.packet[0] = _mm_unpacklo_pd(kernel.packet[0], kernel.packet[1]);
1800 kernel.packet[1] = tmp;
1801}
1802
1803EIGEN_STRONG_INLINE void ptranspose(PacketBlock<Packet2l, 2>& kernel) {
1804 __m128i tmp = _mm_unpackhi_epi64(kernel.packet[0], kernel.packet[1]);
1805 kernel.packet[0] = _mm_unpacklo_epi64(kernel.packet[0], kernel.packet[1]);
1806 kernel.packet[1] = tmp;
1807}
1808
1809EIGEN_STRONG_INLINE void ptranspose(PacketBlock<Packet4i, 4>& kernel) {
1810 __m128i T0 = _mm_unpacklo_epi32(kernel.packet[0], kernel.packet[1]);
1811 __m128i T1 = _mm_unpacklo_epi32(kernel.packet[2], kernel.packet[3]);
1812 __m128i T2 = _mm_unpackhi_epi32(kernel.packet[0], kernel.packet[1]);
1813 __m128i T3 = _mm_unpackhi_epi32(kernel.packet[2], kernel.packet[3]);
1814
1815 kernel.packet[0] = _mm_unpacklo_epi64(T0, T1);
1816 kernel.packet[1] = _mm_unpackhi_epi64(T0, T1);
1817 kernel.packet[2] = _mm_unpacklo_epi64(T2, T3);
1818 kernel.packet[3] = _mm_unpackhi_epi64(T2, T3);
1819}
1820EIGEN_STRONG_INLINE void ptranspose(PacketBlock<Packet4ui, 4>& kernel) {
1821 ptranspose((PacketBlock<Packet4i, 4>&)kernel);
1822}
1823
1824EIGEN_STRONG_INLINE void ptranspose(PacketBlock<Packet16b, 4>& kernel) {
1825 __m128i T0 = _mm_unpacklo_epi8(kernel.packet[0], kernel.packet[1]);
1826 __m128i T1 = _mm_unpackhi_epi8(kernel.packet[0], kernel.packet[1]);
1827 __m128i T2 = _mm_unpacklo_epi8(kernel.packet[2], kernel.packet[3]);
1828 __m128i T3 = _mm_unpackhi_epi8(kernel.packet[2], kernel.packet[3]);
1829 kernel.packet[0] = _mm_unpacklo_epi16(T0, T2);
1830 kernel.packet[1] = _mm_unpackhi_epi16(T0, T2);
1831 kernel.packet[2] = _mm_unpacklo_epi16(T1, T3);
1832 kernel.packet[3] = _mm_unpackhi_epi16(T1, T3);
1833}
1834
1835EIGEN_STRONG_INLINE void ptranspose(PacketBlock<Packet16b, 16>& kernel) {
1836 // If we number the elements in the input thus:
1837 // kernel.packet[ 0] = {00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 0a, 0b, 0c, 0d, 0e, 0f}
1838 // kernel.packet[ 1] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 1a, 1b, 1c, 1d, 1e, 1f}
1839 // ...
1840 // kernel.packet[15] = {f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, fa, fb, fc, fd, fe, ff},
1841 //
1842 // the desired output is:
1843 // kernel.packet[ 0] = {00, 10, 20, 30, 40, 50, 60, 70, 80, 90, a0, b0, c0, d0, e0, f0}
1844 // kernel.packet[ 1] = {01, 11, 21, 31, 41, 51, 61, 71, 81, 91, a1, b1, c1, d1, e1, f1}
1845 // ...
1846 // kernel.packet[15] = {0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f, 9f, af, bf, cf, df, ef, ff},
1847 __m128i t0 =
1848 _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
1849 __m128i t1 =
1850 _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
1851 __m128i t2 =
1852 _mm_unpacklo_epi8(kernel.packet[2], kernel.packet[3]); // 20 30 21 31 22 32 ... 27 37
1853 __m128i t3 =
1854 _mm_unpackhi_epi8(kernel.packet[2], kernel.packet[3]); // 28 38 29 39 2a 3a ... 2f 3f
1855 __m128i t4 =
1856 _mm_unpacklo_epi8(kernel.packet[4], kernel.packet[5]); // 40 50 41 51 42 52 47 57
1857 __m128i t5 = _mm_unpackhi_epi8(kernel.packet[4], kernel.packet[5]); // 48 58 49 59 4a 5a
1858 __m128i t6 = _mm_unpacklo_epi8(kernel.packet[6], kernel.packet[7]);
1859 __m128i t7 = _mm_unpackhi_epi8(kernel.packet[6], kernel.packet[7]);
1860 __m128i t8 = _mm_unpacklo_epi8(kernel.packet[8], kernel.packet[9]);
1861 __m128i t9 = _mm_unpackhi_epi8(kernel.packet[8], kernel.packet[9]);
1862 __m128i ta = _mm_unpacklo_epi8(kernel.packet[10], kernel.packet[11]);
1863 __m128i tb = _mm_unpackhi_epi8(kernel.packet[10], kernel.packet[11]);
1864 __m128i tc = _mm_unpacklo_epi8(kernel.packet[12], kernel.packet[13]);
1865 __m128i td = _mm_unpackhi_epi8(kernel.packet[12], kernel.packet[13]);
1866 __m128i te = _mm_unpacklo_epi8(kernel.packet[14], kernel.packet[15]);
1867 __m128i tf = _mm_unpackhi_epi8(kernel.packet[14], kernel.packet[15]);
1868
1869 __m128i s0 = _mm_unpacklo_epi16(t0, t2); // 00 10 20 30 01 11 21 31 02 12 22 32 03 13 23 33
1870 __m128i s1 = _mm_unpackhi_epi16(t0, t2); // 04 14 24 34
1871 __m128i s2 = _mm_unpacklo_epi16(t1, t3); // 08 18 28 38 ...
1872 __m128i s3 = _mm_unpackhi_epi16(t1, t3); // 0c 1c 2c 3c ...
1873 __m128i s4 = _mm_unpacklo_epi16(t4, t6); // 40 50 60 70 41 51 61 71 42 52 62 72 43 53 63 73
1874 __m128i s5 = _mm_unpackhi_epi16(t4, t6); // 44 54 64 74 ...
1875 __m128i s6 = _mm_unpacklo_epi16(t5, t7);
1876 __m128i s7 = _mm_unpackhi_epi16(t5, t7);
1877 __m128i s8 = _mm_unpacklo_epi16(t8, ta);
1878 __m128i s9 = _mm_unpackhi_epi16(t8, ta);
1879 __m128i sa = _mm_unpacklo_epi16(t9, tb);
1880 __m128i sb = _mm_unpackhi_epi16(t9, tb);
1881 __m128i sc = _mm_unpacklo_epi16(tc, te);
1882 __m128i sd = _mm_unpackhi_epi16(tc, te);
1883 __m128i se = _mm_unpacklo_epi16(td, tf);
1884 __m128i sf = _mm_unpackhi_epi16(td, tf);
1885
1886 __m128i u0 = _mm_unpacklo_epi32(s0, s4); // 00 10 20 30 40 50 60 70 01 11 21 31 41 51 61 71
1887 __m128i u1 = _mm_unpackhi_epi32(s0, s4); // 02 12 22 32 42 52 62 72 03 13 23 33 43 53 63 73
1888 __m128i u2 = _mm_unpacklo_epi32(s1, s5);
1889 __m128i u3 = _mm_unpackhi_epi32(s1, s5);
1890 __m128i u4 = _mm_unpacklo_epi32(s2, s6);
1891 __m128i u5 = _mm_unpackhi_epi32(s2, s6);
1892 __m128i u6 = _mm_unpacklo_epi32(s3, s7);
1893 __m128i u7 = _mm_unpackhi_epi32(s3, s7);
1894 __m128i u8 = _mm_unpacklo_epi32(s8, sc);
1895 __m128i u9 = _mm_unpackhi_epi32(s8, sc);
1896 __m128i ua = _mm_unpacklo_epi32(s9, sd);
1897 __m128i ub = _mm_unpackhi_epi32(s9, sd);
1898 __m128i uc = _mm_unpacklo_epi32(sa, se);
1899 __m128i ud = _mm_unpackhi_epi32(sa, se);
1900 __m128i ue = _mm_unpacklo_epi32(sb, sf);
1901 __m128i uf = _mm_unpackhi_epi32(sb, sf);
1902
1903 kernel.packet[0] = _mm_unpacklo_epi64(u0, u8);
1904 kernel.packet[1] = _mm_unpackhi_epi64(u0, u8);
1905 kernel.packet[2] = _mm_unpacklo_epi64(u1, u9);
1906 kernel.packet[3] = _mm_unpackhi_epi64(u1, u9);
1907 kernel.packet[4] = _mm_unpacklo_epi64(u2, ua);
1908 kernel.packet[5] = _mm_unpackhi_epi64(u2, ua);
1909 kernel.packet[6] = _mm_unpacklo_epi64(u3, ub);
1910 kernel.packet[7] = _mm_unpackhi_epi64(u3, ub);
1911 kernel.packet[8] = _mm_unpacklo_epi64(u4, uc);
1912 kernel.packet[9] = _mm_unpackhi_epi64(u4, uc);
1913 kernel.packet[10] = _mm_unpacklo_epi64(u5, ud);
1914 kernel.packet[11] = _mm_unpackhi_epi64(u5, ud);
1915 kernel.packet[12] = _mm_unpacklo_epi64(u6, ue);
1916 kernel.packet[13] = _mm_unpackhi_epi64(u6, ue);
1917 kernel.packet[14] = _mm_unpacklo_epi64(u7, uf);
1918 kernel.packet[15] = _mm_unpackhi_epi64(u7, uf);
1919}
1920
1921// Scalar path for pmadd with FMA to ensure consistency with vectorized path.
1922#if defined(EIGEN_VECTORIZE_FMA)
1923template <>
1924EIGEN_STRONG_INLINE float pmadd(const float& a, const float& b, const float& c) {
1925 return numext::fma(a, b, c);
1926}
1927template <>
1928EIGEN_STRONG_INLINE double pmadd(const double& a, const double& b, const double& c) {
1929 return numext::fma(a, b, c);
1930}
1931template <>
1932EIGEN_STRONG_INLINE float pmsub(const float& a, const float& b, const float& c) {
1933 return numext::fma(a, b, -c);
1934}
1935template <>
1936EIGEN_STRONG_INLINE double pmsub(const double& a, const double& b, const double& c) {
1937 return numext::fma(a, b, -c);
1938}
1939template <>
1940EIGEN_STRONG_INLINE float pnmadd(const float& a, const float& b, const float& c) {
1941 return numext::fma(-a, b, c);
1942}
1943template <>
1944EIGEN_STRONG_INLINE double pnmadd(const double& a, const double& b, const double& c) {
1945 return numext::fma(-a, b, c);
1946}
1947template <>
1948EIGEN_STRONG_INLINE float pnmsub(const float& a, const float& b, const float& c) {
1949 return numext::fma(-a, b, -c);
1950}
1951template <>
1952EIGEN_STRONG_INLINE double pnmsub(const double& a, const double& b, const double& c) {
1953 return numext::fma(-a, b, -c);
1954}
1955#endif
1956
1957#ifdef EIGEN_VECTORIZE_SSE4_1
1958// Helpers for half->float and float->half conversions.
1959// Currently only used by the AVX code.
1960EIGEN_STRONG_INLINE __m128i half2floatsse(__m128i h) {
1961 __m128i input = _mm_cvtepu16_epi32(h);
1962
1963 // Direct vectorization of half_to_float, C parts in the comments.
1964 __m128i shifted_exp = _mm_set1_epi32(0x7c00 << 13);
1965 // o.u = (h.x & 0x7fff) << 13; // exponent/mantissa bits
1966 __m128i ou = _mm_slli_epi32(_mm_and_si128(input, _mm_set1_epi32(0x7fff)), 13);
1967 // exp = shifted_exp & o.u; // just the exponent
1968 __m128i exp = _mm_and_si128(ou, shifted_exp);
1969 // o.u += (127 - 15) << 23;
1970 ou = _mm_add_epi32(ou, _mm_set1_epi32((127 - 15) << 23));
1971
1972 // Inf/NaN?
1973 __m128i naninf_mask = _mm_cmpeq_epi32(exp, shifted_exp);
1974 // Inf/NaN adjust
1975 __m128i naninf_adj = _mm_and_si128(_mm_set1_epi32((128 - 16) << 23), naninf_mask);
1976 // extra exp adjust for Inf/NaN
1977 ou = _mm_add_epi32(ou, naninf_adj);
1978
1979 // Zero/Denormal?
1980 __m128i zeroden_mask = _mm_cmpeq_epi32(exp, _mm_setzero_si128());
1981 __m128i zeroden_adj = _mm_and_si128(zeroden_mask, _mm_set1_epi32(1 << 23));
1982 // o.u += 1 << 23;
1983 ou = _mm_add_epi32(ou, zeroden_adj);
1984 // magic.u = 113 << 23
1985 __m128i magic = _mm_and_si128(zeroden_mask, _mm_set1_epi32(113 << 23));
1986 // o.f -= magic.f
1987 ou = _mm_castps_si128(_mm_sub_ps(_mm_castsi128_ps(ou), _mm_castsi128_ps(magic)));
1988
1989 __m128i sign = _mm_slli_epi32(_mm_and_si128(input, _mm_set1_epi32(0x8000)), 16);
1990 // o.u |= (h.x & 0x8000) << 16; // sign bit
1991 ou = _mm_or_si128(ou, sign);
1992 // return o.f;
1993 // We are actually returning uint version, to make
1994 // _mm256_insertf128_si256 work.
1995 return ou;
1996}
1997
1998EIGEN_STRONG_INLINE __m128i float2half(__m128 f) {
1999 // unsigned int sign_mask = 0x80000000u;
2000 __m128i sign = _mm_set1_epi32(0x80000000u);
2001 // unsigned int sign = f.u & sign_mask;
2002 sign = _mm_and_si128(sign, _mm_castps_si128(f));
2003 // f.u ^= sign;
2004 f = _mm_xor_ps(f, _mm_castsi128_ps(sign));
2005
2006 __m128i fu = _mm_castps_si128(f);
2007
2008 __m128i f16max = _mm_set1_epi32((127 + 16) << 23);
2009 __m128i f32infty = _mm_set1_epi32(255 << 23);
2010 // if (f.u >= f16max.u) // result is Inf or NaN (all exponent bits set)
2011 // there is no _mm_cmpge_epi32, so use lt and swap operands
2012 __m128i infnan_mask = _mm_cmplt_epi32(f16max, _mm_castps_si128(f));
2013 __m128i inf_mask = _mm_cmpgt_epi32(_mm_castps_si128(f), f32infty);
2014 __m128i nan_mask = _mm_andnot_si128(inf_mask, infnan_mask);
2015 __m128i inf_value = _mm_and_si128(inf_mask, _mm_set1_epi32(0x7e00));
2016 __m128i nan_value = _mm_and_si128(nan_mask, _mm_set1_epi32(0x7c00));
2017 // o.x = (f.u > f32infty.u) ? 0x7e00 : 0x7c00; // NaN->qNaN and Inf->Inf
2018 __m128i naninf_value = _mm_or_si128(inf_value, nan_value);
2019
2020 __m128i denorm_magic = _mm_set1_epi32(((127 - 15) + (23 - 10) + 1) << 23);
2021 __m128i subnorm_mask = _mm_cmplt_epi32(_mm_castps_si128(f), _mm_set1_epi32(113 << 23));
2022 // f.f += denorm_magic.f;
2023 f = _mm_add_ps(f, _mm_castsi128_ps(denorm_magic));
2024 // f.u - denorm_magic.u
2025 __m128i o = _mm_sub_epi32(_mm_castps_si128(f), denorm_magic);
2026 o = _mm_and_si128(o, subnorm_mask);
2027 // Correct result for inf/nan/zero/subnormal, 0 otherwise
2028 o = _mm_or_si128(o, naninf_value);
2029
2030 __m128i mask = _mm_or_si128(infnan_mask, subnorm_mask);
2031 o = _mm_and_si128(o, mask);
2032
2033 // mant_odd = (f.u >> 13) & 1;
2034 __m128i mand_odd = _mm_and_si128(_mm_srli_epi32(fu, 13), _mm_set1_epi32(0x1));
2035 // f.u += 0xc8000fffU;
2036 fu = _mm_add_epi32(fu, _mm_set1_epi32(0xc8000fffU));
2037 // f.u += mant_odd;
2038 fu = _mm_add_epi32(fu, mand_odd);
2039 fu = _mm_andnot_si128(mask, fu);
2040 // f.u >> 13
2041 fu = _mm_srli_epi32(fu, 13);
2042 o = _mm_or_si128(fu, o);
2043
2044 // o.x |= static_cast<numext::uint16_t>(sign >> 16);
2045 o = _mm_or_si128(o, _mm_srli_epi32(sign, 16));
2046
2047 // 16 bit values
2048 return _mm_and_si128(o, _mm_set1_epi32(0xffff));
2049}
2050#endif
2051
2052} // end namespace internal
2053
2054} // end namespace Eigen
2055
2056#if EIGEN_COMP_PGI && EIGEN_COMP_PGI < 1900
2057// PGI++ does not define the following intrinsics in C++ mode.
2058static inline __m128 _mm_castpd_ps(__m128d x) { return reinterpret_cast<__m128&>(x); }
2059static inline __m128i _mm_castpd_si128(__m128d x) { return reinterpret_cast<__m128i&>(x); }
2060static inline __m128d _mm_castps_pd(__m128 x) { return reinterpret_cast<__m128d&>(x); }
2061static inline __m128i _mm_castps_si128(__m128 x) { return reinterpret_cast<__m128i&>(x); }
2062static inline __m128 _mm_castsi128_ps(__m128i x) { return reinterpret_cast<__m128&>(x); }
2063static inline __m128d _mm_castsi128_pd(__m128i x) { return reinterpret_cast<__m128d&>(x); }
2064#endif
2065
2066#endif // EIGEN_PACKET_MATH_SSE_H
@ Aligned16
Definition Constants.h:238