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


win_eigen
Author(s): Daniel Stonier
autogenerated on Mon Oct 6 2014 12:24:18