Eigen  5.0.1-dev+master
 
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) 2018 Wave Computing, Inc.
5// Written by:
6// Chris Larsen
7// Alexey Frunze (afrunze@wavecomp.com)
8//
9// This Source Code Form is subject to the terms of the Mozilla
10// Public License v. 2.0. If a copy of the MPL was not distributed
11// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
12// SPDX-License-Identifier: MPL-2.0
13
14#ifndef EIGEN_COMPLEX_MSA_H
15#define EIGEN_COMPLEX_MSA_H
16
17#include <iostream>
18
19// IWYU pragma: private
20#include "../../InternalHeaderCheck.h"
21
22namespace Eigen {
23
24namespace internal {
25
26EIGEN_GCC_FAST_MATH_COMPLEX_VECTORIZE_WORKAROUND_PUSH
27
28//---------- float ----------
29struct Packet2cf {
30 EIGEN_STRONG_INLINE Packet2cf() {}
31 EIGEN_STRONG_INLINE explicit Packet2cf(const std::complex<float>& a, const std::complex<float>& b) {
32 Packet4f t = {std::real(a), std::imag(a), std::real(b), std::imag(b)};
33 v = t;
34 }
35 EIGEN_STRONG_INLINE explicit Packet2cf(const Packet4f& a) : v(a) {}
36 EIGEN_STRONG_INLINE Packet2cf(const Packet2cf& a) : v(a.v) {}
37 EIGEN_STRONG_INLINE Packet2cf& operator=(const Packet2cf& b) {
38 v = b.v;
39 return *this;
40 }
41 EIGEN_STRONG_INLINE Packet2cf conjugate(void) const {
42 return Packet2cf((Packet4f)__builtin_msa_bnegi_d((v2u64)v, 63));
43 }
44 EIGEN_STRONG_INLINE Packet2cf& operator*=(const Packet2cf& b) {
45 Packet4f v1, v2;
46
47 // Get the real values of a | a1_re | a1_re | a2_re | a2_re |
48 v1 = (Packet4f)__builtin_msa_ilvev_w((v4i32)v, (v4i32)v);
49 // Get the imag values of a | a1_im | a1_im | a2_im | a2_im |
50 v2 = (Packet4f)__builtin_msa_ilvod_w((v4i32)v, (v4i32)v);
51 // Multiply the real a with b
52 v1 = pmul(v1, b.v);
53 // Multiply the imag a with b
54 v2 = pmul(v2, b.v);
55 // Conjugate v2
56 v2 = Packet2cf(v2).conjugate().v;
57 // Swap real/imag elements in v2.
58 v2 = (Packet4f)__builtin_msa_shf_w((v4i32)v2, EIGEN_MSA_SHF_I8(1, 0, 3, 2));
59 // Add and return the result
60 v = padd(v1, v2);
61 return *this;
62 }
63 EIGEN_STRONG_INLINE Packet2cf operator*(const Packet2cf& b) const { return Packet2cf(*this) *= b; }
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 { return Packet2cf(*this) += b; }
69 EIGEN_STRONG_INLINE Packet2cf& operator-=(const Packet2cf& b) {
70 v = psub(v, b.v);
71 return *this;
72 }
73 EIGEN_STRONG_INLINE Packet2cf operator-(const Packet2cf& b) const { return Packet2cf(*this) -= b; }
74 EIGEN_STRONG_INLINE Packet2cf operator/(const Packet2cf& b) const { return pdiv_complex(Packet2cf(*this), b); }
75 EIGEN_STRONG_INLINE Packet2cf& operator/=(const Packet2cf& b) {
76 *this = Packet2cf(*this) / b;
77 return *this;
78 }
79 EIGEN_STRONG_INLINE Packet2cf operator-(void) const { return Packet2cf(pnegate(v)); }
80
81 Packet4f v;
82};
83
84inline std::ostream& operator<<(std::ostream& os, const Packet2cf& value) {
85 os << "[ (" << value.v[0] << ", " << value.v[1]
86 << "i),"
87 " ("
88 << value.v[2] << ", " << value.v[3] << "i) ]";
89 return os;
90}
91
92template <>
93struct packet_traits<std::complex<float> > : default_packet_traits {
94 using type = Packet2cf;
95 using half = Packet2cf;
96 enum {
97 Vectorizable = 1,
98 AlignedOnScalar = 1,
99 size = 2,
100
101 HasAdd = 1,
102 HasSub = 1,
103 HasMul = 1,
104 HasDiv = 1,
105 HasNegate = 1,
106 HasAbs = 0,
107 HasAbs2 = 0,
108 HasMin = 0,
109 HasMax = 0,
110 HasSetLinear = 0,
111 };
112};
113
114template <>
115struct unpacket_traits<Packet2cf> {
116 using type = std::complex<float>;
117 using as_real = Packet4f;
118 enum {
119 size = 2,
120 alignment = Aligned16,
121 vectorizable = true,
122 masked_load_available = false,
123 masked_store_available = false
124 };
125 using half = Packet2cf;
126};
127
128template <>
129EIGEN_STRONG_INLINE Packet2cf pset1<Packet2cf>(const std::complex<float>& from) {
130 EIGEN_MSA_DEBUG;
131
132 float f0 = from.real(), f1 = from.imag();
133 Packet4f v0 = {f0, f0, f0, f0};
134 Packet4f v1 = {f1, f1, f1, f1};
135 return Packet2cf((Packet4f)__builtin_msa_ilvr_w((Packet4i)v1, (Packet4i)v0));
136}
137
138template <>
139EIGEN_STRONG_INLINE Packet2cf padd<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
140 EIGEN_MSA_DEBUG;
141
142 return a + b;
143}
144
145template <>
146EIGEN_STRONG_INLINE Packet2cf psub<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
147 EIGEN_MSA_DEBUG;
148
149 return a - b;
150}
151
152template <>
153EIGEN_STRONG_INLINE Packet2cf pnegate(const Packet2cf& a) {
154 EIGEN_MSA_DEBUG;
155
156 return -a;
157}
158
159template <>
160EIGEN_STRONG_INLINE Packet2cf pconj(const Packet2cf& a) {
161 EIGEN_MSA_DEBUG;
162
163 return a.conjugate();
164}
165
166template <>
167EIGEN_STRONG_INLINE Packet2cf pmul<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
168 EIGEN_MSA_DEBUG;
169
170 return a * b;
171}
172
173template <>
174EIGEN_STRONG_INLINE Packet2cf pand<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
175 EIGEN_MSA_DEBUG;
176
177 return Packet2cf(pand(a.v, b.v));
178}
179
180template <>
181EIGEN_STRONG_INLINE Packet2cf por<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
182 EIGEN_MSA_DEBUG;
183
184 return Packet2cf(por(a.v, b.v));
185}
186
187template <>
188EIGEN_STRONG_INLINE Packet2cf pxor<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
189 EIGEN_MSA_DEBUG;
190
191 return Packet2cf(pxor(a.v, b.v));
192}
193
194template <>
195EIGEN_STRONG_INLINE Packet2cf pandnot<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
196 EIGEN_MSA_DEBUG;
197
198 return Packet2cf(pandnot(a.v, b.v));
199}
200
201template <>
202EIGEN_STRONG_INLINE Packet2cf pload<Packet2cf>(const std::complex<float>* from) {
203 EIGEN_MSA_DEBUG;
204
205 EIGEN_DEBUG_ALIGNED_LOAD return Packet2cf(pload<Packet4f>((const float*)from));
206}
207
208template <>
209EIGEN_STRONG_INLINE Packet2cf ploadu<Packet2cf>(const std::complex<float>* from) {
210 EIGEN_MSA_DEBUG;
211
212 EIGEN_DEBUG_UNALIGNED_LOAD return Packet2cf(ploadu<Packet4f>((const float*)from));
213}
214
215template <>
216EIGEN_STRONG_INLINE Packet2cf ploaddup<Packet2cf>(const std::complex<float>* from) {
217 EIGEN_MSA_DEBUG;
218
219 return pset1<Packet2cf>(*from);
220}
221
222template <>
223EIGEN_STRONG_INLINE void pstore<std::complex<float> >(std::complex<float>* to, const Packet2cf& from) {
224 EIGEN_MSA_DEBUG;
225
226 EIGEN_DEBUG_ALIGNED_STORE pstore<float>((float*)to, from.v);
227}
228
229template <>
230EIGEN_STRONG_INLINE void pstoreu<std::complex<float> >(std::complex<float>* to, const Packet2cf& from) {
231 EIGEN_MSA_DEBUG;
232
233 EIGEN_DEBUG_UNALIGNED_STORE pstoreu<float>((float*)to, from.v);
234}
235
236template <>
237EIGEN_DEVICE_FUNC inline Packet2cf pgather<std::complex<float>, Packet2cf>(const std::complex<float>* from,
238 Index stride) {
239 EIGEN_MSA_DEBUG;
240
241 return Packet2cf(from[0 * stride], from[1 * stride]);
242}
243
244template <>
245EIGEN_DEVICE_FUNC inline void pscatter<std::complex<float>, Packet2cf>(std::complex<float>* to, const Packet2cf& from,
246 Index stride) {
247 EIGEN_MSA_DEBUG;
248
249 *to = std::complex<float>(from.v[0], from.v[1]);
250 to += stride;
251 *to = std::complex<float>(from.v[2], from.v[3]);
252}
253
254template <>
255EIGEN_STRONG_INLINE void prefetch<std::complex<float> >(const std::complex<float>* addr) {
256 EIGEN_MSA_DEBUG;
257
258 prefetch(reinterpret_cast<const float*>(addr));
259}
260
261template <>
262EIGEN_STRONG_INLINE std::complex<float> pfirst<Packet2cf>(const Packet2cf& a) {
263 EIGEN_MSA_DEBUG;
264
265 return std::complex<float>(a.v[0], a.v[1]);
266}
267
268template <>
269EIGEN_STRONG_INLINE Packet2cf preverse(const Packet2cf& a) {
270 EIGEN_MSA_DEBUG;
271
272 return Packet2cf((Packet4f)__builtin_msa_shf_w((v4i32)a.v, EIGEN_MSA_SHF_I8(2, 3, 0, 1)));
273}
274
275template <>
276EIGEN_STRONG_INLINE Packet2cf pcplxflip<Packet2cf>(const Packet2cf& a) {
277 EIGEN_MSA_DEBUG;
278
279 return Packet2cf((Packet4f)__builtin_msa_shf_w((v4i32)a.v, EIGEN_MSA_SHF_I8(1, 0, 3, 2)));
280}
281
282template <>
283EIGEN_STRONG_INLINE std::complex<float> predux<Packet2cf>(const Packet2cf& a) {
284 EIGEN_MSA_DEBUG;
285
286 Packet4f value = (Packet4f)preverse((Packet2d)a.v);
287 value += a.v;
288 return std::complex<float>(value[0], value[1]);
289}
290
291template <>
292EIGEN_STRONG_INLINE std::complex<float> predux_mul<Packet2cf>(const Packet2cf& a) {
293 EIGEN_MSA_DEBUG;
294
295 return std::complex<float>((a.v[0] * a.v[2]) - (a.v[1] * a.v[3]), (a.v[0] * a.v[3]) + (a.v[1] * a.v[2]));
296}
297
298EIGEN_MAKE_CONJ_HELPER_CPLX_REAL(Packet2cf, Packet4f)
299
300template <>
301EIGEN_STRONG_INLINE Packet2cf pdiv<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
302 EIGEN_MSA_DEBUG;
303
304 return a / b;
305}
306
307inline std::ostream& operator<<(std::ostream& os, const PacketBlock<Packet2cf, 2>& value) {
308 os << "[ " << value.packet[0] << ", " << std::endl << " " << value.packet[1] << " ]";
309 return os;
310}
311
312EIGEN_DEVICE_FUNC inline void ptranspose(PacketBlock<Packet2cf, 2>& kernel) {
313 EIGEN_MSA_DEBUG;
314
315 Packet4f tmp = (Packet4f)__builtin_msa_ilvl_d((v2i64)kernel.packet[1].v, (v2i64)kernel.packet[0].v);
316 kernel.packet[0].v = (Packet4f)__builtin_msa_ilvr_d((v2i64)kernel.packet[1].v, (v2i64)kernel.packet[0].v);
317 kernel.packet[1].v = tmp;
318}
319
320//---------- double ----------
321
322struct Packet1cd {
323 EIGEN_STRONG_INLINE Packet1cd() {}
324 EIGEN_STRONG_INLINE explicit Packet1cd(const std::complex<double>& a) {
325 v[0] = std::real(a);
326 v[1] = std::imag(a);
327 }
328 EIGEN_STRONG_INLINE explicit Packet1cd(const Packet2d& a) : v(a) {}
329 EIGEN_STRONG_INLINE Packet1cd(const Packet1cd& a) : v(a.v) {}
330 EIGEN_STRONG_INLINE Packet1cd& operator=(const Packet1cd& b) {
331 v = b.v;
332 return *this;
333 }
334 EIGEN_STRONG_INLINE Packet1cd conjugate(void) const {
335 static const v2u64 p2ul_CONJ_XOR = {0x0, 0x8000000000000000};
336 return (Packet1cd)pxor(v, (Packet2d)p2ul_CONJ_XOR);
337 }
338 EIGEN_STRONG_INLINE Packet1cd& operator*=(const Packet1cd& b) {
339 Packet2d v1, v2;
340
341 // Get the real values of a | a1_re | a1_re
342 v1 = (Packet2d)__builtin_msa_ilvev_d((v2i64)v, (v2i64)v);
343 // Get the imag values of a | a1_im | a1_im
344 v2 = (Packet2d)__builtin_msa_ilvod_d((v2i64)v, (v2i64)v);
345 // Multiply the real a with b
346 v1 = pmul(v1, b.v);
347 // Multiply the imag a with b
348 v2 = pmul(v2, b.v);
349 // Conjugate v2
350 v2 = Packet1cd(v2).conjugate().v;
351 // Swap real/imag elements in v2.
352 v2 = (Packet2d)__builtin_msa_shf_w((v4i32)v2, EIGEN_MSA_SHF_I8(2, 3, 0, 1));
353 // Add and return the result
354 v = padd(v1, v2);
355 return *this;
356 }
357 EIGEN_STRONG_INLINE Packet1cd operator*(const Packet1cd& b) const { return Packet1cd(*this) *= b; }
358 EIGEN_STRONG_INLINE Packet1cd& operator+=(const Packet1cd& b) {
359 v = padd(v, b.v);
360 return *this;
361 }
362 EIGEN_STRONG_INLINE Packet1cd operator+(const Packet1cd& b) const { return Packet1cd(*this) += b; }
363 EIGEN_STRONG_INLINE Packet1cd& operator-=(const Packet1cd& b) {
364 v = psub(v, b.v);
365 return *this;
366 }
367 EIGEN_STRONG_INLINE Packet1cd operator-(const Packet1cd& b) const { return Packet1cd(*this) -= b; }
368 EIGEN_STRONG_INLINE Packet1cd& operator/=(const Packet1cd& b) {
369 *this *= b.conjugate();
370 Packet2d s = pmul<Packet2d>(b.v, b.v);
371 s = padd(s, preverse<Packet2d>(s));
372 v = pdiv(v, s);
373 return *this;
374 }
375 EIGEN_STRONG_INLINE Packet1cd operator/(const Packet1cd& b) const { return Packet1cd(*this) /= b; }
376 EIGEN_STRONG_INLINE Packet1cd operator-(void) const { return Packet1cd(pnegate(v)); }
377
378 Packet2d v;
379};
380
381inline std::ostream& operator<<(std::ostream& os, const Packet1cd& value) {
382 os << "[ (" << value.v[0] << ", " << value.v[1] << "i) ]";
383 return os;
384}
385
386template <>
387struct packet_traits<std::complex<double> > : default_packet_traits {
388 using type = Packet1cd;
389 using half = Packet1cd;
390 enum {
391 Vectorizable = 1,
392 AlignedOnScalar = 0,
393 size = 1,
394
395 HasAdd = 1,
396 HasSub = 1,
397 HasMul = 1,
398 HasDiv = 1,
399 HasNegate = 1,
400 HasAbs = 0,
401 HasAbs2 = 0,
402 HasMin = 0,
403 HasMax = 0,
404 HasSetLinear = 0
405 };
406};
407
408template <>
409struct unpacket_traits<Packet1cd> {
410 using type = std::complex<double>;
411 using as_real = Packet2d;
412 enum {
413 size = 1,
414 alignment = Aligned16,
415 vectorizable = true,
416 masked_load_available = false,
417 masked_store_available = false
418 };
419 using half = Packet1cd;
420};
421
422template <>
423EIGEN_STRONG_INLINE Packet1cd pload<Packet1cd>(const std::complex<double>* from) {
424 EIGEN_MSA_DEBUG;
425
426 EIGEN_DEBUG_ALIGNED_LOAD return Packet1cd(pload<Packet2d>((const double*)from));
427}
428
429template <>
430EIGEN_STRONG_INLINE Packet1cd ploadu<Packet1cd>(const std::complex<double>* from) {
431 EIGEN_MSA_DEBUG;
432
433 EIGEN_DEBUG_UNALIGNED_LOAD return Packet1cd(ploadu<Packet2d>((const double*)from));
434}
435
436template <>
437EIGEN_STRONG_INLINE Packet1cd pset1<Packet1cd>(const std::complex<double>& from) {
438 EIGEN_MSA_DEBUG;
439
440 return Packet1cd(from);
441}
442
443template <>
444EIGEN_STRONG_INLINE Packet1cd padd<Packet1cd>(const Packet1cd& a, const Packet1cd& b) {
445 EIGEN_MSA_DEBUG;
446
447 return a + b;
448}
449
450template <>
451EIGEN_STRONG_INLINE Packet1cd psub<Packet1cd>(const Packet1cd& a, const Packet1cd& b) {
452 EIGEN_MSA_DEBUG;
453
454 return a - b;
455}
456
457template <>
458EIGEN_STRONG_INLINE Packet1cd pnegate(const Packet1cd& a) {
459 EIGEN_MSA_DEBUG;
460
461 return -a;
462}
463
464template <>
465EIGEN_STRONG_INLINE Packet1cd pconj(const Packet1cd& a) {
466 EIGEN_MSA_DEBUG;
467
468 return a.conjugate();
469}
470
471template <>
472EIGEN_STRONG_INLINE Packet1cd pmul<Packet1cd>(const Packet1cd& a, const Packet1cd& b) {
473 EIGEN_MSA_DEBUG;
474
475 return a * b;
476}
477
478template <>
479EIGEN_STRONG_INLINE Packet1cd pand<Packet1cd>(const Packet1cd& a, const Packet1cd& b) {
480 EIGEN_MSA_DEBUG;
481
482 return Packet1cd(pand(a.v, b.v));
483}
484
485template <>
486EIGEN_STRONG_INLINE Packet1cd por<Packet1cd>(const Packet1cd& a, const Packet1cd& b) {
487 EIGEN_MSA_DEBUG;
488
489 return Packet1cd(por(a.v, b.v));
490}
491
492template <>
493EIGEN_STRONG_INLINE Packet1cd pxor<Packet1cd>(const Packet1cd& a, const Packet1cd& b) {
494 EIGEN_MSA_DEBUG;
495
496 return Packet1cd(pxor(a.v, b.v));
497}
498
499template <>
500EIGEN_STRONG_INLINE Packet1cd pandnot<Packet1cd>(const Packet1cd& a, const Packet1cd& b) {
501 EIGEN_MSA_DEBUG;
502
503 return Packet1cd(pandnot(a.v, b.v));
504}
505
506template <>
507EIGEN_STRONG_INLINE Packet1cd ploaddup<Packet1cd>(const std::complex<double>* from) {
508 EIGEN_MSA_DEBUG;
509
510 return pset1<Packet1cd>(*from);
511}
512
513template <>
514EIGEN_STRONG_INLINE void pstore<std::complex<double> >(std::complex<double>* to, const Packet1cd& from) {
515 EIGEN_MSA_DEBUG;
516
517 EIGEN_DEBUG_ALIGNED_STORE pstore<double>((double*)to, from.v);
518}
519
520template <>
521EIGEN_STRONG_INLINE void pstoreu<std::complex<double> >(std::complex<double>* to, const Packet1cd& from) {
522 EIGEN_MSA_DEBUG;
523
524 EIGEN_DEBUG_UNALIGNED_STORE pstoreu<double>((double*)to, from.v);
525}
526
527template <>
528EIGEN_STRONG_INLINE void prefetch<std::complex<double> >(const std::complex<double>* addr) {
529 EIGEN_MSA_DEBUG;
530
531 prefetch(reinterpret_cast<const double*>(addr));
532}
533
534template <>
535EIGEN_DEVICE_FUNC inline Packet1cd pgather<std::complex<double>, Packet1cd>(const std::complex<double>* from,
536 Index stride __attribute__((unused))) {
537 EIGEN_MSA_DEBUG;
538
539 Packet1cd res;
540 res.v[0] = std::real(from[0]);
541 res.v[1] = std::imag(from[0]);
542 return res;
543}
544
545template <>
546EIGEN_DEVICE_FUNC inline void pscatter<std::complex<double>, Packet1cd>(std::complex<double>* to, const Packet1cd& from,
547 Index stride __attribute__((unused))) {
548 EIGEN_MSA_DEBUG;
549
550 pstore(to, from);
551}
552
553template <>
554EIGEN_STRONG_INLINE std::complex<double> pfirst<Packet1cd>(const Packet1cd& a) {
555 EIGEN_MSA_DEBUG;
556
557 return std::complex<double>(a.v[0], a.v[1]);
558}
559
560EIGEN_MAKE_CONJ_HELPER_CPLX_REAL(Packet1cd, Packet2d)
561
562template <>
563EIGEN_STRONG_INLINE Packet1cd pdiv<Packet1cd>(const Packet1cd& a, const Packet1cd& b) {
564 EIGEN_MSA_DEBUG;
565
566 return a / b;
567}
568
569EIGEN_STRONG_INLINE Packet1cd pcplxflip /*<Packet1cd>*/ (const Packet1cd& x) {
570 EIGEN_MSA_DEBUG;
571
572 return Packet1cd(preverse(Packet2d(x.v)));
573}
574
575inline std::ostream& operator<<(std::ostream& os, const PacketBlock<Packet1cd, 2>& value) {
576 os << "[ " << value.packet[0] << ", " << std::endl << " " << value.packet[1] << " ]";
577 return os;
578}
579
580EIGEN_STRONG_INLINE void ptranspose(PacketBlock<Packet1cd, 2>& kernel) {
581 EIGEN_MSA_DEBUG;
582
583 Packet2d v1, v2;
584
585 v1 = (Packet2d)__builtin_msa_ilvev_d((v2i64)kernel.packet[0].v, (v2i64)kernel.packet[1].v);
586 // Get the imag values of a
587 v2 = (Packet2d)__builtin_msa_ilvod_d((v2i64)kernel.packet[0].v, (v2i64)kernel.packet[1].v);
588
589 kernel.packet[0].v = v1;
590 kernel.packet[1].v = v2;
591}
592
593EIGEN_GCC_FAST_MATH_COMPLEX_VECTORIZE_WORKAROUND_POP
594
595} // end namespace internal
596
597} // end namespace Eigen
598
599#endif // EIGEN_COMPLEX_MSA_H
@ Aligned16
Definition Constants.h:238