Main MRPT website > C++ reference
MRPT logo

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



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:16:28 UTC 2011