media/libtheora/lib/ocintrin.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libtheora/lib/ocintrin.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,128 @@
     1.4 +/********************************************************************
     1.5 + *                                                                  *
     1.6 + * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE.   *
     1.7 + * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
     1.8 + * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
     1.9 + * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
    1.10 + *                                                                  *
    1.11 + * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009                *
    1.12 + * by the Xiph.Org Foundation and contributors http://www.xiph.org/ *
    1.13 + *                                                                  *
    1.14 + ********************************************************************
    1.15 +
    1.16 +  function:
    1.17 +    last mod: $Id: ocintrin.h 16503 2009-08-22 18:14:02Z giles $
    1.18 +
    1.19 + ********************************************************************/
    1.20 +
    1.21 +/*Some common macros for potential platform-specific optimization.*/
    1.22 +#include <math.h>
    1.23 +#if !defined(_ocintrin_H)
    1.24 +# define _ocintrin_H (1)
    1.25 +
    1.26 +/*Some specific platforms may have optimized intrinsic or inline assembly
    1.27 +   versions of these functions which can substantially improve performance.
    1.28 +  We define macros for them to allow easy incorporation of these non-ANSI
    1.29 +   features.*/
    1.30 +
    1.31 +/*Note that we do not provide a macro for abs(), because it is provided as a
    1.32 +   library function, which we assume is translated into an intrinsic to avoid
    1.33 +   the function call overhead and then implemented in the smartest way for the
    1.34 +   target platform.
    1.35 +  With modern gcc (4.x), this is true: it uses cmov instructions if the
    1.36 +   architecture supports it and branchless bit-twiddling if it does not (the
    1.37 +   speed difference between the two approaches is not measurable).
    1.38 +  Interestingly, the bit-twiddling method was patented in 2000 (US 6,073,150)
    1.39 +   by Sun Microsystems, despite prior art dating back to at least 1996:
    1.40 +   http://web.archive.org/web/19961201174141/www.x86.org/ftp/articles/pentopt/PENTOPT.TXT
    1.41 +  On gcc 3.x, however, our assumption is not true, as abs() is translated to a
    1.42 +   conditional jump, which is horrible on deeply piplined architectures (e.g.,
    1.43 +   all consumer architectures for the past decade or more).
    1.44 +  Also be warned that -C*abs(x) where C is a constant is mis-optimized as
    1.45 +   abs(C*x) on every gcc release before 4.2.3.
    1.46 +  See bug http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34130 */
    1.47 +
    1.48 +/*Modern gcc (4.x) can compile the naive versions of min and max with cmov if
    1.49 +   given an appropriate architecture, but the branchless bit-twiddling versions
    1.50 +   are just as fast, and do not require any special target architecture.
    1.51 +  Earlier gcc versions (3.x) compiled both code to the same assembly
    1.52 +   instructions, because of the way they represented ((_b)>(_a)) internally.*/
    1.53 +#define OC_MAXI(_a,_b)      ((_a)-((_a)-(_b)&-((_b)>(_a))))
    1.54 +#define OC_MINI(_a,_b)      ((_a)+((_b)-(_a)&-((_b)<(_a))))
    1.55 +/*Clamps an integer into the given range.
    1.56 +  If _a>_c, then the lower bound _a is respected over the upper bound _c (this
    1.57 +   behavior is required to meet our documented API behavior).
    1.58 +  _a: The lower bound.
    1.59 +  _b: The value to clamp.
    1.60 +  _c: The upper boud.*/
    1.61 +#define OC_CLAMPI(_a,_b,_c) (OC_MAXI(_a,OC_MINI(_b,_c)))
    1.62 +#define OC_CLAMP255(_x)     ((unsigned char)((((_x)<0)-1)&((_x)|-((_x)>255))))
    1.63 +/*This has a chance of compiling branchless, and is just as fast as the
    1.64 +   bit-twiddling method, which is slightly less portable, since it relies on a
    1.65 +   sign-extended rightshift, which is not guaranteed by ANSI (but present on
    1.66 +   every relevant platform).*/
    1.67 +#define OC_SIGNI(_a)        (((_a)>0)-((_a)<0))
    1.68 +/*Slightly more portable than relying on a sign-extended right-shift (which is
    1.69 +   not guaranteed by ANSI), and just as fast, since gcc (3.x and 4.x both)
    1.70 +   compile it into the right-shift anyway.*/
    1.71 +#define OC_SIGNMASK(_a)     (-((_a)<0))
    1.72 +/*Divides an integer by a power of two, truncating towards 0.
    1.73 +  _dividend: The integer to divide.
    1.74 +  _shift:    The non-negative power of two to divide by.
    1.75 +  _rmask:    (1<<_shift)-1*/
    1.76 +#define OC_DIV_POW2(_dividend,_shift,_rmask)\
    1.77 +  ((_dividend)+(OC_SIGNMASK(_dividend)&(_rmask))>>(_shift))
    1.78 +/*Divides _x by 65536, truncating towards 0.*/
    1.79 +#define OC_DIV2_16(_x) OC_DIV_POW2(_x,16,0xFFFF)
    1.80 +/*Divides _x by 2, truncating towards 0.*/
    1.81 +#define OC_DIV2(_x) OC_DIV_POW2(_x,1,0x1)
    1.82 +/*Divides _x by 8, truncating towards 0.*/
    1.83 +#define OC_DIV8(_x) OC_DIV_POW2(_x,3,0x7)
    1.84 +/*Divides _x by 16, truncating towards 0.*/
    1.85 +#define OC_DIV16(_x) OC_DIV_POW2(_x,4,0xF)
    1.86 +/*Right shifts _dividend by _shift, adding _rval, and subtracting one for
    1.87 +   negative dividends first.
    1.88 +  When _rval is (1<<_shift-1), this is equivalent to division with rounding
    1.89 +   ties away from zero.*/
    1.90 +#define OC_DIV_ROUND_POW2(_dividend,_shift,_rval)\
    1.91 +  ((_dividend)+OC_SIGNMASK(_dividend)+(_rval)>>(_shift))
    1.92 +/*Divides a _x by 2, rounding towards even numbers.*/
    1.93 +#define OC_DIV2_RE(_x) ((_x)+((_x)>>1&1)>>1)
    1.94 +/*Divides a _x by (1<<(_shift)), rounding towards even numbers.*/
    1.95 +#define OC_DIV_POW2_RE(_x,_shift) \
    1.96 +  ((_x)+((_x)>>(_shift)&1)+((1<<(_shift))-1>>1)>>(_shift))
    1.97 +/*Swaps two integers _a and _b if _a>_b.*/
    1.98 +#define OC_SORT2I(_a,_b) \
    1.99 +  do{ \
   1.100 +    int t__; \
   1.101 +    t__=((_a)^(_b))&-((_b)<(_a)); \
   1.102 +    (_a)^=t__; \
   1.103 +    (_b)^=t__; \
   1.104 +  } \
   1.105 +  while(0)
   1.106 +
   1.107 +/*Accesses one of four (signed) bytes given an index.
   1.108 +  This can be used to avoid small lookup tables.*/
   1.109 +#define OC_BYTE_TABLE32(_a,_b,_c,_d,_i) \
   1.110 +  ((signed char) \
   1.111 +   (((_a)&0xFF|((_b)&0xFF)<<8|((_c)&0xFF)<<16|((_d)&0xFF)<<24)>>(_i)*8))
   1.112 +/*Accesses one of eight (unsigned) nibbles given an index.
   1.113 +  This can be used to avoid small lookup tables.*/
   1.114 +#define OC_UNIBBLE_TABLE32(_a,_b,_c,_d,_e,_f,_g,_h,_i) \
   1.115 +  ((((_a)&0xF|((_b)&0xF)<<4|((_c)&0xF)<<8|((_d)&0xF)<<12| \
   1.116 +   ((_e)&0xF)<<16|((_f)&0xF)<<20|((_g)&0xF)<<24|((_h)&0xF)<<28)>>(_i)*4)&0xF)
   1.117 +
   1.118 +
   1.119 +
   1.120 +/*All of these macros should expect floats as arguments.*/
   1.121 +#define OC_MAXF(_a,_b)      ((_a)<(_b)?(_b):(_a))
   1.122 +#define OC_MINF(_a,_b)      ((_a)>(_b)?(_b):(_a))
   1.123 +#define OC_CLAMPF(_a,_b,_c) (OC_MINF(_a,OC_MAXF(_b,_c)))
   1.124 +#define OC_FABSF(_f)        ((float)fabs(_f))
   1.125 +#define OC_SQRTF(_f)        ((float)sqrt(_f))
   1.126 +#define OC_POWF(_b,_e)      ((float)pow(_b,_e))
   1.127 +#define OC_LOGF(_f)         ((float)log(_f))
   1.128 +#define OC_IFLOORF(_f)      ((int)floor(_f))
   1.129 +#define OC_ICEILF(_f)       ((int)ceil(_f))
   1.130 +
   1.131 +#endif

mercurial