1 #ifndef _LINUX_BITOPS_H
2 #define _LINUX_BITOPS_H
6 #define BIT(nr) (1UL << (nr))
7 #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
8 #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
9 #define BITS_PER_BYTE 8
10 #ifndef BITS_TO_LONGS /* Older kernels define this already */
11 #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
15 extern unsigned int __sw_hweight8(unsigned int w);
16 extern unsigned int __sw_hweight16(unsigned int w);
17 extern unsigned int __sw_hweight32(unsigned int w);
18 extern unsigned long __sw_hweight64(__u64 w);
21 * Include this here because some architectures need generic_ffs/fls in
24 #include <asm/bitops.h>
26 #define for_each_set_bit(bit, addr, size) \
27 for ((bit) = find_first_bit((addr), (size)); \
29 (bit) = find_next_bit((addr), (size), (bit) + 1))
31 static __inline__ int get_bitmask_order(unsigned int count)
36 return order; /* We could be slightly more clever with -1 here... */
39 static __inline__ int get_count_order(unsigned int count)
43 order = fls(count) - 1;
44 if (count & (count - 1))
49 static inline unsigned long hweight_long(unsigned long w)
51 return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
55 * rol32 - rotate a 32-bit value left
56 * @word: value to rotate
57 * @shift: bits to roll
59 static inline __u32 rol32(__u32 word, unsigned int shift)
61 return (word << shift) | (word >> (32 - shift));
65 * ror32 - rotate a 32-bit value right
66 * @word: value to rotate
67 * @shift: bits to roll
69 static inline __u32 ror32(__u32 word, unsigned int shift)
71 return (word >> shift) | (word << (32 - shift));
75 * rol16 - rotate a 16-bit value left
76 * @word: value to rotate
77 * @shift: bits to roll
79 static inline __u16 rol16(__u16 word, unsigned int shift)
81 return (word << shift) | (word >> (16 - shift));
85 * ror16 - rotate a 16-bit value right
86 * @word: value to rotate
87 * @shift: bits to roll
89 static inline __u16 ror16(__u16 word, unsigned int shift)
91 return (word >> shift) | (word << (16 - shift));
95 * rol8 - rotate an 8-bit value left
96 * @word: value to rotate
97 * @shift: bits to roll
99 static inline __u8 rol8(__u8 word, unsigned int shift)
101 return (word << shift) | (word >> (8 - shift));
105 * ror8 - rotate an 8-bit value right
106 * @word: value to rotate
107 * @shift: bits to roll
109 static inline __u8 ror8(__u8 word, unsigned int shift)
111 return (word >> shift) | (word << (8 - shift));
114 static inline unsigned fls_long(unsigned long l)
122 * __ffs64 - find first set bit in a 64 bit word
123 * @word: The 64 bit word
125 * On 64 bit arches this is a synomyn for __ffs
126 * The result is not defined if no bits are set, so check that @word
127 * is non-zero before calling this.
129 static inline unsigned long __ffs64(u64 word)
131 #if BITS_PER_LONG == 32
132 if (((u32)word) == 0UL)
133 return __ffs((u32)(word >> 32)) + 32;
134 #elif BITS_PER_LONG != 64
135 #error BITS_PER_LONG not 32 or 64
137 return __ffs((unsigned long)word);
141 #ifdef CONFIG_GENERIC_FIND_FIRST_BIT
144 * find_first_bit - find the first set bit in a memory region
145 * @addr: The address to start the search at
146 * @size: The maximum size to search
148 * Returns the bit number of the first set bit.
150 extern unsigned long find_first_bit(const unsigned long *addr,
154 * find_first_zero_bit - find the first cleared bit in a memory region
155 * @addr: The address to start the search at
156 * @size: The maximum size to search
158 * Returns the bit number of the first cleared bit.
160 extern unsigned long find_first_zero_bit(const unsigned long *addr,
162 #endif /* CONFIG_GENERIC_FIND_FIRST_BIT */
164 #ifdef CONFIG_GENERIC_FIND_LAST_BIT
166 * find_last_bit - find the last set bit in a memory region
167 * @addr: The address to start the search at
168 * @size: The maximum size to search
170 * Returns the bit number of the first set bit, or size.
172 extern unsigned long find_last_bit(const unsigned long *addr,
174 #endif /* CONFIG_GENERIC_FIND_LAST_BIT */
176 #ifdef CONFIG_GENERIC_FIND_NEXT_BIT
179 * find_next_bit - find the next set 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
184 extern unsigned long find_next_bit(const unsigned long *addr,
185 unsigned long size, unsigned long offset);
188 * find_next_zero_bit - find the next cleared bit in a memory region
189 * @addr: The address to base the search on
190 * @offset: The bitnumber to start searching at
191 * @size: The bitmap size in bits
194 extern unsigned long find_next_zero_bit(const unsigned long *addr,
196 unsigned long offset);
198 #endif /* CONFIG_GENERIC_FIND_NEXT_BIT */
199 #endif /* __KERNEL__ */