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