PacketMath.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) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 // Copyright (C) 2010 Konstantinos Margaritis <markos@codex.gr>
00006 // Heavily based on Gael's SSE version.
00007 //
00008 // Eigen is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU Lesser General Public
00010 // License as published by the Free Software Foundation; either
00011 // version 3 of the License, or (at your option) any later version.
00012 //
00013 // Alternatively, you can redistribute it and/or
00014 // modify it under the terms of the GNU General Public License as
00015 // published by the Free Software Foundation; either version 2 of
00016 // the License, or (at your option) any later version.
00017 //
00018 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00019 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00020 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00021 // GNU General Public License for more details.
00022 //
00023 // You should have received a copy of the GNU Lesser General Public
00024 // License and a copy of the GNU General Public License along with
00025 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 #ifndef EIGEN_PACKET_MATH_NEON_H
00028 #define EIGEN_PACKET_MATH_NEON_H
00029 
00030 namespace internal {
00031 
00032 #ifndef EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD
00033 #define EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD 8
00034 #endif
00035 
00036 // FIXME NEON has 16 quad registers, but since the current register allocator
00037 // is so bad, it is much better to reduce it to 8
00038 #ifndef EIGEN_ARCH_DEFAULT_NUMBER_OF_REGISTERS
00039 #define EIGEN_ARCH_DEFAULT_NUMBER_OF_REGISTERS 8
00040 #endif
00041 
00042 typedef float32x4_t Packet4f;
00043 typedef int32x4_t   Packet4i;
00044 typedef uint32x4_t  Packet4ui;
00045 
00046 #define _EIGEN_DECLARE_CONST_Packet4f(NAME,X) \
00047   const Packet4f p4f_##NAME = pset1<Packet4f>(X)
00048 
00049 #define _EIGEN_DECLARE_CONST_Packet4f_FROM_INT(NAME,X) \
00050   const Packet4f p4f_##NAME = vreinterpretq_f32_u32(pset1<int>(X))
00051 
00052 #define _EIGEN_DECLARE_CONST_Packet4i(NAME,X) \
00053   const Packet4i p4i_##NAME = pset1<Packet4i>(X)
00054 
00055 #ifndef __pld
00056 #define __pld(x) asm volatile ( "   pld [%[addr]]\n" :: [addr] "r" (x) : "cc" );
00057 #endif
00058 
00059 template<> struct packet_traits<float>  : default_packet_traits
00060 {
00061   typedef Packet4f type;
00062   enum {
00063     Vectorizable = 1,
00064     AlignedOnScalar = 1,
00065     size = 4,
00066    
00067     HasDiv  = 1,
00068     // FIXME check the Has*
00069     HasSin  = 0,
00070     HasCos  = 0,
00071     HasLog  = 0,
00072     HasExp  = 0,
00073     HasSqrt = 0
00074   };
00075 };
00076 template<> struct packet_traits<int>    : default_packet_traits
00077 {
00078   typedef Packet4i type;
00079   enum {
00080     Vectorizable = 1,
00081     AlignedOnScalar = 1,
00082     size=4
00083     // FIXME check the Has*
00084   };
00085 };
00086 
00087 #if EIGEN_GNUC_AT_MOST(4,4)
00088 // workaround gcc 4.2, 4.3 and 4.4 compilatin issue
00089 EIGEN_STRONG_INLINE float32x4_t vld1q_f32(const float* x) { return ::vld1q_f32((const float32_t*)x); }
00090 EIGEN_STRONG_INLINE float32x2_t vld1_f32 (const float* x) { return ::vld1_f32 ((const float32_t*)x); }
00091 EIGEN_STRONG_INLINE void        vst1q_f32(float* to, float32x4_t from) { ::vst1q_f32((float32_t*)to,from); }
00092 EIGEN_STRONG_INLINE void        vst1_f32 (float* to, float32x2_t from) { ::vst1_f32 ((float32_t*)to,from); }
00093 #endif
00094 
00095 template<> struct unpacket_traits<Packet4f> { typedef float  type; enum {size=4}; };
00096 template<> struct unpacket_traits<Packet4i> { typedef int    type; enum {size=4}; };
00097 
00098 template<> EIGEN_STRONG_INLINE Packet4f pset1<Packet4f>(const float&  from) { return vdupq_n_f32(from); }
00099 template<> EIGEN_STRONG_INLINE Packet4i pset1<Packet4i>(const int&    from)   { return vdupq_n_s32(from); }
00100 
00101 template<> EIGEN_STRONG_INLINE Packet4f plset<float>(const float& a)
00102 {
00103   Packet4f countdown = { 0, 1, 2, 3 };
00104   return vaddq_f32(pset1<Packet4f>(a), countdown);
00105 }
00106 template<> EIGEN_STRONG_INLINE Packet4i plset<int>(const int& a)
00107 {
00108   Packet4i countdown = { 0, 1, 2, 3 };
00109   return vaddq_s32(pset1<Packet4i>(a), countdown);
00110 }
00111 
00112 template<> EIGEN_STRONG_INLINE Packet4f padd<Packet4f>(const Packet4f& a, const Packet4f& b) { return vaddq_f32(a,b); }
00113 template<> EIGEN_STRONG_INLINE Packet4i padd<Packet4i>(const Packet4i& a, const Packet4i& b) { return vaddq_s32(a,b); }
00114 
00115 template<> EIGEN_STRONG_INLINE Packet4f psub<Packet4f>(const Packet4f& a, const Packet4f& b) { return vsubq_f32(a,b); }
00116 template<> EIGEN_STRONG_INLINE Packet4i psub<Packet4i>(const Packet4i& a, const Packet4i& b) { return vsubq_s32(a,b); }
00117 
00118 template<> EIGEN_STRONG_INLINE Packet4f pnegate(const Packet4f& a) { return vnegq_f32(a); }
00119 template<> EIGEN_STRONG_INLINE Packet4i pnegate(const Packet4i& a) { return vnegq_s32(a); }
00120 
00121 template<> EIGEN_STRONG_INLINE Packet4f pmul<Packet4f>(const Packet4f& a, const Packet4f& b) { return vmulq_f32(a,b); }
00122 template<> EIGEN_STRONG_INLINE Packet4i pmul<Packet4i>(const Packet4i& a, const Packet4i& b) { return vmulq_s32(a,b); }
00123 
00124 template<> EIGEN_STRONG_INLINE Packet4f pdiv<Packet4f>(const Packet4f& a, const Packet4f& b)
00125 {
00126   Packet4f inv, restep, div;
00127 
00128   // NEON does not offer a divide instruction, we have to do a reciprocal approximation
00129   // However NEON in contrast to other SIMD engines (AltiVec/SSE), offers
00130   // a reciprocal estimate AND a reciprocal step -which saves a few instructions
00131   // vrecpeq_f32() returns an estimate to 1/b, which we will finetune with
00132   // Newton-Raphson and vrecpsq_f32()
00133   inv = vrecpeq_f32(b);
00134 
00135   // This returns a differential, by which we will have to multiply inv to get a better
00136   // approximation of 1/b.
00137   restep = vrecpsq_f32(b, inv);
00138   inv = vmulq_f32(restep, inv);
00139 
00140   // Finally, multiply a by 1/b and get the wanted result of the division.
00141   div = vmulq_f32(a, inv);
00142 
00143   return div;
00144 }
00145 template<> EIGEN_STRONG_INLINE Packet4i pdiv<Packet4i>(const Packet4i& /*a*/, const Packet4i& /*b*/)
00146 { eigen_assert(false && "packet integer division are not supported by NEON");
00147   return pset1<Packet4i>(0);
00148 }
00149 
00150 // for some weird raisons, it has to be overloaded for packet of integers
00151 template<> EIGEN_STRONG_INLINE Packet4i pmadd(const Packet4i& a, const Packet4i& b, const Packet4i& c) { return padd(pmul(a,b), c); }
00152 
00153 template<> EIGEN_STRONG_INLINE Packet4f pmin<Packet4f>(const Packet4f& a, const Packet4f& b) { return vminq_f32(a,b); }
00154 template<> EIGEN_STRONG_INLINE Packet4i pmin<Packet4i>(const Packet4i& a, const Packet4i& b) { return vminq_s32(a,b); }
00155 
00156 template<> EIGEN_STRONG_INLINE Packet4f pmax<Packet4f>(const Packet4f& a, const Packet4f& b) { return vmaxq_f32(a,b); }
00157 template<> EIGEN_STRONG_INLINE Packet4i pmax<Packet4i>(const Packet4i& a, const Packet4i& b) { return vmaxq_s32(a,b); }
00158 
00159 // Logical Operations are not supported for float, so we have to reinterpret casts using NEON intrinsics
00160 template<> EIGEN_STRONG_INLINE Packet4f pand<Packet4f>(const Packet4f& a, const Packet4f& b)
00161 {
00162   return vreinterpretq_f32_u32(vandq_u32(vreinterpretq_u32_f32(a),vreinterpretq_u32_f32(b)));
00163 }
00164 template<> EIGEN_STRONG_INLINE Packet4i pand<Packet4i>(const Packet4i& a, const Packet4i& b) { return vandq_s32(a,b); }
00165 
00166 template<> EIGEN_STRONG_INLINE Packet4f por<Packet4f>(const Packet4f& a, const Packet4f& b)
00167 {
00168   return vreinterpretq_f32_u32(vorrq_u32(vreinterpretq_u32_f32(a),vreinterpretq_u32_f32(b)));
00169 }
00170 template<> EIGEN_STRONG_INLINE Packet4i por<Packet4i>(const Packet4i& a, const Packet4i& b) { return vorrq_s32(a,b); }
00171 
00172 template<> EIGEN_STRONG_INLINE Packet4f pxor<Packet4f>(const Packet4f& a, const Packet4f& b)
00173 {
00174   return vreinterpretq_f32_u32(veorq_u32(vreinterpretq_u32_f32(a),vreinterpretq_u32_f32(b)));
00175 }
00176 template<> EIGEN_STRONG_INLINE Packet4i pxor<Packet4i>(const Packet4i& a, const Packet4i& b) { return veorq_s32(a,b); }
00177 
00178 template<> EIGEN_STRONG_INLINE Packet4f pandnot<Packet4f>(const Packet4f& a, const Packet4f& b)
00179 {
00180   return vreinterpretq_f32_u32(vbicq_u32(vreinterpretq_u32_f32(a),vreinterpretq_u32_f32(b)));
00181 }
00182 template<> EIGEN_STRONG_INLINE Packet4i pandnot<Packet4i>(const Packet4i& a, const Packet4i& b) { return vbicq_s32(a,b); }
00183 
00184 template<> EIGEN_STRONG_INLINE Packet4f pload<Packet4f>(const float* from) { EIGEN_DEBUG_ALIGNED_LOAD return vld1q_f32(from); }
00185 template<> EIGEN_STRONG_INLINE Packet4i pload<Packet4i>(const int*   from) { EIGEN_DEBUG_ALIGNED_LOAD return vld1q_s32(from); }
00186 
00187 template<> EIGEN_STRONG_INLINE Packet4f ploadu<Packet4f>(const float* from) { EIGEN_DEBUG_UNALIGNED_LOAD return vld1q_f32(from); }
00188 template<> EIGEN_STRONG_INLINE Packet4i ploadu<Packet4i>(const int* from)   { EIGEN_DEBUG_UNALIGNED_LOAD return vld1q_s32(from); }
00189 
00190 template<> EIGEN_STRONG_INLINE Packet4f ploaddup<Packet4f>(const float*   from)
00191 {
00192   float32x2_t lo, hi;
00193   lo = vdup_n_f32(*from);
00194   hi = vdup_n_f32(*(from+1));
00195   return vcombine_f32(lo, hi);
00196 }
00197 template<> EIGEN_STRONG_INLINE Packet4i ploaddup<Packet4i>(const int*     from)
00198 {
00199   int32x2_t lo, hi;
00200   lo = vdup_n_s32(*from);
00201   hi = vdup_n_s32(*(from+1));
00202   return vcombine_s32(lo, hi);
00203 }
00204 
00205 template<> EIGEN_STRONG_INLINE void pstore<float>(float*   to, const Packet4f& from) { EIGEN_DEBUG_ALIGNED_STORE vst1q_f32(to, from); }
00206 template<> EIGEN_STRONG_INLINE void pstore<int>(int*       to, const Packet4i& from) { EIGEN_DEBUG_ALIGNED_STORE vst1q_s32(to, from); }
00207 
00208 template<> EIGEN_STRONG_INLINE void pstoreu<float>(float*  to, const Packet4f& from) { EIGEN_DEBUG_UNALIGNED_STORE vst1q_f32(to, from); }
00209 template<> EIGEN_STRONG_INLINE void pstoreu<int>(int*      to, const Packet4i& from) { EIGEN_DEBUG_UNALIGNED_STORE vst1q_s32(to, from); }
00210 
00211 template<> EIGEN_STRONG_INLINE void prefetch<float>(const float* addr) { __pld(addr); }
00212 template<> EIGEN_STRONG_INLINE void prefetch<int>(const int*     addr) { __pld(addr); }
00213 
00214 // FIXME only store the 2 first elements ?
00215 template<> EIGEN_STRONG_INLINE float  pfirst<Packet4f>(const Packet4f& a) { float EIGEN_ALIGN16 x[4]; vst1q_f32(x, a); return x[0]; }
00216 template<> EIGEN_STRONG_INLINE int    pfirst<Packet4i>(const Packet4i& a) { int   EIGEN_ALIGN16 x[4]; vst1q_s32(x, a); return x[0]; }
00217 
00218 template<> EIGEN_STRONG_INLINE Packet4f preverse(const Packet4f& a) {
00219   float32x2_t a_lo, a_hi;
00220   Packet4f a_r64;
00221 
00222   a_r64 = vrev64q_f32(a);
00223   a_lo = vget_low_f32(a_r64);
00224   a_hi = vget_high_f32(a_r64);
00225   return vcombine_f32(a_hi, a_lo);
00226 }
00227 template<> EIGEN_STRONG_INLINE Packet4i preverse(const Packet4i& a) {
00228   int32x2_t a_lo, a_hi;
00229   Packet4i a_r64;
00230 
00231   a_r64 = vrev64q_s32(a);
00232   a_lo = vget_low_s32(a_r64);
00233   a_hi = vget_high_s32(a_r64);
00234   return vcombine_s32(a_hi, a_lo);
00235 }
00236 template<> EIGEN_STRONG_INLINE Packet4f pabs(const Packet4f& a) { return vabsq_f32(a); }
00237 template<> EIGEN_STRONG_INLINE Packet4i pabs(const Packet4i& a) { return vabsq_s32(a); }
00238 
00239 template<> EIGEN_STRONG_INLINE float predux<Packet4f>(const Packet4f& a)
00240 {
00241   float32x2_t a_lo, a_hi, sum;
00242   float s[2];
00243 
00244   a_lo = vget_low_f32(a);
00245   a_hi = vget_high_f32(a);
00246   sum = vpadd_f32(a_lo, a_hi);
00247   sum = vpadd_f32(sum, sum);
00248   vst1_f32(s, sum);
00249 
00250   return s[0];
00251 }
00252 
00253 template<> EIGEN_STRONG_INLINE Packet4f preduxp<Packet4f>(const Packet4f* vecs)
00254 {
00255   float32x4x2_t vtrn1, vtrn2, res1, res2;
00256   Packet4f sum1, sum2, sum;
00257 
00258   // NEON zip performs interleaving of the supplied vectors.
00259   // We perform two interleaves in a row to acquire the transposed vector
00260   vtrn1 = vzipq_f32(vecs[0], vecs[2]);
00261   vtrn2 = vzipq_f32(vecs[1], vecs[3]);
00262   res1 = vzipq_f32(vtrn1.val[0], vtrn2.val[0]);
00263   res2 = vzipq_f32(vtrn1.val[1], vtrn2.val[1]);
00264 
00265   // Do the addition of the resulting vectors
00266   sum1 = vaddq_f32(res1.val[0], res1.val[1]);
00267   sum2 = vaddq_f32(res2.val[0], res2.val[1]);
00268   sum = vaddq_f32(sum1, sum2);
00269 
00270   return sum;
00271 }
00272 
00273 template<> EIGEN_STRONG_INLINE int predux<Packet4i>(const Packet4i& a)
00274 {
00275   int32x2_t a_lo, a_hi, sum;
00276   int32_t s[2];
00277 
00278   a_lo = vget_low_s32(a);
00279   a_hi = vget_high_s32(a);
00280   sum = vpadd_s32(a_lo, a_hi);
00281   sum = vpadd_s32(sum, sum);
00282   vst1_s32(s, sum);
00283 
00284   return s[0];
00285 }
00286 
00287 template<> EIGEN_STRONG_INLINE Packet4i preduxp<Packet4i>(const Packet4i* vecs)
00288 {
00289   int32x4x2_t vtrn1, vtrn2, res1, res2;
00290   Packet4i sum1, sum2, sum;
00291 
00292   // NEON zip performs interleaving of the supplied vectors.
00293   // We perform two interleaves in a row to acquire the transposed vector
00294   vtrn1 = vzipq_s32(vecs[0], vecs[2]);
00295   vtrn2 = vzipq_s32(vecs[1], vecs[3]);
00296   res1 = vzipq_s32(vtrn1.val[0], vtrn2.val[0]);
00297   res2 = vzipq_s32(vtrn1.val[1], vtrn2.val[1]);
00298 
00299   // Do the addition of the resulting vectors
00300   sum1 = vaddq_s32(res1.val[0], res1.val[1]);
00301   sum2 = vaddq_s32(res2.val[0], res2.val[1]);
00302   sum = vaddq_s32(sum1, sum2);
00303 
00304   return sum;
00305 }
00306 
00307 // Other reduction functions:
00308 // mul
00309 template<> EIGEN_STRONG_INLINE float predux_mul<Packet4f>(const Packet4f& a)
00310 {
00311   float32x2_t a_lo, a_hi, prod;
00312   float s[2];
00313 
00314   // Get a_lo = |a1|a2| and a_hi = |a3|a4|
00315   a_lo = vget_low_f32(a);
00316   a_hi = vget_high_f32(a);
00317   // Get the product of a_lo * a_hi -> |a1*a3|a2*a4|
00318   prod = vmul_f32(a_lo, a_hi);
00319   // Multiply prod with its swapped value |a2*a4|a1*a3|
00320   prod = vmul_f32(prod, vrev64_f32(prod));
00321   vst1_f32(s, prod);
00322 
00323   return s[0];
00324 }
00325 template<> EIGEN_STRONG_INLINE int predux_mul<Packet4i>(const Packet4i& a)
00326 {
00327   int32x2_t a_lo, a_hi, prod;
00328   int32_t s[2];
00329 
00330   // Get a_lo = |a1|a2| and a_hi = |a3|a4|
00331   a_lo = vget_low_s32(a);
00332   a_hi = vget_high_s32(a);
00333   // Get the product of a_lo * a_hi -> |a1*a3|a2*a4|
00334   prod = vmul_s32(a_lo, a_hi);
00335   // Multiply prod with its swapped value |a2*a4|a1*a3|
00336   prod = vmul_s32(prod, vrev64_s32(prod));
00337   vst1_s32(s, prod);
00338 
00339   return s[0];
00340 }
00341 
00342 // min
00343 template<> EIGEN_STRONG_INLINE float predux_min<Packet4f>(const Packet4f& a)
00344 {
00345   float32x2_t a_lo, a_hi, min;
00346   float s[2];
00347 
00348   a_lo = vget_low_f32(a);
00349   a_hi = vget_high_f32(a);
00350   min = vpmin_f32(a_lo, a_hi);
00351   min = vpmin_f32(min, min);
00352   vst1_f32(s, min);
00353 
00354   return s[0];
00355 }
00356 template<> EIGEN_STRONG_INLINE int predux_min<Packet4i>(const Packet4i& a)
00357 {
00358   int32x2_t a_lo, a_hi, min;
00359   int32_t s[2];
00360 
00361   a_lo = vget_low_s32(a);
00362   a_hi = vget_high_s32(a);
00363   min = vpmin_s32(a_lo, a_hi);
00364   min = vpmin_s32(min, min);
00365   vst1_s32(s, min);
00366 
00367   return s[0];
00368 }
00369 
00370 // max
00371 template<> EIGEN_STRONG_INLINE float predux_max<Packet4f>(const Packet4f& a)
00372 {
00373   float32x2_t a_lo, a_hi, max;
00374   float s[2];
00375 
00376   a_lo = vget_low_f32(a);
00377   a_hi = vget_high_f32(a);
00378   max = vpmax_f32(a_lo, a_hi);
00379   max = vpmax_f32(max, max);
00380   vst1_f32(s, max);
00381 
00382   return s[0];
00383 }
00384 template<> EIGEN_STRONG_INLINE int predux_max<Packet4i>(const Packet4i& a)
00385 {
00386   int32x2_t a_lo, a_hi, max;
00387   int32_t s[2];
00388 
00389   a_lo = vget_low_s32(a);
00390   a_hi = vget_high_s32(a);
00391   max = vpmax_s32(a_lo, a_hi);
00392   max = vpmax_s32(max, max);
00393   vst1_s32(s, max);
00394 
00395   return s[0];
00396 }
00397 
00398 template<int Offset>
00399 struct palign_impl<Offset,Packet4f>
00400 {
00401   EIGEN_STRONG_INLINE static void run(Packet4f& first, const Packet4f& second)
00402   {
00403     if (Offset!=0)
00404       first = vextq_f32(first, second, Offset);
00405   }
00406 };
00407 
00408 template<int Offset>
00409 struct palign_impl<Offset,Packet4i>
00410 {
00411   EIGEN_STRONG_INLINE static void run(Packet4i& first, const Packet4i& second)
00412   {
00413     if (Offset!=0)
00414       first = vextq_s32(first, second, Offset);
00415   }
00416 };
00417 
00418 } // end namespace internal
00419 
00420 #endif // EIGEN_PACKET_MATH_NEON_H


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