1 #ifndef BITS_H
2 #define BITS_H
3 
4 #define UINT64_SUM_OVERFLOWS(a, b) \
5 	(a > (uint64_t)-1 - b)
6 
7 #define BIT(n) (1u << (n))
8 
9 /* These expressions make it easy to ensure that bit test expressions
10    are boolean in order to satisfy the in-house -Wstrict-bool. */
11 /* ((val & bits) == 0) is very common */
12 #define HAS_NO_BITS(val,bits) (((val) & (bits)) == 0)
13 /* ((val & bits) != 0) is even more common */
14 /* Note - illogical behaviour if bits==0, fixing that requires potential
15    multiple evaluation, but it's a corner case that should never occur. */
16 #define HAS_ANY_BITS(val,bits) (((val) & (bits)) != 0)
17 /* ((val & bits) == bits) is uncommon */
18 #define HAS_ALL_BITS(val,bits) ((~(val) & (bits)) == 0)
19 
20 /* Returns x, such that x is the smallest power of 2 >= num. */
21 size_t nearest_power(size_t num) ATTR_CONST;
22 
23 /* Returns TRUE if 2^x=num, i.e. if num has only a single bit set to 1. */
24 static inline bool ATTR_CONST
bits_is_power_of_two(uint64_t num)25 bits_is_power_of_two(uint64_t num)
26 {
27 	return num > 0 && (num & (num - 1)) == 0;
28 }
29 
30 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
31 static inline unsigned int ATTR_CONST
bits_required32(uint32_t num)32 bits_required32(uint32_t num)
33 {
34 	return num == 0 ? 0 : 32 - __builtin_clz(num);
35 }
36 static inline unsigned int ATTR_CONST
bits_required8(uint8_t num)37 bits_required8(uint8_t num)   { return bits_required32(num); }
38 
39 static inline unsigned int ATTR_CONST
bits_required16(uint16_t num)40 bits_required16(uint16_t num) { return bits_required32(num); }
41 
42 static inline unsigned int ATTR_CONST
bits_required64(uint64_t num)43 bits_required64(uint64_t num)
44 {
45 	return num == 0 ? 0 : 64 - __builtin_clzll(num);
46 }
47 #else
48 unsigned int bits_required8(uint8_t num) ATTR_CONST;
49 
50 static inline
bits_required16(uint16_t num)51 unsigned int bits_required16(uint16_t num)
52 {
53 	return (num <= 0xff) ? bits_required8(num)
54 		: 8 + bits_required8(num >> 8);
55 }
56 static inline
bits_required32(uint32_t num)57 unsigned int bits_required32(uint32_t num)
58 {
59 	return (num <= 0xffff) ? bits_required16(num)
60 		: 16 + bits_required16(num >> 16);
61 }
62 static inline
bits_required64(uint64_t num)63 unsigned int bits_required64(uint64_t num)
64 {
65 	return (num <= 0xffffffff) ? bits_required32(num)
66 		: 32 + bits_required32(num >> 32);
67 }
68 #endif
69 
70 static inline uint64_t ATTR_NO_SANITIZE_INTEGER
71 	ATTR_NO_SANITIZE_IMPLICIT_CONVERSION
bits_rotl64(uint64_t num,unsigned int count)72 bits_rotl64(uint64_t num, unsigned int count)
73 {
74 	const unsigned int mask = CHAR_BIT*sizeof(num) - 1;
75 	count &= mask;
76 	return (num << count) | (num >> (-count & mask));
77 }
78 
79 static inline uint32_t ATTR_NO_SANITIZE_INTEGER
80 	ATTR_NO_SANITIZE_IMPLICIT_CONVERSION
bits_rotl32(uint32_t num,unsigned int count)81 bits_rotl32(uint32_t num, unsigned int count)
82 {
83         const unsigned int mask = CHAR_BIT*sizeof(num) - 1;
84         count &= mask;
85         return (num << count) | (num >> (-count & mask));
86 }
87 
88 static inline uint64_t ATTR_NO_SANITIZE_INTEGER
89 	ATTR_NO_SANITIZE_IMPLICIT_CONVERSION
bits_rotr64(uint64_t num,unsigned int count)90 bits_rotr64(uint64_t num, unsigned int count)
91 {
92 	const unsigned int mask = CHAR_BIT*sizeof(num) - 1;
93 	count &= mask;
94 	return (num >> count) | (num << (-count & mask));
95 }
96 
97 static inline uint32_t ATTR_NO_SANITIZE_INTEGER
98 	ATTR_NO_SANITIZE_IMPLICIT_CONVERSION
bits_rotr32(uint32_t num,unsigned int count)99 bits_rotr32(uint32_t num, unsigned int count)
100 {
101 	const unsigned int mask = CHAR_BIT*sizeof(num) - 1;
102 	count &= mask;
103 	return (num >> count) | (num << (-count & mask));
104 }
105 
106 /* These functions look too big to be inline, but in almost all expected
107    uses, 'fracbits' will be a compile-time constant, and most of the
108    expressions will simplify greatly.
109 */
110 
111 /* Perform a piecewise-linear approximation to a log2, with fracbits "fractional" bits.
112    Best explained with examples:
113    With 2 fractional bits splitting each power of 2 into 4 bands:
114      00,   01,   10,   11 ->   00,   01,   10,   11 (small corner cases)
115     100,  101,  110,  111 ->  100,  101,  110,  111 ([4-8) split into 4 bands)
116    1000, 1001, 1010, 1011 -> 1000, 1000, 1001, 1001 ([8-15) split ...
117    1100, 1101, 1110, 1111 -> 1010, 1010, 1011, 1011  ... into 4 bands)
118    [16..31) -> 11bb
119    [32..63) -> 100bb
120    [64..127) -> 101bb
121    [128..255) -> 110bb
122    e.g. 236 = 11101100 -> ((8-2)<<2 == 11000) + (111.....>> 5 == 111) - 100 == 11011
123  */
124 static inline unsigned int ATTR_CONST
bits_fraclog(unsigned int val,unsigned int fracbits)125 bits_fraclog(unsigned int val, unsigned int fracbits)
126 {
127 	unsigned bits = bits_required32(val);
128 	if (bits <= fracbits + 1)
129 		return val;
130 
131 	unsigned int bandnum = bits - fracbits;
132 	unsigned int bandstart = bandnum << fracbits;
133 	unsigned int fracoffsbad = val >> (bandnum - 1); /* has leading 1 still */
134 	unsigned int bucket = bandstart + fracoffsbad - BIT(fracbits);
135 	return bucket;
136 }
137 static inline unsigned int ATTR_CONST
bits_fraclog_bucket_start(unsigned int bucket,unsigned int fracbits)138 bits_fraclog_bucket_start(unsigned int bucket, unsigned int fracbits)
139 {
140 	unsigned int bandnum = bucket >> fracbits;
141 	if (bandnum <= 1)
142 		return bucket;
143 	if (fracbits == 0)
144 		return BIT(bucket - 1);
145 	unsigned int fracoffs = bucket & (BIT(fracbits)-1);
146 	unsigned int fracoffs1 = BIT(fracbits) + fracoffs;
147 	unsigned int bandstart = fracoffs1 << (bandnum - 1);
148 	return bandstart;
149 }
150 static inline unsigned int ATTR_CONST ATTR_NO_SANITIZE_INTEGER
151 	ATTR_NO_SANITIZE_IMPLICIT_CONVERSION
bits_fraclog_bucket_end(unsigned int bucket,unsigned int fracbits)152 bits_fraclog_bucket_end(unsigned int bucket, unsigned int fracbits)
153 {
154 	unsigned int bandnum = bucket >> fracbits;
155 	if (bandnum <= 1)
156 		return bucket;
157 	if (fracbits == 0)
158 		return BIT(bucket - 1) * 2 - 1;
159 	unsigned int fracoffs = bucket & (BIT(fracbits)-1);
160 	unsigned int nextfracoffs1 = 1 + BIT(fracbits) + fracoffs;
161 	unsigned int nextbandstart = nextfracoffs1 << (bandnum - 1);
162 	return nextbandstart - 1;
163 }
164 /* UNSAFE: multiple use of parameter (but expecting a constant in reality).
165    But a macro as it's most likely to be used to declare an array size.
166 */
167 #define BITS_FRACLOG_BUCKETS(bits) ((33u - (bits)) << (bits))
168 
169 #endif
170