2
* bitops.h : macros and functions for bit operations.
3
* (C) 2002 - Willy Tarreau - willy@ant-computing.com
10
/* how many bits are needed to code the size of an int (eg: 32bits -> 5) */
16
/* very fast FFS function : returns the position of the lowest 1 */
17
#define __ffs_fast32(___a) ({ \
18
register int ___x, ___bits = 32; \
22
if (___x & 0x0000ffff) { ___x &= 0x0000ffff; ___bits -= 16;} \
23
if (___x & 0x00ff00ff) { ___x &= 0x00ff00ff; ___bits -= 8;} \
24
if (___x & 0x0f0f0f0f) { ___x &= 0x0f0f0f0f; ___bits -= 4;} \
25
if (___x & 0x33333333) { ___x &= 0x33333333; ___bits -= 2;} \
26
if (___x & 0x55555555) { ___x &= 0x55555555; ___bits -= 1;} \
31
/* very fast FLS function : returns the position of the highest 1 */
32
#define __fls_fast32(___a) ({ \
33
register int ___x, ___bits = 0; \
36
if (___x & 0xffff0000) { ___x &= 0xffff0000; ___bits += 16;} \
37
if (___x & 0xff00ff00) { ___x &= 0xff00ff00; ___bits += 8;} \
38
if (___x & 0xf0f0f0f0) { ___x &= 0xf0f0f0f0; ___bits += 4;} \
39
if (___x & 0xcccccccc) { ___x &= 0xcccccccc; ___bits += 2;} \
40
if (___x & 0xaaaaaaaa) { ___x &= 0xaaaaaaaa; ___bits += 1;} \
47
/* very fast FFS function working on 64 bits */
48
#define __ffs_fast64(___a) ({ \
49
register int ___bits = 64; \
50
register unsigned long ___x = ((___a) >> 32); \
51
if ((___a) & 0xffffffffUL) { \
52
___x = (___a) & 0xffffffffUL; \
57
if (___x & 0x0000ffff) { ___x &= 0x0000ffff; ___bits -= 16;} \
58
if (___x & 0x00ff00ff) { ___x &= 0x00ff00ff; ___bits -= 8;} \
59
if (___x & 0x0f0f0f0f) { ___x &= 0x0f0f0f0f; ___bits -= 4;} \
60
if (___x & 0x33333333) { ___x &= 0x33333333; ___bits -= 2;} \
61
if (___x & 0x55555555) { ___x &= 0x55555555; ___bits -= 1;} \
67
/* very fast FLS function working on 64 bits */
68
#define __fls_fast64(___a) ({ \
69
register int ___bits = 0; \
70
register unsigned long ___x = (___a); \
71
if (((unsigned long long)(___a)) >> 32) { \
72
___x = ((unsigned long long)(___a)) >> 32; \
76
if (___x & 0xffff0000) { ___x &= 0xffff0000; ___bits += 16;} \
77
if (___x & 0xff00ff00) { ___x &= 0xff00ff00; ___bits += 8;} \
78
if (___x & 0xf0f0f0f0) { ___x &= 0xf0f0f0f0; ___bits += 4;} \
79
if (___x & 0xcccccccc) { ___x &= 0xcccccccc; ___bits += 2;} \
80
if (___x & 0xaaaaaaaa) { ___x &= 0xaaaaaaaa; ___bits += 1;} \
87
static int ffs_fast32(register unsigned long a) {
88
return __ffs_fast32(a);
91
static int fls_fast32(unsigned long a) {
92
return __fls_fast32(a);
95
static int ffs_fast64(unsigned long long a) {
96
return __ffs_fast64(a);
99
static int fls_fast64(unsigned long long a) {
100
return __fls_fast64(a);
103
#endif /* __BITOPS_H__ */