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