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