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


win_eigen
Author(s): Daniel Stonier
autogenerated on Wed Sep 16 2015 07:11:18