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//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10#ifndef EIGEN_COMPLEX_SSE_H
11#define EIGEN_COMPLEX_SSE_H
12
13// IWYU pragma: private
14#include "../../InternalHeaderCheck.h"
15
16namespace Eigen {
17
18namespace internal {
19
20//---------- float ----------
21struct Packet2cf {
22 EIGEN_STRONG_INLINE Packet2cf() {}
23 EIGEN_STRONG_INLINE explicit Packet2cf(const __m128& a) : v(a) {}
24 Packet4f v;
25};
26
27// Use the packet_traits defined in AVX/PacketMath.h instead if we're going
28// to leverage AVX instructions.
29#ifndef EIGEN_VECTORIZE_AVX
30template <>
31struct packet_traits<std::complex<float> > : default_packet_traits {
32 typedef Packet2cf type;
33 typedef Packet2cf half;
34 enum {
35 Vectorizable = 1,
36 AlignedOnScalar = 1,
37 size = 2,
38
39 HasAdd = 1,
40 HasSub = 1,
41 HasMul = 1,
42 HasDiv = 1,
43 HasNegate = 1,
44 HasSqrt = 1,
45 HasLog = 1,
46 HasExp = 1,
47 HasAbs = 0,
48 HasAbs2 = 0,
49 HasMin = 0,
50 HasMax = 0,
51 HasSetLinear = 0,
52 };
53};
54#endif
55
56template <>
57struct unpacket_traits<Packet2cf> {
58 typedef std::complex<float> type;
59 typedef Packet2cf half;
60 typedef Packet4f as_real;
61 enum {
62 size = 2,
63 alignment = Aligned16,
64 vectorizable = true,
65 masked_load_available = false,
66 masked_store_available = false
67 };
68};
69
70template <>
71EIGEN_STRONG_INLINE Packet2cf padd<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
72 return Packet2cf(_mm_add_ps(a.v, b.v));
73}
74template <>
75EIGEN_STRONG_INLINE Packet2cf psub<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
76 return Packet2cf(_mm_sub_ps(a.v, b.v));
77}
78
79template <>
80EIGEN_STRONG_INLINE Packet2cf pnegate(const Packet2cf& a) {
81 const __m128 mask = _mm_castsi128_ps(_mm_setr_epi32(0x80000000, 0x80000000, 0x80000000, 0x80000000));
82 return Packet2cf(_mm_xor_ps(a.v, mask));
83}
84template <>
85EIGEN_STRONG_INLINE Packet2cf pconj(const Packet2cf& a) {
86 const __m128 mask = _mm_castsi128_ps(_mm_setr_epi32(0x00000000, 0x80000000, 0x00000000, 0x80000000));
87 return Packet2cf(_mm_xor_ps(a.v, mask));
88}
89
90template <>
91EIGEN_STRONG_INLINE Packet2cf pmul(const Packet2cf& a, const Packet2cf& b) {
92#ifdef EIGEN_VECTORIZE_SSE3
93 __m128 tmp1 = _mm_mul_ps(_mm_movehdup_ps(a.v), vec4f_swizzle1(b.v, 1, 0, 3, 2));
94 __m128 tmp2 = _mm_moveldup_ps(a.v);
95#else
96 __m128 tmp1 = _mm_mul_ps(vec4f_swizzle1(a.v, 1, 1, 3, 3), vec4f_swizzle1(b.v, 1, 0, 3, 2));
97 __m128 tmp2 = vec4f_swizzle1(a.v, 0, 0, 2, 2);
98#endif
99#ifdef EIGEN_VECTORIZE_FMA
100 __m128 result = _mm_fmaddsub_ps(tmp2, b.v, tmp1);
101#else
102#ifdef EIGEN_VECTORIZE_SSE3
103 __m128 result = _mm_addsub_ps(_mm_mul_ps(tmp2, b.v), tmp1);
104#else
105 const __m128 mask = _mm_setr_ps(-0.0f, 0.0f, -0.0f, 0.0f);
106 __m128 result = _mm_add_ps(_mm_mul_ps(tmp2, b.v), _mm_xor_ps(tmp1, mask));
107#endif
108#endif
109 return Packet2cf(result);
110}
111
112template <>
113EIGEN_STRONG_INLINE Packet2cf ptrue<Packet2cf>(const Packet2cf& a) {
114 return Packet2cf(ptrue(Packet4f(a.v)));
115}
116template <>
117EIGEN_STRONG_INLINE Packet2cf pand<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
118 return Packet2cf(_mm_and_ps(a.v, b.v));
119}
120template <>
121EIGEN_STRONG_INLINE Packet2cf por<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
122 return Packet2cf(_mm_or_ps(a.v, b.v));
123}
124template <>
125EIGEN_STRONG_INLINE Packet2cf pxor<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
126 return Packet2cf(_mm_xor_ps(a.v, b.v));
127}
128template <>
129EIGEN_STRONG_INLINE Packet2cf pandnot<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
130 return Packet2cf(_mm_andnot_ps(b.v, a.v));
131}
132
133template <>
134EIGEN_STRONG_INLINE Packet2cf pload<Packet2cf>(const std::complex<float>* from) {
135 EIGEN_DEBUG_ALIGNED_LOAD return Packet2cf(_mm_load_ps(&numext::real_ref(*from)));
136}
137template <>
138EIGEN_STRONG_INLINE Packet2cf ploadu<Packet2cf>(const std::complex<float>* from) {
139 EIGEN_DEBUG_UNALIGNED_LOAD return Packet2cf(_mm_loadu_ps(&numext::real_ref(*from)));
140}
141
142template <>
143EIGEN_STRONG_INLINE Packet2cf pset1<Packet2cf>(const std::complex<float>& from) {
144 const float re = std::real(from);
145 const float im = std::imag(from);
146 return Packet2cf(_mm_set_ps(im, re, im, re));
147}
148
149template <>
150EIGEN_STRONG_INLINE Packet2cf ploaddup<Packet2cf>(const std::complex<float>* from) {
151 return pset1<Packet2cf>(*from);
152}
153
154template <>
155EIGEN_STRONG_INLINE void pstore<std::complex<float> >(std::complex<float>* to, const Packet2cf& from) {
156 EIGEN_DEBUG_ALIGNED_STORE _mm_store_ps(&numext::real_ref(*to), from.v);
157}
158template <>
159EIGEN_STRONG_INLINE void pstoreu<std::complex<float> >(std::complex<float>* to, const Packet2cf& from) {
160 EIGEN_DEBUG_UNALIGNED_STORE _mm_storeu_ps(&numext::real_ref(*to), from.v);
161}
162
163template <>
164EIGEN_DEVICE_FUNC inline Packet2cf pgather<std::complex<float>, Packet2cf>(const std::complex<float>* from,
165 Index stride) {
166 return Packet2cf(_mm_set_ps(std::imag(from[1 * stride]), std::real(from[1 * stride]), std::imag(from[0 * stride]),
167 std::real(from[0 * stride])));
168}
169
170template <>
171EIGEN_DEVICE_FUNC inline void pscatter<std::complex<float>, Packet2cf>(std::complex<float>* to, const Packet2cf& from,
172 Index stride) {
173 to[stride * 0] = std::complex<float>(_mm_cvtss_f32(_mm_shuffle_ps(from.v, from.v, 0)),
174 _mm_cvtss_f32(_mm_shuffle_ps(from.v, from.v, 1)));
175 to[stride * 1] = std::complex<float>(_mm_cvtss_f32(_mm_shuffle_ps(from.v, from.v, 2)),
176 _mm_cvtss_f32(_mm_shuffle_ps(from.v, from.v, 3)));
177}
178
179template <>
180EIGEN_STRONG_INLINE void prefetch<std::complex<float> >(const std::complex<float>* addr) {
181 _mm_prefetch((SsePrefetchPtrType)(addr), _MM_HINT_T0);
182}
183
184template <>
185EIGEN_STRONG_INLINE std::complex<float> pfirst<Packet2cf>(const Packet2cf& a) {
186 alignas(alignof(__m64)) std::complex<float> res;
187 _mm_storel_pi((__m64*)&res, a.v);
188 return res;
189}
190
191template <>
192EIGEN_STRONG_INLINE Packet2cf preverse(const Packet2cf& a) {
193 return Packet2cf(_mm_castpd_ps(preverse(Packet2d(_mm_castps_pd(a.v)))));
194}
195
196template <>
197EIGEN_STRONG_INLINE std::complex<float> predux<Packet2cf>(const Packet2cf& a) {
198 return pfirst(Packet2cf(_mm_add_ps(a.v, _mm_movehl_ps(a.v, a.v))));
199}
200
201template <>
202EIGEN_STRONG_INLINE std::complex<float> predux_mul<Packet2cf>(const Packet2cf& a) {
203 return pfirst(pmul(a, Packet2cf(_mm_movehl_ps(a.v, a.v))));
204}
205
206EIGEN_STRONG_INLINE Packet2cf pcplxflip /* <Packet2cf> */ (const Packet2cf& x) {
207 return Packet2cf(vec4f_swizzle1(x.v, 1, 0, 3, 2));
208}
209
210EIGEN_MAKE_CONJ_HELPER_CPLX_REAL(Packet2cf, Packet4f)
211
212template <>
213EIGEN_STRONG_INLINE Packet2cf pdiv<Packet2cf>(const Packet2cf& a, const Packet2cf& b) {
214 return pdiv_complex(a, b);
215}
216
217//---------- double ----------
218struct Packet1cd {
219 EIGEN_STRONG_INLINE Packet1cd() {}
220 EIGEN_STRONG_INLINE explicit Packet1cd(const __m128d& a) : v(a) {}
221 Packet2d v;
222};
223
224// Use the packet_traits defined in AVX/PacketMath.h instead if we're going
225// to leverage AVX instructions.
226#ifndef EIGEN_VECTORIZE_AVX
227template <>
228struct packet_traits<std::complex<double> > : default_packet_traits {
229 typedef Packet1cd type;
230 typedef Packet1cd half;
231 enum {
232 Vectorizable = 1,
233 AlignedOnScalar = 0,
234 size = 1,
235
236 HasAdd = 1,
237 HasSub = 1,
238 HasMul = 1,
239 HasDiv = 1,
240 HasNegate = 1,
241 HasSqrt = 1,
242 HasLog = 1,
243 HasAbs = 0,
244 HasAbs2 = 0,
245 HasMin = 0,
246 HasMax = 0,
247 HasSetLinear = 0
248 };
249};
250#endif
251
252template <>
253struct unpacket_traits<Packet1cd> {
254 typedef std::complex<double> type;
255 typedef Packet1cd half;
256 typedef Packet2d as_real;
257 enum {
258 size = 1,
259 alignment = Aligned16,
260 vectorizable = true,
261 masked_load_available = false,
262 masked_store_available = false
263 };
264};
265
266template <>
267EIGEN_STRONG_INLINE Packet1cd padd<Packet1cd>(const Packet1cd& a, const Packet1cd& b) {
268 return Packet1cd(_mm_add_pd(a.v, b.v));
269}
270template <>
271EIGEN_STRONG_INLINE Packet1cd psub<Packet1cd>(const Packet1cd& a, const Packet1cd& b) {
272 return Packet1cd(_mm_sub_pd(a.v, b.v));
273}
274template <>
275EIGEN_STRONG_INLINE Packet1cd pnegate(const Packet1cd& a) {
276 return Packet1cd(pnegate(Packet2d(a.v)));
277}
278template <>
279EIGEN_STRONG_INLINE Packet1cd pconj(const Packet1cd& a) {
280 const __m128d mask = _mm_castsi128_pd(_mm_set_epi32(0x80000000, 0x0, 0x0, 0x0));
281 return Packet1cd(_mm_xor_pd(a.v, mask));
282}
283
284template <>
285EIGEN_STRONG_INLINE Packet1cd pmul(const Packet1cd& a, const Packet1cd& b) {
286 __m128d tmp1 = _mm_mul_pd(_mm_unpackhi_pd(a.v, a.v), vec2d_swizzle1(b.v, 1, 0));
287#ifdef EIGEN_VECTORIZE_SSE3
288 __m128d tmp2 = _mm_movedup_pd(a.v);
289#else
290 __m128d tmp2 = _mm_unpacklo_pd(a.v, a.v);
291#endif
292#ifdef EIGEN_VECTORIZE_FMA
293 __m128d result = _mm_fmaddsub_pd(tmp2, b.v, tmp1);
294#else
295#ifdef EIGEN_VECTORIZE_SSE3
296 __m128d result = _mm_addsub_pd(_mm_mul_pd(tmp2, b.v), tmp1);
297#else
298 const __m128d mask = _mm_setr_pd(-0.0, 0.0);
299 __m128d result = _mm_add_pd(_mm_mul_pd(tmp2, b.v), _mm_xor_pd(tmp1, mask));
300#endif
301#endif
302 return Packet1cd(result);
303}
304
305template <>
306EIGEN_STRONG_INLINE Packet1cd ptrue<Packet1cd>(const Packet1cd& a) {
307 return Packet1cd(ptrue(Packet2d(a.v)));
308}
309template <>
310EIGEN_STRONG_INLINE Packet1cd pand<Packet1cd>(const Packet1cd& a, const Packet1cd& b) {
311 return Packet1cd(_mm_and_pd(a.v, b.v));
312}
313template <>
314EIGEN_STRONG_INLINE Packet1cd por<Packet1cd>(const Packet1cd& a, const Packet1cd& b) {
315 return Packet1cd(_mm_or_pd(a.v, b.v));
316}
317template <>
318EIGEN_STRONG_INLINE Packet1cd pxor<Packet1cd>(const Packet1cd& a, const Packet1cd& b) {
319 return Packet1cd(_mm_xor_pd(a.v, b.v));
320}
321template <>
322EIGEN_STRONG_INLINE Packet1cd pandnot<Packet1cd>(const Packet1cd& a, const Packet1cd& b) {
323 return Packet1cd(_mm_andnot_pd(b.v, a.v));
324}
325
326// FIXME force unaligned load, this is a temporary fix
327template <>
328EIGEN_STRONG_INLINE Packet1cd pload<Packet1cd>(const std::complex<double>* from) {
329 EIGEN_DEBUG_ALIGNED_LOAD return Packet1cd(_mm_load_pd((const double*)from));
330}
331template <>
332EIGEN_STRONG_INLINE Packet1cd ploadu<Packet1cd>(const std::complex<double>* from) {
333 EIGEN_DEBUG_UNALIGNED_LOAD return Packet1cd(_mm_loadu_pd((const double*)from));
334}
335template <>
336EIGEN_STRONG_INLINE Packet1cd
337pset1<Packet1cd>(const std::complex<double>& from) { /* here we really have to use unaligned loads :( */
338 return ploadu<Packet1cd>(&from);
339}
340
341template <>
342EIGEN_STRONG_INLINE Packet1cd ploaddup<Packet1cd>(const std::complex<double>* from) {
343 return pset1<Packet1cd>(*from);
344}
345
346// FIXME force unaligned store, this is a temporary fix
347template <>
348EIGEN_STRONG_INLINE void pstore<std::complex<double> >(std::complex<double>* to, const Packet1cd& from) {
349 EIGEN_DEBUG_ALIGNED_STORE _mm_store_pd((double*)to, from.v);
350}
351template <>
352EIGEN_STRONG_INLINE void pstoreu<std::complex<double> >(std::complex<double>* to, const Packet1cd& from) {
353 EIGEN_DEBUG_UNALIGNED_STORE _mm_storeu_pd((double*)to, from.v);
354}
355
356template <>
357EIGEN_STRONG_INLINE void prefetch<std::complex<double> >(const std::complex<double>* addr) {
358 _mm_prefetch((SsePrefetchPtrType)(addr), _MM_HINT_T0);
359}
360
361template <>
362EIGEN_STRONG_INLINE std::complex<double> pfirst<Packet1cd>(const Packet1cd& a) {
363 EIGEN_ALIGN16 double res[2];
364 _mm_store_pd(res, a.v);
365 return std::complex<double>(res[0], res[1]);
366}
367
368template <>
369EIGEN_STRONG_INLINE Packet1cd preverse(const Packet1cd& a) {
370 return a;
371}
372
373template <>
374EIGEN_STRONG_INLINE std::complex<double> predux<Packet1cd>(const Packet1cd& a) {
375 return pfirst(a);
376}
377
378template <>
379EIGEN_STRONG_INLINE std::complex<double> predux_mul<Packet1cd>(const Packet1cd& a) {
380 return pfirst(a);
381}
382
383EIGEN_MAKE_CONJ_HELPER_CPLX_REAL(Packet1cd, Packet2d)
384
385template <>
386EIGEN_STRONG_INLINE Packet1cd pdiv<Packet1cd>(const Packet1cd& a, const Packet1cd& b) {
387 return pdiv_complex(a, b);
388}
389
390EIGEN_STRONG_INLINE Packet1cd pcplxflip /* <Packet1cd> */ (const Packet1cd& x) {
391 return Packet1cd(preverse(Packet2d(x.v)));
392}
393
394EIGEN_DEVICE_FUNC inline void ptranspose(PacketBlock<Packet2cf, 2>& kernel) {
395 __m128d w1 = _mm_castps_pd(kernel.packet[0].v);
396 __m128d w2 = _mm_castps_pd(kernel.packet[1].v);
397
398 __m128 tmp = _mm_castpd_ps(_mm_unpackhi_pd(w1, w2));
399 kernel.packet[0].v = _mm_castpd_ps(_mm_unpacklo_pd(w1, w2));
400 kernel.packet[1].v = tmp;
401}
402
403template <>
404EIGEN_STRONG_INLINE Packet2cf pcmp_eq(const Packet2cf& a, const Packet2cf& b) {
405 __m128 eq = _mm_cmpeq_ps(a.v, b.v);
406 return Packet2cf(pand<Packet4f>(eq, vec4f_swizzle1(eq, 1, 0, 3, 2)));
407}
408
409template <>
410EIGEN_STRONG_INLINE Packet1cd pcmp_eq(const Packet1cd& a, const Packet1cd& b) {
411 __m128d eq = _mm_cmpeq_pd(a.v, b.v);
412 return Packet1cd(pand<Packet2d>(eq, vec2d_swizzle1(eq, 1, 0)));
413}
414
415template <>
416EIGEN_STRONG_INLINE Packet1cd psqrt<Packet1cd>(const Packet1cd& a) {
417 return psqrt_complex<Packet1cd>(a);
418}
419
420template <>
421EIGEN_STRONG_INLINE Packet2cf psqrt<Packet2cf>(const Packet2cf& a) {
422 return psqrt_complex<Packet2cf>(a);
423}
424
425template <>
426EIGEN_STRONG_INLINE Packet1cd plog<Packet1cd>(const Packet1cd& a) {
427 return plog_complex<Packet1cd>(a);
428}
429
430template <>
431EIGEN_STRONG_INLINE Packet2cf plog<Packet2cf>(const Packet2cf& a) {
432 return plog_complex<Packet2cf>(a);
433}
434
435template <>
436EIGEN_STRONG_INLINE Packet2cf pexp<Packet2cf>(const Packet2cf& a) {
437 return pexp_complex<Packet2cf>(a);
438}
439
440#ifdef EIGEN_VECTORIZE_FMA
441// std::complex<float>
442template <>
443EIGEN_STRONG_INLINE Packet2cf pmadd(const Packet2cf& a, const Packet2cf& b, const Packet2cf& c) {
444 __m128 a_odd = _mm_movehdup_ps(a.v);
445 __m128 a_even = _mm_moveldup_ps(a.v);
446 __m128 b_swap = _mm_permute_ps(b.v, _MM_SHUFFLE(2, 3, 0, 1));
447 __m128 result = _mm_fmaddsub_ps(a_even, b.v, _mm_fmaddsub_ps(a_odd, b_swap, c.v));
448 return Packet2cf(result);
449}
450template <>
451EIGEN_STRONG_INLINE Packet2cf pmsub(const Packet2cf& a, const Packet2cf& b, const Packet2cf& c) {
452 __m128 a_odd = _mm_movehdup_ps(a.v);
453 __m128 a_even = _mm_moveldup_ps(a.v);
454 __m128 b_swap = _mm_permute_ps(b.v, _MM_SHUFFLE(2, 3, 0, 1));
455 __m128 result = _mm_fmaddsub_ps(a_even, b.v, _mm_fmsubadd_ps(a_odd, b_swap, c.v));
456 return Packet2cf(result);
457}
458template <>
459EIGEN_STRONG_INLINE Packet2cf pnmadd(const Packet2cf& a, const Packet2cf& b, const Packet2cf& c) {
460 return pnegate(pmsub(a, b, c));
461}
462template <>
463EIGEN_STRONG_INLINE Packet2cf pnmsub(const Packet2cf& a, const Packet2cf& b, const Packet2cf& c) {
464 return pnegate(pmadd(a, b, c));
465}
466// std::complex<double>
467template <>
468EIGEN_STRONG_INLINE Packet1cd pmadd(const Packet1cd& a, const Packet1cd& b, const Packet1cd& c) {
469 __m128d a_odd = _mm_permute_pd(a.v, 0x3);
470 __m128d a_even = _mm_movedup_pd(a.v);
471 __m128d b_swap = _mm_permute_pd(b.v, 0x1);
472 __m128d result = _mm_fmaddsub_pd(a_even, b.v, _mm_fmaddsub_pd(a_odd, b_swap, c.v));
473 return Packet1cd(result);
474}
475template <>
476EIGEN_STRONG_INLINE Packet1cd pmsub(const Packet1cd& a, const Packet1cd& b, const Packet1cd& c) {
477 __m128d a_odd = _mm_permute_pd(a.v, 0x3);
478 __m128d a_even = _mm_movedup_pd(a.v);
479 __m128d b_swap = _mm_permute_pd(b.v, 0x1);
480 __m128d result = _mm_fmaddsub_pd(a_even, b.v, _mm_fmsubadd_pd(a_odd, b_swap, c.v));
481 return Packet1cd(result);
482}
483template <>
484EIGEN_STRONG_INLINE Packet1cd pnmadd(const Packet1cd& a, const Packet1cd& b, const Packet1cd& c) {
485 return pnegate(pmsub(a, b, c));
486}
487template <>
488EIGEN_STRONG_INLINE Packet1cd pnmsub(const Packet1cd& a, const Packet1cd& b, const Packet1cd& c) {
489 return pnegate(pmadd(a, b, c));
490}
491#endif
492} // end namespace internal
493} // end namespace Eigen
494
495#endif // EIGEN_COMPLEX_SSE_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