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//
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
13namespace Eigen {
14
15namespace internal {
16
17//---------- float ----------
18struct Packet2cf
19{
20 EIGEN_STRONG_INLINE Packet2cf() {}
21 EIGEN_STRONG_INLINE explicit Packet2cf(const __m128& a) : v(a) {}
22 Packet4f v;
23};
24
25// Use the packet_traits defined in AVX/PacketMath.h instead if we're going
26// to leverage AVX instructions.
27#ifndef EIGEN_VECTORIZE_AVX
28template<> struct packet_traits<std::complex<float> > : default_packet_traits
29{
30 typedef Packet2cf type;
31 typedef Packet2cf half;
32 enum {
33 Vectorizable = 1,
34 AlignedOnScalar = 1,
35 size = 2,
36 HasHalfPacket = 0,
37
38 HasAdd = 1,
39 HasSub = 1,
40 HasMul = 1,
41 HasDiv = 1,
42 HasNegate = 1,
43 HasSqrt = 1,
44 HasAbs = 0,
45 HasAbs2 = 0,
46 HasMin = 0,
47 HasMax = 0,
48 HasSetLinear = 0,
49 HasBlend = 1
50 };
51};
52#endif
53
54template<> struct unpacket_traits<Packet2cf> {
55 typedef std::complex<float> type;
56 typedef Packet2cf half;
57 typedef Packet4f as_real;
58 enum {
59 size=2,
60 alignment=Aligned16,
61 vectorizable=true,
62 masked_load_available=false,
63 masked_store_available=false
64 };
65};
66
67template<> EIGEN_STRONG_INLINE Packet2cf padd<Packet2cf>(const Packet2cf& a, const Packet2cf& b) { return Packet2cf(_mm_add_ps(a.v,b.v)); }
68template<> EIGEN_STRONG_INLINE Packet2cf psub<Packet2cf>(const Packet2cf& a, const Packet2cf& b) { return Packet2cf(_mm_sub_ps(a.v,b.v)); }
69
70template<> EIGEN_STRONG_INLINE Packet2cf pnegate(const Packet2cf& a)
71{
72 const __m128 mask = _mm_castsi128_ps(_mm_setr_epi32(0x80000000,0x80000000,0x80000000,0x80000000));
73 return Packet2cf(_mm_xor_ps(a.v,mask));
74}
75template<> EIGEN_STRONG_INLINE Packet2cf pconj(const Packet2cf& a)
76{
77 const __m128 mask = _mm_castsi128_ps(_mm_setr_epi32(0x00000000,0x80000000,0x00000000,0x80000000));
78 return Packet2cf(_mm_xor_ps(a.v,mask));
79}
80
81template<> EIGEN_STRONG_INLINE Packet2cf pmul<Packet2cf>(const Packet2cf& a, const Packet2cf& b)
82{
83 #ifdef EIGEN_VECTORIZE_SSE3
84 return Packet2cf(_mm_addsub_ps(_mm_mul_ps(_mm_moveldup_ps(a.v), b.v),
85 _mm_mul_ps(_mm_movehdup_ps(a.v),
86 vec4f_swizzle1(b.v, 1, 0, 3, 2))));
87// return Packet2cf(_mm_addsub_ps(_mm_mul_ps(vec4f_swizzle1(a.v, 0, 0, 2, 2), b.v),
88// _mm_mul_ps(vec4f_swizzle1(a.v, 1, 1, 3, 3),
89// vec4f_swizzle1(b.v, 1, 0, 3, 2))));
90 #else
91 const __m128 mask = _mm_castsi128_ps(_mm_setr_epi32(0x80000000,0x00000000,0x80000000,0x00000000));
92 return Packet2cf(_mm_add_ps(_mm_mul_ps(vec4f_swizzle1(a.v, 0, 0, 2, 2), b.v),
93 _mm_xor_ps(_mm_mul_ps(vec4f_swizzle1(a.v, 1, 1, 3, 3),
94 vec4f_swizzle1(b.v, 1, 0, 3, 2)), mask)));
95 #endif
96}
97
98template<> EIGEN_STRONG_INLINE Packet2cf ptrue <Packet2cf>(const Packet2cf& a) { return Packet2cf(ptrue(Packet4f(a.v))); }
99template<> EIGEN_STRONG_INLINE Packet2cf pand <Packet2cf>(const Packet2cf& a, const Packet2cf& b) { return Packet2cf(_mm_and_ps(a.v,b.v)); }
100template<> EIGEN_STRONG_INLINE Packet2cf por <Packet2cf>(const Packet2cf& a, const Packet2cf& b) { return Packet2cf(_mm_or_ps(a.v,b.v)); }
101template<> EIGEN_STRONG_INLINE Packet2cf pxor <Packet2cf>(const Packet2cf& a, const Packet2cf& b) { return Packet2cf(_mm_xor_ps(a.v,b.v)); }
102template<> EIGEN_STRONG_INLINE Packet2cf pandnot<Packet2cf>(const Packet2cf& a, const Packet2cf& b) { return Packet2cf(_mm_andnot_ps(b.v,a.v)); }
103
104template<> EIGEN_STRONG_INLINE Packet2cf pload <Packet2cf>(const std::complex<float>* from) { EIGEN_DEBUG_ALIGNED_LOAD return Packet2cf(pload<Packet4f>(&numext::real_ref(*from))); }
105template<> EIGEN_STRONG_INLINE Packet2cf ploadu<Packet2cf>(const std::complex<float>* from) { EIGEN_DEBUG_UNALIGNED_LOAD return Packet2cf(ploadu<Packet4f>(&numext::real_ref(*from))); }
106
107template<> EIGEN_STRONG_INLINE Packet2cf pset1<Packet2cf>(const std::complex<float>& from)
108{
109 const float re = std::real(from);
110 const float im = std::imag(from);
111 return Packet2cf(_mm_set_ps(im, re, im, re));
112}
113
114template<> EIGEN_STRONG_INLINE Packet2cf ploaddup<Packet2cf>(const std::complex<float>* from) { return pset1<Packet2cf>(*from); }
115
116template<> EIGEN_STRONG_INLINE void pstore <std::complex<float> >(std::complex<float> * to, const Packet2cf& from) { EIGEN_DEBUG_ALIGNED_STORE pstore(&numext::real_ref(*to), Packet4f(from.v)); }
117template<> EIGEN_STRONG_INLINE void pstoreu<std::complex<float> >(std::complex<float> * to, const Packet2cf& from) { EIGEN_DEBUG_UNALIGNED_STORE pstoreu(&numext::real_ref(*to), Packet4f(from.v)); }
118
119
120template<> EIGEN_DEVICE_FUNC inline Packet2cf pgather<std::complex<float>, Packet2cf>(const std::complex<float>* from, Index stride)
121{
122 return Packet2cf(_mm_set_ps(std::imag(from[1*stride]), std::real(from[1*stride]),
123 std::imag(from[0*stride]), std::real(from[0*stride])));
124}
125
126template<> EIGEN_DEVICE_FUNC inline void pscatter<std::complex<float>, Packet2cf>(std::complex<float>* to, const Packet2cf& from, Index stride)
127{
128 to[stride*0] = std::complex<float>(_mm_cvtss_f32(_mm_shuffle_ps(from.v, from.v, 0)),
129 _mm_cvtss_f32(_mm_shuffle_ps(from.v, from.v, 1)));
130 to[stride*1] = std::complex<float>(_mm_cvtss_f32(_mm_shuffle_ps(from.v, from.v, 2)),
131 _mm_cvtss_f32(_mm_shuffle_ps(from.v, from.v, 3)));
132}
133
134template<> EIGEN_STRONG_INLINE void prefetch<std::complex<float> >(const std::complex<float> * addr) { _mm_prefetch((SsePrefetchPtrType)(addr), _MM_HINT_T0); }
135
136template<> EIGEN_STRONG_INLINE std::complex<float> pfirst<Packet2cf>(const Packet2cf& a)
137{
138 #if EIGEN_GNUC_AT_MOST(4,3)
139 // Workaround gcc 4.2 ICE - this is not performance wise ideal, but who cares...
140 // This workaround also fix invalid code generation with gcc 4.3
141 EIGEN_ALIGN16 std::complex<float> res[2];
142 _mm_store_ps((float*)res, a.v);
143 return res[0];
144 #else
145 std::complex<float> res;
146 _mm_storel_pi((__m64*)&res, a.v);
147 return res;
148 #endif
149}
150
151template<> EIGEN_STRONG_INLINE Packet2cf preverse(const Packet2cf& a) { return Packet2cf(_mm_castpd_ps(preverse(Packet2d(_mm_castps_pd(a.v))))); }
152
153template<> EIGEN_STRONG_INLINE std::complex<float> predux<Packet2cf>(const Packet2cf& a)
154{
155 return pfirst(Packet2cf(_mm_add_ps(a.v, _mm_movehl_ps(a.v,a.v))));
156}
157
158template<> EIGEN_STRONG_INLINE std::complex<float> predux_mul<Packet2cf>(const Packet2cf& a)
159{
160 return pfirst(pmul(a, Packet2cf(_mm_movehl_ps(a.v,a.v))));
161}
162
163EIGEN_STRONG_INLINE Packet2cf pcplxflip/* <Packet2cf> */(const Packet2cf& x)
164{
165 return Packet2cf(vec4f_swizzle1(x.v, 1, 0, 3, 2));
166}
167
168EIGEN_MAKE_CONJ_HELPER_CPLX_REAL(Packet2cf,Packet4f)
169
170template<> EIGEN_STRONG_INLINE Packet2cf pdiv<Packet2cf>(const Packet2cf& a, const Packet2cf& b)
171{
172 return pdiv_complex(a, b);
173}
174
175//---------- double ----------
176struct Packet1cd
177{
178 EIGEN_STRONG_INLINE Packet1cd() {}
179 EIGEN_STRONG_INLINE explicit Packet1cd(const __m128d& a) : v(a) {}
180 Packet2d v;
181};
182
183// Use the packet_traits defined in AVX/PacketMath.h instead if we're going
184// to leverage AVX instructions.
185#ifndef EIGEN_VECTORIZE_AVX
186template<> struct packet_traits<std::complex<double> > : default_packet_traits
187{
188 typedef Packet1cd type;
189 typedef Packet1cd half;
190 enum {
191 Vectorizable = 1,
192 AlignedOnScalar = 0,
193 size = 1,
194 HasHalfPacket = 0,
195
196 HasAdd = 1,
197 HasSub = 1,
198 HasMul = 1,
199 HasDiv = 1,
200 HasNegate = 1,
201 HasSqrt = 1,
202 HasAbs = 0,
203 HasAbs2 = 0,
204 HasMin = 0,
205 HasMax = 0,
206 HasSetLinear = 0
207 };
208};
209#endif
210
211template<> struct unpacket_traits<Packet1cd> {
212 typedef std::complex<double> type;
213 typedef Packet1cd half;
214 typedef Packet2d as_real;
215 enum {
216 size=1,
217 alignment=Aligned16,
218 vectorizable=true,
219 masked_load_available=false,
220 masked_store_available=false
221 };
222};
223
224template<> EIGEN_STRONG_INLINE Packet1cd padd<Packet1cd>(const Packet1cd& a, const Packet1cd& b) { return Packet1cd(_mm_add_pd(a.v,b.v)); }
225template<> EIGEN_STRONG_INLINE Packet1cd psub<Packet1cd>(const Packet1cd& a, const Packet1cd& b) { return Packet1cd(_mm_sub_pd(a.v,b.v)); }
226template<> EIGEN_STRONG_INLINE Packet1cd pnegate(const Packet1cd& a) { return Packet1cd(pnegate(Packet2d(a.v))); }
227template<> EIGEN_STRONG_INLINE Packet1cd pconj(const Packet1cd& a)
228{
229 const __m128d mask = _mm_castsi128_pd(_mm_set_epi32(0x80000000,0x0,0x0,0x0));
230 return Packet1cd(_mm_xor_pd(a.v,mask));
231}
232
233template<> EIGEN_STRONG_INLINE Packet1cd pmul<Packet1cd>(const Packet1cd& a, const Packet1cd& b)
234{
235 #ifdef EIGEN_VECTORIZE_SSE3
236 return Packet1cd(_mm_addsub_pd(_mm_mul_pd(_mm_movedup_pd(a.v), b.v),
237 _mm_mul_pd(vec2d_swizzle1(a.v, 1, 1),
238 vec2d_swizzle1(b.v, 1, 0))));
239 #else
240 const __m128d mask = _mm_castsi128_pd(_mm_set_epi32(0x0,0x0,0x80000000,0x0));
241 return Packet1cd(_mm_add_pd(_mm_mul_pd(vec2d_swizzle1(a.v, 0, 0), b.v),
242 _mm_xor_pd(_mm_mul_pd(vec2d_swizzle1(a.v, 1, 1),
243 vec2d_swizzle1(b.v, 1, 0)), mask)));
244 #endif
245}
246
247template<> EIGEN_STRONG_INLINE Packet1cd ptrue <Packet1cd>(const Packet1cd& a) { return Packet1cd(ptrue(Packet2d(a.v))); }
248template<> EIGEN_STRONG_INLINE Packet1cd pand <Packet1cd>(const Packet1cd& a, const Packet1cd& b) { return Packet1cd(_mm_and_pd(a.v,b.v)); }
249template<> EIGEN_STRONG_INLINE Packet1cd por <Packet1cd>(const Packet1cd& a, const Packet1cd& b) { return Packet1cd(_mm_or_pd(a.v,b.v)); }
250template<> EIGEN_STRONG_INLINE Packet1cd pxor <Packet1cd>(const Packet1cd& a, const Packet1cd& b) { return Packet1cd(_mm_xor_pd(a.v,b.v)); }
251template<> EIGEN_STRONG_INLINE Packet1cd pandnot<Packet1cd>(const Packet1cd& a, const Packet1cd& b) { return Packet1cd(_mm_andnot_pd(b.v,a.v)); }
252
253// FIXME force unaligned load, this is a temporary fix
254template<> EIGEN_STRONG_INLINE Packet1cd pload <Packet1cd>(const std::complex<double>* from)
255{ EIGEN_DEBUG_ALIGNED_LOAD return Packet1cd(pload<Packet2d>((const double*)from)); }
256template<> EIGEN_STRONG_INLINE Packet1cd ploadu<Packet1cd>(const std::complex<double>* from)
257{ EIGEN_DEBUG_UNALIGNED_LOAD return Packet1cd(ploadu<Packet2d>((const double*)from)); }
258template<> EIGEN_STRONG_INLINE Packet1cd pset1<Packet1cd>(const std::complex<double>& from)
259{ /* here we really have to use unaligned loads :( */ return ploadu<Packet1cd>(&from); }
260
261template<> EIGEN_STRONG_INLINE Packet1cd ploaddup<Packet1cd>(const std::complex<double>* from) { return pset1<Packet1cd>(*from); }
262
263// FIXME force unaligned store, this is a temporary fix
264template<> EIGEN_STRONG_INLINE void pstore <std::complex<double> >(std::complex<double> * to, const Packet1cd& from) { EIGEN_DEBUG_ALIGNED_STORE pstore((double*)to, Packet2d(from.v)); }
265template<> EIGEN_STRONG_INLINE void pstoreu<std::complex<double> >(std::complex<double> * to, const Packet1cd& from) { EIGEN_DEBUG_UNALIGNED_STORE pstoreu((double*)to, Packet2d(from.v)); }
266
267template<> EIGEN_STRONG_INLINE void prefetch<std::complex<double> >(const std::complex<double> * addr) { _mm_prefetch((SsePrefetchPtrType)(addr), _MM_HINT_T0); }
268
269template<> EIGEN_STRONG_INLINE std::complex<double> pfirst<Packet1cd>(const Packet1cd& a)
270{
271 EIGEN_ALIGN16 double res[2];
272 _mm_store_pd(res, a.v);
273 return std::complex<double>(res[0],res[1]);
274}
275
276template<> EIGEN_STRONG_INLINE Packet1cd preverse(const Packet1cd& a) { return a; }
277
278template<> EIGEN_STRONG_INLINE std::complex<double> predux<Packet1cd>(const Packet1cd& a)
279{
280 return pfirst(a);
281}
282
283template<> EIGEN_STRONG_INLINE std::complex<double> predux_mul<Packet1cd>(const Packet1cd& a)
284{
285 return pfirst(a);
286}
287
288EIGEN_MAKE_CONJ_HELPER_CPLX_REAL(Packet1cd,Packet2d)
289
290template<> EIGEN_STRONG_INLINE Packet1cd pdiv<Packet1cd>(const Packet1cd& a, const Packet1cd& b)
291{
292 return pdiv_complex(a, b);
293}
294
295EIGEN_STRONG_INLINE Packet1cd pcplxflip/* <Packet1cd> */(const Packet1cd& x)
296{
297 return Packet1cd(preverse(Packet2d(x.v)));
298}
299
300EIGEN_DEVICE_FUNC inline void
301ptranspose(PacketBlock<Packet2cf,2>& kernel) {
302 __m128d w1 = _mm_castps_pd(kernel.packet[0].v);
303 __m128d w2 = _mm_castps_pd(kernel.packet[1].v);
304
305 __m128 tmp = _mm_castpd_ps(_mm_unpackhi_pd(w1, w2));
306 kernel.packet[0].v = _mm_castpd_ps(_mm_unpacklo_pd(w1, w2));
307 kernel.packet[1].v = tmp;
308}
309
310template<> EIGEN_STRONG_INLINE Packet2cf pcmp_eq(const Packet2cf& a, const Packet2cf& b)
311{
312 __m128 eq = _mm_cmpeq_ps(a.v, b.v);
313 return Packet2cf(pand<Packet4f>(eq, vec4f_swizzle1(eq, 1, 0, 3, 2)));
314}
315
316template<> EIGEN_STRONG_INLINE Packet1cd pcmp_eq(const Packet1cd& a, const Packet1cd& b)
317{
318 __m128d eq = _mm_cmpeq_pd(a.v, b.v);
319 return Packet1cd(pand<Packet2d>(eq, vec2d_swizzle1(eq, 1, 0)));
320}
321
322template<> EIGEN_STRONG_INLINE Packet2cf pblend(const Selector<2>& ifPacket, const Packet2cf& thenPacket, const Packet2cf& elsePacket) {
323 __m128d result = pblend<Packet2d>(ifPacket, _mm_castps_pd(thenPacket.v), _mm_castps_pd(elsePacket.v));
324 return Packet2cf(_mm_castpd_ps(result));
325}
326
327template<> EIGEN_STRONG_INLINE Packet1cd psqrt<Packet1cd>(const Packet1cd& a) {
328 return psqrt_complex<Packet1cd>(a);
329}
330
331template<> EIGEN_STRONG_INLINE Packet2cf psqrt<Packet2cf>(const Packet2cf& a) {
332 return psqrt_complex<Packet2cf>(a);
333}
334
335} // end namespace internal
336} // end namespace Eigen
337
338#endif // EIGEN_COMPLEX_SSE_H
@ 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