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


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Thu Aug 27 2015 11:58:00