Medial Code Documentation
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) 2010-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_ALTIVEC_H
12#define EIGEN_COMPLEX32_ALTIVEC_H
13
14namespace Eigen {
15
16namespace internal {
17
18inline Packet4ui p4ui_CONJ_XOR() {
19 return vec_mergeh((Packet4ui)p4i_ZERO, (Packet4ui)p4f_MZERO);//{ 0x00000000, 0x80000000, 0x00000000, 0x80000000 };
20}
21#ifdef EIGEN_VECTORIZE_VSX
22#if defined(_BIG_ENDIAN)
23static Packet2ul p2ul_CONJ_XOR1 = (Packet2ul) vec_sld((Packet4ui) p2d_MZERO, (Packet4ui) p2l_ZERO, 8);//{ 0x8000000000000000, 0x0000000000000000 };
24static Packet2ul p2ul_CONJ_XOR2 = (Packet2ul) vec_sld((Packet4ui) p2l_ZERO, (Packet4ui) p2d_MZERO, 8);//{ 0x8000000000000000, 0x0000000000000000 };
25#else
26static Packet2ul p2ul_CONJ_XOR1 = (Packet2ul) vec_sld((Packet4ui) p2l_ZERO, (Packet4ui) p2d_MZERO, 8);//{ 0x8000000000000000, 0x0000000000000000 };
27static Packet2ul p2ul_CONJ_XOR2 = (Packet2ul) vec_sld((Packet4ui) p2d_MZERO, (Packet4ui) p2l_ZERO, 8);//{ 0x8000000000000000, 0x0000000000000000 };
28#endif
29#endif
30
31//---------- float ----------
33{
34 EIGEN_STRONG_INLINE explicit Packet2cf() {}
35 EIGEN_STRONG_INLINE explicit Packet2cf(const Packet4f& a) : v(a) {}
36
37 EIGEN_STRONG_INLINE Packet2cf pmul(const Packet2cf& a, const Packet2cf& b)
38 {
39 Packet4f v1, v2;
40
41 // Permute and multiply the real parts of a and b
42 v1 = vec_perm(a.v, a.v, p16uc_PSET32_WODD);
43 // Get the imaginary parts of a
44 v2 = vec_perm(a.v, a.v, p16uc_PSET32_WEVEN);
45 // multiply a_re * b
46 v1 = vec_madd(v1, b.v, p4f_ZERO);
47 // multiply a_im * b and get the conjugate result
48 v2 = vec_madd(v2, b.v, p4f_ZERO);
49 v2 = reinterpret_cast<Packet4f>(pxor(v2, reinterpret_cast<Packet4f>(p4ui_CONJ_XOR())));
50 // permute back to a proper order
51 v2 = vec_perm(v2, v2, p16uc_COMPLEX32_REV);
52
53 return Packet2cf(padd<Packet4f>(v1, v2));
54 }
55
56 EIGEN_STRONG_INLINE Packet2cf& operator*=(const Packet2cf& b) {
57 v = pmul(Packet2cf(*this), b).v;
58 return *this;
59 }
60 EIGEN_STRONG_INLINE Packet2cf operator*(const Packet2cf& b) const {
61 return Packet2cf(*this) *= b;
62 }
63
64 EIGEN_STRONG_INLINE Packet2cf& operator+=(const Packet2cf& b) {
65 v = padd(v, b.v);
66 return *this;
67 }
68 EIGEN_STRONG_INLINE Packet2cf operator+(const Packet2cf& b) const {
69 return Packet2cf(*this) += b;
70 }
71 EIGEN_STRONG_INLINE Packet2cf& operator-=(const Packet2cf& b) {
72 v = psub(v, b.v);
73 return *this;
74 }
75 EIGEN_STRONG_INLINE Packet2cf operator-(const Packet2cf& b) const {
76 return Packet2cf(*this) -= b;
77 }
78 EIGEN_STRONG_INLINE Packet2cf operator-(void) const {
79 return Packet2cf(-v);
80 }
81
82 Packet4f v;
83};
84
85template<> struct packet_traits<std::complex<float> > : default_packet_traits
86{
87 typedef Packet2cf type;
88 typedef Packet2cf half;
89 typedef Packet4f as_real;
90 enum {
91 Vectorizable = 1,
92 AlignedOnScalar = 1,
93 size = 2,
94 HasHalfPacket = 0,
95
96 HasAdd = 1,
97 HasSub = 1,
98 HasMul = 1,
99 HasDiv = 1,
100 HasNegate = 1,
101 HasAbs = 0,
102 HasAbs2 = 0,
103 HasMin = 0,
104 HasMax = 0,
105 HasSqrt = 1,
106#ifdef EIGEN_VECTORIZE_VSX
107 HasBlend = 1,
108#endif
109 HasSetLinear = 0
110 };
111};
112
113template<> struct unpacket_traits<Packet2cf> { typedef std::complex<float> type; enum {size=2, alignment=Aligned16, vectorizable=true, masked_load_available=false, masked_store_available=false}; typedef Packet2cf half; typedef Packet4f as_real; };
114
115template<> EIGEN_STRONG_INLINE Packet2cf pset1<Packet2cf>(const std::complex<float>& from)
116{
117 Packet2cf res;
118 if((std::ptrdiff_t(&from) % 16) == 0)
119 res.v = pload<Packet4f>((const float *)&from);
120 else
121 res.v = ploadu<Packet4f>((const float *)&from);
122 res.v = vec_perm(res.v, res.v, p16uc_PSET64_HI);
123 return res;
124}
125
126template<> EIGEN_STRONG_INLINE Packet2cf pload<Packet2cf>(const std::complex<float>* from) { return Packet2cf(pload<Packet4f>((const float *) from)); }
127template<> EIGEN_STRONG_INLINE Packet2cf ploadu<Packet2cf>(const std::complex<float>* from) { return Packet2cf(ploadu<Packet4f>((const float*) from)); }
128template<> EIGEN_STRONG_INLINE Packet2cf ploaddup<Packet2cf>(const std::complex<float>* from) { return pset1<Packet2cf>(*from); }
129
130template<> EIGEN_STRONG_INLINE void pstore <std::complex<float> >(std::complex<float> * to, const Packet2cf& from) { pstore((float*)to, from.v); }
131template<> EIGEN_STRONG_INLINE void pstoreu<std::complex<float> >(std::complex<float> * to, const Packet2cf& from) { pstoreu((float*)to, from.v); }
132
133EIGEN_STRONG_INLINE Packet2cf pload2(const std::complex<float>& from0, const std::complex<float>& from1)
134{
135 Packet4f res0, res1;
136#ifdef EIGEN_VECTORIZE_VSX
137 __asm__ ("lxsdx %x0,%y1" : "=wa" (res0) : "Z" (from0));
138 __asm__ ("lxsdx %x0,%y1" : "=wa" (res1) : "Z" (from1));
139#ifdef _BIG_ENDIAN
140 __asm__ ("xxpermdi %x0, %x1, %x2, 0" : "=wa" (res0) : "wa" (res0), "wa" (res1));
141#else
142 __asm__ ("xxpermdi %x0, %x2, %x1, 0" : "=wa" (res0) : "wa" (res0), "wa" (res1));
143#endif
144#else
145 *reinterpret_cast<std::complex<float> *>(&res0) = from0;
146 *reinterpret_cast<std::complex<float> *>(&res1) = from1;
147 res0 = vec_perm(res0, res1, p16uc_TRANSPOSE64_HI);
148#endif
149 return Packet2cf(res0);
150}
151
152template<> EIGEN_DEVICE_FUNC inline Packet2cf pgather<std::complex<float>, Packet2cf>(const std::complex<float>* from, Index stride)
153{
154 EIGEN_ALIGN16 std::complex<float> af[2];
155 af[0] = from[0*stride];
156 af[1] = from[1*stride];
157 return pload<Packet2cf>(af);
158}
159template<> EIGEN_DEVICE_FUNC inline void pscatter<std::complex<float>, Packet2cf>(std::complex<float>* to, const Packet2cf& from, Index stride)
160{
161 EIGEN_ALIGN16 std::complex<float> af[2];
162 pstore<std::complex<float> >((std::complex<float> *) af, from);
163 to[0*stride] = af[0];
164 to[1*stride] = af[1];
165}
166
167template<> EIGEN_STRONG_INLINE Packet2cf padd<Packet2cf>(const Packet2cf& a, const Packet2cf& b) { return Packet2cf(a.v + b.v); }
168template<> EIGEN_STRONG_INLINE Packet2cf psub<Packet2cf>(const Packet2cf& a, const Packet2cf& b) { return Packet2cf(a.v - b.v); }
169template<> EIGEN_STRONG_INLINE Packet2cf pnegate(const Packet2cf& a) { return Packet2cf(pnegate(a.v)); }
170template<> EIGEN_STRONG_INLINE Packet2cf pconj(const Packet2cf& a) { return Packet2cf(pxor<Packet4f>(a.v, reinterpret_cast<Packet4f>(p4ui_CONJ_XOR()))); }
171
172template<> EIGEN_STRONG_INLINE Packet2cf pand <Packet2cf>(const Packet2cf& a, const Packet2cf& b) { return Packet2cf(pand<Packet4f>(a.v, b.v)); }
173template<> EIGEN_STRONG_INLINE Packet2cf por <Packet2cf>(const Packet2cf& a, const Packet2cf& b) { return Packet2cf(por<Packet4f>(a.v, b.v)); }
174template<> EIGEN_STRONG_INLINE Packet2cf pxor <Packet2cf>(const Packet2cf& a, const Packet2cf& b) { return Packet2cf(pxor<Packet4f>(a.v, b.v)); }
175template<> EIGEN_STRONG_INLINE Packet2cf pandnot<Packet2cf>(const Packet2cf& a, const Packet2cf& b) { return Packet2cf(pandnot<Packet4f>(a.v, b.v)); }
176
177template<> EIGEN_STRONG_INLINE void prefetch<std::complex<float> >(const std::complex<float> * addr) { EIGEN_PPC_PREFETCH(addr); }
178
179template<> EIGEN_STRONG_INLINE std::complex<float> pfirst<Packet2cf>(const Packet2cf& a)
180{
181 EIGEN_ALIGN16 std::complex<float> res[2];
182 pstore((float *)&res, a.v);
183
184 return res[0];
185}
186
187template<> EIGEN_STRONG_INLINE Packet2cf preverse(const Packet2cf& a)
188{
189 Packet4f rev_a;
190 rev_a = vec_perm(a.v, a.v, p16uc_COMPLEX32_REV2);
191 return Packet2cf(rev_a);
192}
193
194template<> EIGEN_STRONG_INLINE std::complex<float> predux<Packet2cf>(const Packet2cf& a)
195{
196 Packet4f b;
197 b = vec_sld(a.v, a.v, 8);
198 b = padd<Packet4f>(a.v, b);
199 return pfirst<Packet2cf>(Packet2cf(b));
200}
201
202template<> EIGEN_STRONG_INLINE std::complex<float> predux_mul<Packet2cf>(const Packet2cf& a)
203{
204 Packet4f b;
205 Packet2cf prod;
206 b = vec_sld(a.v, a.v, 8);
207 prod = pmul<Packet2cf>(a, Packet2cf(b));
208
209 return pfirst<Packet2cf>(prod);
210}
211
212EIGEN_MAKE_CONJ_HELPER_CPLX_REAL(Packet2cf,Packet4f)
213
214template<> EIGEN_STRONG_INLINE Packet2cf pdiv<Packet2cf>(const Packet2cf& a, const Packet2cf& b)
215{
216 return pdiv_complex(a, b);
217}
218
219template<> EIGEN_STRONG_INLINE Packet2cf pcplxflip<Packet2cf>(const Packet2cf& x)
220{
221 return Packet2cf(vec_perm(x.v, x.v, p16uc_COMPLEX32_REV));
222}
223
224EIGEN_STRONG_INLINE void ptranspose(PacketBlock<Packet2cf,2>& kernel)
225{
226 Packet4f tmp = vec_perm(kernel.packet[0].v, kernel.packet[1].v, p16uc_TRANSPOSE64_HI);
227 kernel.packet[1].v = vec_perm(kernel.packet[0].v, kernel.packet[1].v, p16uc_TRANSPOSE64_LO);
228 kernel.packet[0].v = tmp;
229}
230
231template<> EIGEN_STRONG_INLINE Packet2cf pcmp_eq(const Packet2cf& a, const Packet2cf& b) {
232 Packet4f eq = reinterpret_cast<Packet4f>(vec_cmpeq(a.v,b.v));
233 return Packet2cf(vec_and(eq, vec_perm(eq, eq, p16uc_COMPLEX32_REV)));
234}
235
236#ifdef EIGEN_VECTORIZE_VSX
237template<> EIGEN_STRONG_INLINE Packet2cf pblend(const Selector<2>& ifPacket, const Packet2cf& thenPacket, const Packet2cf& elsePacket) {
238 Packet2cf result;
239 result.v = reinterpret_cast<Packet4f>(pblend<Packet2d>(ifPacket, reinterpret_cast<Packet2d>(thenPacket.v), reinterpret_cast<Packet2d>(elsePacket.v)));
240 return result;
241}
242
243template<> EIGEN_STRONG_INLINE Packet2cf psqrt<Packet2cf>(const Packet2cf& a)
244{
245 return psqrt_complex<Packet2cf>(a);
246}
247#endif
248
249//---------- double ----------
250#ifdef EIGEN_VECTORIZE_VSX
251struct Packet1cd
252{
253 EIGEN_STRONG_INLINE Packet1cd() {}
254 EIGEN_STRONG_INLINE explicit Packet1cd(const Packet2d& a) : v(a) {}
255
256 EIGEN_STRONG_INLINE Packet1cd pmul(const Packet1cd& a, const Packet1cd& b)
257 {
258 Packet2d a_re, a_im, v1, v2;
259
260 // Permute and multiply the real parts of a and b
261 a_re = vec_perm(a.v, a.v, p16uc_PSET64_HI);
262 // Get the imaginary parts of a
263 a_im = vec_perm(a.v, a.v, p16uc_PSET64_LO);
264 // multiply a_re * b
265 v1 = vec_madd(a_re, b.v, p2d_ZERO);
266 // multiply a_im * b and get the conjugate result
267 v2 = vec_madd(a_im, b.v, p2d_ZERO);
268 v2 = reinterpret_cast<Packet2d>(vec_sld(reinterpret_cast<Packet4ui>(v2), reinterpret_cast<Packet4ui>(v2), 8));
269 v2 = pxor(v2, reinterpret_cast<Packet2d>(p2ul_CONJ_XOR1));
270
271 return Packet1cd(padd<Packet2d>(v1, v2));
272 }
273
274 EIGEN_STRONG_INLINE Packet1cd& operator*=(const Packet1cd& b) {
275 v = pmul(Packet1cd(*this), b).v;
276 return *this;
277 }
278 EIGEN_STRONG_INLINE Packet1cd operator*(const Packet1cd& b) const {
279 return Packet1cd(*this) *= b;
280 }
281
282 EIGEN_STRONG_INLINE Packet1cd& operator+=(const Packet1cd& b) {
283 v = padd(v, b.v);
284 return *this;
285 }
286 EIGEN_STRONG_INLINE Packet1cd operator+(const Packet1cd& b) const {
287 return Packet1cd(*this) += b;
288 }
289 EIGEN_STRONG_INLINE Packet1cd& operator-=(const Packet1cd& b) {
290 v = psub(v, b.v);
291 return *this;
292 }
293 EIGEN_STRONG_INLINE Packet1cd operator-(const Packet1cd& b) const {
294 return Packet1cd(*this) -= b;
295 }
296 EIGEN_STRONG_INLINE Packet1cd operator-(void) const {
297 return Packet1cd(-v);
298 }
299
300 Packet2d v;
301};
302
303template<> struct packet_traits<std::complex<double> > : default_packet_traits
304{
305 typedef Packet1cd type;
306 typedef Packet1cd half;
307 typedef Packet2d as_real;
308 enum {
309 Vectorizable = 1,
310 AlignedOnScalar = 0,
311 size = 1,
312 HasHalfPacket = 0,
313
314 HasAdd = 1,
315 HasSub = 1,
316 HasMul = 1,
317 HasDiv = 1,
318 HasNegate = 1,
319 HasAbs = 0,
320 HasAbs2 = 0,
321 HasMin = 0,
322 HasMax = 0,
323 HasSqrt = 1,
324 HasSetLinear = 0
325 };
326};
327
328template<> struct unpacket_traits<Packet1cd> { typedef std::complex<double> type; enum {size=1, alignment=Aligned16, vectorizable=true, masked_load_available=false, masked_store_available=false}; typedef Packet1cd half; typedef Packet2d as_real; };
329
330template<> EIGEN_STRONG_INLINE Packet1cd pload <Packet1cd>(const std::complex<double>* from) { return Packet1cd(pload<Packet2d>((const double*)from)); }
331template<> EIGEN_STRONG_INLINE Packet1cd ploadu<Packet1cd>(const std::complex<double>* from) { return Packet1cd(ploadu<Packet2d>((const double*)from)); }
332template<> EIGEN_STRONG_INLINE void pstore <std::complex<double> >(std::complex<double> * to, const Packet1cd& from) { pstore((double*)to, from.v); }
333template<> EIGEN_STRONG_INLINE void pstoreu<std::complex<double> >(std::complex<double> * to, const Packet1cd& from) { pstoreu((double*)to, from.v); }
334
335template<> EIGEN_STRONG_INLINE Packet1cd pset1<Packet1cd>(const std::complex<double>& from)
336{ /* here we really have to use unaligned loads :( */ return ploadu<Packet1cd>(&from); }
337
338template<> EIGEN_DEVICE_FUNC inline Packet1cd pgather<std::complex<double>, Packet1cd>(const std::complex<double>* from, Index)
339{
340 return pload<Packet1cd>(from);
341}
342template<> EIGEN_DEVICE_FUNC inline void pscatter<std::complex<double>, Packet1cd>(std::complex<double>* to, const Packet1cd& from, Index)
343{
344 pstore<std::complex<double> >(to, from);
345}
346
347template<> EIGEN_STRONG_INLINE Packet1cd padd<Packet1cd>(const Packet1cd& a, const Packet1cd& b) { return Packet1cd(a.v + b.v); }
348template<> EIGEN_STRONG_INLINE Packet1cd psub<Packet1cd>(const Packet1cd& a, const Packet1cd& b) { return Packet1cd(a.v - b.v); }
349template<> EIGEN_STRONG_INLINE Packet1cd pnegate(const Packet1cd& a) { return Packet1cd(pnegate(Packet2d(a.v))); }
350template<> EIGEN_STRONG_INLINE Packet1cd pconj(const Packet1cd& a) { return Packet1cd(pxor(a.v, reinterpret_cast<Packet2d>(p2ul_CONJ_XOR2))); }
351
352template<> EIGEN_STRONG_INLINE Packet1cd pand <Packet1cd>(const Packet1cd& a, const Packet1cd& b) { return Packet1cd(pand(a.v,b.v)); }
353template<> EIGEN_STRONG_INLINE Packet1cd por <Packet1cd>(const Packet1cd& a, const Packet1cd& b) { return Packet1cd(por(a.v,b.v)); }
354template<> EIGEN_STRONG_INLINE Packet1cd pxor <Packet1cd>(const Packet1cd& a, const Packet1cd& b) { return Packet1cd(pxor(a.v,b.v)); }
355template<> EIGEN_STRONG_INLINE Packet1cd pandnot<Packet1cd>(const Packet1cd& a, const Packet1cd& b) { return Packet1cd(pandnot(a.v, b.v)); }
356
357template<> EIGEN_STRONG_INLINE Packet1cd ploaddup<Packet1cd>(const std::complex<double>* from) { return pset1<Packet1cd>(*from); }
358
359template<> EIGEN_STRONG_INLINE void prefetch<std::complex<double> >(const std::complex<double> * addr) { EIGEN_PPC_PREFETCH(addr); }
360
361template<> EIGEN_STRONG_INLINE std::complex<double> pfirst<Packet1cd>(const Packet1cd& a)
362{
363 EIGEN_ALIGN16 std::complex<double> res[2];
364 pstore<std::complex<double> >(res, a);
365
366 return res[0];
367}
368
369template<> EIGEN_STRONG_INLINE Packet1cd preverse(const Packet1cd& a) { return a; }
370
371template<> EIGEN_STRONG_INLINE std::complex<double> predux<Packet1cd>(const Packet1cd& a) { return pfirst(a); }
372
373template<> EIGEN_STRONG_INLINE std::complex<double> predux_mul<Packet1cd>(const Packet1cd& a) { return pfirst(a); }
374
375EIGEN_MAKE_CONJ_HELPER_CPLX_REAL(Packet1cd,Packet2d)
376
377template<> EIGEN_STRONG_INLINE Packet1cd pdiv<Packet1cd>(const Packet1cd& a, const Packet1cd& b)
378{
379 return pdiv_complex(a, b);
380}
381
382EIGEN_STRONG_INLINE Packet1cd pcplxflip/*<Packet1cd>*/(const Packet1cd& x)
383{
384 return Packet1cd(preverse(Packet2d(x.v)));
385}
386
387EIGEN_STRONG_INLINE void ptranspose(PacketBlock<Packet1cd,2>& kernel)
388{
389 Packet2d tmp = vec_perm(kernel.packet[0].v, kernel.packet[1].v, p16uc_TRANSPOSE64_HI);
390 kernel.packet[1].v = vec_perm(kernel.packet[0].v, kernel.packet[1].v, p16uc_TRANSPOSE64_LO);
391 kernel.packet[0].v = tmp;
392}
393
394template<> EIGEN_STRONG_INLINE Packet1cd pcmp_eq(const Packet1cd& a, const Packet1cd& b) {
395 // Compare real and imaginary parts of a and b to get the mask vector:
396 // [re(a)==re(b), im(a)==im(b)]
397 Packet2d eq = reinterpret_cast<Packet2d>(vec_cmpeq(a.v,b.v));
398 // Swap real/imag elements in the mask in to get:
399 // [im(a)==im(b), re(a)==re(b)]
400 Packet2d eq_swapped = reinterpret_cast<Packet2d>(vec_sld(reinterpret_cast<Packet4ui>(eq), reinterpret_cast<Packet4ui>(eq), 8));
401 // Return re(a)==re(b) & im(a)==im(b) by computing bitwise AND of eq and eq_swapped
402 return Packet1cd(vec_and(eq, eq_swapped));
403}
404
405template<> EIGEN_STRONG_INLINE Packet1cd psqrt<Packet1cd>(const Packet1cd& a)
406{
407 return psqrt_complex<Packet1cd>(a);
408}
409
410#endif // EIGEN_VECTORIZE_VSX
411} // end namespace internal
412
413} // end namespace Eigen
414
415#endif // EIGEN_COMPLEX32_ALTIVEC_H
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:50
@ Aligned16
Data pointer is aligned on a 16 bytes boundary.
Definition Constants.h:235
Namespace containing all symbols from the Eigen library.
Definition LDLT.h:16
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:74
Definition BFloat16.h:88
Definition Complex.h:33
Definition GenericPacketMath.h:43
Definition GenericPacketMath.h:107
Definition GenericPacketMath.h:133