diff options
Diffstat (limited to 'include/linux/bitops.h')
-rw-r--r-- | include/linux/bitops.h | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/include/linux/bitops.h b/include/linux/bitops.h index c05a29cb9bb2..b796eab5ca75 100644 --- a/include/linux/bitops.h +++ b/include/linux/bitops.h | |||
@@ -16,16 +16,15 @@ | |||
16 | */ | 16 | */ |
17 | #include <asm/bitops.h> | 17 | #include <asm/bitops.h> |
18 | 18 | ||
19 | #define for_each_bit(bit, addr, size) \ | 19 | #define for_each_set_bit(bit, addr, size) \ |
20 | for ((bit) = find_first_bit((addr), (size)); \ | 20 | for ((bit) = find_first_bit((addr), (size)); \ |
21 | (bit) < (size); \ | 21 | (bit) < (size); \ |
22 | (bit) = find_next_bit((addr), (size), (bit) + 1)) | 22 | (bit) = find_next_bit((addr), (size), (bit) + 1)) |
23 | 23 | ||
24 | |||
25 | static __inline__ int get_bitmask_order(unsigned int count) | 24 | static __inline__ int get_bitmask_order(unsigned int count) |
26 | { | 25 | { |
27 | int order; | 26 | int order; |
28 | 27 | ||
29 | order = fls(count); | 28 | order = fls(count); |
30 | return order; /* We could be slightly more clever with -1 here... */ | 29 | return order; /* We could be slightly more clever with -1 here... */ |
31 | } | 30 | } |
@@ -33,7 +32,7 @@ static __inline__ int get_bitmask_order(unsigned int count) | |||
33 | static __inline__ int get_count_order(unsigned int count) | 32 | static __inline__ int get_count_order(unsigned int count) |
34 | { | 33 | { |
35 | int order; | 34 | int order; |
36 | 35 | ||
37 | order = fls(count) - 1; | 36 | order = fls(count) - 1; |
38 | if (count & (count - 1)) | 37 | if (count & (count - 1)) |
39 | order++; | 38 | order++; |
@@ -45,6 +44,31 @@ static inline unsigned long hweight_long(unsigned long w) | |||
45 | return sizeof(w) == 4 ? hweight32(w) : hweight64(w); | 44 | return sizeof(w) == 4 ? hweight32(w) : hweight64(w); |
46 | } | 45 | } |
47 | 46 | ||
47 | /* | ||
48 | * Clearly slow versions of the hweightN() functions, their benefit is | ||
49 | * of course compile time evaluation of constant arguments. | ||
50 | */ | ||
51 | #define HWEIGHT8(w) \ | ||
52 | ( BUILD_BUG_ON_ZERO(!__builtin_constant_p(w)) + \ | ||
53 | (!!((w) & (1ULL << 0))) + \ | ||
54 | (!!((w) & (1ULL << 1))) + \ | ||
55 | (!!((w) & (1ULL << 2))) + \ | ||
56 | (!!((w) & (1ULL << 3))) + \ | ||
57 | (!!((w) & (1ULL << 4))) + \ | ||
58 | (!!((w) & (1ULL << 5))) + \ | ||
59 | (!!((w) & (1ULL << 6))) + \ | ||
60 | (!!((w) & (1ULL << 7))) ) | ||
61 | |||
62 | #define HWEIGHT16(w) (HWEIGHT8(w) + HWEIGHT8((w) >> 8)) | ||
63 | #define HWEIGHT32(w) (HWEIGHT16(w) + HWEIGHT16((w) >> 16)) | ||
64 | #define HWEIGHT64(w) (HWEIGHT32(w) + HWEIGHT32((w) >> 32)) | ||
65 | |||
66 | /* | ||
67 | * Type invariant version that simply casts things to the | ||
68 | * largest type. | ||
69 | */ | ||
70 | #define HWEIGHT(w) HWEIGHT64((u64)(w)) | ||
71 | |||
48 | /** | 72 | /** |
49 | * rol32 - rotate a 32-bit value left | 73 | * rol32 - rotate a 32-bit value left |
50 | * @word: value to rotate | 74 | * @word: value to rotate |