Rev 1408 | Rev 3031 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1408 | Rev 1964 | ||
---|---|---|---|
1 | #ifndef _LINUX_BITOPS_H |
1 | #ifndef _LINUX_BITOPS_H |
2 | #define _LINUX_BITOPS_H |
2 | #define _LINUX_BITOPS_H |
- | 3 | #include |
|
- | 4 | ||
3 | 5 | #ifdef __KERNEL__ |
|
4 | #define BIT(nr) (1UL << (nr)) |
6 | #define BIT(nr) (1UL << (nr)) |
5 | #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG)) |
7 | #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG)) |
6 | #define BIT_WORD(nr) ((nr) / BITS_PER_LONG) |
8 | #define BIT_WORD(nr) ((nr) / BITS_PER_LONG) |
7 | #define BITS_PER_BYTE 8 |
9 | #define BITS_PER_BYTE 8 |
8 | #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long)) |
10 | #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long)) |
- | 11 | #endif |
|
- | 12 | ||
- | 13 | extern unsigned int __sw_hweight8(unsigned int w); |
|
- | 14 | extern unsigned int __sw_hweight16(unsigned int w); |
|
- | 15 | extern unsigned int __sw_hweight32(unsigned int w); |
|
- | 16 | extern unsigned long __sw_hweight64(__u64 w); |
|
9 | 17 | ||
10 | /* |
18 | /* |
11 | * Include this here because some architectures need generic_ffs/fls in |
19 | * Include this here because some architectures need generic_ffs/fls in |
12 | * scope |
20 | * scope |
13 | */ |
21 | */ |
14 | #include |
22 | #include |
15 | 23 | ||
16 | #define for_each_bit(bit, addr, size) \ |
24 | #define for_each_set_bit(bit, addr, size) \ |
17 | for ((bit) = find_first_bit((addr), (size)); \ |
25 | for ((bit) = find_first_bit((addr), (size)); \ |
18 | (bit) < (size); \ |
26 | (bit) < (size); \ |
19 | (bit) = find_next_bit((addr), (size), (bit) + 1)) |
27 | (bit) = find_next_bit((addr), (size), (bit) + 1)) |
20 | - | ||
21 | 28 | ||
22 | static __inline__ int get_bitmask_order(unsigned int count) |
29 | static __inline__ int get_bitmask_order(unsigned int count) |
23 | { |
30 | { |
24 | int order; |
31 | int order; |
25 | 32 | ||
26 | order = fls(count); |
33 | order = fls(count); |
27 | return order; /* We could be slightly more clever with -1 here... */ |
34 | return order; /* We could be slightly more clever with -1 here... */ |
28 | } |
35 | } |
29 | 36 | ||
30 | static __inline__ int get_count_order(unsigned int count) |
37 | static __inline__ int get_count_order(unsigned int count) |
31 | { |
38 | { |
32 | int order; |
39 | int order; |
33 | 40 | ||
34 | order = fls(count) - 1; |
41 | order = fls(count) - 1; |
35 | if (count & (count - 1)) |
42 | if (count & (count - 1)) |
36 | order++; |
43 | order++; |
37 | return order; |
44 | return order; |
38 | } |
45 | } |
39 | 46 | ||
40 | static inline unsigned long hweight_long(unsigned long w) |
47 | static inline unsigned long hweight_long(unsigned long w) |
41 | { |
48 | { |
42 | return sizeof(w) == 4 ? hweight32(w) : hweight64(w); |
49 | return sizeof(w) == 4 ? hweight32(w) : hweight64(w); |
43 | } |
50 | } |
44 | 51 | ||
45 | /** |
52 | /** |
46 | * rol32 - rotate a 32-bit value left |
53 | * rol32 - rotate a 32-bit value left |
47 | * @word: value to rotate |
54 | * @word: value to rotate |
48 | * @shift: bits to roll |
55 | * @shift: bits to roll |
49 | */ |
56 | */ |
50 | static inline __u32 rol32(__u32 word, unsigned int shift) |
57 | static inline __u32 rol32(__u32 word, unsigned int shift) |
51 | { |
58 | { |
52 | return (word << shift) | (word >> (32 - shift)); |
59 | return (word << shift) | (word >> (32 - shift)); |
53 | } |
60 | } |
54 | 61 | ||
55 | /** |
62 | /** |
56 | * ror32 - rotate a 32-bit value right |
63 | * ror32 - rotate a 32-bit value right |
57 | * @word: value to rotate |
64 | * @word: value to rotate |
58 | * @shift: bits to roll |
65 | * @shift: bits to roll |
59 | */ |
66 | */ |
60 | static inline __u32 ror32(__u32 word, unsigned int shift) |
67 | static inline __u32 ror32(__u32 word, unsigned int shift) |
61 | { |
68 | { |
62 | return (word >> shift) | (word << (32 - shift)); |
69 | return (word >> shift) | (word << (32 - shift)); |
63 | } |
70 | } |
64 | 71 | ||
65 | /** |
72 | /** |
66 | * rol16 - rotate a 16-bit value left |
73 | * rol16 - rotate a 16-bit value left |
67 | * @word: value to rotate |
74 | * @word: value to rotate |
68 | * @shift: bits to roll |
75 | * @shift: bits to roll |
69 | */ |
76 | */ |
70 | static inline __u16 rol16(__u16 word, unsigned int shift) |
77 | static inline __u16 rol16(__u16 word, unsigned int shift) |
71 | { |
78 | { |
72 | return (word << shift) | (word >> (16 - shift)); |
79 | return (word << shift) | (word >> (16 - shift)); |
73 | } |
80 | } |
74 | 81 | ||
75 | /** |
82 | /** |
76 | * ror16 - rotate a 16-bit value right |
83 | * ror16 - rotate a 16-bit value right |
77 | * @word: value to rotate |
84 | * @word: value to rotate |
78 | * @shift: bits to roll |
85 | * @shift: bits to roll |
79 | */ |
86 | */ |
80 | static inline __u16 ror16(__u16 word, unsigned int shift) |
87 | static inline __u16 ror16(__u16 word, unsigned int shift) |
81 | { |
88 | { |
82 | return (word >> shift) | (word << (16 - shift)); |
89 | return (word >> shift) | (word << (16 - shift)); |
83 | } |
90 | } |
84 | 91 | ||
85 | /** |
92 | /** |
86 | * rol8 - rotate an 8-bit value left |
93 | * rol8 - rotate an 8-bit value left |
87 | * @word: value to rotate |
94 | * @word: value to rotate |
88 | * @shift: bits to roll |
95 | * @shift: bits to roll |
89 | */ |
96 | */ |
90 | static inline __u8 rol8(__u8 word, unsigned int shift) |
97 | static inline __u8 rol8(__u8 word, unsigned int shift) |
91 | { |
98 | { |
92 | return (word << shift) | (word >> (8 - shift)); |
99 | return (word << shift) | (word >> (8 - shift)); |
93 | } |
100 | } |
94 | 101 | ||
95 | /** |
102 | /** |
96 | * ror8 - rotate an 8-bit value right |
103 | * ror8 - rotate an 8-bit value right |
97 | * @word: value to rotate |
104 | * @word: value to rotate |
98 | * @shift: bits to roll |
105 | * @shift: bits to roll |
99 | */ |
106 | */ |
100 | static inline __u8 ror8(__u8 word, unsigned int shift) |
107 | static inline __u8 ror8(__u8 word, unsigned int shift) |
101 | { |
108 | { |
102 | return (word >> shift) | (word << (8 - shift)); |
109 | return (word >> shift) | (word << (8 - shift)); |
103 | } |
110 | } |
104 | 111 | ||
105 | static inline unsigned fls_long(unsigned long l) |
112 | static inline unsigned fls_long(unsigned long l) |
106 | { |
113 | { |
107 | if (sizeof(l) == 4) |
114 | if (sizeof(l) == 4) |
108 | return fls(l); |
115 | return fls(l); |
109 | return fls64(l); |
116 | return fls64(l); |
110 | } |
117 | } |
111 | 118 | ||
112 | /** |
119 | /** |
113 | * __ffs64 - find first set bit in a 64 bit word |
120 | * __ffs64 - find first set bit in a 64 bit word |
114 | * @word: The 64 bit word |
121 | * @word: The 64 bit word |
115 | * |
122 | * |
116 | * On 64 bit arches this is a synomyn for __ffs |
123 | * On 64 bit arches this is a synomyn for __ffs |
117 | * The result is not defined if no bits are set, so check that @word |
124 | * The result is not defined if no bits are set, so check that @word |
118 | * is non-zero before calling this. |
125 | * is non-zero before calling this. |
119 | */ |
126 | */ |
120 | static inline unsigned long __ffs64(u64 word) |
127 | static inline unsigned long __ffs64(u64 word) |
121 | { |
128 | { |
122 | #if BITS_PER_LONG == 32 |
129 | #if BITS_PER_LONG == 32 |
123 | if (((u32)word) == 0UL) |
130 | if (((u32)word) == 0UL) |
124 | return __ffs((u32)(word >> 32)) + 32; |
131 | return __ffs((u32)(word >> 32)) + 32; |
125 | #elif BITS_PER_LONG != 64 |
132 | #elif BITS_PER_LONG != 64 |
126 | #error BITS_PER_LONG not 32 or 64 |
133 | #error BITS_PER_LONG not 32 or 64 |
127 | #endif |
134 | #endif |
128 | return __ffs((unsigned long)word); |
135 | return __ffs((unsigned long)word); |
129 | } |
136 | } |
130 | 137 | ||
131 | #ifdef __KERNEL__ |
138 | #ifdef __KERNEL__ |
132 | #ifdef CONFIG_GENERIC_FIND_FIRST_BIT |
- | |
133 | - | ||
134 | /** |
- | |
135 | * find_first_bit - find the first set bit in a memory region |
- | |
136 | * @addr: The address to start the search at |
- | |
137 | * @size: The maximum size to search |
- | |
138 | * |
- | |
139 | * Returns the bit number of the first set bit. |
- | |
140 | */ |
- | |
141 | extern unsigned long find_first_bit(const unsigned long *addr, |
- | |
142 | unsigned long size); |
- | |
143 | - | ||
144 | /** |
- | |
145 | * find_first_zero_bit - find the first cleared bit in a memory region |
- | |
146 | * @addr: The address to start the search at |
- | |
147 | * @size: The maximum size to search |
- | |
148 | * |
- | |
149 | * Returns the bit number of the first cleared bit. |
- | |
150 | */ |
- | |
151 | extern unsigned long find_first_zero_bit(const unsigned long *addr, |
- | |
152 | unsigned long size); |
- | |
153 | #endif /* CONFIG_GENERIC_FIND_FIRST_BIT */ |
- | |
154 | 139 | ||
155 | #ifdef CONFIG_GENERIC_FIND_LAST_BIT |
140 | #ifdef CONFIG_GENERIC_FIND_LAST_BIT |
156 | /** |
141 | /** |
157 | * find_last_bit - find the last set bit in a memory region |
142 | * find_last_bit - find the last set bit in a memory region |
158 | * @addr: The address to start the search at |
143 | * @addr: The address to start the search at |
159 | * @size: The maximum size to search |
144 | * @size: The maximum size to search |
160 | * |
145 | * |
161 | * Returns the bit number of the first set bit, or size. |
146 | * Returns the bit number of the first set bit, or size. |
162 | */ |
147 | */ |
163 | extern unsigned long find_last_bit(const unsigned long *addr, |
148 | extern unsigned long find_last_bit(const unsigned long *addr, |
164 | unsigned long size); |
149 | unsigned long size); |
165 | #endif /* CONFIG_GENERIC_FIND_LAST_BIT */ |
150 | #endif /* CONFIG_GENERIC_FIND_LAST_BIT */ |
166 | - | ||
167 | #ifdef CONFIG_GENERIC_FIND_NEXT_BIT |
- | |
168 | - | ||
169 | /** |
- | |
170 | * find_next_bit - find the next set bit in a memory region |
- | |
171 | * @addr: The address to base the search on |
- | |
172 | * @offset: The bitnumber to start searching at |
- | |
173 | * @size: The bitmap size in bits |
- | |
174 | */ |
- | |
175 | extern unsigned long find_next_bit(const unsigned long *addr, |
- | |
176 | unsigned long size, unsigned long offset); |
- | |
177 | - | ||
178 | /** |
- | |
179 | * find_next_zero_bit - find the next cleared bit in a memory region |
- | |
180 | * @addr: The address to base the search on |
- | |
181 | * @offset: The bitnumber to start searching at |
- | |
182 | * @size: The bitmap size in bits |
- | |
183 | */ |
- | |
184 | - | ||
185 | extern unsigned long find_next_zero_bit(const unsigned long *addr, |
- | |
186 | unsigned long size, |
- | |
187 | unsigned long offset); |
- | |
188 | - | ||
189 | #endif /* CONFIG_GENERIC_FIND_NEXT_BIT */ |
151 | |
190 | #endif /* __KERNEL__ */ |
152 | #endif /* __KERNEL__ */ |
191 | #endif><>><>><>><>><>><>>><>><> |
153 | #endif><>><>><>><>><>><>>><>><> |