michael@0: /******************************************************************** michael@0: * * michael@0: * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE. * michael@0: * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * michael@0: * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * michael@0: * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * michael@0: * * michael@0: * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 * michael@0: * by the Xiph.Org Foundation and contributors http://www.xiph.org/ * michael@0: * * michael@0: ******************************************************************** michael@0: michael@0: function: michael@0: last mod: $Id: ocintrin.h 16503 2009-08-22 18:14:02Z giles $ michael@0: michael@0: ********************************************************************/ michael@0: michael@0: /*Some common macros for potential platform-specific optimization.*/ michael@0: #include michael@0: #if !defined(_ocintrin_H) michael@0: # define _ocintrin_H (1) michael@0: michael@0: /*Some specific platforms may have optimized intrinsic or inline assembly michael@0: versions of these functions which can substantially improve performance. michael@0: We define macros for them to allow easy incorporation of these non-ANSI michael@0: features.*/ michael@0: michael@0: /*Note that we do not provide a macro for abs(), because it is provided as a michael@0: library function, which we assume is translated into an intrinsic to avoid michael@0: the function call overhead and then implemented in the smartest way for the michael@0: target platform. michael@0: With modern gcc (4.x), this is true: it uses cmov instructions if the michael@0: architecture supports it and branchless bit-twiddling if it does not (the michael@0: speed difference between the two approaches is not measurable). michael@0: Interestingly, the bit-twiddling method was patented in 2000 (US 6,073,150) michael@0: by Sun Microsystems, despite prior art dating back to at least 1996: michael@0: http://web.archive.org/web/19961201174141/www.x86.org/ftp/articles/pentopt/PENTOPT.TXT michael@0: On gcc 3.x, however, our assumption is not true, as abs() is translated to a michael@0: conditional jump, which is horrible on deeply piplined architectures (e.g., michael@0: all consumer architectures for the past decade or more). michael@0: Also be warned that -C*abs(x) where C is a constant is mis-optimized as michael@0: abs(C*x) on every gcc release before 4.2.3. michael@0: See bug http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34130 */ michael@0: michael@0: /*Modern gcc (4.x) can compile the naive versions of min and max with cmov if michael@0: given an appropriate architecture, but the branchless bit-twiddling versions michael@0: are just as fast, and do not require any special target architecture. michael@0: Earlier gcc versions (3.x) compiled both code to the same assembly michael@0: instructions, because of the way they represented ((_b)>(_a)) internally.*/ michael@0: #define OC_MAXI(_a,_b) ((_a)-((_a)-(_b)&-((_b)>(_a)))) michael@0: #define OC_MINI(_a,_b) ((_a)+((_b)-(_a)&-((_b)<(_a)))) michael@0: /*Clamps an integer into the given range. michael@0: If _a>_c, then the lower bound _a is respected over the upper bound _c (this michael@0: behavior is required to meet our documented API behavior). michael@0: _a: The lower bound. michael@0: _b: The value to clamp. michael@0: _c: The upper boud.*/ michael@0: #define OC_CLAMPI(_a,_b,_c) (OC_MAXI(_a,OC_MINI(_b,_c))) michael@0: #define OC_CLAMP255(_x) ((unsigned char)((((_x)<0)-1)&((_x)|-((_x)>255)))) michael@0: /*This has a chance of compiling branchless, and is just as fast as the michael@0: bit-twiddling method, which is slightly less portable, since it relies on a michael@0: sign-extended rightshift, which is not guaranteed by ANSI (but present on michael@0: every relevant platform).*/ michael@0: #define OC_SIGNI(_a) (((_a)>0)-((_a)<0)) michael@0: /*Slightly more portable than relying on a sign-extended right-shift (which is michael@0: not guaranteed by ANSI), and just as fast, since gcc (3.x and 4.x both) michael@0: compile it into the right-shift anyway.*/ michael@0: #define OC_SIGNMASK(_a) (-((_a)<0)) michael@0: /*Divides an integer by a power of two, truncating towards 0. michael@0: _dividend: The integer to divide. michael@0: _shift: The non-negative power of two to divide by. michael@0: _rmask: (1<<_shift)-1*/ michael@0: #define OC_DIV_POW2(_dividend,_shift,_rmask)\ michael@0: ((_dividend)+(OC_SIGNMASK(_dividend)&(_rmask))>>(_shift)) michael@0: /*Divides _x by 65536, truncating towards 0.*/ michael@0: #define OC_DIV2_16(_x) OC_DIV_POW2(_x,16,0xFFFF) michael@0: /*Divides _x by 2, truncating towards 0.*/ michael@0: #define OC_DIV2(_x) OC_DIV_POW2(_x,1,0x1) michael@0: /*Divides _x by 8, truncating towards 0.*/ michael@0: #define OC_DIV8(_x) OC_DIV_POW2(_x,3,0x7) michael@0: /*Divides _x by 16, truncating towards 0.*/ michael@0: #define OC_DIV16(_x) OC_DIV_POW2(_x,4,0xF) michael@0: /*Right shifts _dividend by _shift, adding _rval, and subtracting one for michael@0: negative dividends first. michael@0: When _rval is (1<<_shift-1), this is equivalent to division with rounding michael@0: ties away from zero.*/ michael@0: #define OC_DIV_ROUND_POW2(_dividend,_shift,_rval)\ michael@0: ((_dividend)+OC_SIGNMASK(_dividend)+(_rval)>>(_shift)) michael@0: /*Divides a _x by 2, rounding towards even numbers.*/ michael@0: #define OC_DIV2_RE(_x) ((_x)+((_x)>>1&1)>>1) michael@0: /*Divides a _x by (1<<(_shift)), rounding towards even numbers.*/ michael@0: #define OC_DIV_POW2_RE(_x,_shift) \ michael@0: ((_x)+((_x)>>(_shift)&1)+((1<<(_shift))-1>>1)>>(_shift)) michael@0: /*Swaps two integers _a and _b if _a>_b.*/ michael@0: #define OC_SORT2I(_a,_b) \ michael@0: do{ \ michael@0: int t__; \ michael@0: t__=((_a)^(_b))&-((_b)<(_a)); \ michael@0: (_a)^=t__; \ michael@0: (_b)^=t__; \ michael@0: } \ michael@0: while(0) michael@0: michael@0: /*Accesses one of four (signed) bytes given an index. michael@0: This can be used to avoid small lookup tables.*/ michael@0: #define OC_BYTE_TABLE32(_a,_b,_c,_d,_i) \ michael@0: ((signed char) \ michael@0: (((_a)&0xFF|((_b)&0xFF)<<8|((_c)&0xFF)<<16|((_d)&0xFF)<<24)>>(_i)*8)) michael@0: /*Accesses one of eight (unsigned) nibbles given an index. michael@0: This can be used to avoid small lookup tables.*/ michael@0: #define OC_UNIBBLE_TABLE32(_a,_b,_c,_d,_e,_f,_g,_h,_i) \ michael@0: ((((_a)&0xF|((_b)&0xF)<<4|((_c)&0xF)<<8|((_d)&0xF)<<12| \ michael@0: ((_e)&0xF)<<16|((_f)&0xF)<<20|((_g)&0xF)<<24|((_h)&0xF)<<28)>>(_i)*4)&0xF) michael@0: michael@0: michael@0: michael@0: /*All of these macros should expect floats as arguments.*/ michael@0: #define OC_MAXF(_a,_b) ((_a)<(_b)?(_b):(_a)) michael@0: #define OC_MINF(_a,_b) ((_a)>(_b)?(_b):(_a)) michael@0: #define OC_CLAMPF(_a,_b,_c) (OC_MINF(_a,OC_MAXF(_b,_c))) michael@0: #define OC_FABSF(_f) ((float)fabs(_f)) michael@0: #define OC_SQRTF(_f) ((float)sqrt(_f)) michael@0: #define OC_POWF(_b,_e) ((float)pow(_b,_e)) michael@0: #define OC_LOGF(_f) ((float)log(_f)) michael@0: #define OC_IFLOORF(_f) ((int)floor(_f)) michael@0: #define OC_ICEILF(_f) ((int)ceil(_f)) michael@0: michael@0: #endif