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_ALTIVEC_H
00026 #define EIGEN_COMPLEX_ALTIVEC_H
00027 
00028 namespace internal {
00029 
00030 static Packet4ui  p4ui_CONJ_XOR = vec_mergeh((Packet4ui)p4i_ZERO, (Packet4ui)p4f_ZERO_);//{ 0x00000000, 0x80000000, 0x00000000, 0x80000000 };
00031 static Packet16uc p16uc_COMPLEX_RE   = vec_sld((Packet16uc) vec_splat((Packet4ui)p16uc_FORWARD, 0), (Packet16uc) vec_splat((Packet4ui)p16uc_FORWARD, 2), 8);//{ 0,1,2,3, 0,1,2,3, 8,9,10,11, 8,9,10,11 };
00032 static Packet16uc p16uc_COMPLEX_IM   = vec_sld((Packet16uc) vec_splat((Packet4ui)p16uc_FORWARD, 1), (Packet16uc) vec_splat((Packet4ui)p16uc_FORWARD, 3), 8);//{ 4,5,6,7, 4,5,6,7, 12,13,14,15, 12,13,14,15 };
00033 static Packet16uc p16uc_COMPLEX_REV  = vec_sld(p16uc_REVERSE, p16uc_REVERSE, 8);//{ 4,5,6,7, 0,1,2,3, 12,13,14,15, 8,9,10,11 };
00034 static Packet16uc p16uc_COMPLEX_REV2 = vec_sld(p16uc_FORWARD, p16uc_FORWARD, 8);//{ 8,9,10,11, 12,13,14,15, 0,1,2,3, 4,5,6,7 };
00035 static Packet16uc p16uc_PSET_HI = (Packet16uc) vec_mergeh((Packet4ui) vec_splat((Packet4ui)p16uc_FORWARD, 0), (Packet4ui) vec_splat((Packet4ui)p16uc_FORWARD, 1));//{ 0,1,2,3, 4,5,6,7, 0,1,2,3, 4,5,6,7 };
00036 static Packet16uc p16uc_PSET_LO = (Packet16uc) vec_mergeh((Packet4ui) vec_splat((Packet4ui)p16uc_FORWARD, 2), (Packet4ui) vec_splat((Packet4ui)p16uc_FORWARD, 3));//{ 8,9,10,11, 12,13,14,15, 8,9,10,11, 12,13,14,15 };
00037 
00038 //---------- float ----------
00039 struct Packet2cf
00040 {
00041   EIGEN_STRONG_INLINE Packet2cf() {}
00042   EIGEN_STRONG_INLINE explicit Packet2cf(const Packet4f& a) : v(a) {}
00043   Packet4f  v;
00044 };
00045 
00046 template<> struct packet_traits<std::complex<float> >  : default_packet_traits
00047 {
00048   typedef Packet2cf type;
00049   enum {
00050     Vectorizable = 1,
00051     size = 2,
00052 
00053     HasAdd    = 1,
00054     HasSub    = 1,
00055     HasMul    = 1,
00056     HasDiv    = 1,
00057     HasNegate = 1,
00058     HasAbs    = 0,
00059     HasAbs2   = 0,
00060     HasMin    = 0,
00061     HasMax    = 0,
00062     HasSetLinear = 0
00063   };
00064 };
00065 
00066 template<> struct unpacket_traits<Packet2cf> { typedef std::complex<float> type; enum {size=2}; };
00067 
00068 template<> EIGEN_STRONG_INLINE Packet2cf pset1<Packet2cf>(const std::complex<float>&  from)
00069 {
00070   Packet2cf res;
00071   /* On AltiVec we cannot load 64-bit registers, so wa have to take care of alignment */
00072   if ((ptrdiff_t)&from % 16 == 0) {
00073     res.v = pload((const float *)&from);
00074     res.v = vec_perm(res.v, res.v, p16uc_PSET_HI);
00075   } else {
00076     res.v = ploadu((const float *)&from);
00077     res.v = vec_perm(res.v, res.v, p16uc_PSET_LO);
00078   }
00079   return res;
00080 }
00081 
00082 template<> EIGEN_STRONG_INLINE Packet2cf padd<Packet2cf>(const Packet2cf& a, const Packet2cf& b) { return Packet2cf(vec_add(a.v,b.v)); }
00083 template<> EIGEN_STRONG_INLINE Packet2cf psub<Packet2cf>(const Packet2cf& a, const Packet2cf& b) { return Packet2cf(vec_sub(a.v,b.v)); }
00084 template<> EIGEN_STRONG_INLINE Packet2cf pnegate(const Packet2cf& a) { return Packet2cf(psub<Packet4f>(p4f_ZERO, a.v)); }
00085 template<> EIGEN_STRONG_INLINE Packet2cf pconj(const Packet2cf& a) { return Packet2cf((Packet4f)vec_xor((Packet4ui)a.v, p4ui_CONJ_XOR)); }
00086 
00087 template<> EIGEN_STRONG_INLINE Packet2cf pmul<Packet2cf>(const Packet2cf& a, const Packet2cf& b)
00088 {
00089   Packet4f v1, v2;
00090 
00091   // Permute and multiply the real parts of a and b
00092   v1 = vec_perm(a.v, a.v, p16uc_COMPLEX_RE);
00093   // Get the imaginary parts of a
00094   v2 = vec_perm(a.v, a.v, p16uc_COMPLEX_IM);
00095   // multiply a_re * b 
00096   v1 = vec_madd(v1, b.v, p4f_ZERO);
00097   // multiply a_im * b and get the conjugate result
00098   v2 = vec_madd(v2, b.v, p4f_ZERO);
00099   v2 = (Packet4f) vec_xor((Packet4ui)v2, p4ui_CONJ_XOR);
00100   // permute back to a proper order
00101   v2 = vec_perm(v2, v2, p16uc_COMPLEX_REV);
00102   
00103   return Packet2cf(vec_add(v1, v2));
00104 }
00105 
00106 template<> EIGEN_STRONG_INLINE Packet2cf pand   <Packet2cf>(const Packet2cf& a, const Packet2cf& b) { return Packet2cf(vec_and(a.v,b.v)); }
00107 template<> EIGEN_STRONG_INLINE Packet2cf por    <Packet2cf>(const Packet2cf& a, const Packet2cf& b) { return Packet2cf(vec_or(a.v,b.v)); }
00108 template<> EIGEN_STRONG_INLINE Packet2cf pxor   <Packet2cf>(const Packet2cf& a, const Packet2cf& b) { return Packet2cf(vec_xor(a.v,b.v)); }
00109 template<> EIGEN_STRONG_INLINE Packet2cf pandnot<Packet2cf>(const Packet2cf& a, const Packet2cf& b) { return Packet2cf(vec_and(a.v, vec_nor(b.v,b.v))); }
00110 
00111 template<> EIGEN_STRONG_INLINE Packet2cf pload <std::complex<float> >(const std::complex<float>* from) { EIGEN_DEBUG_ALIGNED_LOAD return Packet2cf(pload((const float*)from)); }
00112 template<> EIGEN_STRONG_INLINE Packet2cf ploadu<std::complex<float> >(const std::complex<float>* from) { EIGEN_DEBUG_UNALIGNED_LOAD return Packet2cf(ploadu((const float*)from)); }
00113 
00114 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); }
00115 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); }
00116 
00117 template<> EIGEN_STRONG_INLINE void prefetch<std::complex<float> >(const std::complex<float> *   addr) { vec_dstt((float *)addr, DST_CTRL(2,2,32), DST_CHAN); }
00118 
00119 template<> EIGEN_STRONG_INLINE std::complex<float>  pfirst<Packet2cf>(const Packet2cf& a)
00120 {
00121   std::complex<float> EIGEN_ALIGN16 res[2];
00122   pstore((float *)&res, a.v);
00123 
00124   return res[0];
00125 }
00126 
00127 template<> EIGEN_STRONG_INLINE Packet2cf preverse(const Packet2cf& a)
00128 {
00129   Packet4f rev_a;
00130   rev_a = vec_perm(a.v, a.v, p16uc_COMPLEX_REV2);
00131   return Packet2cf(rev_a);
00132 }
00133 
00134 template<> EIGEN_STRONG_INLINE std::complex<float> predux<Packet2cf>(const Packet2cf& a)
00135 {
00136   Packet4f b;
00137   b = (Packet4f) vec_sld(a.v, a.v, 8);
00138   b = padd(a.v, b);
00139   return pfirst(Packet2cf(sum));
00140 }
00141 
00142 template<> EIGEN_STRONG_INLINE Packet2cf preduxp<Packet2cf>(const Packet2cf* vecs)
00143 {
00144   Packet4f b1, b2;
00145   
00146   b1 = (Packet4f) vec_sld(vecs[0].v, vecs[1].v, 8);
00147   b2 = (Packet4f) vec_sld(vecs[1].v, vecs[0].v, 8);
00148   b2 = (Packet4f) vec_sld(b2, b2, 8);
00149   b2 = padd(b1, b2);
00150 
00151   return Packet2cf(b2);
00152 }
00153 
00154 template<> EIGEN_STRONG_INLINE std::complex<float> predux_mul<Packet2cf>(const Packet2cf& a)
00155 {
00156   Packet4f b;
00157   Packet2cf prod;
00158   b = (Packet4f) vec_sld(a.v, a.v, 8);
00159   prod = pmul(a, Packet2cf(b));
00160 
00161   return pfirst(prod);
00162 }
00163 
00164 template<int Offset>
00165 struct palign_impl<Offset,Packet2cf>
00166 {
00167   EIGEN_STRONG_INLINE static void run(Packet2cf& first, const Packet2cf& second)
00168   {
00169     if (Offset==1)
00170     {
00171       first.v = vec_sld(first.v, second.v, 8);
00172     }
00173   }
00174 };
00175 
00176 template<> struct conj_helper<Packet2cf, Packet2cf, false,true>
00177 {
00178   EIGEN_STRONG_INLINE Packet2cf pmadd(const Packet2cf& x, const Packet2cf& y, const Packet2cf& c) const
00179   { return padd(pmul(x,y),c); }
00180 
00181   EIGEN_STRONG_INLINE Packet2cf pmul(const Packet2cf& a, const Packet2cf& b) const
00182   {
00183     return pmul(a, pconj(b));
00184   }
00185 };
00186 
00187 template<> struct conj_helper<Packet2cf, Packet2cf, true,false>
00188 {
00189   EIGEN_STRONG_INLINE Packet2cf pmadd(const Packet2cf& x, const Packet2cf& y, const Packet2cf& c) const
00190   { return padd(pmul(x,y),c); }
00191 
00192   EIGEN_STRONG_INLINE Packet2cf pmul(const Packet2cf& a, const Packet2cf& b) const
00193   {
00194     return pmul(pconj(a), b);
00195   }
00196 };
00197 
00198 template<> struct conj_helper<Packet2cf, Packet2cf, true,true>
00199 {
00200   EIGEN_STRONG_INLINE Packet2cf pmadd(const Packet2cf& x, const Packet2cf& y, const Packet2cf& c) const
00201   { return padd(pmul(x,y),c); }
00202 
00203   EIGEN_STRONG_INLINE Packet2cf pmul(const Packet2cf& a, const Packet2cf& b) const
00204   {
00205     return pconj(pmul(a, b));
00206   }
00207 };
00208 
00209 template<> EIGEN_STRONG_INLINE Packet2cf pdiv<Packet2cf>(const Packet2cf& a, const Packet2cf& b)
00210 {
00211   // TODO optimize it for AltiVec
00212   Packet2cf res = conj_helper<Packet2cf,Packet2cf,false,true>().pmul(a,b);
00213   Packet4f s = vec_madd(b.v, b.v, p4f_ZERO);
00214   return Packet2cf(pdiv(res.v, vec_add(s,vec_perm(s, s, p16uc_COMPLEX_REV))));
00215 }
00216 
00217 } // end namespace internal
00218 
00219 #endif // EIGEN_COMPLEX_ALTIVEC_H



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN:exported at Tue Jan 25 21:56:31 UTC 2011