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