#if !defined(_mathops_H)1# define _mathops_H (1)2# include <ogg/ogg.h>34# if __GNUC_PREREQ(3,4)5# include <limits.h>6/*Note the casts to (int) below: this prevents OC_CLZ{32|64}_OFFS from7"upgrading" the type of an entire expression to an (unsigned) size_t.*/8# if INT_MAX>=21474836479# define OC_CLZ32_OFFS ((int)sizeof(unsigned)*CHAR_BIT)10# define OC_CLZ32(_x) (__builtin_clz(_x))11# elif LONG_MAX>=2147483647L12# define OC_CLZ32_OFFS ((int)sizeof(unsigned long)*CHAR_BIT)13# define OC_CLZ32(_x) (__builtin_clzl(_x))14# endif15# if INT_MAX>=9223372036854775807LL16# define OC_CLZ64_OFFS ((int)sizeof(unsigned)*CHAR_BIT)17# define OC_CLZ64(_x) (__builtin_clz(_x))18# elif LONG_MAX>=9223372036854775807LL19# define OC_CLZ64_OFFS ((int)sizeof(unsigned long)*CHAR_BIT)20# define OC_CLZ64(_x) (__builtin_clzl(_x))21# elif LLONG_MAX>=9223372036854775807LL|| \22__LONG_LONG_MAX__>=9223372036854775807LL23# define OC_CLZ64_OFFS ((int)sizeof(unsigned long long)*CHAR_BIT)24# define OC_CLZ64(_x) (__builtin_clzll(_x))25# endif26# endif27282930/**31* oc_ilog32 - Integer binary logarithm of a 32-bit value.32* @_v: A 32-bit value.33* Returns floor(log2(_v))+1, or 0 if _v==0.34* This is the number of bits that would be required to represent _v in two's35* complement notation with all of the leading zeros stripped.36* The OC_ILOG_32() or OC_ILOGNZ_32() macros may be able to use a builtin37* function instead, which should be faster.38*/39int oc_ilog32(ogg_uint32_t _v);40/**41* oc_ilog64 - Integer binary logarithm of a 64-bit value.42* @_v: A 64-bit value.43* Returns floor(log2(_v))+1, or 0 if _v==0.44* This is the number of bits that would be required to represent _v in two's45* complement notation with all of the leading zeros stripped.46* The OC_ILOG_64() or OC_ILOGNZ_64() macros may be able to use a builtin47* function instead, which should be faster.48*/49int oc_ilog64(ogg_int64_t _v);505152# if defined(OC_CLZ32)53/**54* OC_ILOGNZ_32 - Integer binary logarithm of a non-zero 32-bit value.55* @_v: A non-zero 32-bit value.56* Returns floor(log2(_v))+1.57* This is the number of bits that would be required to represent _v in two's58* complement notation with all of the leading zeros stripped.59* If _v is zero, the return value is undefined; use OC_ILOG_32() instead.60*/61# define OC_ILOGNZ_32(_v) (OC_CLZ32_OFFS-OC_CLZ32(_v))62/**63* OC_ILOG_32 - Integer binary logarithm of a 32-bit value.64* @_v: A 32-bit value.65* Returns floor(log2(_v))+1, or 0 if _v==0.66* This is the number of bits that would be required to represent _v in two's67* complement notation with all of the leading zeros stripped.68*/69# define OC_ILOG_32(_v) ((_v)?OC_ILOGNZ_32(_v):0)70# else71# define OC_ILOGNZ_32(_v) (oc_ilog32(_v))72# define OC_ILOG_32(_v) (oc_ilog32(_v))73# endif7475# if defined(CLZ64)76/**77* OC_ILOGNZ_64 - Integer binary logarithm of a non-zero 64-bit value.78* @_v: A non-zero 64-bit value.79* Returns floor(log2(_v))+1.80* This is the number of bits that would be required to represent _v in two's81* complement notation with all of the leading zeros stripped.82* If _v is zero, the return value is undefined; use OC_ILOG_64() instead.83*/84# define OC_ILOGNZ_64(_v) (CLZ64_OFFS-CLZ64(_v))85/**86* OC_ILOG_64 - Integer binary logarithm of a 64-bit value.87* @_v: A 64-bit value.88* Returns floor(log2(_v))+1, or 0 if _v==0.89* This is the number of bits that would be required to represent _v in two's90* complement notation with all of the leading zeros stripped.91*/92# define OC_ILOG_64(_v) ((_v)?OC_ILOGNZ_64(_v):0)93# else94# define OC_ILOGNZ_64(_v) (oc_ilog64(_v))95# define OC_ILOG_64(_v) (oc_ilog64(_v))96# endif9798# define OC_STATIC_ILOG0(_v) (!!(_v))99# define OC_STATIC_ILOG1(_v) (((_v)&0x2)?2:OC_STATIC_ILOG0(_v))100# define OC_STATIC_ILOG2(_v) \101(((_v)&0xC)?2+OC_STATIC_ILOG1((_v)>>2):OC_STATIC_ILOG1(_v))102# define OC_STATIC_ILOG3(_v) \103(((_v)&0xF0)?4+OC_STATIC_ILOG2((_v)>>4):OC_STATIC_ILOG2(_v))104# define OC_STATIC_ILOG4(_v) \105(((_v)&0xFF00)?8+OC_STATIC_ILOG3((_v)>>8):OC_STATIC_ILOG3(_v))106# define OC_STATIC_ILOG5(_v) \107(((_v)&0xFFFF0000)?16+OC_STATIC_ILOG4((_v)>>16):OC_STATIC_ILOG4(_v))108# define OC_STATIC_ILOG6(_v) \109(((_v)&0xFFFFFFFF00000000ULL)?32+OC_STATIC_ILOG5((_v)>>32):OC_STATIC_ILOG5(_v))110/**111* OC_STATIC_ILOG_32 - The integer logarithm of an (unsigned, 32-bit) constant.112* @_v: A non-negative 32-bit constant.113* Returns floor(log2(_v))+1, or 0 if _v==0.114* This is the number of bits that would be required to represent _v in two's115* complement notation with all of the leading zeros stripped.116* This macro is suitable for evaluation at compile time, but it should not be117* used on values that can change at runtime, as it operates via exhaustive118* search.119*/120# define OC_STATIC_ILOG_32(_v) (OC_STATIC_ILOG5((ogg_uint32_t)(_v)))121/**122* OC_STATIC_ILOG_64 - The integer logarithm of an (unsigned, 64-bit) constant.123* @_v: A non-negative 64-bit constant.124* Returns floor(log2(_v))+1, or 0 if _v==0.125* This is the number of bits that would be required to represent _v in two's126* complement notation with all of the leading zeros stripped.127* This macro is suitable for evaluation at compile time, but it should not be128* used on values that can change at runtime, as it operates via exhaustive129* search.130*/131# define OC_STATIC_ILOG_64(_v) (OC_STATIC_ILOG6((ogg_int64_t)(_v)))132133#define OC_Q57(_v) ((_v)*((ogg_int64_t)1<<57))134#define OC_Q10(_v) ((_v)<<10)135136ogg_int64_t oc_bexp64(ogg_int64_t _z);137ogg_int64_t oc_blog64(ogg_int64_t _w);138139ogg_uint32_t oc_bexp32_q10(int _z);140int oc_blog32_q10(ogg_uint32_t _w);141142#endif143144145