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


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