2
Some useful bit functions
8
extern const char _my_bits_nbits[256];
9
extern const uchar _my_bits_reverse_table[256];
12
Find smallest X in 2^X >= value
13
This can be used to divide a number with value by doing a shift instead
16
STATIC_INLINE uint my_bit_log2(ulong value)
19
for (bit=0 ; value > 1 ; value>>=1, bit++) ;
23
STATIC_INLINE uint my_count_bits(ulonglong v)
25
#if SIZEOF_LONG_LONG > 4
26
/* The following code is a bit faster on 16 bit machines than if we would
28
ulong v2=(ulong) (v >> 32);
29
return (uint) (uchar) (_my_bits_nbits[(uchar) v] +
30
_my_bits_nbits[(uchar) (v >> 8)] +
31
_my_bits_nbits[(uchar) (v >> 16)] +
32
_my_bits_nbits[(uchar) (v >> 24)] +
33
_my_bits_nbits[(uchar) (v2)] +
34
_my_bits_nbits[(uchar) (v2 >> 8)] +
35
_my_bits_nbits[(uchar) (v2 >> 16)] +
36
_my_bits_nbits[(uchar) (v2 >> 24)]);
38
return (uint) (uchar) (_my_bits_nbits[(uchar) v] +
39
_my_bits_nbits[(uchar) (v >> 8)] +
40
_my_bits_nbits[(uchar) (v >> 16)] +
41
_my_bits_nbits[(uchar) (v >> 24)]);
45
STATIC_INLINE uint my_count_bits_ushort(ushort v)
47
return _my_bits_nbits[v];
52
Next highest power of two
55
my_round_up_to_next_power()
59
Next or equal power of 2
63
Algorithm by Sean Anderson, according to:
64
http://graphics.stanford.edu/~seander/bithacks.html
65
(Orignal code public domain)
67
Comments shows how this works with 01100000000000000000000000001011
70
STATIC_INLINE uint32 my_round_up_to_next_power(uint32 v)
72
v--; /* 01100000000000000000000000001010 */
73
v|= v >> 1; /* 01110000000000000000000000001111 */
74
v|= v >> 2; /* 01111100000000000000000000001111 */
75
v|= v >> 4; /* 01111111110000000000000000001111 */
76
v|= v >> 8; /* 01111111111111111100000000001111 */
77
v|= v >> 16; /* 01111111111111111111111111111111 */
78
return v+1; /* 10000000000000000000000000000000 */
81
STATIC_INLINE uint32 my_clear_highest_bit(uint32 v)
92
STATIC_INLINE uint32 my_reverse_bits(uint32 key)
95
(_my_bits_reverse_table[ key & 255] << 24) |
96
(_my_bits_reverse_table[(key>> 8) & 255] << 16) |
97
(_my_bits_reverse_table[(key>>16) & 255] << 8) |
98
_my_bits_reverse_table[(key>>24) ];
101
#else /* HAVE_INLINE */
102
extern uint my_bit_log2(ulong value);
103
extern uint32 my_round_up_to_next_power(uint32 v);
104
uint32 my_clear_highest_bit(uint32 v);
105
uint32 my_reverse_bits(uint32 key);
106
extern uint my_count_bits(ulonglong v);
107
extern uint my_count_bits_ushort(ushort v);
108
#endif /* HAVE_INLINE */