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