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