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 | #if !defined(_mathops_H) |
michael@0 | 2 | # define _mathops_H (1) |
michael@0 | 3 | # include <ogg/ogg.h> |
michael@0 | 4 | |
michael@0 | 5 | # if __GNUC_PREREQ(3,4) |
michael@0 | 6 | # include <limits.h> |
michael@0 | 7 | /*Note the casts to (int) below: this prevents OC_CLZ{32|64}_OFFS from |
michael@0 | 8 | "upgrading" the type of an entire expression to an (unsigned) size_t.*/ |
michael@0 | 9 | # if INT_MAX>=2147483647 |
michael@0 | 10 | # define OC_CLZ32_OFFS ((int)sizeof(unsigned)*CHAR_BIT) |
michael@0 | 11 | # define OC_CLZ32(_x) (__builtin_clz(_x)) |
michael@0 | 12 | # elif LONG_MAX>=2147483647L |
michael@0 | 13 | # define OC_CLZ32_OFFS ((int)sizeof(unsigned long)*CHAR_BIT) |
michael@0 | 14 | # define OC_CLZ32(_x) (__builtin_clzl(_x)) |
michael@0 | 15 | # endif |
michael@0 | 16 | # if INT_MAX>=9223372036854775807LL |
michael@0 | 17 | # define OC_CLZ64_OFFS ((int)sizeof(unsigned)*CHAR_BIT) |
michael@0 | 18 | # define OC_CLZ64(_x) (__builtin_clz(_x)) |
michael@0 | 19 | # elif LONG_MAX>=9223372036854775807LL |
michael@0 | 20 | # define OC_CLZ64_OFFS ((int)sizeof(unsigned long)*CHAR_BIT) |
michael@0 | 21 | # define OC_CLZ64(_x) (__builtin_clzl(_x)) |
michael@0 | 22 | # elif LLONG_MAX>=9223372036854775807LL|| \ |
michael@0 | 23 | __LONG_LONG_MAX__>=9223372036854775807LL |
michael@0 | 24 | # define OC_CLZ64_OFFS ((int)sizeof(unsigned long long)*CHAR_BIT) |
michael@0 | 25 | # define OC_CLZ64(_x) (__builtin_clzll(_x)) |
michael@0 | 26 | # endif |
michael@0 | 27 | # endif |
michael@0 | 28 | |
michael@0 | 29 | |
michael@0 | 30 | |
michael@0 | 31 | /** |
michael@0 | 32 | * oc_ilog32 - Integer binary logarithm of a 32-bit value. |
michael@0 | 33 | * @_v: A 32-bit value. |
michael@0 | 34 | * Returns floor(log2(_v))+1, or 0 if _v==0. |
michael@0 | 35 | * This is the number of bits that would be required to represent _v in two's |
michael@0 | 36 | * complement notation with all of the leading zeros stripped. |
michael@0 | 37 | * The OC_ILOG_32() or OC_ILOGNZ_32() macros may be able to use a builtin |
michael@0 | 38 | * function instead, which should be faster. |
michael@0 | 39 | */ |
michael@0 | 40 | int oc_ilog32(ogg_uint32_t _v); |
michael@0 | 41 | /** |
michael@0 | 42 | * oc_ilog64 - Integer binary logarithm of a 64-bit value. |
michael@0 | 43 | * @_v: A 64-bit value. |
michael@0 | 44 | * Returns floor(log2(_v))+1, or 0 if _v==0. |
michael@0 | 45 | * This is the number of bits that would be required to represent _v in two's |
michael@0 | 46 | * complement notation with all of the leading zeros stripped. |
michael@0 | 47 | * The OC_ILOG_64() or OC_ILOGNZ_64() macros may be able to use a builtin |
michael@0 | 48 | * function instead, which should be faster. |
michael@0 | 49 | */ |
michael@0 | 50 | int oc_ilog64(ogg_int64_t _v); |
michael@0 | 51 | |
michael@0 | 52 | |
michael@0 | 53 | # if defined(OC_CLZ32) |
michael@0 | 54 | /** |
michael@0 | 55 | * OC_ILOGNZ_32 - Integer binary logarithm of a non-zero 32-bit value. |
michael@0 | 56 | * @_v: A non-zero 32-bit value. |
michael@0 | 57 | * Returns floor(log2(_v))+1. |
michael@0 | 58 | * This is the number of bits that would be required to represent _v in two's |
michael@0 | 59 | * complement notation with all of the leading zeros stripped. |
michael@0 | 60 | * If _v is zero, the return value is undefined; use OC_ILOG_32() instead. |
michael@0 | 61 | */ |
michael@0 | 62 | # define OC_ILOGNZ_32(_v) (OC_CLZ32_OFFS-OC_CLZ32(_v)) |
michael@0 | 63 | /** |
michael@0 | 64 | * OC_ILOG_32 - Integer binary logarithm of a 32-bit value. |
michael@0 | 65 | * @_v: A 32-bit value. |
michael@0 | 66 | * Returns floor(log2(_v))+1, or 0 if _v==0. |
michael@0 | 67 | * This is the number of bits that would be required to represent _v in two's |
michael@0 | 68 | * complement notation with all of the leading zeros stripped. |
michael@0 | 69 | */ |
michael@0 | 70 | # define OC_ILOG_32(_v) (OC_ILOGNZ_32(_v)&-!!(_v)) |
michael@0 | 71 | # else |
michael@0 | 72 | # define OC_ILOGNZ_32(_v) (oc_ilog32(_v)) |
michael@0 | 73 | # define OC_ILOG_32(_v) (oc_ilog32(_v)) |
michael@0 | 74 | # endif |
michael@0 | 75 | |
michael@0 | 76 | # if defined(CLZ64) |
michael@0 | 77 | /** |
michael@0 | 78 | * OC_ILOGNZ_64 - Integer binary logarithm of a non-zero 64-bit value. |
michael@0 | 79 | * @_v: A non-zero 64-bit value. |
michael@0 | 80 | * Returns floor(log2(_v))+1. |
michael@0 | 81 | * This is the number of bits that would be required to represent _v in two's |
michael@0 | 82 | * complement notation with all of the leading zeros stripped. |
michael@0 | 83 | * If _v is zero, the return value is undefined; use OC_ILOG_64() instead. |
michael@0 | 84 | */ |
michael@0 | 85 | # define OC_ILOGNZ_64(_v) (CLZ64_OFFS-CLZ64(_v)) |
michael@0 | 86 | /** |
michael@0 | 87 | * OC_ILOG_64 - Integer binary logarithm of a 64-bit value. |
michael@0 | 88 | * @_v: A 64-bit value. |
michael@0 | 89 | * Returns floor(log2(_v))+1, or 0 if _v==0. |
michael@0 | 90 | * This is the number of bits that would be required to represent _v in two's |
michael@0 | 91 | * complement notation with all of the leading zeros stripped. |
michael@0 | 92 | */ |
michael@0 | 93 | # define OC_ILOG_64(_v) (OC_ILOGNZ_64(_v)&-!!(_v)) |
michael@0 | 94 | # else |
michael@0 | 95 | # define OC_ILOGNZ_64(_v) (oc_ilog64(_v)) |
michael@0 | 96 | # define OC_ILOG_64(_v) (oc_ilog64(_v)) |
michael@0 | 97 | # endif |
michael@0 | 98 | |
michael@0 | 99 | # define OC_STATIC_ILOG0(_v) (!!(_v)) |
michael@0 | 100 | # define OC_STATIC_ILOG1(_v) (((_v)&0x2)?2:OC_STATIC_ILOG0(_v)) |
michael@0 | 101 | # define OC_STATIC_ILOG2(_v) \ |
michael@0 | 102 | (((_v)&0xC)?2+OC_STATIC_ILOG1((_v)>>2):OC_STATIC_ILOG1(_v)) |
michael@0 | 103 | # define OC_STATIC_ILOG3(_v) \ |
michael@0 | 104 | (((_v)&0xF0)?4+OC_STATIC_ILOG2((_v)>>4):OC_STATIC_ILOG2(_v)) |
michael@0 | 105 | # define OC_STATIC_ILOG4(_v) \ |
michael@0 | 106 | (((_v)&0xFF00)?8+OC_STATIC_ILOG3((_v)>>8):OC_STATIC_ILOG3(_v)) |
michael@0 | 107 | # define OC_STATIC_ILOG5(_v) \ |
michael@0 | 108 | (((_v)&0xFFFF0000)?16+OC_STATIC_ILOG4((_v)>>16):OC_STATIC_ILOG4(_v)) |
michael@0 | 109 | # define OC_STATIC_ILOG6(_v) \ |
michael@0 | 110 | (((_v)&0xFFFFFFFF00000000ULL)?32+OC_STATIC_ILOG5((_v)>>32):OC_STATIC_ILOG5(_v)) |
michael@0 | 111 | /** |
michael@0 | 112 | * OC_STATIC_ILOG_32 - The integer logarithm of an (unsigned, 32-bit) constant. |
michael@0 | 113 | * @_v: A non-negative 32-bit constant. |
michael@0 | 114 | * Returns floor(log2(_v))+1, or 0 if _v==0. |
michael@0 | 115 | * This is the number of bits that would be required to represent _v in two's |
michael@0 | 116 | * complement notation with all of the leading zeros stripped. |
michael@0 | 117 | * This macro is suitable for evaluation at compile time, but it should not be |
michael@0 | 118 | * used on values that can change at runtime, as it operates via exhaustive |
michael@0 | 119 | * search. |
michael@0 | 120 | */ |
michael@0 | 121 | # define OC_STATIC_ILOG_32(_v) (OC_STATIC_ILOG5((ogg_uint32_t)(_v))) |
michael@0 | 122 | /** |
michael@0 | 123 | * OC_STATIC_ILOG_64 - The integer logarithm of an (unsigned, 64-bit) constant. |
michael@0 | 124 | * @_v: A non-negative 64-bit constant. |
michael@0 | 125 | * Returns floor(log2(_v))+1, or 0 if _v==0. |
michael@0 | 126 | * This is the number of bits that would be required to represent _v in two's |
michael@0 | 127 | * complement notation with all of the leading zeros stripped. |
michael@0 | 128 | * This macro is suitable for evaluation at compile time, but it should not be |
michael@0 | 129 | * used on values that can change at runtime, as it operates via exhaustive |
michael@0 | 130 | * search. |
michael@0 | 131 | */ |
michael@0 | 132 | # define OC_STATIC_ILOG_64(_v) (OC_STATIC_ILOG6((ogg_int64_t)(_v))) |
michael@0 | 133 | |
michael@0 | 134 | #define OC_Q57(_v) ((ogg_int64_t)(_v)<<57) |
michael@0 | 135 | #define OC_Q10(_v) ((_v)<<10) |
michael@0 | 136 | |
michael@0 | 137 | ogg_int64_t oc_bexp64(ogg_int64_t _z); |
michael@0 | 138 | ogg_int64_t oc_blog64(ogg_int64_t _w); |
michael@0 | 139 | |
michael@0 | 140 | ogg_uint32_t oc_bexp32_q10(int _z); |
michael@0 | 141 | int oc_blog32_q10(ogg_uint32_t _w); |
michael@0 | 142 | |
michael@0 | 143 | #endif |