Home | History | Annotate | Download | only in AltiVec
      1 // This file is part of Eigen, a lightweight C++ template library
      2 // for linear algebra.
      3 //
      4 // Copyright (C) 2010 Gael Guennebaud <gael.guennebaud (at) inria.fr>
      5 //
      6 // This Source Code Form is subject to the terms of the Mozilla
      7 // Public License v. 2.0. If a copy of the MPL was not distributed
      8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
      9 
     10 #ifndef EIGEN_COMPLEX_ALTIVEC_H
     11 #define EIGEN_COMPLEX_ALTIVEC_H
     12 
     13 namespace Eigen {
     14 
     15 namespace internal {
     16 
     17 static Packet4ui  p4ui_CONJ_XOR = vec_mergeh((Packet4ui)p4i_ZERO, (Packet4ui)p4f_ZERO_);//{ 0x00000000, 0x80000000, 0x00000000, 0x80000000 };
     18 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 };
     19 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 };
     20 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 };
     21 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 };
     22 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 };
     23 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 };
     24 
     25 //---------- float ----------
     26 struct Packet2cf
     27 {
     28   EIGEN_STRONG_INLINE Packet2cf() {}
     29   EIGEN_STRONG_INLINE explicit Packet2cf(const Packet4f& a) : v(a) {}
     30   Packet4f  v;
     31 };
     32 
     33 template<> struct packet_traits<std::complex<float> >  : default_packet_traits
     34 {
     35   typedef Packet2cf type;
     36   enum {
     37     Vectorizable = 1,
     38     AlignedOnScalar = 1,
     39     size = 2,
     40 
     41     HasAdd    = 1,
     42     HasSub    = 1,
     43     HasMul    = 1,
     44     HasDiv    = 1,
     45     HasNegate = 1,
     46     HasAbs    = 0,
     47     HasAbs2   = 0,
     48     HasMin    = 0,
     49     HasMax    = 0,
     50     HasSetLinear = 0
     51   };
     52 };
     53 
     54 template<> struct unpacket_traits<Packet2cf> { typedef std::complex<float> type; enum {size=2}; };
     55 
     56 template<> EIGEN_STRONG_INLINE Packet2cf pset1<Packet2cf>(const std::complex<float>&  from)
     57 {
     58   Packet2cf res;
     59   /* On AltiVec we cannot load 64-bit registers, so wa have to take care of alignment */
     60   if((ptrdiff_t(&from) % 16) == 0)
     61     res.v = pload<Packet4f>((const float *)&from);
     62   else
     63     res.v = ploadu<Packet4f>((const float *)&from);
     64   res.v = vec_perm(res.v, res.v, p16uc_PSET_HI);
     65   return res;
     66 }
     67 
     68 template<> EIGEN_STRONG_INLINE Packet2cf padd<Packet2cf>(const Packet2cf& a, const Packet2cf& b) { return Packet2cf(vec_add(a.v,b.v)); }
     69 template<> EIGEN_STRONG_INLINE Packet2cf psub<Packet2cf>(const Packet2cf& a, const Packet2cf& b) { return Packet2cf(vec_sub(a.v,b.v)); }
     70 template<> EIGEN_STRONG_INLINE Packet2cf pnegate(const Packet2cf& a) { return Packet2cf(pnegate(a.v)); }
     71 template<> EIGEN_STRONG_INLINE Packet2cf pconj(const Packet2cf& a) { return Packet2cf((Packet4f)vec_xor((Packet4ui)a.v, p4ui_CONJ_XOR)); }
     72 
     73 template<> EIGEN_STRONG_INLINE Packet2cf pmul<Packet2cf>(const Packet2cf& a, const Packet2cf& b)
     74 {
     75   Packet4f v1, v2;
     76 
     77   // Permute and multiply the real parts of a and b
     78   v1 = vec_perm(a.v, a.v, p16uc_COMPLEX_RE);
     79   // Get the imaginary parts of a
     80   v2 = vec_perm(a.v, a.v, p16uc_COMPLEX_IM);
     81   // multiply a_re * b
     82   v1 = vec_madd(v1, b.v, p4f_ZERO);
     83   // multiply a_im * b and get the conjugate result
     84   v2 = vec_madd(v2, b.v, p4f_ZERO);
     85   v2 = (Packet4f) vec_xor((Packet4ui)v2, p4ui_CONJ_XOR);
     86   // permute back to a proper order
     87   v2 = vec_perm(v2, v2, p16uc_COMPLEX_REV);
     88 
     89   return Packet2cf(vec_add(v1, v2));
     90 }
     91 
     92 template<> EIGEN_STRONG_INLINE Packet2cf pand   <Packet2cf>(const Packet2cf& a, const Packet2cf& b) { return Packet2cf(vec_and(a.v,b.v)); }
     93 template<> EIGEN_STRONG_INLINE Packet2cf por    <Packet2cf>(const Packet2cf& a, const Packet2cf& b) { return Packet2cf(vec_or(a.v,b.v)); }
     94 template<> EIGEN_STRONG_INLINE Packet2cf pxor   <Packet2cf>(const Packet2cf& a, const Packet2cf& b) { return Packet2cf(vec_xor(a.v,b.v)); }
     95 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))); }
     96 
     97 template<> EIGEN_STRONG_INLINE Packet2cf pload <Packet2cf>(const std::complex<float>* from) { EIGEN_DEBUG_ALIGNED_LOAD return Packet2cf(pload<Packet4f>((const float*)from)); }
     98 template<> EIGEN_STRONG_INLINE Packet2cf ploadu<Packet2cf>(const std::complex<float>* from) { EIGEN_DEBUG_UNALIGNED_LOAD return Packet2cf(ploadu<Packet4f>((const float*)from)); }
     99 
    100 template<> EIGEN_STRONG_INLINE Packet2cf ploaddup<Packet2cf>(const std::complex<float>*     from)
    101 {
    102   return pset1<Packet2cf>(*from);
    103 }
    104 
    105 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); }
    106 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); }
    107 
    108 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); }
    109 
    110 template<> EIGEN_STRONG_INLINE std::complex<float>  pfirst<Packet2cf>(const Packet2cf& a)
    111 {
    112   std::complex<float> EIGEN_ALIGN16 res[2];
    113   pstore((float *)&res, a.v);
    114 
    115   return res[0];
    116 }
    117 
    118 template<> EIGEN_STRONG_INLINE Packet2cf preverse(const Packet2cf& a)
    119 {
    120   Packet4f rev_a;
    121   rev_a = vec_perm(a.v, a.v, p16uc_COMPLEX_REV2);
    122   return Packet2cf(rev_a);
    123 }
    124 
    125 template<> EIGEN_STRONG_INLINE std::complex<float> predux<Packet2cf>(const Packet2cf& a)
    126 {
    127   Packet4f b;
    128   b = (Packet4f) vec_sld(a.v, a.v, 8);
    129   b = padd(a.v, b);
    130   return pfirst(Packet2cf(b));
    131 }
    132 
    133 template<> EIGEN_STRONG_INLINE Packet2cf preduxp<Packet2cf>(const Packet2cf* vecs)
    134 {
    135   Packet4f b1, b2;
    136 
    137   b1 = (Packet4f) vec_sld(vecs[0].v, vecs[1].v, 8);
    138   b2 = (Packet4f) vec_sld(vecs[1].v, vecs[0].v, 8);
    139   b2 = (Packet4f) vec_sld(b2, b2, 8);
    140   b2 = padd(b1, b2);
    141 
    142   return Packet2cf(b2);
    143 }
    144 
    145 template<> EIGEN_STRONG_INLINE std::complex<float> predux_mul<Packet2cf>(const Packet2cf& a)
    146 {
    147   Packet4f b;
    148   Packet2cf prod;
    149   b = (Packet4f) vec_sld(a.v, a.v, 8);
    150   prod = pmul(a, Packet2cf(b));
    151 
    152   return pfirst(prod);
    153 }
    154 
    155 template<int Offset>
    156 struct palign_impl<Offset,Packet2cf>
    157 {
    158   static EIGEN_STRONG_INLINE void run(Packet2cf& first, const Packet2cf& second)
    159   {
    160     if (Offset==1)
    161     {
    162       first.v = vec_sld(first.v, second.v, 8);
    163     }
    164   }
    165 };
    166 
    167 template<> struct conj_helper<Packet2cf, Packet2cf, false,true>
    168 {
    169   EIGEN_STRONG_INLINE Packet2cf pmadd(const Packet2cf& x, const Packet2cf& y, const Packet2cf& c) const
    170   { return padd(pmul(x,y),c); }
    171 
    172   EIGEN_STRONG_INLINE Packet2cf pmul(const Packet2cf& a, const Packet2cf& b) const
    173   {
    174     return internal::pmul(a, pconj(b));
    175   }
    176 };
    177 
    178 template<> struct conj_helper<Packet2cf, Packet2cf, true,false>
    179 {
    180   EIGEN_STRONG_INLINE Packet2cf pmadd(const Packet2cf& x, const Packet2cf& y, const Packet2cf& c) const
    181   { return padd(pmul(x,y),c); }
    182 
    183   EIGEN_STRONG_INLINE Packet2cf pmul(const Packet2cf& a, const Packet2cf& b) const
    184   {
    185     return internal::pmul(pconj(a), b);
    186   }
    187 };
    188 
    189 template<> struct conj_helper<Packet2cf, Packet2cf, true,true>
    190 {
    191   EIGEN_STRONG_INLINE Packet2cf pmadd(const Packet2cf& x, const Packet2cf& y, const Packet2cf& c) const
    192   { return padd(pmul(x,y),c); }
    193 
    194   EIGEN_STRONG_INLINE Packet2cf pmul(const Packet2cf& a, const Packet2cf& b) const
    195   {
    196     return pconj(internal::pmul(a, b));
    197   }
    198 };
    199 
    200 template<> EIGEN_STRONG_INLINE Packet2cf pdiv<Packet2cf>(const Packet2cf& a, const Packet2cf& b)
    201 {
    202   // TODO optimize it for AltiVec
    203   Packet2cf res = conj_helper<Packet2cf,Packet2cf,false,true>().pmul(a,b);
    204   Packet4f s = vec_madd(b.v, b.v, p4f_ZERO);
    205   return Packet2cf(pdiv(res.v, vec_add(s,vec_perm(s, s, p16uc_COMPLEX_REV))));
    206 }
    207 
    208 template<> EIGEN_STRONG_INLINE Packet2cf pcplxflip<Packet2cf>(const Packet2cf& x)
    209 {
    210   return Packet2cf(vec_perm(x.v, x.v, p16uc_COMPLEX_REV));
    211 }
    212 
    213 } // end namespace internal
    214 
    215 } // end namespace Eigen
    216 
    217 #endif // EIGEN_COMPLEX_ALTIVEC_H
    218