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