NEON/Complex.h
Go to the documentation of this file.
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 // Eigen is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3 of the License, or (at your option) any later version.
10 //
11 // Alternatively, you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 2 of
14 // the License, or (at your option) any later version.
15 //
16 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License and a copy of the GNU General Public License along with
23 // Eigen. If not, see <http://www.gnu.org/licenses/>.
24 
25 #ifndef EIGEN_COMPLEX_NEON_H
26 #define EIGEN_COMPLEX_NEON_H
27 
28 namespace Eigen {
29 
30 namespace internal {
31 
32 static uint32x4_t p4ui_CONJ_XOR = EIGEN_INIT_NEON_PACKET4(0x00000000, 0x80000000, 0x00000000, 0x80000000);
33 static uint32x2_t p2ui_CONJ_XOR = EIGEN_INIT_NEON_PACKET2(0x00000000, 0x80000000);
34 
35 //---------- float ----------
36 struct Packet2cf
37 {
38  EIGEN_STRONG_INLINE Packet2cf() {}
39  EIGEN_STRONG_INLINE explicit Packet2cf(const Packet4f& a) : v(a) {}
40  Packet4f v;
41 };
42 
43 template<> struct packet_traits<std::complex<float> > : default_packet_traits
44 {
45  typedef Packet2cf type;
46  enum {
47  Vectorizable = 1,
48  AlignedOnScalar = 1,
49  size = 2,
50 
51  HasAdd = 1,
52  HasSub = 1,
53  HasMul = 1,
54  HasDiv = 1,
55  HasNegate = 1,
56  HasAbs = 0,
57  HasAbs2 = 0,
58  HasMin = 0,
59  HasMax = 0,
60  HasSetLinear = 0
61  };
62 };
63 
64 template<> struct unpacket_traits<Packet2cf> { typedef std::complex<float> type; enum {size=2}; };
65 
66 template<> EIGEN_STRONG_INLINE Packet2cf pset1<Packet2cf>(const std::complex<float>& from)
67 {
68  float32x2_t r64;
69  r64 = vld1_f32((float *)&from);
70 
71  return Packet2cf(vcombine_f32(r64, r64));
72 }
73 
74 template<> EIGEN_STRONG_INLINE Packet2cf padd<Packet2cf>(const Packet2cf& a, const Packet2cf& b) { return Packet2cf(padd<Packet4f>(a.v,b.v)); }
75 template<> EIGEN_STRONG_INLINE Packet2cf psub<Packet2cf>(const Packet2cf& a, const Packet2cf& b) { return Packet2cf(psub<Packet4f>(a.v,b.v)); }
76 template<> EIGEN_STRONG_INLINE Packet2cf pnegate(const Packet2cf& a) { return Packet2cf(pnegate<Packet4f>(a.v)); }
77 template<> EIGEN_STRONG_INLINE Packet2cf pconj(const Packet2cf& a)
78 {
79  Packet4ui b = vreinterpretq_u32_f32(a.v);
80  return Packet2cf(vreinterpretq_f32_u32(veorq_u32(b, p4ui_CONJ_XOR)));
81 }
82 
83 template<> EIGEN_STRONG_INLINE Packet2cf pmul<Packet2cf>(const Packet2cf& a, const Packet2cf& b)
84 {
85  Packet4f v1, v2;
86  float32x2_t a_lo, a_hi;
87 
88  // Get the real values of a | a1_re | a1_re | a2_re | a2_re |
89  v1 = vcombine_f32(vdup_lane_f32(vget_low_f32(a.v), 0), vdup_lane_f32(vget_high_f32(a.v), 0));
90  // Get the real values of a | a1_im | a1_im | a2_im | a2_im |
91  v2 = vcombine_f32(vdup_lane_f32(vget_low_f32(a.v), 1), vdup_lane_f32(vget_high_f32(a.v), 1));
92  // Multiply the real a with b
93  v1 = vmulq_f32(v1, b.v);
94  // Multiply the imag a with b
95  v2 = vmulq_f32(v2, b.v);
96  // Conjugate v2
97  v2 = vreinterpretq_f32_u32(veorq_u32(vreinterpretq_u32_f32(v2), p4ui_CONJ_XOR));
98  // Swap real/imag elements in v2.
99  a_lo = vrev64_f32(vget_low_f32(v2));
100  a_hi = vrev64_f32(vget_high_f32(v2));
101  v2 = vcombine_f32(a_lo, a_hi);
102  // Add and return the result
103  return Packet2cf(vaddq_f32(v1, v2));
104 }
105 
106 template<> EIGEN_STRONG_INLINE Packet2cf pand <Packet2cf>(const Packet2cf& a, const Packet2cf& b)
107 {
108  return Packet2cf(vreinterpretq_f32_u32(vorrq_u32(vreinterpretq_u32_f32(a.v),vreinterpretq_u32_f32(b.v))));
109 }
110 template<> EIGEN_STRONG_INLINE Packet2cf por <Packet2cf>(const Packet2cf& a, const Packet2cf& b)
111 {
112  return Packet2cf(vreinterpretq_f32_u32(vorrq_u32(vreinterpretq_u32_f32(a.v),vreinterpretq_u32_f32(b.v))));
113 }
114 template<> EIGEN_STRONG_INLINE Packet2cf pxor <Packet2cf>(const Packet2cf& a, const Packet2cf& b)
115 {
116  return Packet2cf(vreinterpretq_f32_u32(veorq_u32(vreinterpretq_u32_f32(a.v),vreinterpretq_u32_f32(b.v))));
117 }
118 template<> EIGEN_STRONG_INLINE Packet2cf pandnot<Packet2cf>(const Packet2cf& a, const Packet2cf& b)
119 {
120  return Packet2cf(vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(a.v),vreinterpretq_u32_f32(b.v))));
121 }
122 
123 template<> EIGEN_STRONG_INLINE Packet2cf pload<Packet2cf>(const std::complex<float>* from) { EIGEN_DEBUG_ALIGNED_LOAD return Packet2cf(pload<Packet4f>((const float*)from)); }
124 template<> EIGEN_STRONG_INLINE Packet2cf ploadu<Packet2cf>(const std::complex<float>* from) { EIGEN_DEBUG_UNALIGNED_LOAD return Packet2cf(ploadu<Packet4f>((const float*)from)); }
125 
126 template<> EIGEN_STRONG_INLINE Packet2cf ploaddup<Packet2cf>(const std::complex<float>* from) { return pset1<Packet2cf>(*from); }
127 
128 template<> EIGEN_STRONG_INLINE void pstore <std::complex<float> >(std::complex<float> * to, const Packet2cf& from) { EIGEN_DEBUG_ALIGNED_STORE pstore((float*)to, from.v); }
129 template<> EIGEN_STRONG_INLINE void pstoreu<std::complex<float> >(std::complex<float> * to, const Packet2cf& from) { EIGEN_DEBUG_UNALIGNED_STORE pstoreu((float*)to, from.v); }
130 
131 template<> EIGEN_STRONG_INLINE void prefetch<std::complex<float> >(const std::complex<float> * addr) { __pld((float *)addr); }
132 
133 template<> EIGEN_STRONG_INLINE std::complex<float> pfirst<Packet2cf>(const Packet2cf& a)
134 {
135  std::complex<float> EIGEN_ALIGN16 x[2];
136  vst1q_f32((float *)x, a.v);
137  return x[0];
138 }
139 
140 template<> EIGEN_STRONG_INLINE Packet2cf preverse(const Packet2cf& a)
141 {
142  float32x2_t a_lo, a_hi;
143  Packet4f a_r128;
144 
145  a_lo = vget_low_f32(a.v);
146  a_hi = vget_high_f32(a.v);
147  a_r128 = vcombine_f32(a_hi, a_lo);
148 
149  return Packet2cf(a_r128);
150 }
151 
152 template<> EIGEN_STRONG_INLINE Packet2cf pcplxflip<Packet2cf>(const Packet2cf& a)
153 {
154  return Packet2cf(vrev64q_f32(a.v));
155 }
156 
157 template<> EIGEN_STRONG_INLINE std::complex<float> predux<Packet2cf>(const Packet2cf& a)
158 {
159  float32x2_t a1, a2;
160  std::complex<float> s;
161 
162  a1 = vget_low_f32(a.v);
163  a2 = vget_high_f32(a.v);
164  a2 = vadd_f32(a1, a2);
165  vst1_f32((float *)&s, a2);
166 
167  return s;
168 }
169 
170 template<> EIGEN_STRONG_INLINE Packet2cf preduxp<Packet2cf>(const Packet2cf* vecs)
171 {
172  Packet4f sum1, sum2, sum;
173 
174  // Add the first two 64-bit float32x2_t of vecs[0]
175  sum1 = vcombine_f32(vget_low_f32(vecs[0].v), vget_low_f32(vecs[1].v));
176  sum2 = vcombine_f32(vget_high_f32(vecs[0].v), vget_high_f32(vecs[1].v));
177  sum = vaddq_f32(sum1, sum2);
178 
179  return Packet2cf(sum);
180 }
181 
182 template<> EIGEN_STRONG_INLINE std::complex<float> predux_mul<Packet2cf>(const Packet2cf& a)
183 {
184  float32x2_t a1, a2, v1, v2, prod;
185  std::complex<float> s;
186 
187  a1 = vget_low_f32(a.v);
188  a2 = vget_high_f32(a.v);
189  // Get the real values of a | a1_re | a1_re | a2_re | a2_re |
190  v1 = vdup_lane_f32(a1, 0);
191  // Get the real values of a | a1_im | a1_im | a2_im | a2_im |
192  v2 = vdup_lane_f32(a1, 1);
193  // Multiply the real a with b
194  v1 = vmul_f32(v1, a2);
195  // Multiply the imag a with b
196  v2 = vmul_f32(v2, a2);
197  // Conjugate v2
198  v2 = vreinterpret_f32_u32(veor_u32(vreinterpret_u32_f32(v2), p2ui_CONJ_XOR));
199  // Swap real/imag elements in v2.
200  v2 = vrev64_f32(v2);
201  // Add v1, v2
202  prod = vadd_f32(v1, v2);
203 
204  vst1_f32((float *)&s, prod);
205 
206  return s;
207 }
208 
209 template<int Offset>
210 struct palign_impl<Offset,Packet2cf>
211 {
212  EIGEN_STRONG_INLINE static void run(Packet2cf& first, const Packet2cf& second)
213  {
214  if (Offset==1)
215  {
216  first.v = vextq_f32(first.v, second.v, 2);
217  }
218  }
219 };
220 
221 template<> struct conj_helper<Packet2cf, Packet2cf, false,true>
222 {
223  EIGEN_STRONG_INLINE Packet2cf pmadd(const Packet2cf& x, const Packet2cf& y, const Packet2cf& c) const
224  { return padd(pmul(x,y),c); }
225 
226  EIGEN_STRONG_INLINE Packet2cf pmul(const Packet2cf& a, const Packet2cf& b) const
227  {
228  return internal::pmul(a, pconj(b));
229  }
230 };
231 
232 template<> struct conj_helper<Packet2cf, Packet2cf, true,false>
233 {
234  EIGEN_STRONG_INLINE Packet2cf pmadd(const Packet2cf& x, const Packet2cf& y, const Packet2cf& c) const
235  { return padd(pmul(x,y),c); }
236 
237  EIGEN_STRONG_INLINE Packet2cf pmul(const Packet2cf& a, const Packet2cf& b) const
238  {
239  return internal::pmul(pconj(a), b);
240  }
241 };
242 
243 template<> struct conj_helper<Packet2cf, Packet2cf, true,true>
244 {
245  EIGEN_STRONG_INLINE Packet2cf pmadd(const Packet2cf& x, const Packet2cf& y, const Packet2cf& c) const
246  { return padd(pmul(x,y),c); }
247 
248  EIGEN_STRONG_INLINE Packet2cf pmul(const Packet2cf& a, const Packet2cf& b) const
249  {
250  return pconj(internal::pmul(a, b));
251  }
252 };
253 
254 template<> EIGEN_STRONG_INLINE Packet2cf pdiv<Packet2cf>(const Packet2cf& a, const Packet2cf& b)
255 {
256  // TODO optimize it for AltiVec
257  Packet2cf res = conj_helper<Packet2cf,Packet2cf,false,true>().pmul(a,b);
258  Packet4f s, rev_s;
259  float32x2_t a_lo, a_hi;
260 
261  // this computes the norm
262  s = vmulq_f32(b.v, b.v);
263  a_lo = vrev64_f32(vget_low_f32(s));
264  a_hi = vrev64_f32(vget_high_f32(s));
265  rev_s = vcombine_f32(a_lo, a_hi);
266 
267  return Packet2cf(pdiv(res.v, vaddq_f32(s,rev_s)));
268 }
269 
270 } // end namespace internal
271 
272 } // end namespace Eigen
273 
274 #endif // EIGEN_COMPLEX_NEON_H