Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /******************************************************************** |
michael@0 | 2 | * * |
michael@0 | 3 | * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE. * |
michael@0 | 4 | * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * |
michael@0 | 5 | * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * |
michael@0 | 6 | * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * |
michael@0 | 7 | * * |
michael@0 | 8 | * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 * |
michael@0 | 9 | * by the Xiph.Org Foundation and contributors http://www.xiph.org/ * |
michael@0 | 10 | * * |
michael@0 | 11 | ******************************************************************** |
michael@0 | 12 | |
michael@0 | 13 | function: |
michael@0 | 14 | last mod: $Id: ocintrin.h 16503 2009-08-22 18:14:02Z giles $ |
michael@0 | 15 | |
michael@0 | 16 | ********************************************************************/ |
michael@0 | 17 | |
michael@0 | 18 | /*Some common macros for potential platform-specific optimization.*/ |
michael@0 | 19 | #include <math.h> |
michael@0 | 20 | #if !defined(_ocintrin_H) |
michael@0 | 21 | # define _ocintrin_H (1) |
michael@0 | 22 | |
michael@0 | 23 | /*Some specific platforms may have optimized intrinsic or inline assembly |
michael@0 | 24 | versions of these functions which can substantially improve performance. |
michael@0 | 25 | We define macros for them to allow easy incorporation of these non-ANSI |
michael@0 | 26 | features.*/ |
michael@0 | 27 | |
michael@0 | 28 | /*Note that we do not provide a macro for abs(), because it is provided as a |
michael@0 | 29 | library function, which we assume is translated into an intrinsic to avoid |
michael@0 | 30 | the function call overhead and then implemented in the smartest way for the |
michael@0 | 31 | target platform. |
michael@0 | 32 | With modern gcc (4.x), this is true: it uses cmov instructions if the |
michael@0 | 33 | architecture supports it and branchless bit-twiddling if it does not (the |
michael@0 | 34 | speed difference between the two approaches is not measurable). |
michael@0 | 35 | Interestingly, the bit-twiddling method was patented in 2000 (US 6,073,150) |
michael@0 | 36 | by Sun Microsystems, despite prior art dating back to at least 1996: |
michael@0 | 37 | http://web.archive.org/web/19961201174141/www.x86.org/ftp/articles/pentopt/PENTOPT.TXT |
michael@0 | 38 | On gcc 3.x, however, our assumption is not true, as abs() is translated to a |
michael@0 | 39 | conditional jump, which is horrible on deeply piplined architectures (e.g., |
michael@0 | 40 | all consumer architectures for the past decade or more). |
michael@0 | 41 | Also be warned that -C*abs(x) where C is a constant is mis-optimized as |
michael@0 | 42 | abs(C*x) on every gcc release before 4.2.3. |
michael@0 | 43 | See bug http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34130 */ |
michael@0 | 44 | |
michael@0 | 45 | /*Modern gcc (4.x) can compile the naive versions of min and max with cmov if |
michael@0 | 46 | given an appropriate architecture, but the branchless bit-twiddling versions |
michael@0 | 47 | are just as fast, and do not require any special target architecture. |
michael@0 | 48 | Earlier gcc versions (3.x) compiled both code to the same assembly |
michael@0 | 49 | instructions, because of the way they represented ((_b)>(_a)) internally.*/ |
michael@0 | 50 | #define OC_MAXI(_a,_b) ((_a)-((_a)-(_b)&-((_b)>(_a)))) |
michael@0 | 51 | #define OC_MINI(_a,_b) ((_a)+((_b)-(_a)&-((_b)<(_a)))) |
michael@0 | 52 | /*Clamps an integer into the given range. |
michael@0 | 53 | If _a>_c, then the lower bound _a is respected over the upper bound _c (this |
michael@0 | 54 | behavior is required to meet our documented API behavior). |
michael@0 | 55 | _a: The lower bound. |
michael@0 | 56 | _b: The value to clamp. |
michael@0 | 57 | _c: The upper boud.*/ |
michael@0 | 58 | #define OC_CLAMPI(_a,_b,_c) (OC_MAXI(_a,OC_MINI(_b,_c))) |
michael@0 | 59 | #define OC_CLAMP255(_x) ((unsigned char)((((_x)<0)-1)&((_x)|-((_x)>255)))) |
michael@0 | 60 | /*This has a chance of compiling branchless, and is just as fast as the |
michael@0 | 61 | bit-twiddling method, which is slightly less portable, since it relies on a |
michael@0 | 62 | sign-extended rightshift, which is not guaranteed by ANSI (but present on |
michael@0 | 63 | every relevant platform).*/ |
michael@0 | 64 | #define OC_SIGNI(_a) (((_a)>0)-((_a)<0)) |
michael@0 | 65 | /*Slightly more portable than relying on a sign-extended right-shift (which is |
michael@0 | 66 | not guaranteed by ANSI), and just as fast, since gcc (3.x and 4.x both) |
michael@0 | 67 | compile it into the right-shift anyway.*/ |
michael@0 | 68 | #define OC_SIGNMASK(_a) (-((_a)<0)) |
michael@0 | 69 | /*Divides an integer by a power of two, truncating towards 0. |
michael@0 | 70 | _dividend: The integer to divide. |
michael@0 | 71 | _shift: The non-negative power of two to divide by. |
michael@0 | 72 | _rmask: (1<<_shift)-1*/ |
michael@0 | 73 | #define OC_DIV_POW2(_dividend,_shift,_rmask)\ |
michael@0 | 74 | ((_dividend)+(OC_SIGNMASK(_dividend)&(_rmask))>>(_shift)) |
michael@0 | 75 | /*Divides _x by 65536, truncating towards 0.*/ |
michael@0 | 76 | #define OC_DIV2_16(_x) OC_DIV_POW2(_x,16,0xFFFF) |
michael@0 | 77 | /*Divides _x by 2, truncating towards 0.*/ |
michael@0 | 78 | #define OC_DIV2(_x) OC_DIV_POW2(_x,1,0x1) |
michael@0 | 79 | /*Divides _x by 8, truncating towards 0.*/ |
michael@0 | 80 | #define OC_DIV8(_x) OC_DIV_POW2(_x,3,0x7) |
michael@0 | 81 | /*Divides _x by 16, truncating towards 0.*/ |
michael@0 | 82 | #define OC_DIV16(_x) OC_DIV_POW2(_x,4,0xF) |
michael@0 | 83 | /*Right shifts _dividend by _shift, adding _rval, and subtracting one for |
michael@0 | 84 | negative dividends first. |
michael@0 | 85 | When _rval is (1<<_shift-1), this is equivalent to division with rounding |
michael@0 | 86 | ties away from zero.*/ |
michael@0 | 87 | #define OC_DIV_ROUND_POW2(_dividend,_shift,_rval)\ |
michael@0 | 88 | ((_dividend)+OC_SIGNMASK(_dividend)+(_rval)>>(_shift)) |
michael@0 | 89 | /*Divides a _x by 2, rounding towards even numbers.*/ |
michael@0 | 90 | #define OC_DIV2_RE(_x) ((_x)+((_x)>>1&1)>>1) |
michael@0 | 91 | /*Divides a _x by (1<<(_shift)), rounding towards even numbers.*/ |
michael@0 | 92 | #define OC_DIV_POW2_RE(_x,_shift) \ |
michael@0 | 93 | ((_x)+((_x)>>(_shift)&1)+((1<<(_shift))-1>>1)>>(_shift)) |
michael@0 | 94 | /*Swaps two integers _a and _b if _a>_b.*/ |
michael@0 | 95 | #define OC_SORT2I(_a,_b) \ |
michael@0 | 96 | do{ \ |
michael@0 | 97 | int t__; \ |
michael@0 | 98 | t__=((_a)^(_b))&-((_b)<(_a)); \ |
michael@0 | 99 | (_a)^=t__; \ |
michael@0 | 100 | (_b)^=t__; \ |
michael@0 | 101 | } \ |
michael@0 | 102 | while(0) |
michael@0 | 103 | |
michael@0 | 104 | /*Accesses one of four (signed) bytes given an index. |
michael@0 | 105 | This can be used to avoid small lookup tables.*/ |
michael@0 | 106 | #define OC_BYTE_TABLE32(_a,_b,_c,_d,_i) \ |
michael@0 | 107 | ((signed char) \ |
michael@0 | 108 | (((_a)&0xFF|((_b)&0xFF)<<8|((_c)&0xFF)<<16|((_d)&0xFF)<<24)>>(_i)*8)) |
michael@0 | 109 | /*Accesses one of eight (unsigned) nibbles given an index. |
michael@0 | 110 | This can be used to avoid small lookup tables.*/ |
michael@0 | 111 | #define OC_UNIBBLE_TABLE32(_a,_b,_c,_d,_e,_f,_g,_h,_i) \ |
michael@0 | 112 | ((((_a)&0xF|((_b)&0xF)<<4|((_c)&0xF)<<8|((_d)&0xF)<<12| \ |
michael@0 | 113 | ((_e)&0xF)<<16|((_f)&0xF)<<20|((_g)&0xF)<<24|((_h)&0xF)<<28)>>(_i)*4)&0xF) |
michael@0 | 114 | |
michael@0 | 115 | |
michael@0 | 116 | |
michael@0 | 117 | /*All of these macros should expect floats as arguments.*/ |
michael@0 | 118 | #define OC_MAXF(_a,_b) ((_a)<(_b)?(_b):(_a)) |
michael@0 | 119 | #define OC_MINF(_a,_b) ((_a)>(_b)?(_b):(_a)) |
michael@0 | 120 | #define OC_CLAMPF(_a,_b,_c) (OC_MINF(_a,OC_MAXF(_b,_c))) |
michael@0 | 121 | #define OC_FABSF(_f) ((float)fabs(_f)) |
michael@0 | 122 | #define OC_SQRTF(_f) ((float)sqrt(_f)) |
michael@0 | 123 | #define OC_POWF(_b,_e) ((float)pow(_b,_e)) |
michael@0 | 124 | #define OC_LOGF(_f) ((float)log(_f)) |
michael@0 | 125 | #define OC_IFLOORF(_f) ((int)floor(_f)) |
michael@0 | 126 | #define OC_ICEILF(_f) ((int)ceil(_f)) |
michael@0 | 127 | |
michael@0 | 128 | #endif |