Eigen  5.0.1-dev+7c7d8473
 
Loading...
Searching...
No Matches
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// Copyright (C) 2016 Konstantinos Margaritis <markos@freevec.org>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10
11#ifndef EIGEN_COMPLEX32_ZVECTOR_H
12#define EIGEN_COMPLEX32_ZVECTOR_H
13
14// IWYU pragma: private
15#include "../../InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20
21#if !defined(__ARCH__) || (defined(__ARCH__) && __ARCH__ >= 12)
22inline Packet4ui p4ui_CONJ_XOR() {
23 return Packet4ui {0x00000000, 0x80000000, 0x00000000, 0x80000000}; // vec_mergeh((Packet4ui)p4i_ZERO, (Packet4ui)p4f_MZERO);
24}
25#endif
26
27inline Packet2ul p2ul_CONJ_XOR1() {
28 return (Packet2ul)vec_sld((Packet4ui)p2d_ZERO_, (Packet4ui)p2l_ZERO,
29 8); //{ 0x8000000000000000, 0x0000000000000000 };
30}
31inline Packet2ul p2ul_CONJ_XOR2() {
32 return (Packet2ul)vec_sld((Packet4ui)p2l_ZERO, (Packet4ui)p2d_ZERO_,
33 8); //{ 0x8000000000000000, 0x0000000000000000 };
34}
35
36struct Packet1cd {
37 EIGEN_STRONG_INLINE Packet1cd() {}
38 EIGEN_STRONG_INLINE explicit Packet1cd(const Packet2d& a) : v(a) {}
39 Packet2d v;
40};
41
42struct Packet2cf {
43 EIGEN_STRONG_INLINE Packet2cf() {}
44 EIGEN_STRONG_INLINE explicit Packet2cf(const Packet4f& a) : v(a) {}
45#if !defined(__ARCH__) || (defined(__ARCH__) && __ARCH__ < 12)
46 union {
47 Packet4f v;
48 Packet1cd cd[2];
49 };
50#else
51 Packet4f v;
52#endif
53};
54
55template <>
56struct packet_traits<std::complex<float> > : default_packet_traits {
57 typedef Packet2cf type;
58 typedef Packet2cf half;
59 enum {
60 Vectorizable = 1,
61 AlignedOnScalar = 1,
62 size = 2,
63
64 HasAdd = 1,
65 HasSub = 1,
66 HasMul = 1,
67 HasDiv = 1,
68 HasLog = 1,
69 HasExp = 1,
70 HasNegate = 1,
71 HasAbs = 0,
72 HasAbs2 = 0,
73 HasMin = 0,
74 HasMax = 0,
75 HasSetLinear = 0
76 };
77};
78
79template <>
80struct packet_traits<std::complex<double> > : default_packet_traits {
81 typedef Packet1cd type;
82 typedef Packet1cd half;
83 enum {
84 Vectorizable = 1,
85 AlignedOnScalar = 1,
86 size = 1,
87
88 HasAdd = 1,
89 HasSub = 1,
90 HasMul = 1,
91 HasDiv = 1,
92 HasLog = 1,
93 HasNegate = 1,
94 HasAbs = 0,
95 HasAbs2 = 0,
96 HasMin = 0,
97 HasMax = 0,
98 HasSetLinear = 0
99 };
100};
101
102template <>
103struct unpacket_traits<Packet2cf> {
104 typedef std::complex<float> type;
105 enum {
106 size = 2,
107 alignment = Aligned16,
108 vectorizable = true,
109 masked_load_available = false,
110 masked_store_available = false
111 };
112 typedef Packet2cf half;
113 typedef Packet4f as_real;
114};
115template <>
116struct unpacket_traits<Packet1cd> {
117 typedef std::complex<double> type;
118 enum {
119 size = 1,
120 alignment = Aligned16,
121 vectorizable = true,
122 masked_load_available = false,
123 masked_store_available = false
124 };
125 typedef Packet1cd half;
126 typedef Packet2d as_real;
127};
128
129/* Forward declaration */
130EIGEN_STRONG_INLINE void ptranspose(PacketBlock<Packet2cf, 2>& kernel);
131
132/* complex<double> first */
133template <>
134EIGEN_STRONG_INLINE Packet1cd pload<Packet1cd>(const std::complex<double>* from) {
135 EIGEN_DEBUG_ALIGNED_LOAD return Packet1cd(pload<Packet2d>((const double*)from));
136}
137template <>
138EIGEN_STRONG_INLINE Packet1cd ploadu<Packet1cd>(const std::complex<double>* from) {
139 EIGEN_DEBUG_UNALIGNED_LOAD return Packet1cd(ploadu<Packet2d>((const double*)from));
140}
141template <>
142EIGEN_STRONG_INLINE void pstore<std::complex<double> >(std::complex<double>* to, const Packet1cd& from) {
143 EIGEN_DEBUG_ALIGNED_STORE pstore((double*)to, from.v);
144}
145template <>
146EIGEN_STRONG_INLINE void pstoreu<std::complex<double> >(std::complex<double>* to, const Packet1cd& from) {
147 EIGEN_DEBUG_UNALIGNED_STORE pstoreu((double*)to, from.v);
148}
149
150template <>
151EIGEN_STRONG_INLINE Packet1cd
152pset1<Packet1cd>(const std::complex<double>& from) { /* here we really have to use unaligned loads :( */
153 return ploadu<Packet1cd>(&from);
154}
155
156template <>
157EIGEN_DEVICE_FUNC inline Packet1cd pgather<std::complex<double>, Packet1cd>(const std::complex<double>* from,
158 Index stride EIGEN_UNUSED) {
159 return pload<Packet1cd>(from);
160}
161template <>
162EIGEN_DEVICE_FUNC inline void pscatter<std::complex<double>, Packet1cd>(std::complex<double>* to, const Packet1cd& from,
163 Index stride EIGEN_UNUSED) {
164 pstore<std::complex<double> >(to, from);
165}
166template <>
167EIGEN_STRONG_INLINE Packet1cd padd<Packet1cd>(const Packet1cd& a, const Packet1cd& b) {
168 return Packet1cd(a.v + b.v);
169}
170template <>
171EIGEN_STRONG_INLINE Packet1cd psub<Packet1cd>(const Packet1cd& a, const Packet1cd& b) {
172 return Packet1cd(a.v - b.v);
173}
174template <>
175EIGEN_STRONG_INLINE Packet1cd pnegate(const Packet1cd& a) {
176 return Packet1cd(pnegate(Packet2d(a.v)));
177}
178template <>
179EIGEN_STRONG_INLINE Packet1cd pconj(const Packet1cd& a) {
180 return Packet1cd((Packet2d)vec_xor((Packet2d)a.v, (Packet2d)p2ul_CONJ_XOR2()));
181}
182template <>
183EIGEN_STRONG_INLINE Packet1cd pmul<Packet1cd>(const Packet1cd& a, const Packet1cd& b) {
184 Packet2d a_re, a_im, v1, v2;
185
186 // Permute and multiply the real parts of a and b
187 a_re = vec_perm(a.v, a.v, p16uc_PSET64_HI);
188 // Get the imaginary parts of a
189 a_im = vec_perm(a.v, a.v, p16uc_PSET64_LO);
190 // multiply a_re * b
191 v1 = vec_madd(a_re, b.v, p2d_ZERO);
192 // multiply a_im * b and get the conjugate result
193 v2 = vec_madd(a_im, b.v, p2d_ZERO);
194 v2 = (Packet2d)vec_sld((Packet4ui)v2, (Packet4ui)v2, 8);
195 v2 = (Packet2d)vec_xor((Packet2d)v2, (Packet2d)p2ul_CONJ_XOR1());
196
197 return Packet1cd(v1 + v2);
198}
199template <>
200EIGEN_STRONG_INLINE Packet1cd pand<Packet1cd>(const Packet1cd& a, const Packet1cd& b) {
201 return Packet1cd(vec_and(a.v, b.v));
202}
203template <>
204EIGEN_STRONG_INLINE Packet1cd por<Packet1cd>(const Packet1cd& a, const Packet1cd& b) {
205 return Packet1cd(vec_or(a.v, b.v));
206}
207template <>
208EIGEN_STRONG_INLINE Packet1cd pxor<Packet1cd>(const Packet1cd& a, const Packet1cd& b) {
209 return Packet1cd(vec_xor(a.v, b.v));
210}
211template <>
212EIGEN_STRONG_INLINE Packet1cd pandnot<Packet1cd>(const Packet1cd& a, const Packet1cd& b) {
213 return Packet1cd(vec_and(a.v, vec_nor(b.v, b.v)));
214}
215template <>
216EIGEN_STRONG_INLINE Packet1cd ploaddup<Packet1cd>(const std::complex<double>* from) {
217 return pset1<Packet1cd>(*from);
218}
219template <>
220EIGEN_STRONG_INLINE Packet1cd pcmp_eq(const Packet1cd& a, const Packet1cd& b) {
221 Packet2d eq = vec_cmpeq(a.v, b.v);
222 Packet2d tmp = {eq[1], eq[0]};
223 return (Packet1cd)pand<Packet2d>(eq, tmp);
224}
225
226template <>
227EIGEN_STRONG_INLINE void prefetch<std::complex<double> >(const std::complex<double>* addr) {
228 EIGEN_ZVECTOR_PREFETCH(addr);
229}
230
231template <>
232EIGEN_STRONG_INLINE std::complex<double> pfirst<Packet1cd>(const Packet1cd& a) {
233 EIGEN_ALIGN16 std::complex<double> res;
234 pstore<std::complex<double> >(&res, a);
235
236 return res;
237}
238
239template <>
240EIGEN_STRONG_INLINE Packet1cd preverse(const Packet1cd& a) {
241 return a;
242}
243template <>
244EIGEN_STRONG_INLINE std::complex<double> predux<Packet1cd>(const Packet1cd& a) {
245 return pfirst(a);
246}
247template <>
248EIGEN_STRONG_INLINE std::complex<double> predux_mul<Packet1cd>(const Packet1cd& a) {
249 return pfirst(a);
250}
251EIGEN_MAKE_CONJ_HELPER_CPLX_REAL(Packet1cd, Packet2d)
252
253template <>
254EIGEN_STRONG_INLINE Packet1cd pdiv<Packet1cd>(const Packet1cd& a, const Packet1cd& b) {
255 return pdiv_complex(a, b);
256}
257
258template <>
259EIGEN_STRONG_INLINE Packet1cd psqrt<Packet1cd>(const Packet1cd& a) {
260 return psqrt_complex<Packet1cd>(a);
261}
262
263template <>
264EIGEN_STRONG_INLINE Packet2cf psqrt<Packet2cf>(const Packet2cf& a) {
265 return psqrt_complex<Packet2cf>(a);
266}
267
268template <>
269EIGEN_STRONG_INLINE Packet1cd plog<Packet1cd>(const Packet1cd& a) {
270 return plog_complex<Packet1cd>(a);
271}
272template <>
273EIGEN_STRONG_INLINE Packet2cf plog<Packet2cf>(const Packet2cf& a) {
274 return plog_complex<Packet2cf>(a);
275}
276
277template <>
278EIGEN_STRONG_INLINE Packet2cf pexp<Packet2cf>(const Packet2cf& a) {
279 return pexp_complex(a);
280}
281
282EIGEN_STRONG_INLINE Packet1cd pcplxflip /*<Packet1cd>*/ (const Packet1cd& x) {
283 return Packet1cd(preverse(Packet2d(x.v)));
284}
285
286EIGEN_STRONG_INLINE void ptranspose(PacketBlock<Packet1cd, 2>& kernel) {
287 Packet2d tmp = vec_perm(kernel.packet[0].v, kernel.packet[1].v, p16uc_TRANSPOSE64_HI);
288 kernel.packet[1].v = vec_perm(kernel.packet[0].v, kernel.packet[1].v, p16uc_TRANSPOSE64_LO);
289 kernel.packet[0].v = tmp;
290}
291
292/* complex<float> follows */
293template <>
294EIGEN_STRONG_INLINE Packet2cf pload<Packet2cf>(const std::complex<float>* from) {
295 EIGEN_DEBUG_ALIGNED_LOAD return Packet2cf(pload<Packet4f>((const float*)from));
296}
297template <>
298EIGEN_STRONG_INLINE Packet2cf ploadu<Packet2cf>(const std::complex<float>* from) {
299 EIGEN_DEBUG_UNALIGNED_LOAD return Packet2cf(ploadu<Packet4f>((const float*)from));
300}
301template <>
302EIGEN_STRONG_INLINE void pstore<std::complex<float> >(std::complex<float>* to, const Packet2cf& from) {
303 EIGEN_DEBUG_ALIGNED_STORE pstore((float*)to, from.v);
304}
305template <>
306EIGEN_STRONG_INLINE void pstoreu<std::complex<float> >(std::complex<float>* to, const Packet2cf& from) {
307 EIGEN_DEBUG_UNALIGNED_STORE pstoreu((float*)to, from.v);
308}
309
310template <>
311EIGEN_STRONG_INLINE std::complex<float> pfirst<Packet2cf>(const Packet2cf& a) {
312 EIGEN_ALIGN16 std::complex<float> res[2];
313 pstore<std::complex<float> >(res, a);
314
315 return res[0];
316}
317
318#if !defined(__ARCH__) || (defined(__ARCH__) && __ARCH__ < 12)
319template <>
320EIGEN_STRONG_INLINE Packet2cf pset1<Packet2cf>(const std::complex<float>& from) {
321 Packet2cf res;
322 res.cd[0] = Packet1cd(vec_ld2f((const float*)&from));
323 res.cd[1] = res.cd[0];
324 return res;
325}
326#else
327template <>
328EIGEN_STRONG_INLINE Packet2cf pset1<Packet2cf>(const std::complex<float>& from) {
329 Packet2cf res;
330 if ((std::ptrdiff_t(&from) % 16) == 0)
331 res.v = pload<Packet4f>((const float*)&from);
332 else
333 res.v = ploadu<Packet4f>((const float*)&from);
334 res.v = vec_perm(res.v, res.v, p16uc_PSET64_HI);
335 return res;
336}
337#endif
338
339template <>
340EIGEN_DEVICE_FUNC inline Packet2cf pgather<std::complex<float>, Packet2cf>(const std::complex<float>* from,
341 Index stride) {
342 EIGEN_ALIGN16 std::complex<float> af[2];
343 af[0] = from[0 * stride];
344 af[1] = from[1 * stride];
345 return pload<Packet2cf>(af);
346}
347template <>
348EIGEN_DEVICE_FUNC inline void pscatter<std::complex<float>, Packet2cf>(std::complex<float>* to, const Packet2cf& from,
349 Index stride) {
350 EIGEN_ALIGN16 std::complex<float> af[2];
351 pstore<std::complex<float> >((std::complex<float>*)af, from);
352 to[0 * stride] = af[0];
353 to[1 * stride] = af[1];
354}
355
356template <>
357EIGEN_STRONG_INLINE Packet2cf padd<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
358 return Packet2cf(padd<Packet4f>(a.v, b.v));
359}
360template <>
361EIGEN_STRONG_INLINE Packet2cf psub<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
362 return Packet2cf(psub<Packet4f>(a.v, b.v));
363}
364template <>
365EIGEN_STRONG_INLINE Packet2cf pnegate(const Packet2cf& a) {
366 return Packet2cf(pnegate(Packet4f(a.v)));
367}
368
369template <>
370EIGEN_STRONG_INLINE Packet2cf pand<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
371 return Packet2cf(pand<Packet4f>(a.v, b.v));
372}
373template <>
374EIGEN_STRONG_INLINE Packet2cf por<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
375 return Packet2cf(por<Packet4f>(a.v, b.v));
376}
377template <>
378EIGEN_STRONG_INLINE Packet2cf pxor<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
379 return Packet2cf(pxor<Packet4f>(a.v, b.v));
380}
381template <>
382EIGEN_STRONG_INLINE Packet2cf pandnot<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
383 return Packet2cf(pandnot<Packet4f>(a.v, b.v));
384}
385
386template <>
387EIGEN_STRONG_INLINE Packet2cf ploaddup<Packet2cf>(const std::complex<float>* from) {
388 return pset1<Packet2cf>(*from);
389}
390
391template <>
392EIGEN_STRONG_INLINE void prefetch<std::complex<float> >(const std::complex<float>* addr) {
393 EIGEN_ZVECTOR_PREFETCH(addr);
394}
395
396#if !defined(__ARCH__) || (defined(__ARCH__) && __ARCH__ < 12)
397
398template <>
399EIGEN_STRONG_INLINE Packet2cf pcmp_eq(const Packet2cf& a, const Packet2cf& b) {
400 Packet4f eq = pcmp_eq<Packet4f>(a.v, b.v);
401 Packet2cf res;
402 Packet2d tmp1 = {eq.v4f[0][1], eq.v4f[0][0]};
403 Packet2d tmp2 = {eq.v4f[1][1], eq.v4f[1][0]};
404 res.v.v4f[0] = pand<Packet2d>(eq.v4f[0], tmp1);
405 res.v.v4f[1] = pand<Packet2d>(eq.v4f[1], tmp2);
406 return res;
407}
408
409template <>
410EIGEN_STRONG_INLINE Packet2cf pconj(const Packet2cf& a) {
411 Packet2cf res;
412 res.v.v4f[0] = pconj(Packet1cd(reinterpret_cast<Packet2d>(a.v.v4f[0]))).v;
413 res.v.v4f[1] = pconj(Packet1cd(reinterpret_cast<Packet2d>(a.v.v4f[1]))).v;
414 return res;
415}
416
417template <>
418EIGEN_STRONG_INLINE Packet2cf pmul<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
419 Packet2cf res;
420 res.v.v4f[0] =
421 pmul(Packet1cd(reinterpret_cast<Packet2d>(a.v.v4f[0])), Packet1cd(reinterpret_cast<Packet2d>(b.v.v4f[0]))).v;
422 res.v.v4f[1] =
423 pmul(Packet1cd(reinterpret_cast<Packet2d>(a.v.v4f[1])), Packet1cd(reinterpret_cast<Packet2d>(b.v.v4f[1]))).v;
424 return res;
425}
426
427template <>
428EIGEN_STRONG_INLINE Packet2cf preverse(const Packet2cf& a) {
429 Packet2cf res;
430 res.cd[0] = a.cd[1];
431 res.cd[1] = a.cd[0];
432 return res;
433}
434
435template <>
436EIGEN_STRONG_INLINE std::complex<float> predux<Packet2cf>(const Packet2cf& a) {
437 std::complex<float> res;
438 Packet1cd b = padd<Packet1cd>(a.cd[0], a.cd[1]);
439 vec_st2f(b.v, (float*)&res);
440 return res;
441}
442
443template <>
444EIGEN_STRONG_INLINE std::complex<float> predux_mul<Packet2cf>(const Packet2cf& a) {
445 std::complex<float> res;
446 Packet1cd b = pmul<Packet1cd>(a.cd[0], a.cd[1]);
447 vec_st2f(b.v, (float*)&res);
448 return res;
449}
450
451EIGEN_MAKE_CONJ_HELPER_CPLX_REAL(Packet2cf, Packet4f)
452
453template <>
454EIGEN_STRONG_INLINE Packet2cf pdiv<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
455 return pdiv_complex(a, b);
456}
457
458EIGEN_STRONG_INLINE Packet2cf pcplxflip /*<Packet2cf>*/ (const Packet2cf& x) {
459 Packet2cf res;
460 res.cd[0] = pcplxflip(x.cd[0]);
461 res.cd[1] = pcplxflip(x.cd[1]);
462 return res;
463}
464
465EIGEN_STRONG_INLINE void ptranspose(PacketBlock<Packet2cf, 2>& kernel) {
466 Packet1cd tmp = kernel.packet[0].cd[1];
467 kernel.packet[0].cd[1] = kernel.packet[1].cd[0];
468 kernel.packet[1].cd[0] = tmp;
469}
470
471#else
472template <>
473EIGEN_STRONG_INLINE Packet2cf pcmp_eq(const Packet2cf& a, const Packet2cf& b) {
474 Packet4f eq = vec_cmpeq(a.v, b.v);
475 Packet4f tmp = {eq[1], eq[0], eq[3], eq[2]};
476 return (Packet2cf)pand<Packet4f>(eq, tmp);
477}
478template <>
479EIGEN_STRONG_INLINE Packet2cf pconj(const Packet2cf& a) {
480 return Packet2cf(pxor<Packet4f>(a.v, reinterpret_cast<Packet4f>(p4ui_CONJ_XOR())));
481}
482template <>
483EIGEN_STRONG_INLINE Packet2cf pmul<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
484 Packet4f a_re, a_im, prod, prod_im;
485
486 // Permute and multiply the real parts of a and b
487 a_re = vec_perm(a.v, a.v, p16uc_PSET32_WODD);
488
489 // Get the imaginary parts of a
490 a_im = vec_perm(a.v, a.v, p16uc_PSET32_WEVEN);
491
492 // multiply a_im * b and get the conjugate result
493 prod_im = a_im * b.v;
494 prod_im = pxor<Packet4f>(prod_im, reinterpret_cast<Packet4f>(p4ui_CONJ_XOR()));
495 // permute back to a proper order
496 prod_im = vec_perm(prod_im, prod_im, p16uc_COMPLEX32_REV);
497
498 // multiply a_re * b, add prod_im
499 prod = pmadd<Packet4f>(a_re, b.v, prod_im);
500
501 return Packet2cf(prod);
502}
503
504template <>
505EIGEN_STRONG_INLINE Packet2cf preverse(const Packet2cf& a) {
506 Packet4f rev_a;
507 rev_a = vec_perm(a.v, a.v, p16uc_COMPLEX32_REV2);
508 return Packet2cf(rev_a);
509}
510
511template <>
512EIGEN_STRONG_INLINE std::complex<float> predux<Packet2cf>(const Packet2cf& a) {
513 Packet4f b;
514 b = vec_sld(a.v, a.v, 8);
515 b = padd<Packet4f>(a.v, b);
516 return pfirst<Packet2cf>(Packet2cf(b));
517}
518
519template <>
520EIGEN_STRONG_INLINE std::complex<float> predux_mul<Packet2cf>(const Packet2cf& a) {
521 Packet4f b;
522 Packet2cf prod;
523 b = vec_sld(a.v, a.v, 8);
524 prod = pmul<Packet2cf>(a, Packet2cf(b));
525
526 return pfirst<Packet2cf>(prod);
527}
528
529EIGEN_MAKE_CONJ_HELPER_CPLX_REAL(Packet2cf, Packet4f)
530
531template <>
532EIGEN_STRONG_INLINE Packet2cf pdiv<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
533 return pdiv_complex(a, b);
534}
535
536template <>
537EIGEN_STRONG_INLINE Packet2cf pcplxflip<Packet2cf>(const Packet2cf& x) {
538 return Packet2cf(vec_perm(x.v, x.v, p16uc_COMPLEX32_REV));
539}
540
541EIGEN_STRONG_INLINE void ptranspose(PacketBlock<Packet2cf, 2>& kernel) {
542 Packet4f tmp = vec_perm(kernel.packet[0].v, kernel.packet[1].v, p16uc_TRANSPOSE64_HI);
543 kernel.packet[1].v = vec_perm(kernel.packet[0].v, kernel.packet[1].v, p16uc_TRANSPOSE64_LO);
544 kernel.packet[0].v = tmp;
545}
546
547#endif
548
549} // end namespace internal
550
551} // end namespace Eigen
552
553#endif // EIGEN_COMPLEX32_ZVECTOR_H
@ Aligned16
Definition Constants.h:237
Namespace containing all symbols from the Eigen library.
Definition B01_Experimental.dox:1
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:82