Complex.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2010 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #ifndef EIGEN_COMPLEX_NEON_H
00026 #define EIGEN_COMPLEX_NEON_H
00027 
00028 namespace Eigen {
00029 
00030 namespace internal {
00031 
00032 static uint32x4_t p4ui_CONJ_XOR = EIGEN_INIT_NEON_PACKET4(0x00000000, 0x80000000, 0x00000000, 0x80000000);
00033 static uint32x2_t p2ui_CONJ_XOR = EIGEN_INIT_NEON_PACKET2(0x00000000, 0x80000000);
00034 
00035 //---------- float ----------
00036 struct Packet2cf
00037 {
00038   EIGEN_STRONG_INLINE Packet2cf() {}
00039   EIGEN_STRONG_INLINE explicit Packet2cf(const Packet4f& a) : v(a) {}
00040   Packet4f  v;
00041 };
00042 
00043 template<> struct packet_traits<std::complex<float> >  : default_packet_traits
00044 {
00045   typedef Packet2cf type;
00046   enum {
00047     Vectorizable = 1,
00048     AlignedOnScalar = 1,
00049     size = 2,
00050 
00051     HasAdd    = 1,
00052     HasSub    = 1,
00053     HasMul    = 1,
00054     HasDiv    = 1,
00055     HasNegate = 1,
00056     HasAbs    = 0,
00057     HasAbs2   = 0,
00058     HasMin    = 0,
00059     HasMax    = 0,
00060     HasSetLinear = 0
00061   };
00062 };
00063 
00064 template<> struct unpacket_traits<Packet2cf> { typedef std::complex<float> type; enum {size=2}; };
00065 
00066 template<> EIGEN_STRONG_INLINE Packet2cf pset1<Packet2cf>(const std::complex<float>&  from)
00067 {
00068   float32x2_t r64;
00069   r64 = vld1_f32((float *)&from);
00070 
00071   return Packet2cf(vcombine_f32(r64, r64));
00072 }
00073 
00074 template<> EIGEN_STRONG_INLINE Packet2cf padd<Packet2cf>(const Packet2cf& a, const Packet2cf& b) { return Packet2cf(padd<Packet4f>(a.v,b.v)); }
00075 template<> EIGEN_STRONG_INLINE Packet2cf psub<Packet2cf>(const Packet2cf& a, const Packet2cf& b) { return Packet2cf(psub<Packet4f>(a.v,b.v)); }
00076 template<> EIGEN_STRONG_INLINE Packet2cf pnegate(const Packet2cf& a) { return Packet2cf(pnegate<Packet4f>(a.v)); }
00077 template<> EIGEN_STRONG_INLINE Packet2cf pconj(const Packet2cf& a)
00078 {
00079   Packet4ui b = vreinterpretq_u32_f32(a.v);
00080   return Packet2cf(vreinterpretq_f32_u32(veorq_u32(b, p4ui_CONJ_XOR)));
00081 }
00082 
00083 template<> EIGEN_STRONG_INLINE Packet2cf pmul<Packet2cf>(const Packet2cf& a, const Packet2cf& b)
00084 {
00085   Packet4f v1, v2;
00086   float32x2_t a_lo, a_hi;
00087 
00088   // Get the real values of a | a1_re | a1_re | a2_re | a2_re |
00089   v1 = vcombine_f32(vdup_lane_f32(vget_low_f32(a.v), 0), vdup_lane_f32(vget_high_f32(a.v), 0));
00090   // Get the real values of a | a1_im | a1_im | a2_im | a2_im |
00091   v2 = vcombine_f32(vdup_lane_f32(vget_low_f32(a.v), 1), vdup_lane_f32(vget_high_f32(a.v), 1));
00092   // Multiply the real a with b
00093   v1 = vmulq_f32(v1, b.v);
00094   // Multiply the imag a with b
00095   v2 = vmulq_f32(v2, b.v);
00096   // Conjugate v2 
00097   v2 = vreinterpretq_f32_u32(veorq_u32(vreinterpretq_u32_f32(v2), p4ui_CONJ_XOR));
00098   // Swap real/imag elements in v2.
00099   a_lo = vrev64_f32(vget_low_f32(v2));
00100   a_hi = vrev64_f32(vget_high_f32(v2));
00101   v2 = vcombine_f32(a_lo, a_hi);
00102   // Add and return the result
00103   return Packet2cf(vaddq_f32(v1, v2));
00104 }
00105 
00106 template<> EIGEN_STRONG_INLINE Packet2cf pand   <Packet2cf>(const Packet2cf& a, const Packet2cf& b)
00107 {
00108   return Packet2cf(vreinterpretq_f32_u32(vorrq_u32(vreinterpretq_u32_f32(a.v),vreinterpretq_u32_f32(b.v))));
00109 }
00110 template<> EIGEN_STRONG_INLINE Packet2cf por    <Packet2cf>(const Packet2cf& a, const Packet2cf& b)
00111 {
00112   return Packet2cf(vreinterpretq_f32_u32(vorrq_u32(vreinterpretq_u32_f32(a.v),vreinterpretq_u32_f32(b.v))));
00113 }
00114 template<> EIGEN_STRONG_INLINE Packet2cf pxor   <Packet2cf>(const Packet2cf& a, const Packet2cf& b)
00115 {
00116   return Packet2cf(vreinterpretq_f32_u32(veorq_u32(vreinterpretq_u32_f32(a.v),vreinterpretq_u32_f32(b.v))));
00117 }
00118 template<> EIGEN_STRONG_INLINE Packet2cf pandnot<Packet2cf>(const Packet2cf& a, const Packet2cf& b)
00119 {
00120   return Packet2cf(vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(a.v),vreinterpretq_u32_f32(b.v))));
00121 }
00122 
00123 template<> EIGEN_STRONG_INLINE Packet2cf pload<Packet2cf>(const std::complex<float>* from) { EIGEN_DEBUG_ALIGNED_LOAD return Packet2cf(pload<Packet4f>((const float*)from)); }
00124 template<> EIGEN_STRONG_INLINE Packet2cf ploadu<Packet2cf>(const std::complex<float>* from) { EIGEN_DEBUG_UNALIGNED_LOAD return Packet2cf(ploadu<Packet4f>((const float*)from)); }
00125 
00126 template<> EIGEN_STRONG_INLINE Packet2cf ploaddup<Packet2cf>(const std::complex<float>* from) { return pset1<Packet2cf>(*from); }
00127 
00128 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); }
00129 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); }
00130 
00131 template<> EIGEN_STRONG_INLINE void prefetch<std::complex<float> >(const std::complex<float> *   addr) { __pld((float *)addr); }
00132 
00133 template<> EIGEN_STRONG_INLINE std::complex<float>  pfirst<Packet2cf>(const Packet2cf& a)
00134 {
00135   std::complex<float> EIGEN_ALIGN16 x[2];
00136   vst1q_f32((float *)x, a.v);
00137   return x[0];
00138 }
00139 
00140 template<> EIGEN_STRONG_INLINE Packet2cf preverse(const Packet2cf& a)
00141 {
00142   float32x2_t a_lo, a_hi;
00143   Packet4f a_r128;
00144 
00145   a_lo = vget_low_f32(a.v);
00146   a_hi = vget_high_f32(a.v);
00147   a_r128 = vcombine_f32(a_hi, a_lo);
00148 
00149   return Packet2cf(a_r128);
00150 }
00151 
00152 template<> EIGEN_STRONG_INLINE Packet2cf pcplxflip<Packet2cf>(const Packet2cf& a)
00153 {
00154   return Packet2cf(vrev64q_f32(a.v));
00155 }
00156 
00157 template<> EIGEN_STRONG_INLINE std::complex<float> predux<Packet2cf>(const Packet2cf& a)
00158 {
00159   float32x2_t a1, a2;
00160   std::complex<float> s;
00161 
00162   a1 = vget_low_f32(a.v);
00163   a2 = vget_high_f32(a.v);
00164   a2 = vadd_f32(a1, a2);
00165   vst1_f32((float *)&s, a2);
00166 
00167   return s;
00168 }
00169 
00170 template<> EIGEN_STRONG_INLINE Packet2cf preduxp<Packet2cf>(const Packet2cf* vecs)
00171 {
00172   Packet4f sum1, sum2, sum;
00173 
00174   // Add the first two 64-bit float32x2_t of vecs[0]
00175   sum1 = vcombine_f32(vget_low_f32(vecs[0].v), vget_low_f32(vecs[1].v));
00176   sum2 = vcombine_f32(vget_high_f32(vecs[0].v), vget_high_f32(vecs[1].v));
00177   sum = vaddq_f32(sum1, sum2);
00178 
00179   return Packet2cf(sum);
00180 }
00181 
00182 template<> EIGEN_STRONG_INLINE std::complex<float> predux_mul<Packet2cf>(const Packet2cf& a)
00183 {
00184   float32x2_t a1, a2, v1, v2, prod;
00185   std::complex<float> s;
00186 
00187   a1 = vget_low_f32(a.v);
00188   a2 = vget_high_f32(a.v);
00189    // Get the real values of a | a1_re | a1_re | a2_re | a2_re |
00190   v1 = vdup_lane_f32(a1, 0);
00191   // Get the real values of a | a1_im | a1_im | a2_im | a2_im |
00192   v2 = vdup_lane_f32(a1, 1);
00193   // Multiply the real a with b
00194   v1 = vmul_f32(v1, a2);
00195   // Multiply the imag a with b
00196   v2 = vmul_f32(v2, a2);
00197   // Conjugate v2 
00198   v2 = vreinterpret_f32_u32(veor_u32(vreinterpret_u32_f32(v2), p2ui_CONJ_XOR));
00199   // Swap real/imag elements in v2.
00200   v2 = vrev64_f32(v2);
00201   // Add v1, v2
00202   prod = vadd_f32(v1, v2);
00203 
00204   vst1_f32((float *)&s, prod);
00205 
00206   return s;
00207 }
00208 
00209 template<int Offset>
00210 struct palign_impl<Offset,Packet2cf>
00211 {
00212   EIGEN_STRONG_INLINE static void run(Packet2cf& first, const Packet2cf& second)
00213   {
00214     if (Offset==1)
00215     {
00216       first.v = vextq_f32(first.v, second.v, 2);
00217     }
00218   }
00219 };
00220 
00221 template<> struct conj_helper<Packet2cf, Packet2cf, false,true>
00222 {
00223   EIGEN_STRONG_INLINE Packet2cf pmadd(const Packet2cf& x, const Packet2cf& y, const Packet2cf& c) const
00224   { return padd(pmul(x,y),c); }
00225 
00226   EIGEN_STRONG_INLINE Packet2cf pmul(const Packet2cf& a, const Packet2cf& b) const
00227   {
00228     return internal::pmul(a, pconj(b));
00229   }
00230 };
00231 
00232 template<> struct conj_helper<Packet2cf, Packet2cf, true,false>
00233 {
00234   EIGEN_STRONG_INLINE Packet2cf pmadd(const Packet2cf& x, const Packet2cf& y, const Packet2cf& c) const
00235   { return padd(pmul(x,y),c); }
00236 
00237   EIGEN_STRONG_INLINE Packet2cf pmul(const Packet2cf& a, const Packet2cf& b) const
00238   {
00239     return internal::pmul(pconj(a), b);
00240   }
00241 };
00242 
00243 template<> struct conj_helper<Packet2cf, Packet2cf, true,true>
00244 {
00245   EIGEN_STRONG_INLINE Packet2cf pmadd(const Packet2cf& x, const Packet2cf& y, const Packet2cf& c) const
00246   { return padd(pmul(x,y),c); }
00247 
00248   EIGEN_STRONG_INLINE Packet2cf pmul(const Packet2cf& a, const Packet2cf& b) const
00249   {
00250     return pconj(internal::pmul(a, b));
00251   }
00252 };
00253 
00254 template<> EIGEN_STRONG_INLINE Packet2cf pdiv<Packet2cf>(const Packet2cf& a, const Packet2cf& b)
00255 {
00256   // TODO optimize it for AltiVec
00257   Packet2cf res = conj_helper<Packet2cf,Packet2cf,false,true>().pmul(a,b);
00258   Packet4f s, rev_s;
00259   float32x2_t a_lo, a_hi;
00260 
00261   // this computes the norm
00262   s = vmulq_f32(b.v, b.v);
00263   a_lo = vrev64_f32(vget_low_f32(s));
00264   a_hi = vrev64_f32(vget_high_f32(s));
00265   rev_s = vcombine_f32(a_lo, a_hi);
00266 
00267   return Packet2cf(pdiv(res.v, vaddq_f32(s,rev_s)));
00268 }
00269 
00270 } // end namespace internal
00271 
00272 } // end namespace Eigen
00273 
00274 #endif // EIGEN_COMPLEX_NEON_H