Eigen  3.2.91
AVX/PacketMath.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2014 Benoit Steiner (benoit.steiner.goog@gmail.com)
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_AVX_H
11 #define EIGEN_PACKET_MATH_AVX_H
12 
13 namespace Eigen {
14 
15 namespace internal {
16 
17 #ifndef EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD
18 #define EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD 8
19 #endif
20 
21 #ifndef EIGEN_ARCH_DEFAULT_NUMBER_OF_REGISTERS
22 #define EIGEN_ARCH_DEFAULT_NUMBER_OF_REGISTERS (2*sizeof(void*))
23 #endif
24 
25 #ifdef __FMA__
26 #ifndef EIGEN_HAS_SINGLE_INSTRUCTION_MADD
27 #define EIGEN_HAS_SINGLE_INSTRUCTION_MADD
28 #endif
29 #endif
30 
31 typedef __m256 Packet8f;
32 typedef __m256i Packet8i;
33 typedef __m256d Packet4d;
34 
35 template<> struct is_arithmetic<__m256> { enum { value = true }; };
36 template<> struct is_arithmetic<__m256i> { enum { value = true }; };
37 template<> struct is_arithmetic<__m256d> { enum { value = true }; };
38 
39 #define _EIGEN_DECLARE_CONST_Packet8f(NAME,X) \
40  const Packet8f p8f_##NAME = pset1<Packet8f>(X)
41 
42 #define _EIGEN_DECLARE_CONST_Packet4d(NAME,X) \
43  const Packet4d p4d_##NAME = pset1<Packet4d>(X)
44 
45 #define _EIGEN_DECLARE_CONST_Packet8f_FROM_INT(NAME,X) \
46  const Packet8f p8f_##NAME = (__m256)pset1<Packet8i>(X)
47 
48 #define _EIGEN_DECLARE_CONST_Packet8i(NAME,X) \
49  const Packet8i p8i_##NAME = pset1<Packet8i>(X)
50 
51 
52 template<> struct packet_traits<float> : default_packet_traits
53 {
54  typedef Packet8f type;
55  typedef Packet4f half;
56  enum {
57  Vectorizable = 1,
58  AlignedOnScalar = 1,
59  size=8,
60  HasHalfPacket = 1,
61 
62  HasDiv = 1,
63  HasSin = EIGEN_FAST_MATH,
64  HasCos = 0,
65  HasLog = 1,
66  HasExp = 1,
67  HasSqrt = 1,
68  HasRsqrt = 1,
69  HasBlend = 1
70  };
71 };
72 template<> struct packet_traits<double> : default_packet_traits
73 {
74  typedef Packet4d type;
75  typedef Packet2d half;
76  enum {
77  Vectorizable = 1,
78  AlignedOnScalar = 1,
79  size=4,
80  HasHalfPacket = 1,
81 
82  HasDiv = 1,
83  HasExp = 1,
84  HasSqrt = 1,
85  HasRsqrt = 1,
86  HasBlend = 1
87  };
88 };
89 
90 /* Proper support for integers is only provided by AVX2. In the meantime, we'll
91  use SSE instructions and packets to deal with integers.
92 template<> struct packet_traits<int> : default_packet_traits
93 {
94  typedef Packet8i type;
95  enum {
96  Vectorizable = 1,
97  AlignedOnScalar = 1,
98  size=8
99  };
100 };
101 */
102 
103 template<> struct unpacket_traits<Packet8f> { typedef float type; typedef Packet4f half; enum {size=8, alignment=Aligned32}; };
104 template<> struct unpacket_traits<Packet4d> { typedef double type; typedef Packet2d half; enum {size=4, alignment=Aligned32}; };
105 template<> struct unpacket_traits<Packet8i> { typedef int type; typedef Packet4i half; enum {size=8, alignment=Aligned32}; };
106 
107 template<> EIGEN_STRONG_INLINE Packet8f pset1<Packet8f>(const float& from) { return _mm256_set1_ps(from); }
108 template<> EIGEN_STRONG_INLINE Packet4d pset1<Packet4d>(const double& from) { return _mm256_set1_pd(from); }
109 template<> EIGEN_STRONG_INLINE Packet8i pset1<Packet8i>(const int& from) { return _mm256_set1_epi32(from); }
110 
111 template<> EIGEN_STRONG_INLINE Packet8f pload1<Packet8f>(const float* from) { return _mm256_broadcast_ss(from); }
112 template<> EIGEN_STRONG_INLINE Packet4d pload1<Packet4d>(const double* from) { return _mm256_broadcast_sd(from); }
113 
114 template<> EIGEN_STRONG_INLINE Packet8f plset<Packet8f>(const float& a) { return _mm256_add_ps(_mm256_set1_ps(a), _mm256_set_ps(7.0,6.0,5.0,4.0,3.0,2.0,1.0,0.0)); }
115 template<> EIGEN_STRONG_INLINE Packet4d plset<Packet4d>(const double& a) { return _mm256_add_pd(_mm256_set1_pd(a), _mm256_set_pd(3.0,2.0,1.0,0.0)); }
116 
117 template<> EIGEN_STRONG_INLINE Packet8f padd<Packet8f>(const Packet8f& a, const Packet8f& b) { return _mm256_add_ps(a,b); }
118 template<> EIGEN_STRONG_INLINE Packet4d padd<Packet4d>(const Packet4d& a, const Packet4d& b) { return _mm256_add_pd(a,b); }
119 
120 template<> EIGEN_STRONG_INLINE Packet8f psub<Packet8f>(const Packet8f& a, const Packet8f& b) { return _mm256_sub_ps(a,b); }
121 template<> EIGEN_STRONG_INLINE Packet4d psub<Packet4d>(const Packet4d& a, const Packet4d& b) { return _mm256_sub_pd(a,b); }
122 
123 template<> EIGEN_STRONG_INLINE Packet8f pnegate(const Packet8f& a)
124 {
125  return _mm256_sub_ps(_mm256_set1_ps(0.0),a);
126 }
127 template<> EIGEN_STRONG_INLINE Packet4d pnegate(const Packet4d& a)
128 {
129  return _mm256_sub_pd(_mm256_set1_pd(0.0),a);
130 }
131 
132 template<> EIGEN_STRONG_INLINE Packet8f pconj(const Packet8f& a) { return a; }
133 template<> EIGEN_STRONG_INLINE Packet4d pconj(const Packet4d& a) { return a; }
134 template<> EIGEN_STRONG_INLINE Packet8i pconj(const Packet8i& a) { return a; }
135 
136 template<> EIGEN_STRONG_INLINE Packet8f pmul<Packet8f>(const Packet8f& a, const Packet8f& b) { return _mm256_mul_ps(a,b); }
137 template<> EIGEN_STRONG_INLINE Packet4d pmul<Packet4d>(const Packet4d& a, const Packet4d& b) { return _mm256_mul_pd(a,b); }
138 
139 
140 template<> EIGEN_STRONG_INLINE Packet8f pdiv<Packet8f>(const Packet8f& a, const Packet8f& b) { return _mm256_div_ps(a,b); }
141 template<> EIGEN_STRONG_INLINE Packet4d pdiv<Packet4d>(const Packet4d& a, const Packet4d& b) { return _mm256_div_pd(a,b); }
142 template<> EIGEN_STRONG_INLINE Packet8i pdiv<Packet8i>(const Packet8i& /*a*/, const Packet8i& /*b*/)
143 { eigen_assert(false && "packet integer division are not supported by AVX");
144  return pset1<Packet8i>(0);
145 }
146 
147 #ifdef __FMA__
148 template<> EIGEN_STRONG_INLINE Packet8f pmadd(const Packet8f& a, const Packet8f& b, const Packet8f& c) {
149 #if EIGEN_COMP_GNUC || EIGEN_COMP_CLANG
150  // clang stupidly generates a vfmadd213ps instruction plus some vmovaps on registers,
151  // and gcc stupidly generates a vfmadd132ps instruction,
152  // so let's enforce it to generate a vfmadd231ps instruction since the most common use case is to accumulate
153  // the result of the product.
154  Packet8f res = c;
155  __asm__("vfmadd231ps %[a], %[b], %[c]" : [c] "+x" (res) : [a] "x" (a), [b] "x" (b));
156  return res;
157 #else
158  return _mm256_fmadd_ps(a,b,c);
159 #endif
160 }
161 template<> EIGEN_STRONG_INLINE Packet4d pmadd(const Packet4d& a, const Packet4d& b, const Packet4d& c) {
162 #if EIGEN_COMP_GNUC || EIGEN_COMP_CLANG
163  // see above
164  Packet4d res = c;
165  __asm__("vfmadd231pd %[a], %[b], %[c]" : [c] "+x" (res) : [a] "x" (a), [b] "x" (b));
166  return res;
167 #else
168  return _mm256_fmadd_pd(a,b,c);
169 #endif
170 }
171 #endif
172 
173 template<> EIGEN_STRONG_INLINE Packet8f pmin<Packet8f>(const Packet8f& a, const Packet8f& b) { return _mm256_min_ps(a,b); }
174 template<> EIGEN_STRONG_INLINE Packet4d pmin<Packet4d>(const Packet4d& a, const Packet4d& b) { return _mm256_min_pd(a,b); }
175 
176 template<> EIGEN_STRONG_INLINE Packet8f pmax<Packet8f>(const Packet8f& a, const Packet8f& b) { return _mm256_max_ps(a,b); }
177 template<> EIGEN_STRONG_INLINE Packet4d pmax<Packet4d>(const Packet4d& a, const Packet4d& b) { return _mm256_max_pd(a,b); }
178 
179 template<> EIGEN_STRONG_INLINE Packet8f pand<Packet8f>(const Packet8f& a, const Packet8f& b) { return _mm256_and_ps(a,b); }
180 template<> EIGEN_STRONG_INLINE Packet4d pand<Packet4d>(const Packet4d& a, const Packet4d& b) { return _mm256_and_pd(a,b); }
181 
182 template<> EIGEN_STRONG_INLINE Packet8f por<Packet8f>(const Packet8f& a, const Packet8f& b) { return _mm256_or_ps(a,b); }
183 template<> EIGEN_STRONG_INLINE Packet4d por<Packet4d>(const Packet4d& a, const Packet4d& b) { return _mm256_or_pd(a,b); }
184 
185 template<> EIGEN_STRONG_INLINE Packet8f pxor<Packet8f>(const Packet8f& a, const Packet8f& b) { return _mm256_xor_ps(a,b); }
186 template<> EIGEN_STRONG_INLINE Packet4d pxor<Packet4d>(const Packet4d& a, const Packet4d& b) { return _mm256_xor_pd(a,b); }
187 
188 template<> EIGEN_STRONG_INLINE Packet8f pandnot<Packet8f>(const Packet8f& a, const Packet8f& b) { return _mm256_andnot_ps(a,b); }
189 template<> EIGEN_STRONG_INLINE Packet4d pandnot<Packet4d>(const Packet4d& a, const Packet4d& b) { return _mm256_andnot_pd(a,b); }
190 
191 template<> EIGEN_STRONG_INLINE Packet8f pload<Packet8f>(const float* from) { EIGEN_DEBUG_ALIGNED_LOAD return _mm256_load_ps(from); }
192 template<> EIGEN_STRONG_INLINE Packet4d pload<Packet4d>(const double* from) { EIGEN_DEBUG_ALIGNED_LOAD return _mm256_load_pd(from); }
193 template<> EIGEN_STRONG_INLINE Packet8i pload<Packet8i>(const int* from) { EIGEN_DEBUG_ALIGNED_LOAD return _mm256_load_si256(reinterpret_cast<const __m256i*>(from)); }
194 
195 template<> EIGEN_STRONG_INLINE Packet8f ploadu<Packet8f>(const float* from) { EIGEN_DEBUG_UNALIGNED_LOAD return _mm256_loadu_ps(from); }
196 template<> EIGEN_STRONG_INLINE Packet4d ploadu<Packet4d>(const double* from) { EIGEN_DEBUG_UNALIGNED_LOAD return _mm256_loadu_pd(from); }
197 template<> EIGEN_STRONG_INLINE Packet8i ploadu<Packet8i>(const int* from) { EIGEN_DEBUG_UNALIGNED_LOAD return _mm256_loadu_si256(reinterpret_cast<const __m256i*>(from)); }
198 
199 // Loads 4 floats from memory a returns the packet {a0, a0 a1, a1, a2, a2, a3, a3}
200 template<> EIGEN_STRONG_INLINE Packet8f ploaddup<Packet8f>(const float* from)
201 {
202  // TODO try to find a way to avoid the need of a temporary register
203 // Packet8f tmp = _mm256_castps128_ps256(_mm_loadu_ps(from));
204 // tmp = _mm256_insertf128_ps(tmp, _mm_movehl_ps(_mm256_castps256_ps128(tmp),_mm256_castps256_ps128(tmp)), 1);
205 // return _mm256_unpacklo_ps(tmp,tmp);
206 
207  // _mm256_insertf128_ps is very slow on Haswell, thus:
208  Packet8f tmp = _mm256_broadcast_ps((const __m128*)(const void*)from);
209  // mimic an "inplace" permutation of the lower 128bits using a blend
210  tmp = _mm256_blend_ps(tmp,_mm256_castps128_ps256(_mm_permute_ps( _mm256_castps256_ps128(tmp), _MM_SHUFFLE(1,0,1,0))), 15);
211  // then we can perform a consistent permutation on the global register to get everything in shape:
212  return _mm256_permute_ps(tmp, _MM_SHUFFLE(3,3,2,2));
213 }
214 // Loads 2 doubles from memory a returns the packet {a0, a0 a1, a1}
215 template<> EIGEN_STRONG_INLINE Packet4d ploaddup<Packet4d>(const double* from)
216 {
217  Packet4d tmp = _mm256_broadcast_pd((const __m128d*)(const void*)from);
218  return _mm256_permute_pd(tmp, 3<<2);
219 }
220 
221 // Loads 2 floats from memory a returns the packet {a0, a0 a0, a0, a1, a1, a1, a1}
222 template<> EIGEN_STRONG_INLINE Packet8f ploadquad<Packet8f>(const float* from)
223 {
224  Packet8f tmp = _mm256_castps128_ps256(_mm_broadcast_ss(from));
225  return _mm256_insertf128_ps(tmp, _mm_broadcast_ss(from+1), 1);
226 }
227 
228 template<> EIGEN_STRONG_INLINE void pstore<float>(float* to, const Packet8f& from) { EIGEN_DEBUG_ALIGNED_STORE _mm256_store_ps(to, from); }
229 template<> EIGEN_STRONG_INLINE void pstore<double>(double* to, const Packet4d& from) { EIGEN_DEBUG_ALIGNED_STORE _mm256_store_pd(to, from); }
230 template<> EIGEN_STRONG_INLINE void pstore<int>(int* to, const Packet8i& from) { EIGEN_DEBUG_ALIGNED_STORE _mm256_storeu_si256(reinterpret_cast<__m256i*>(to), from); }
231 
232 template<> EIGEN_STRONG_INLINE void pstoreu<float>(float* to, const Packet8f& from) { EIGEN_DEBUG_UNALIGNED_STORE _mm256_storeu_ps(to, from); }
233 template<> EIGEN_STRONG_INLINE void pstoreu<double>(double* to, const Packet4d& from) { EIGEN_DEBUG_UNALIGNED_STORE _mm256_storeu_pd(to, from); }
234 template<> EIGEN_STRONG_INLINE void pstoreu<int>(int* to, const Packet8i& from) { EIGEN_DEBUG_UNALIGNED_STORE _mm256_storeu_si256(reinterpret_cast<__m256i*>(to), from); }
235 
236 // NOTE: leverage _mm256_i32gather_ps and _mm256_i32gather_pd if AVX2 instructions are available
237 // NOTE: for the record the following seems to be slower: return _mm256_i32gather_ps(from, _mm256_set1_epi32(stride), 4);
238 template<> EIGEN_DEVICE_FUNC inline Packet8f pgather<float, Packet8f>(const float* from, Index stride)
239 {
240  return _mm256_set_ps(from[7*stride], from[6*stride], from[5*stride], from[4*stride],
241  from[3*stride], from[2*stride], from[1*stride], from[0*stride]);
242 }
243 template<> EIGEN_DEVICE_FUNC inline Packet4d pgather<double, Packet4d>(const double* from, Index stride)
244 {
245  return _mm256_set_pd(from[3*stride], from[2*stride], from[1*stride], from[0*stride]);
246 }
247 
248 template<> EIGEN_DEVICE_FUNC inline void pscatter<float, Packet8f>(float* to, const Packet8f& from, Index stride)
249 {
250  __m128 low = _mm256_extractf128_ps(from, 0);
251  to[stride*0] = _mm_cvtss_f32(low);
252  to[stride*1] = _mm_cvtss_f32(_mm_shuffle_ps(low, low, 1));
253  to[stride*2] = _mm_cvtss_f32(_mm_shuffle_ps(low, low, 2));
254  to[stride*3] = _mm_cvtss_f32(_mm_shuffle_ps(low, low, 3));
255 
256  __m128 high = _mm256_extractf128_ps(from, 1);
257  to[stride*4] = _mm_cvtss_f32(high);
258  to[stride*5] = _mm_cvtss_f32(_mm_shuffle_ps(high, high, 1));
259  to[stride*6] = _mm_cvtss_f32(_mm_shuffle_ps(high, high, 2));
260  to[stride*7] = _mm_cvtss_f32(_mm_shuffle_ps(high, high, 3));
261 }
262 template<> EIGEN_DEVICE_FUNC inline void pscatter<double, Packet4d>(double* to, const Packet4d& from, Index stride)
263 {
264  __m128d low = _mm256_extractf128_pd(from, 0);
265  to[stride*0] = _mm_cvtsd_f64(low);
266  to[stride*1] = _mm_cvtsd_f64(_mm_shuffle_pd(low, low, 1));
267  __m128d high = _mm256_extractf128_pd(from, 1);
268  to[stride*2] = _mm_cvtsd_f64(high);
269  to[stride*3] = _mm_cvtsd_f64(_mm_shuffle_pd(high, high, 1));
270 }
271 
272 template<> EIGEN_STRONG_INLINE void pstore1<Packet8f>(float* to, const float& a)
273 {
274  Packet8f pa = pset1<Packet8f>(a);
275  pstore(to, pa);
276 }
277 template<> EIGEN_STRONG_INLINE void pstore1<Packet4d>(double* to, const double& a)
278 {
279  Packet4d pa = pset1<Packet4d>(a);
280  pstore(to, pa);
281 }
282 template<> EIGEN_STRONG_INLINE void pstore1<Packet8i>(int* to, const int& a)
283 {
284  Packet8i pa = pset1<Packet8i>(a);
285  pstore(to, pa);
286 }
287 
288 template<> EIGEN_STRONG_INLINE void prefetch<float>(const float* addr) { _mm_prefetch((const char*)(addr), _MM_HINT_T0); }
289 template<> EIGEN_STRONG_INLINE void prefetch<double>(const double* addr) { _mm_prefetch((const char*)(addr), _MM_HINT_T0); }
290 template<> EIGEN_STRONG_INLINE void prefetch<int>(const int* addr) { _mm_prefetch((const char*)(addr), _MM_HINT_T0); }
291 
292 template<> EIGEN_STRONG_INLINE float pfirst<Packet8f>(const Packet8f& a) {
293  return _mm_cvtss_f32(_mm256_castps256_ps128(a));
294 }
295 template<> EIGEN_STRONG_INLINE double pfirst<Packet4d>(const Packet4d& a) {
296  return _mm_cvtsd_f64(_mm256_castpd256_pd128(a));
297 }
298 template<> EIGEN_STRONG_INLINE int pfirst<Packet8i>(const Packet8i& a) {
299  return _mm_cvtsi128_si32(_mm256_castsi256_si128(a));
300 }
301 
302 
303 template<> EIGEN_STRONG_INLINE Packet8f preverse(const Packet8f& a)
304 {
305  __m256 tmp = _mm256_shuffle_ps(a,a,0x1b);
306  return _mm256_permute2f128_ps(tmp, tmp, 1);
307 }
308 template<> EIGEN_STRONG_INLINE Packet4d preverse(const Packet4d& a)
309 {
310  __m256d tmp = _mm256_shuffle_pd(a,a,5);
311  return _mm256_permute2f128_pd(tmp, tmp, 1);
312 
313  __m256d swap_halves = _mm256_permute2f128_pd(a,a,1);
314  return _mm256_permute_pd(swap_halves,5);
315 }
316 
317 // pabs should be ok
318 template<> EIGEN_STRONG_INLINE Packet8f pabs(const Packet8f& a)
319 {
320  const Packet8f mask = _mm256_castsi256_ps(_mm256_setr_epi32(0x7FFFFFFF,0x7FFFFFFF,0x7FFFFFFF,0x7FFFFFFF,0x7FFFFFFF,0x7FFFFFFF,0x7FFFFFFF,0x7FFFFFFF));
321  return _mm256_and_ps(a,mask);
322 }
323 template<> EIGEN_STRONG_INLINE Packet4d pabs(const Packet4d& a)
324 {
325  const Packet4d mask = _mm256_castsi256_pd(_mm256_setr_epi32(0xFFFFFFFF,0x7FFFFFFF,0xFFFFFFFF,0x7FFFFFFF,0xFFFFFFFF,0x7FFFFFFF,0xFFFFFFFF,0x7FFFFFFF));
326  return _mm256_and_pd(a,mask);
327 }
328 
329 // preduxp should be ok
330 // FIXME: why is this ok? why isn't the simply implementation working as expected?
331 template<> EIGEN_STRONG_INLINE Packet8f preduxp<Packet8f>(const Packet8f* vecs)
332 {
333  __m256 hsum1 = _mm256_hadd_ps(vecs[0], vecs[1]);
334  __m256 hsum2 = _mm256_hadd_ps(vecs[2], vecs[3]);
335  __m256 hsum3 = _mm256_hadd_ps(vecs[4], vecs[5]);
336  __m256 hsum4 = _mm256_hadd_ps(vecs[6], vecs[7]);
337 
338  __m256 hsum5 = _mm256_hadd_ps(hsum1, hsum1);
339  __m256 hsum6 = _mm256_hadd_ps(hsum2, hsum2);
340  __m256 hsum7 = _mm256_hadd_ps(hsum3, hsum3);
341  __m256 hsum8 = _mm256_hadd_ps(hsum4, hsum4);
342 
343  __m256 perm1 = _mm256_permute2f128_ps(hsum5, hsum5, 0x23);
344  __m256 perm2 = _mm256_permute2f128_ps(hsum6, hsum6, 0x23);
345  __m256 perm3 = _mm256_permute2f128_ps(hsum7, hsum7, 0x23);
346  __m256 perm4 = _mm256_permute2f128_ps(hsum8, hsum8, 0x23);
347 
348  __m256 sum1 = _mm256_add_ps(perm1, hsum5);
349  __m256 sum2 = _mm256_add_ps(perm2, hsum6);
350  __m256 sum3 = _mm256_add_ps(perm3, hsum7);
351  __m256 sum4 = _mm256_add_ps(perm4, hsum8);
352 
353  __m256 blend1 = _mm256_blend_ps(sum1, sum2, 0xcc);
354  __m256 blend2 = _mm256_blend_ps(sum3, sum4, 0xcc);
355 
356  __m256 final = _mm256_blend_ps(blend1, blend2, 0xf0);
357  return final;
358 }
359 template<> EIGEN_STRONG_INLINE Packet4d preduxp<Packet4d>(const Packet4d* vecs)
360 {
361  Packet4d tmp0, tmp1;
362 
363  tmp0 = _mm256_hadd_pd(vecs[0], vecs[1]);
364  tmp0 = _mm256_add_pd(tmp0, _mm256_permute2f128_pd(tmp0, tmp0, 1));
365 
366  tmp1 = _mm256_hadd_pd(vecs[2], vecs[3]);
367  tmp1 = _mm256_add_pd(tmp1, _mm256_permute2f128_pd(tmp1, tmp1, 1));
368 
369  return _mm256_blend_pd(tmp0, tmp1, 0xC);
370 }
371 
372 template<> EIGEN_STRONG_INLINE float predux<Packet8f>(const Packet8f& a)
373 {
374  Packet8f tmp0 = _mm256_hadd_ps(a,_mm256_permute2f128_ps(a,a,1));
375  tmp0 = _mm256_hadd_ps(tmp0,tmp0);
376  return pfirst(_mm256_hadd_ps(tmp0, tmp0));
377 }
378 template<> EIGEN_STRONG_INLINE double predux<Packet4d>(const Packet4d& a)
379 {
380  Packet4d tmp0 = _mm256_hadd_pd(a,_mm256_permute2f128_pd(a,a,1));
381  return pfirst(_mm256_hadd_pd(tmp0,tmp0));
382 }
383 
384 template<> EIGEN_STRONG_INLINE Packet4f predux4<Packet8f>(const Packet8f& a)
385 {
386  return _mm_add_ps(_mm256_castps256_ps128(a),_mm256_extractf128_ps(a,1));
387 }
388 
389 template<> EIGEN_STRONG_INLINE float predux_mul<Packet8f>(const Packet8f& a)
390 {
391  Packet8f tmp;
392  tmp = _mm256_mul_ps(a, _mm256_permute2f128_ps(a,a,1));
393  tmp = _mm256_mul_ps(tmp, _mm256_shuffle_ps(tmp,tmp,_MM_SHUFFLE(1,0,3,2)));
394  return pfirst(_mm256_mul_ps(tmp, _mm256_shuffle_ps(tmp,tmp,1)));
395 }
396 template<> EIGEN_STRONG_INLINE double predux_mul<Packet4d>(const Packet4d& a)
397 {
398  Packet4d tmp;
399  tmp = _mm256_mul_pd(a, _mm256_permute2f128_pd(a,a,1));
400  return pfirst(_mm256_mul_pd(tmp, _mm256_shuffle_pd(tmp,tmp,1)));
401 }
402 
403 template<> EIGEN_STRONG_INLINE float predux_min<Packet8f>(const Packet8f& a)
404 {
405  Packet8f tmp = _mm256_min_ps(a, _mm256_permute2f128_ps(a,a,1));
406  tmp = _mm256_min_ps(tmp, _mm256_shuffle_ps(tmp,tmp,_MM_SHUFFLE(1,0,3,2)));
407  return pfirst(_mm256_min_ps(tmp, _mm256_shuffle_ps(tmp,tmp,1)));
408 }
409 template<> EIGEN_STRONG_INLINE double predux_min<Packet4d>(const Packet4d& a)
410 {
411  Packet4d tmp = _mm256_min_pd(a, _mm256_permute2f128_pd(a,a,1));
412  return pfirst(_mm256_min_pd(tmp, _mm256_shuffle_pd(tmp, tmp, 1)));
413 }
414 
415 template<> EIGEN_STRONG_INLINE float predux_max<Packet8f>(const Packet8f& a)
416 {
417  Packet8f tmp = _mm256_max_ps(a, _mm256_permute2f128_ps(a,a,1));
418  tmp = _mm256_max_ps(tmp, _mm256_shuffle_ps(tmp,tmp,_MM_SHUFFLE(1,0,3,2)));
419  return pfirst(_mm256_max_ps(tmp, _mm256_shuffle_ps(tmp,tmp,1)));
420 }
421 
422 template<> EIGEN_STRONG_INLINE double predux_max<Packet4d>(const Packet4d& a)
423 {
424  Packet4d tmp = _mm256_max_pd(a, _mm256_permute2f128_pd(a,a,1));
425  return pfirst(_mm256_max_pd(tmp, _mm256_shuffle_pd(tmp, tmp, 1)));
426 }
427 
428 
429 template<int Offset>
430 struct palign_impl<Offset,Packet8f>
431 {
432  static EIGEN_STRONG_INLINE void run(Packet8f& first, const Packet8f& second)
433  {
434  if (Offset==1)
435  {
436  first = _mm256_blend_ps(first, second, 1);
437  Packet8f tmp1 = _mm256_permute_ps (first, _MM_SHUFFLE(0,3,2,1));
438  Packet8f tmp2 = _mm256_permute2f128_ps (tmp1, tmp1, 1);
439  first = _mm256_blend_ps(tmp1, tmp2, 0x88);
440  }
441  else if (Offset==2)
442  {
443  first = _mm256_blend_ps(first, second, 3);
444  Packet8f tmp1 = _mm256_permute_ps (first, _MM_SHUFFLE(1,0,3,2));
445  Packet8f tmp2 = _mm256_permute2f128_ps (tmp1, tmp1, 1);
446  first = _mm256_blend_ps(tmp1, tmp2, 0xcc);
447  }
448  else if (Offset==3)
449  {
450  first = _mm256_blend_ps(first, second, 7);
451  Packet8f tmp1 = _mm256_permute_ps (first, _MM_SHUFFLE(2,1,0,3));
452  Packet8f tmp2 = _mm256_permute2f128_ps (tmp1, tmp1, 1);
453  first = _mm256_blend_ps(tmp1, tmp2, 0xee);
454  }
455  else if (Offset==4)
456  {
457  first = _mm256_blend_ps(first, second, 15);
458  Packet8f tmp1 = _mm256_permute_ps (first, _MM_SHUFFLE(3,2,1,0));
459  Packet8f tmp2 = _mm256_permute2f128_ps (tmp1, tmp1, 1);
460  first = _mm256_permute_ps(tmp2, _MM_SHUFFLE(3,2,1,0));
461  }
462  else if (Offset==5)
463  {
464  first = _mm256_blend_ps(first, second, 31);
465  first = _mm256_permute2f128_ps(first, first, 1);
466  Packet8f tmp = _mm256_permute_ps (first, _MM_SHUFFLE(0,3,2,1));
467  first = _mm256_permute2f128_ps(tmp, tmp, 1);
468  first = _mm256_blend_ps(tmp, first, 0x88);
469  }
470  else if (Offset==6)
471  {
472  first = _mm256_blend_ps(first, second, 63);
473  first = _mm256_permute2f128_ps(first, first, 1);
474  Packet8f tmp = _mm256_permute_ps (first, _MM_SHUFFLE(1,0,3,2));
475  first = _mm256_permute2f128_ps(tmp, tmp, 1);
476  first = _mm256_blend_ps(tmp, first, 0xcc);
477  }
478  else if (Offset==7)
479  {
480  first = _mm256_blend_ps(first, second, 127);
481  first = _mm256_permute2f128_ps(first, first, 1);
482  Packet8f tmp = _mm256_permute_ps (first, _MM_SHUFFLE(2,1,0,3));
483  first = _mm256_permute2f128_ps(tmp, tmp, 1);
484  first = _mm256_blend_ps(tmp, first, 0xee);
485  }
486  }
487 };
488 
489 template<int Offset>
490 struct palign_impl<Offset,Packet4d>
491 {
492  static EIGEN_STRONG_INLINE void run(Packet4d& first, const Packet4d& second)
493  {
494  if (Offset==1)
495  {
496  first = _mm256_blend_pd(first, second, 1);
497  __m256d tmp = _mm256_permute_pd(first, 5);
498  first = _mm256_permute2f128_pd(tmp, tmp, 1);
499  first = _mm256_blend_pd(tmp, first, 0xA);
500  }
501  else if (Offset==2)
502  {
503  first = _mm256_blend_pd(first, second, 3);
504  first = _mm256_permute2f128_pd(first, first, 1);
505  }
506  else if (Offset==3)
507  {
508  first = _mm256_blend_pd(first, second, 7);
509  __m256d tmp = _mm256_permute_pd(first, 5);
510  first = _mm256_permute2f128_pd(tmp, tmp, 1);
511  first = _mm256_blend_pd(tmp, first, 5);
512  }
513  }
514 };
515 
516 EIGEN_DEVICE_FUNC inline void
517 ptranspose(PacketBlock<Packet8f,8>& kernel) {
518  __m256 T0 = _mm256_unpacklo_ps(kernel.packet[0], kernel.packet[1]);
519  __m256 T1 = _mm256_unpackhi_ps(kernel.packet[0], kernel.packet[1]);
520  __m256 T2 = _mm256_unpacklo_ps(kernel.packet[2], kernel.packet[3]);
521  __m256 T3 = _mm256_unpackhi_ps(kernel.packet[2], kernel.packet[3]);
522  __m256 T4 = _mm256_unpacklo_ps(kernel.packet[4], kernel.packet[5]);
523  __m256 T5 = _mm256_unpackhi_ps(kernel.packet[4], kernel.packet[5]);
524  __m256 T6 = _mm256_unpacklo_ps(kernel.packet[6], kernel.packet[7]);
525  __m256 T7 = _mm256_unpackhi_ps(kernel.packet[6], kernel.packet[7]);
526  __m256 S0 = _mm256_shuffle_ps(T0,T2,_MM_SHUFFLE(1,0,1,0));
527  __m256 S1 = _mm256_shuffle_ps(T0,T2,_MM_SHUFFLE(3,2,3,2));
528  __m256 S2 = _mm256_shuffle_ps(T1,T3,_MM_SHUFFLE(1,0,1,0));
529  __m256 S3 = _mm256_shuffle_ps(T1,T3,_MM_SHUFFLE(3,2,3,2));
530  __m256 S4 = _mm256_shuffle_ps(T4,T6,_MM_SHUFFLE(1,0,1,0));
531  __m256 S5 = _mm256_shuffle_ps(T4,T6,_MM_SHUFFLE(3,2,3,2));
532  __m256 S6 = _mm256_shuffle_ps(T5,T7,_MM_SHUFFLE(1,0,1,0));
533  __m256 S7 = _mm256_shuffle_ps(T5,T7,_MM_SHUFFLE(3,2,3,2));
534  kernel.packet[0] = _mm256_permute2f128_ps(S0, S4, 0x20);
535  kernel.packet[1] = _mm256_permute2f128_ps(S1, S5, 0x20);
536  kernel.packet[2] = _mm256_permute2f128_ps(S2, S6, 0x20);
537  kernel.packet[3] = _mm256_permute2f128_ps(S3, S7, 0x20);
538  kernel.packet[4] = _mm256_permute2f128_ps(S0, S4, 0x31);
539  kernel.packet[5] = _mm256_permute2f128_ps(S1, S5, 0x31);
540  kernel.packet[6] = _mm256_permute2f128_ps(S2, S6, 0x31);
541  kernel.packet[7] = _mm256_permute2f128_ps(S3, S7, 0x31);
542 }
543 
544 EIGEN_DEVICE_FUNC inline void
545 ptranspose(PacketBlock<Packet8f,4>& kernel) {
546  __m256 T0 = _mm256_unpacklo_ps(kernel.packet[0], kernel.packet[1]);
547  __m256 T1 = _mm256_unpackhi_ps(kernel.packet[0], kernel.packet[1]);
548  __m256 T2 = _mm256_unpacklo_ps(kernel.packet[2], kernel.packet[3]);
549  __m256 T3 = _mm256_unpackhi_ps(kernel.packet[2], kernel.packet[3]);
550 
551  __m256 S0 = _mm256_shuffle_ps(T0,T2,_MM_SHUFFLE(1,0,1,0));
552  __m256 S1 = _mm256_shuffle_ps(T0,T2,_MM_SHUFFLE(3,2,3,2));
553  __m256 S2 = _mm256_shuffle_ps(T1,T3,_MM_SHUFFLE(1,0,1,0));
554  __m256 S3 = _mm256_shuffle_ps(T1,T3,_MM_SHUFFLE(3,2,3,2));
555 
556  kernel.packet[0] = _mm256_permute2f128_ps(S0, S1, 0x20);
557  kernel.packet[1] = _mm256_permute2f128_ps(S2, S3, 0x20);
558  kernel.packet[2] = _mm256_permute2f128_ps(S0, S1, 0x31);
559  kernel.packet[3] = _mm256_permute2f128_ps(S2, S3, 0x31);
560 }
561 
562 EIGEN_DEVICE_FUNC inline void
563 ptranspose(PacketBlock<Packet4d,4>& kernel) {
564  __m256d T0 = _mm256_shuffle_pd(kernel.packet[0], kernel.packet[1], 15);
565  __m256d T1 = _mm256_shuffle_pd(kernel.packet[0], kernel.packet[1], 0);
566  __m256d T2 = _mm256_shuffle_pd(kernel.packet[2], kernel.packet[3], 15);
567  __m256d T3 = _mm256_shuffle_pd(kernel.packet[2], kernel.packet[3], 0);
568 
569  kernel.packet[1] = _mm256_permute2f128_pd(T0, T2, 32);
570  kernel.packet[3] = _mm256_permute2f128_pd(T0, T2, 49);
571  kernel.packet[0] = _mm256_permute2f128_pd(T1, T3, 32);
572  kernel.packet[2] = _mm256_permute2f128_pd(T1, T3, 49);
573 }
574 
575 template<> EIGEN_STRONG_INLINE Packet8f pblend(const Selector<8>& ifPacket, const Packet8f& thenPacket, const Packet8f& elsePacket) {
576  const __m256 zero = _mm256_setzero_ps();
577  const __m256 select = _mm256_set_ps(ifPacket.select[7], ifPacket.select[6], ifPacket.select[5], ifPacket.select[4], ifPacket.select[3], ifPacket.select[2], ifPacket.select[1], ifPacket.select[0]);
578  __m256 false_mask = _mm256_cmp_ps(select, zero, _CMP_EQ_UQ);
579  return _mm256_blendv_ps(thenPacket, elsePacket, false_mask);
580 }
581 template<> EIGEN_STRONG_INLINE Packet4d pblend(const Selector<4>& ifPacket, const Packet4d& thenPacket, const Packet4d& elsePacket) {
582  const __m256d zero = _mm256_setzero_pd();
583  const __m256d select = _mm256_set_pd(ifPacket.select[3], ifPacket.select[2], ifPacket.select[1], ifPacket.select[0]);
584  __m256d false_mask = _mm256_cmp_pd(select, zero, _CMP_EQ_UQ);
585  return _mm256_blendv_pd(thenPacket, elsePacket, false_mask);
586 }
587 
588 } // end namespace internal
589 
590 } // end namespace Eigen
591 
592 #endif // EIGEN_PACKET_MATH_AVX_H
Definition: LDLT.h:16
Definition: Constants.h:223
Definition: Eigen_Colamd.h:54