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 internal {
00029 
00030 static uint32x4_t p4ui_CONJ_XOR = { 0x00000000, 0x80000000, 0x00000000, 0x80000000 };
00031 static uint32x2_t p2ui_CONJ_XOR = { 0x00000000, 0x80000000 };
00032 
00033 //---------- float ----------
00034 struct Packet2cf
00035 {
00036   EIGEN_STRONG_INLINE Packet2cf() {}
00037   EIGEN_STRONG_INLINE explicit Packet2cf(const Packet4f& a) : v(a) {}
00038   Packet4f  v;
00039 };
00040 
00041 template<> struct packet_traits<std::complex<float> >  : default_packet_traits
00042 {
00043   typedef Packet2cf type;
00044   enum {
00045     Vectorizable = 1,
00046     AlignedOnScalar = 1,
00047     size = 2,
00048 
00049     HasAdd    = 1,
00050     HasSub    = 1,
00051     HasMul    = 1,
00052     HasDiv    = 1,
00053     HasNegate = 1,
00054     HasAbs    = 0,
00055     HasAbs2   = 0,
00056     HasMin    = 0,
00057     HasMax    = 0,
00058     HasSetLinear = 0
00059   };
00060 };
00061 
00062 template<> struct unpacket_traits<Packet2cf> { typedef std::complex<float> type; enum {size=2}; };
00063 
00064 template<> EIGEN_STRONG_INLINE Packet2cf pset1<Packet2cf>(const std::complex<float>&  from)
00065 {
00066   float32x2_t r64;
00067   r64 = vld1_f32((float *)&from);
00068 
00069   return Packet2cf(vcombine_f32(r64, r64));
00070 }
00071 
00072 template<> EIGEN_STRONG_INLINE Packet2cf padd<Packet2cf>(const Packet2cf& a, const Packet2cf& b) { return Packet2cf(padd<Packet4f>(a.v,b.v)); }
00073 template<> EIGEN_STRONG_INLINE Packet2cf psub<Packet2cf>(const Packet2cf& a, const Packet2cf& b) { return Packet2cf(psub<Packet4f>(a.v,b.v)); }
00074 template<> EIGEN_STRONG_INLINE Packet2cf pnegate(const Packet2cf& a) { return Packet2cf(pnegate<Packet4f>(a.v)); }
00075 template<> EIGEN_STRONG_INLINE Packet2cf pconj(const Packet2cf& a)
00076 {
00077   Packet4ui b = vreinterpretq_u32_f32(a.v);
00078   return Packet2cf(vreinterpretq_f32_u32(veorq_u32(b, p4ui_CONJ_XOR)));
00079 }
00080 
00081 template<> EIGEN_STRONG_INLINE Packet2cf pmul<Packet2cf>(const Packet2cf& a, const Packet2cf& b)
00082 {
00083   Packet4f v1, v2;
00084   float32x2_t a_lo, a_hi;
00085 
00086   // Get the real values of a | a1_re | a1_re | a2_re | a2_re |
00087   v1 = vcombine_f32(vdup_lane_f32(vget_low_f32(a.v), 0), vdup_lane_f32(vget_high_f32(a.v), 0));
00088   // Get the real values of a | a1_im | a1_im | a2_im | a2_im |
00089   v2 = vcombine_f32(vdup_lane_f32(vget_low_f32(a.v), 1), vdup_lane_f32(vget_high_f32(a.v), 1));
00090   // Multiply the real a with b
00091   v1 = vmulq_f32(v1, b.v);
00092   // Multiply the imag a with b
00093   v2 = vmulq_f32(v2, b.v);
00094   // Conjugate v2 
00095   v2 = vreinterpretq_f32_u32(veorq_u32(vreinterpretq_u32_f32(v2), p4ui_CONJ_XOR));
00096   // Swap real/imag elements in v2.
00097   a_lo = vrev64_f32(vget_low_f32(v2));
00098   a_hi = vrev64_f32(vget_high_f32(v2));
00099   v2 = vcombine_f32(a_lo, a_hi);
00100   // Add and return the result
00101   return Packet2cf(vaddq_f32(v1, v2));
00102 }
00103 
00104 template<> EIGEN_STRONG_INLINE Packet2cf pand   <Packet2cf>(const Packet2cf& a, const Packet2cf& b)
00105 {
00106   return Packet2cf(vreinterpretq_f32_u32(vorrq_u32(vreinterpretq_u32_f32(a.v),vreinterpretq_u32_f32(b.v))));
00107 }
00108 template<> EIGEN_STRONG_INLINE Packet2cf por    <Packet2cf>(const Packet2cf& a, const Packet2cf& b)
00109 {
00110   return Packet2cf(vreinterpretq_f32_u32(vorrq_u32(vreinterpretq_u32_f32(a.v),vreinterpretq_u32_f32(b.v))));
00111 }
00112 template<> EIGEN_STRONG_INLINE Packet2cf pxor   <Packet2cf>(const Packet2cf& a, const Packet2cf& b)
00113 {
00114   return Packet2cf(vreinterpretq_f32_u32(veorq_u32(vreinterpretq_u32_f32(a.v),vreinterpretq_u32_f32(b.v))));
00115 }
00116 template<> EIGEN_STRONG_INLINE Packet2cf pandnot<Packet2cf>(const Packet2cf& a, const Packet2cf& b)
00117 {
00118   return Packet2cf(vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(a.v),vreinterpretq_u32_f32(b.v))));
00119 }
00120 
00121 template<> EIGEN_STRONG_INLINE Packet2cf pload<Packet2cf>(const std::complex<float>* from) { EIGEN_DEBUG_ALIGNED_LOAD return Packet2cf(pload<Packet4f>((const float*)from)); }
00122 template<> EIGEN_STRONG_INLINE Packet2cf ploadu<Packet2cf>(const std::complex<float>* from) { EIGEN_DEBUG_UNALIGNED_LOAD return Packet2cf(ploadu<Packet4f>((const float*)from)); }
00123 
00124 template<> EIGEN_STRONG_INLINE Packet2cf ploaddup<Packet2cf>(const std::complex<float>* from) { return pset1<Packet2cf>(*from); }
00125 
00126 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); }
00127 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); }
00128 
00129 template<> EIGEN_STRONG_INLINE void prefetch<std::complex<float> >(const std::complex<float> *   addr) { __pld((float *)addr); }
00130 
00131 template<> EIGEN_STRONG_INLINE std::complex<float>  pfirst<Packet2cf>(const Packet2cf& a)
00132 {
00133   std::complex<float> EIGEN_ALIGN16 x[2];
00134   vst1q_f32((float *)x, a.v);
00135   return x[0];
00136 }
00137 
00138 template<> EIGEN_STRONG_INLINE Packet2cf preverse(const Packet2cf& a)
00139 {
00140   float32x2_t a_lo, a_hi;
00141   Packet4f a_r128;
00142 
00143   a_lo = vget_low_f32(a.v);
00144   a_hi = vget_high_f32(a.v);
00145   a_r128 = vcombine_f32(a_hi, a_lo);
00146 
00147   return Packet2cf(a_r128);
00148 }
00149 
00150 template<> EIGEN_STRONG_INLINE Packet2cf pcplxflip<Packet2cf>(const Packet2cf& a)
00151 {
00152   return Packet2cf(vrev64q_f32(a.v));
00153 }
00154 
00155 template<> EIGEN_STRONG_INLINE std::complex<float> predux<Packet2cf>(const Packet2cf& a)
00156 {
00157   float32x2_t a1, a2;
00158   std::complex<float> s;
00159 
00160   a1 = vget_low_f32(a.v);
00161   a2 = vget_high_f32(a.v);
00162   a2 = vadd_f32(a1, a2);
00163   vst1_f32((float *)&s, a2);
00164 
00165   return s;
00166 }
00167 
00168 template<> EIGEN_STRONG_INLINE Packet2cf preduxp<Packet2cf>(const Packet2cf* vecs)
00169 {
00170   Packet4f sum1, sum2, sum;
00171 
00172   // Add the first two 64-bit float32x2_t of vecs[0]
00173   sum1 = vcombine_f32(vget_low_f32(vecs[0].v), vget_low_f32(vecs[1].v));
00174   sum2 = vcombine_f32(vget_high_f32(vecs[0].v), vget_high_f32(vecs[1].v));
00175   sum = vaddq_f32(sum1, sum2);
00176 
00177   return Packet2cf(sum);
00178 }
00179 
00180 template<> EIGEN_STRONG_INLINE std::complex<float> predux_mul<Packet2cf>(const Packet2cf& a)
00181 {
00182   float32x2_t a1, a2, v1, v2, prod;
00183   std::complex<float> s;
00184 
00185   a1 = vget_low_f32(a.v);
00186   a2 = vget_high_f32(a.v);
00187    // Get the real values of a | a1_re | a1_re | a2_re | a2_re |
00188   v1 = vdup_lane_f32(a1, 0);
00189   // Get the real values of a | a1_im | a1_im | a2_im | a2_im |
00190   v2 = vdup_lane_f32(a1, 1);
00191   // Multiply the real a with b
00192   v1 = vmul_f32(v1, a2);
00193   // Multiply the imag a with b
00194   v2 = vmul_f32(v2, a2);
00195   // Conjugate v2 
00196   v2 = vreinterpret_f32_u32(veor_u32(vreinterpret_u32_f32(v2), p2ui_CONJ_XOR));
00197   // Swap real/imag elements in v2.
00198   v2 = vrev64_f32(v2);
00199   // Add v1, v2
00200   prod = vadd_f32(v1, v2);
00201 
00202   vst1_f32((float *)&s, prod);
00203 
00204   return s;
00205 }
00206 
00207 template<int Offset>
00208 struct palign_impl<Offset,Packet2cf>
00209 {
00210   EIGEN_STRONG_INLINE static void run(Packet2cf& first, const Packet2cf& second)
00211   {
00212     if (Offset==1)
00213     {
00214       first.v = vextq_f32(first.v, second.v, 2);
00215     }
00216   }
00217 };
00218 
00219 template<> struct conj_helper<Packet2cf, Packet2cf, false,true>
00220 {
00221   EIGEN_STRONG_INLINE Packet2cf pmadd(const Packet2cf& x, const Packet2cf& y, const Packet2cf& c) const
00222   { return padd(pmul(x,y),c); }
00223 
00224   EIGEN_STRONG_INLINE Packet2cf pmul(const Packet2cf& a, const Packet2cf& b) const
00225   {
00226     return internal::pmul(a, pconj(b));
00227   }
00228 };
00229 
00230 template<> struct conj_helper<Packet2cf, Packet2cf, true,false>
00231 {
00232   EIGEN_STRONG_INLINE Packet2cf pmadd(const Packet2cf& x, const Packet2cf& y, const Packet2cf& c) const
00233   { return padd(pmul(x,y),c); }
00234 
00235   EIGEN_STRONG_INLINE Packet2cf pmul(const Packet2cf& a, const Packet2cf& b) const
00236   {
00237     return internal::pmul(pconj(a), b);
00238   }
00239 };
00240 
00241 template<> struct conj_helper<Packet2cf, Packet2cf, true,true>
00242 {
00243   EIGEN_STRONG_INLINE Packet2cf pmadd(const Packet2cf& x, const Packet2cf& y, const Packet2cf& c) const
00244   { return padd(pmul(x,y),c); }
00245 
00246   EIGEN_STRONG_INLINE Packet2cf pmul(const Packet2cf& a, const Packet2cf& b) const
00247   {
00248     return pconj(internal::pmul(a, b));
00249   }
00250 };
00251 
00252 template<> EIGEN_STRONG_INLINE Packet2cf pdiv<Packet2cf>(const Packet2cf& a, const Packet2cf& b)
00253 {
00254   // TODO optimize it for AltiVec
00255   Packet2cf res = conj_helper<Packet2cf,Packet2cf,false,true>().pmul(a,b);
00256   Packet4f s, rev_s;
00257   float32x2_t a_lo, a_hi;
00258 
00259   // this computes the norm
00260   s = vmulq_f32(b.v, b.v);
00261   a_lo = vrev64_f32(vget_low_f32(s));
00262   a_hi = vrev64_f32(vget_high_f32(s));
00263   rev_s = vcombine_f32(a_lo, a_hi);
00264 
00265   return Packet2cf(pdiv(res.v, vaddq_f32(s,rev_s)));
00266 }
00267 
00268 } // end namespace internal
00269 
00270 #endif // EIGEN_COMPLEX_NEON_H


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:30:58