$darkmode
Eigen  5.0.1-dev
Complex.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2010 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 
10 #ifndef EIGEN_COMPLEX_SSE_H
11 #define EIGEN_COMPLEX_SSE_H
12 
13 // IWYU pragma: private
14 #include "../../InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 namespace internal {
19 
20 //---------- float ----------
21 struct Packet2cf {
22  EIGEN_STRONG_INLINE Packet2cf() {}
23  EIGEN_STRONG_INLINE explicit Packet2cf(const __m128& a) : v(a) {}
24  Packet4f v;
25 };
26 
27 // Use the packet_traits defined in AVX/PacketMath.h instead if we're going
28 // to leverage AVX instructions.
29 #ifndef EIGEN_VECTORIZE_AVX
30 template <>
31 struct packet_traits<std::complex<float> > : default_packet_traits {
32  typedef Packet2cf type;
33  typedef Packet2cf half;
34  enum {
35  Vectorizable = 1,
36  AlignedOnScalar = 1,
37  size = 2,
38 
39  HasAdd = 1,
40  HasSub = 1,
41  HasMul = 1,
42  HasDiv = 1,
43  HasNegate = 1,
44  HasSqrt = 1,
45  HasLog = 1,
46  HasExp = 1,
47  HasAbs = 0,
48  HasAbs2 = 0,
49  HasMin = 0,
50  HasMax = 0,
51  HasSetLinear = 0,
52  HasBlend = 1
53  };
54 };
55 #endif
56 
57 template <>
58 struct unpacket_traits<Packet2cf> {
59  typedef std::complex<float> type;
60  typedef Packet2cf half;
61  typedef Packet4f as_real;
62  enum {
63  size = 2,
64  alignment = Aligned16,
65  vectorizable = true,
66  masked_load_available = false,
67  masked_store_available = false
68  };
69 };
70 
71 template <>
72 EIGEN_STRONG_INLINE Packet2cf padd<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
73  return Packet2cf(_mm_add_ps(a.v, b.v));
74 }
75 template <>
76 EIGEN_STRONG_INLINE Packet2cf psub<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
77  return Packet2cf(_mm_sub_ps(a.v, b.v));
78 }
79 
80 template <>
81 EIGEN_STRONG_INLINE Packet2cf pnegate(const Packet2cf& a) {
82  const __m128 mask = _mm_castsi128_ps(_mm_setr_epi32(0x80000000, 0x80000000, 0x80000000, 0x80000000));
83  return Packet2cf(_mm_xor_ps(a.v, mask));
84 }
85 template <>
86 EIGEN_STRONG_INLINE Packet2cf pconj(const Packet2cf& a) {
87  const __m128 mask = _mm_castsi128_ps(_mm_setr_epi32(0x00000000, 0x80000000, 0x00000000, 0x80000000));
88  return Packet2cf(_mm_xor_ps(a.v, mask));
89 }
90 
91 template <>
92 EIGEN_STRONG_INLINE Packet2cf pmul(const Packet2cf& a, const Packet2cf& b) {
93 #ifdef EIGEN_VECTORIZE_SSE3
94  __m128 tmp1 = _mm_mul_ps(_mm_movehdup_ps(a.v), vec4f_swizzle1(b.v, 1, 0, 3, 2));
95  __m128 tmp2 = _mm_moveldup_ps(a.v);
96 #else
97  __m128 tmp1 = _mm_mul_ps(vec4f_swizzle1(a.v, 1, 1, 3, 3), vec4f_swizzle1(b.v, 1, 0, 3, 2));
98  __m128 tmp2 = vec4f_swizzle1(a.v, 0, 0, 2, 2);
99 #endif
100 #ifdef EIGEN_VECTORIZE_FMA
101  __m128 result = _mm_fmaddsub_ps(tmp2, b.v, tmp1);
102 #else
103 #ifdef EIGEN_VECTORIZE_SSE3
104  __m128 result = _mm_addsub_ps(_mm_mul_ps(tmp2, b.v), tmp1);
105 #else
106  const __m128 mask = _mm_setr_ps(-0.0f, 0.0f, -0.0f, 0.0f);
107  __m128 result = _mm_add_ps(_mm_mul_ps(tmp2, b.v), _mm_xor_ps(tmp1, mask));
108 #endif
109 #endif
110  return Packet2cf(result);
111 }
112 
113 template <>
114 EIGEN_STRONG_INLINE Packet2cf ptrue<Packet2cf>(const Packet2cf& a) {
115  return Packet2cf(ptrue(Packet4f(a.v)));
116 }
117 template <>
118 EIGEN_STRONG_INLINE Packet2cf pand<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
119  return Packet2cf(_mm_and_ps(a.v, b.v));
120 }
121 template <>
122 EIGEN_STRONG_INLINE Packet2cf por<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
123  return Packet2cf(_mm_or_ps(a.v, b.v));
124 }
125 template <>
126 EIGEN_STRONG_INLINE Packet2cf pxor<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
127  return Packet2cf(_mm_xor_ps(a.v, b.v));
128 }
129 template <>
130 EIGEN_STRONG_INLINE Packet2cf pandnot<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
131  return Packet2cf(_mm_andnot_ps(b.v, a.v));
132 }
133 
134 template <>
135 EIGEN_STRONG_INLINE Packet2cf pload<Packet2cf>(const std::complex<float>* from) {
136  EIGEN_DEBUG_ALIGNED_LOAD return Packet2cf(_mm_load_ps(&numext::real_ref(*from)));
137 }
138 template <>
139 EIGEN_STRONG_INLINE Packet2cf ploadu<Packet2cf>(const std::complex<float>* from) {
140  EIGEN_DEBUG_UNALIGNED_LOAD return Packet2cf(_mm_loadu_ps(&numext::real_ref(*from)));
141 }
142 
143 template <>
144 EIGEN_STRONG_INLINE Packet2cf pset1<Packet2cf>(const std::complex<float>& from) {
145  const float re = std::real(from);
146  const float im = std::imag(from);
147  return Packet2cf(_mm_set_ps(im, re, im, re));
148 }
149 
150 template <>
151 EIGEN_STRONG_INLINE Packet2cf ploaddup<Packet2cf>(const std::complex<float>* from) {
152  return pset1<Packet2cf>(*from);
153 }
154 
155 template <>
156 EIGEN_STRONG_INLINE void pstore<std::complex<float> >(std::complex<float>* to, const Packet2cf& from) {
157  EIGEN_DEBUG_ALIGNED_STORE _mm_store_ps(&numext::real_ref(*to), from.v);
158 }
159 template <>
160 EIGEN_STRONG_INLINE void pstoreu<std::complex<float> >(std::complex<float>* to, const Packet2cf& from) {
161  EIGEN_DEBUG_UNALIGNED_STORE _mm_storeu_ps(&numext::real_ref(*to), from.v);
162 }
163 
164 template <>
165 EIGEN_DEVICE_FUNC inline Packet2cf pgather<std::complex<float>, Packet2cf>(const std::complex<float>* from,
166  Index stride) {
167  return Packet2cf(_mm_set_ps(std::imag(from[1 * stride]), std::real(from[1 * stride]), std::imag(from[0 * stride]),
168  std::real(from[0 * stride])));
169 }
170 
171 template <>
172 EIGEN_DEVICE_FUNC inline void pscatter<std::complex<float>, Packet2cf>(std::complex<float>* to, const Packet2cf& from,
173  Index stride) {
174  to[stride * 0] = std::complex<float>(_mm_cvtss_f32(_mm_shuffle_ps(from.v, from.v, 0)),
175  _mm_cvtss_f32(_mm_shuffle_ps(from.v, from.v, 1)));
176  to[stride * 1] = std::complex<float>(_mm_cvtss_f32(_mm_shuffle_ps(from.v, from.v, 2)),
177  _mm_cvtss_f32(_mm_shuffle_ps(from.v, from.v, 3)));
178 }
179 
180 template <>
181 EIGEN_STRONG_INLINE void prefetch<std::complex<float> >(const std::complex<float>* addr) {
182  _mm_prefetch((SsePrefetchPtrType)(addr), _MM_HINT_T0);
183 }
184 
185 template <>
186 EIGEN_STRONG_INLINE std::complex<float> pfirst<Packet2cf>(const Packet2cf& a) {
187  alignas(alignof(__m64)) std::complex<float> res;
188  _mm_storel_pi((__m64*)&res, a.v);
189  return res;
190 }
191 
192 template <>
193 EIGEN_STRONG_INLINE Packet2cf preverse(const Packet2cf& a) {
194  return Packet2cf(_mm_castpd_ps(preverse(Packet2d(_mm_castps_pd(a.v)))));
195 }
196 
197 template <>
198 EIGEN_STRONG_INLINE std::complex<float> predux<Packet2cf>(const Packet2cf& a) {
199  return pfirst(Packet2cf(_mm_add_ps(a.v, _mm_movehl_ps(a.v, a.v))));
200 }
201 
202 template <>
203 EIGEN_STRONG_INLINE std::complex<float> predux_mul<Packet2cf>(const Packet2cf& a) {
204  return pfirst(pmul(a, Packet2cf(_mm_movehl_ps(a.v, a.v))));
205 }
206 
207 EIGEN_STRONG_INLINE Packet2cf pcplxflip /* <Packet2cf> */ (const Packet2cf& x) {
208  return Packet2cf(vec4f_swizzle1(x.v, 1, 0, 3, 2));
209 }
210 
211 EIGEN_MAKE_CONJ_HELPER_CPLX_REAL(Packet2cf, Packet4f)
212 
213 template <>
214 EIGEN_STRONG_INLINE Packet2cf pdiv<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
215  return pdiv_complex(a, b);
216 }
217 
218 //---------- double ----------
219 struct Packet1cd {
220  EIGEN_STRONG_INLINE Packet1cd() {}
221  EIGEN_STRONG_INLINE explicit Packet1cd(const __m128d& a) : v(a) {}
222  Packet2d v;
223 };
224 
225 // Use the packet_traits defined in AVX/PacketMath.h instead if we're going
226 // to leverage AVX instructions.
227 #ifndef EIGEN_VECTORIZE_AVX
228 template <>
229 struct packet_traits<std::complex<double> > : default_packet_traits {
230  typedef Packet1cd type;
231  typedef Packet1cd half;
232  enum {
233  Vectorizable = 1,
234  AlignedOnScalar = 0,
235  size = 1,
236 
237  HasAdd = 1,
238  HasSub = 1,
239  HasMul = 1,
240  HasDiv = 1,
241  HasNegate = 1,
242  HasSqrt = 1,
243  HasLog = 1,
244  HasAbs = 0,
245  HasAbs2 = 0,
246  HasMin = 0,
247  HasMax = 0,
248  HasSetLinear = 0
249  };
250 };
251 #endif
252 
253 template <>
254 struct unpacket_traits<Packet1cd> {
255  typedef std::complex<double> type;
256  typedef Packet1cd half;
257  typedef Packet2d as_real;
258  enum {
259  size = 1,
260  alignment = Aligned16,
261  vectorizable = true,
262  masked_load_available = false,
263  masked_store_available = false
264  };
265 };
266 
267 template <>
268 EIGEN_STRONG_INLINE Packet1cd padd<Packet1cd>(const Packet1cd& a, const Packet1cd& b) {
269  return Packet1cd(_mm_add_pd(a.v, b.v));
270 }
271 template <>
272 EIGEN_STRONG_INLINE Packet1cd psub<Packet1cd>(const Packet1cd& a, const Packet1cd& b) {
273  return Packet1cd(_mm_sub_pd(a.v, b.v));
274 }
275 template <>
276 EIGEN_STRONG_INLINE Packet1cd pnegate(const Packet1cd& a) {
277  return Packet1cd(pnegate(Packet2d(a.v)));
278 }
279 template <>
280 EIGEN_STRONG_INLINE Packet1cd pconj(const Packet1cd& a) {
281  const __m128d mask = _mm_castsi128_pd(_mm_set_epi32(0x80000000, 0x0, 0x0, 0x0));
282  return Packet1cd(_mm_xor_pd(a.v, mask));
283 }
284 
285 template <>
286 EIGEN_STRONG_INLINE Packet1cd pmul(const Packet1cd& a, const Packet1cd& b) {
287  __m128d tmp1 = _mm_mul_pd(_mm_unpackhi_pd(a.v, a.v), vec2d_swizzle1(b.v, 1, 0));
288 #ifdef EIGEN_VECTORIZE_SSE3
289  __m128d tmp2 = _mm_movedup_pd(a.v);
290 #else
291  __m128d tmp2 = _mm_unpacklo_pd(a.v, a.v);
292 #endif
293 #ifdef EIGEN_VECTORIZE_FMA
294  __m128d result = _mm_fmaddsub_pd(tmp2, b.v, tmp1);
295 #else
296 #ifdef EIGEN_VECTORIZE_SSE3
297  __m128d result = _mm_addsub_pd(_mm_mul_pd(tmp2, b.v), tmp1);
298 #else
299  const __m128d mask = _mm_setr_pd(-0.0, 0.0);
300  __m128d result = _mm_add_pd(_mm_mul_pd(tmp2, b.v), _mm_xor_pd(tmp1, mask));
301 #endif
302 #endif
303  return Packet1cd(result);
304 }
305 
306 template <>
307 EIGEN_STRONG_INLINE Packet1cd ptrue<Packet1cd>(const Packet1cd& a) {
308  return Packet1cd(ptrue(Packet2d(a.v)));
309 }
310 template <>
311 EIGEN_STRONG_INLINE Packet1cd pand<Packet1cd>(const Packet1cd& a, const Packet1cd& b) {
312  return Packet1cd(_mm_and_pd(a.v, b.v));
313 }
314 template <>
315 EIGEN_STRONG_INLINE Packet1cd por<Packet1cd>(const Packet1cd& a, const Packet1cd& b) {
316  return Packet1cd(_mm_or_pd(a.v, b.v));
317 }
318 template <>
319 EIGEN_STRONG_INLINE Packet1cd pxor<Packet1cd>(const Packet1cd& a, const Packet1cd& b) {
320  return Packet1cd(_mm_xor_pd(a.v, b.v));
321 }
322 template <>
323 EIGEN_STRONG_INLINE Packet1cd pandnot<Packet1cd>(const Packet1cd& a, const Packet1cd& b) {
324  return Packet1cd(_mm_andnot_pd(b.v, a.v));
325 }
326 
327 // FIXME force unaligned load, this is a temporary fix
328 template <>
329 EIGEN_STRONG_INLINE Packet1cd pload<Packet1cd>(const std::complex<double>* from) {
330  EIGEN_DEBUG_ALIGNED_LOAD return Packet1cd(_mm_load_pd((const double*)from));
331 }
332 template <>
333 EIGEN_STRONG_INLINE Packet1cd ploadu<Packet1cd>(const std::complex<double>* from) {
334  EIGEN_DEBUG_UNALIGNED_LOAD return Packet1cd(_mm_loadu_pd((const double*)from));
335 }
336 template <>
337 EIGEN_STRONG_INLINE Packet1cd
338 pset1<Packet1cd>(const std::complex<double>& from) { /* here we really have to use unaligned loads :( */
339  return ploadu<Packet1cd>(&from);
340 }
341 
342 template <>
343 EIGEN_STRONG_INLINE Packet1cd ploaddup<Packet1cd>(const std::complex<double>* from) {
344  return pset1<Packet1cd>(*from);
345 }
346 
347 // FIXME force unaligned store, this is a temporary fix
348 template <>
349 EIGEN_STRONG_INLINE void pstore<std::complex<double> >(std::complex<double>* to, const Packet1cd& from) {
350  EIGEN_DEBUG_ALIGNED_STORE _mm_store_pd((double*)to, from.v);
351 }
352 template <>
353 EIGEN_STRONG_INLINE void pstoreu<std::complex<double> >(std::complex<double>* to, const Packet1cd& from) {
354  EIGEN_DEBUG_UNALIGNED_STORE _mm_storeu_pd((double*)to, from.v);
355 }
356 
357 template <>
358 EIGEN_STRONG_INLINE void prefetch<std::complex<double> >(const std::complex<double>* addr) {
359  _mm_prefetch((SsePrefetchPtrType)(addr), _MM_HINT_T0);
360 }
361 
362 template <>
363 EIGEN_STRONG_INLINE std::complex<double> pfirst<Packet1cd>(const Packet1cd& a) {
364  EIGEN_ALIGN16 double res[2];
365  _mm_store_pd(res, a.v);
366  return std::complex<double>(res[0], res[1]);
367 }
368 
369 template <>
370 EIGEN_STRONG_INLINE Packet1cd preverse(const Packet1cd& a) {
371  return a;
372 }
373 
374 template <>
375 EIGEN_STRONG_INLINE std::complex<double> predux<Packet1cd>(const Packet1cd& a) {
376  return pfirst(a);
377 }
378 
379 template <>
380 EIGEN_STRONG_INLINE std::complex<double> predux_mul<Packet1cd>(const Packet1cd& a) {
381  return pfirst(a);
382 }
383 
384 EIGEN_MAKE_CONJ_HELPER_CPLX_REAL(Packet1cd, Packet2d)
385 
386 template <>
387 EIGEN_STRONG_INLINE Packet1cd pdiv<Packet1cd>(const Packet1cd& a, const Packet1cd& b) {
388  return pdiv_complex(a, b);
389 }
390 
391 EIGEN_STRONG_INLINE Packet1cd pcplxflip /* <Packet1cd> */ (const Packet1cd& x) {
392  return Packet1cd(preverse(Packet2d(x.v)));
393 }
394 
395 EIGEN_DEVICE_FUNC inline void ptranspose(PacketBlock<Packet2cf, 2>& kernel) {
396  __m128d w1 = _mm_castps_pd(kernel.packet[0].v);
397  __m128d w2 = _mm_castps_pd(kernel.packet[1].v);
398 
399  __m128 tmp = _mm_castpd_ps(_mm_unpackhi_pd(w1, w2));
400  kernel.packet[0].v = _mm_castpd_ps(_mm_unpacklo_pd(w1, w2));
401  kernel.packet[1].v = tmp;
402 }
403 
404 template <>
405 EIGEN_STRONG_INLINE Packet2cf pcmp_eq(const Packet2cf& a, const Packet2cf& b) {
406  __m128 eq = _mm_cmpeq_ps(a.v, b.v);
407  return Packet2cf(pand<Packet4f>(eq, vec4f_swizzle1(eq, 1, 0, 3, 2)));
408 }
409 
410 template <>
411 EIGEN_STRONG_INLINE Packet1cd pcmp_eq(const Packet1cd& a, const Packet1cd& b) {
412  __m128d eq = _mm_cmpeq_pd(a.v, b.v);
413  return Packet1cd(pand<Packet2d>(eq, vec2d_swizzle1(eq, 1, 0)));
414 }
415 
416 template <>
417 EIGEN_STRONG_INLINE Packet2cf pblend(const Selector<2>& ifPacket, const Packet2cf& thenPacket,
418  const Packet2cf& elsePacket) {
419  __m128d result = pblend<Packet2d>(ifPacket, _mm_castps_pd(thenPacket.v), _mm_castps_pd(elsePacket.v));
420  return Packet2cf(_mm_castpd_ps(result));
421 }
422 
423 template <>
424 EIGEN_STRONG_INLINE Packet1cd psqrt<Packet1cd>(const Packet1cd& a) {
425  return psqrt_complex<Packet1cd>(a);
426 }
427 
428 template <>
429 EIGEN_STRONG_INLINE Packet2cf psqrt<Packet2cf>(const Packet2cf& a) {
430  return psqrt_complex<Packet2cf>(a);
431 }
432 
433 template <>
434 EIGEN_STRONG_INLINE Packet1cd plog<Packet1cd>(const Packet1cd& a) {
435  return plog_complex<Packet1cd>(a);
436 }
437 
438 template <>
439 EIGEN_STRONG_INLINE Packet2cf plog<Packet2cf>(const Packet2cf& a) {
440  return plog_complex<Packet2cf>(a);
441 }
442 
443 template <>
444 EIGEN_STRONG_INLINE Packet2cf pexp<Packet2cf>(const Packet2cf& a) {
445  return pexp_complex<Packet2cf>(a);
446 }
447 
448 #ifdef EIGEN_VECTORIZE_FMA
449 // std::complex<float>
450 template <>
451 EIGEN_STRONG_INLINE Packet2cf pmadd(const Packet2cf& a, const Packet2cf& b, const Packet2cf& c) {
452  __m128 a_odd = _mm_movehdup_ps(a.v);
453  __m128 a_even = _mm_moveldup_ps(a.v);
454  __m128 b_swap = _mm_permute_ps(b.v, _MM_SHUFFLE(2, 3, 0, 1));
455  __m128 result = _mm_fmaddsub_ps(a_even, b.v, _mm_fmaddsub_ps(a_odd, b_swap, c.v));
456  return Packet2cf(result);
457 }
458 template <>
459 EIGEN_STRONG_INLINE Packet2cf pmsub(const Packet2cf& a, const Packet2cf& b, const Packet2cf& c) {
460  __m128 a_odd = _mm_movehdup_ps(a.v);
461  __m128 a_even = _mm_moveldup_ps(a.v);
462  __m128 b_swap = _mm_permute_ps(b.v, _MM_SHUFFLE(2, 3, 0, 1));
463  __m128 result = _mm_fmaddsub_ps(a_even, b.v, _mm_fmsubadd_ps(a_odd, b_swap, c.v));
464  return Packet2cf(result);
465 }
466 template <>
467 EIGEN_STRONG_INLINE Packet2cf pnmadd(const Packet2cf& a, const Packet2cf& b, const Packet2cf& c) {
468  return pnegate(pmsub(a, b, c));
469 }
470 template <>
471 EIGEN_STRONG_INLINE Packet2cf pnmsub(const Packet2cf& a, const Packet2cf& b, const Packet2cf& c) {
472  return pnegate(pmadd(a, b, c));
473 }
474 // std::complex<double>
475 template <>
476 EIGEN_STRONG_INLINE Packet1cd pmadd(const Packet1cd& a, const Packet1cd& b, const Packet1cd& c) {
477  __m128d a_odd = _mm_permute_pd(a.v, 0x3);
478  __m128d a_even = _mm_movedup_pd(a.v);
479  __m128d b_swap = _mm_permute_pd(b.v, 0x1);
480  __m128d result = _mm_fmaddsub_pd(a_even, b.v, _mm_fmaddsub_pd(a_odd, b_swap, c.v));
481  return Packet1cd(result);
482 }
483 template <>
484 EIGEN_STRONG_INLINE Packet1cd pmsub(const Packet1cd& a, const Packet1cd& b, const Packet1cd& c) {
485  __m128d a_odd = _mm_permute_pd(a.v, 0x3);
486  __m128d a_even = _mm_movedup_pd(a.v);
487  __m128d b_swap = _mm_permute_pd(b.v, 0x1);
488  __m128d result = _mm_fmaddsub_pd(a_even, b.v, _mm_fmsubadd_pd(a_odd, b_swap, c.v));
489  return Packet1cd(result);
490 }
491 template <>
492 EIGEN_STRONG_INLINE Packet1cd pnmadd(const Packet1cd& a, const Packet1cd& b, const Packet1cd& c) {
493  return pnegate(pmsub(a, b, c));
494 }
495 template <>
496 EIGEN_STRONG_INLINE Packet1cd pnmsub(const Packet1cd& a, const Packet1cd& b, const Packet1cd& c) {
497  return pnegate(pmadd(a, b, c));
498 }
499 #endif
500 } // end namespace internal
501 } // end namespace Eigen
502 
503 #endif // EIGEN_COMPLEX_SSE_H
Definition: Constants.h:237
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
Definition: BFloat16.h:231
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82