1
by brian
clean slate |
1 |
/*
|
2 |
Some useful bit functions
|
|
3 |
*/
|
|
4 |
||
5 |
C_MODE_START
|
|
6 |
||
7 |
extern const char _my_bits_nbits[256]; |
|
8 |
extern const uchar _my_bits_reverse_table[256]; |
|
9 |
||
10 |
/*
|
|
11 |
Find smallest X in 2^X >= value
|
|
12 |
This can be used to divide a number with value by doing a shift instead
|
|
13 |
*/
|
|
14 |
||
53.2.4
by Monty Taylor
Changes so that client/ builds cleanly with no warnings. |
15 |
static inline uint my_bit_log2(ulong value) |
1
by brian
clean slate |
16 |
{
|
17 |
uint bit; |
|
18 |
for (bit=0 ; value > 1 ; value>>=1, bit++) ; |
|
19 |
return bit; |
|
20 |
}
|
|
21 |
||
151
by Brian Aker
Ulonglong to uint64_t |
22 |
static inline uint my_count_bits(uint64_t v) |
1
by brian
clean slate |
23 |
{
|
24 |
#if SIZEOF_LONG_LONG > 4
|
|
25 |
/* The following code is a bit faster on 16 bit machines than if we would
|
|
26 |
only shift v */
|
|
27 |
ulong v2=(ulong) (v >> 32); |
|
28 |
return (uint) (uchar) (_my_bits_nbits[(uchar) v] + |
|
29 |
_my_bits_nbits[(uchar) (v >> 8)] + |
|
30 |
_my_bits_nbits[(uchar) (v >> 16)] + |
|
31 |
_my_bits_nbits[(uchar) (v >> 24)] + |
|
32 |
_my_bits_nbits[(uchar) (v2)] + |
|
33 |
_my_bits_nbits[(uchar) (v2 >> 8)] + |
|
34 |
_my_bits_nbits[(uchar) (v2 >> 16)] + |
|
35 |
_my_bits_nbits[(uchar) (v2 >> 24)]); |
|
36 |
#else
|
|
37 |
return (uint) (uchar) (_my_bits_nbits[(uchar) v] + |
|
38 |
_my_bits_nbits[(uchar) (v >> 8)] + |
|
39 |
_my_bits_nbits[(uchar) (v >> 16)] + |
|
40 |
_my_bits_nbits[(uchar) (v >> 24)]); |
|
41 |
#endif
|
|
42 |
}
|
|
43 |
||
53.2.4
by Monty Taylor
Changes so that client/ builds cleanly with no warnings. |
44 |
static inline uint my_count_bits_ushort(ushort v) |
1
by brian
clean slate |
45 |
{
|
46 |
return _my_bits_nbits[v]; |
|
47 |
}
|
|
48 |
||
49 |
||
50 |
/*
|
|
51 |
Next highest power of two
|
|
52 |
||
53 |
SYNOPSIS
|
|
54 |
my_round_up_to_next_power()
|
|
55 |
v Value to check
|
|
56 |
||
57 |
RETURN
|
|
58 |
Next or equal power of 2
|
|
59 |
Note: 0 will return 0
|
|
60 |
||
61 |
NOTES
|
|
62 |
Algorithm by Sean Anderson, according to:
|
|
63 |
http://graphics.stanford.edu/~seander/bithacks.html
|
|
64 |
(Orignal code public domain)
|
|
65 |
||
66 |
Comments shows how this works with 01100000000000000000000000001011
|
|
67 |
*/
|
|
68 |
||
53.2.4
by Monty Taylor
Changes so that client/ builds cleanly with no warnings. |
69 |
static inline uint32 my_round_up_to_next_power(uint32 v) |
1
by brian
clean slate |
70 |
{
|
71 |
v--; /* 01100000000000000000000000001010 */ |
|
72 |
v|= v >> 1; /* 01110000000000000000000000001111 */ |
|
73 |
v|= v >> 2; /* 01111100000000000000000000001111 */ |
|
74 |
v|= v >> 4; /* 01111111110000000000000000001111 */ |
|
75 |
v|= v >> 8; /* 01111111111111111100000000001111 */ |
|
76 |
v|= v >> 16; /* 01111111111111111111111111111111 */ |
|
77 |
return v+1; /* 10000000000000000000000000000000 */ |
|
78 |
}
|
|
79 |
||
53.2.4
by Monty Taylor
Changes so that client/ builds cleanly with no warnings. |
80 |
static inline uint32 my_clear_highest_bit(uint32 v) |
1
by brian
clean slate |
81 |
{
|
82 |
uint32 w=v >> 1; |
|
83 |
w|= w >> 1; |
|
84 |
w|= w >> 2; |
|
85 |
w|= w >> 4; |
|
86 |
w|= w >> 8; |
|
87 |
w|= w >> 16; |
|
88 |
return v & w; |
|
89 |
}
|
|
90 |
||
53.2.4
by Monty Taylor
Changes so that client/ builds cleanly with no warnings. |
91 |
static inline uint32 my_reverse_bits(uint32 key) |
1
by brian
clean slate |
92 |
{
|
93 |
return
|
|
94 |
(_my_bits_reverse_table[ key & 255] << 24) | |
|
95 |
(_my_bits_reverse_table[(key>> 8) & 255] << 16) | |
|
96 |
(_my_bits_reverse_table[(key>>16) & 255] << 8) | |
|
97 |
_my_bits_reverse_table[(key>>24) ]; |
|
98 |
}
|
|
99 |
||
100 |
C_MODE_END
|