michael@0: #if !defined(_mathops_H) michael@0: # define _mathops_H (1) michael@0: # include michael@0: michael@0: # if __GNUC_PREREQ(3,4) michael@0: # include michael@0: /*Note the casts to (int) below: this prevents OC_CLZ{32|64}_OFFS from michael@0: "upgrading" the type of an entire expression to an (unsigned) size_t.*/ michael@0: # if INT_MAX>=2147483647 michael@0: # define OC_CLZ32_OFFS ((int)sizeof(unsigned)*CHAR_BIT) michael@0: # define OC_CLZ32(_x) (__builtin_clz(_x)) michael@0: # elif LONG_MAX>=2147483647L michael@0: # define OC_CLZ32_OFFS ((int)sizeof(unsigned long)*CHAR_BIT) michael@0: # define OC_CLZ32(_x) (__builtin_clzl(_x)) michael@0: # endif michael@0: # if INT_MAX>=9223372036854775807LL michael@0: # define OC_CLZ64_OFFS ((int)sizeof(unsigned)*CHAR_BIT) michael@0: # define OC_CLZ64(_x) (__builtin_clz(_x)) michael@0: # elif LONG_MAX>=9223372036854775807LL michael@0: # define OC_CLZ64_OFFS ((int)sizeof(unsigned long)*CHAR_BIT) michael@0: # define OC_CLZ64(_x) (__builtin_clzl(_x)) michael@0: # elif LLONG_MAX>=9223372036854775807LL|| \ michael@0: __LONG_LONG_MAX__>=9223372036854775807LL michael@0: # define OC_CLZ64_OFFS ((int)sizeof(unsigned long long)*CHAR_BIT) michael@0: # define OC_CLZ64(_x) (__builtin_clzll(_x)) michael@0: # endif michael@0: # endif michael@0: michael@0: michael@0: michael@0: /** michael@0: * oc_ilog32 - Integer binary logarithm of a 32-bit value. michael@0: * @_v: A 32-bit value. michael@0: * Returns floor(log2(_v))+1, or 0 if _v==0. michael@0: * This is the number of bits that would be required to represent _v in two's michael@0: * complement notation with all of the leading zeros stripped. michael@0: * The OC_ILOG_32() or OC_ILOGNZ_32() macros may be able to use a builtin michael@0: * function instead, which should be faster. michael@0: */ michael@0: int oc_ilog32(ogg_uint32_t _v); michael@0: /** michael@0: * oc_ilog64 - Integer binary logarithm of a 64-bit value. michael@0: * @_v: A 64-bit value. michael@0: * Returns floor(log2(_v))+1, or 0 if _v==0. michael@0: * This is the number of bits that would be required to represent _v in two's michael@0: * complement notation with all of the leading zeros stripped. michael@0: * The OC_ILOG_64() or OC_ILOGNZ_64() macros may be able to use a builtin michael@0: * function instead, which should be faster. michael@0: */ michael@0: int oc_ilog64(ogg_int64_t _v); michael@0: michael@0: michael@0: # if defined(OC_CLZ32) michael@0: /** michael@0: * OC_ILOGNZ_32 - Integer binary logarithm of a non-zero 32-bit value. michael@0: * @_v: A non-zero 32-bit value. michael@0: * Returns floor(log2(_v))+1. michael@0: * This is the number of bits that would be required to represent _v in two's michael@0: * complement notation with all of the leading zeros stripped. michael@0: * If _v is zero, the return value is undefined; use OC_ILOG_32() instead. michael@0: */ michael@0: # define OC_ILOGNZ_32(_v) (OC_CLZ32_OFFS-OC_CLZ32(_v)) michael@0: /** michael@0: * OC_ILOG_32 - Integer binary logarithm of a 32-bit value. michael@0: * @_v: A 32-bit value. michael@0: * Returns floor(log2(_v))+1, or 0 if _v==0. michael@0: * This is the number of bits that would be required to represent _v in two's michael@0: * complement notation with all of the leading zeros stripped. michael@0: */ michael@0: # define OC_ILOG_32(_v) (OC_ILOGNZ_32(_v)&-!!(_v)) michael@0: # else michael@0: # define OC_ILOGNZ_32(_v) (oc_ilog32(_v)) michael@0: # define OC_ILOG_32(_v) (oc_ilog32(_v)) michael@0: # endif michael@0: michael@0: # if defined(CLZ64) michael@0: /** michael@0: * OC_ILOGNZ_64 - Integer binary logarithm of a non-zero 64-bit value. michael@0: * @_v: A non-zero 64-bit value. michael@0: * Returns floor(log2(_v))+1. michael@0: * This is the number of bits that would be required to represent _v in two's michael@0: * complement notation with all of the leading zeros stripped. michael@0: * If _v is zero, the return value is undefined; use OC_ILOG_64() instead. michael@0: */ michael@0: # define OC_ILOGNZ_64(_v) (CLZ64_OFFS-CLZ64(_v)) michael@0: /** michael@0: * OC_ILOG_64 - Integer binary logarithm of a 64-bit value. michael@0: * @_v: A 64-bit value. michael@0: * Returns floor(log2(_v))+1, or 0 if _v==0. michael@0: * This is the number of bits that would be required to represent _v in two's michael@0: * complement notation with all of the leading zeros stripped. michael@0: */ michael@0: # define OC_ILOG_64(_v) (OC_ILOGNZ_64(_v)&-!!(_v)) michael@0: # else michael@0: # define OC_ILOGNZ_64(_v) (oc_ilog64(_v)) michael@0: # define OC_ILOG_64(_v) (oc_ilog64(_v)) michael@0: # endif michael@0: michael@0: # define OC_STATIC_ILOG0(_v) (!!(_v)) michael@0: # define OC_STATIC_ILOG1(_v) (((_v)&0x2)?2:OC_STATIC_ILOG0(_v)) michael@0: # define OC_STATIC_ILOG2(_v) \ michael@0: (((_v)&0xC)?2+OC_STATIC_ILOG1((_v)>>2):OC_STATIC_ILOG1(_v)) michael@0: # define OC_STATIC_ILOG3(_v) \ michael@0: (((_v)&0xF0)?4+OC_STATIC_ILOG2((_v)>>4):OC_STATIC_ILOG2(_v)) michael@0: # define OC_STATIC_ILOG4(_v) \ michael@0: (((_v)&0xFF00)?8+OC_STATIC_ILOG3((_v)>>8):OC_STATIC_ILOG3(_v)) michael@0: # define OC_STATIC_ILOG5(_v) \ michael@0: (((_v)&0xFFFF0000)?16+OC_STATIC_ILOG4((_v)>>16):OC_STATIC_ILOG4(_v)) michael@0: # define OC_STATIC_ILOG6(_v) \ michael@0: (((_v)&0xFFFFFFFF00000000ULL)?32+OC_STATIC_ILOG5((_v)>>32):OC_STATIC_ILOG5(_v)) michael@0: /** michael@0: * OC_STATIC_ILOG_32 - The integer logarithm of an (unsigned, 32-bit) constant. michael@0: * @_v: A non-negative 32-bit constant. michael@0: * Returns floor(log2(_v))+1, or 0 if _v==0. michael@0: * This is the number of bits that would be required to represent _v in two's michael@0: * complement notation with all of the leading zeros stripped. michael@0: * This macro is suitable for evaluation at compile time, but it should not be michael@0: * used on values that can change at runtime, as it operates via exhaustive michael@0: * search. michael@0: */ michael@0: # define OC_STATIC_ILOG_32(_v) (OC_STATIC_ILOG5((ogg_uint32_t)(_v))) michael@0: /** michael@0: * OC_STATIC_ILOG_64 - The integer logarithm of an (unsigned, 64-bit) constant. michael@0: * @_v: A non-negative 64-bit constant. michael@0: * Returns floor(log2(_v))+1, or 0 if _v==0. michael@0: * This is the number of bits that would be required to represent _v in two's michael@0: * complement notation with all of the leading zeros stripped. michael@0: * This macro is suitable for evaluation at compile time, but it should not be michael@0: * used on values that can change at runtime, as it operates via exhaustive michael@0: * search. michael@0: */ michael@0: # define OC_STATIC_ILOG_64(_v) (OC_STATIC_ILOG6((ogg_int64_t)(_v))) michael@0: michael@0: #define OC_Q57(_v) ((ogg_int64_t)(_v)<<57) michael@0: #define OC_Q10(_v) ((_v)<<10) michael@0: michael@0: ogg_int64_t oc_bexp64(ogg_int64_t _z); michael@0: ogg_int64_t oc_blog64(ogg_int64_t _w); michael@0: michael@0: ogg_uint32_t oc_bexp32_q10(int _z); michael@0: int oc_blog32_q10(ogg_uint32_t _w); michael@0: michael@0: #endif