1 #ifndef _LINUX_BITOPS_H
2 #define _LINUX_BITOPS_H
5 #include <asm-generic/bitsperlong.h>
6 #include <linux/compiler.h>
7 #include <linux/kernel.h>
10 #define BIT(nr) (1UL << (nr))
11 #define BIT_ULL(nr) (1ULL << (nr))
12 #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
13 #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
14 #define BIT_ULL_MASK(nr) (1ULL << ((nr) % BITS_PER_LONG_LONG))
15 #define BIT_ULL_WORD(nr) ((nr) / BITS_PER_LONG_LONG)
16 #define BITS_PER_BYTE 8
17 #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
21 * Create a contiguous bitmask starting at bit position @l and ending at
22 * position @h. For example
23 * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
26 #define GENMASK(h, l) \
27 (((~0UL) << (l)) & (~0UL >> (CONFIG_SANDBOX_BITS_PER_LONG - 1 - (h))))
29 #define GENMASK(h, l) \
30 (((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
33 #define GENMASK_ULL(h, l) \
34 (((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
37 * ffs: find first bit set. This is defined the same way as
38 * the libc and compiler builtin ffs routines, therefore
39 * differs in spirit from the above ffz (man ffs).
42 static inline int generic_ffs(int x)
72 * fls - find last (most-significant) bit set
73 * @x: the word to search
75 * This is defined the same way as ffs.
76 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
78 static inline int generic_fls(int x)
84 if (!(x & 0xffff0000u)) {
88 if (!(x & 0xff000000u)) {
92 if (!(x & 0xf0000000u)) {
96 if (!(x & 0xc0000000u)) {
100 if (!(x & 0x80000000u)) {
109 * hweightN: returns the hamming weight (i.e. the number
110 * of bits set) of a N-bit word
113 static inline unsigned int generic_hweight32(unsigned int w)
115 unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
116 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
117 res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
118 res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
119 return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
122 static inline unsigned int generic_hweight16(unsigned int w)
124 unsigned int res = (w & 0x5555) + ((w >> 1) & 0x5555);
125 res = (res & 0x3333) + ((res >> 2) & 0x3333);
126 res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F);
127 return (res & 0x00FF) + ((res >> 8) & 0x00FF);
130 static inline unsigned int generic_hweight8(unsigned int w)
132 unsigned int res = (w & 0x55) + ((w >> 1) & 0x55);
133 res = (res & 0x33) + ((res >> 2) & 0x33);
134 return (res & 0x0F) + ((res >> 4) & 0x0F);
137 static inline unsigned long generic_hweight64(__u64 w)
139 return generic_hweight32((unsigned int)(w >> 32)) +
140 generic_hweight32((unsigned int)w);
143 static inline unsigned long hweight_long(unsigned long w)
145 return sizeof(w) == 4 ? generic_hweight32(w) : generic_hweight64(w);
148 #include <asm/bitops.h>
150 /* linux/include/asm-generic/bitops/non-atomic.h */
152 #ifndef PLATFORM__SET_BIT
153 # define __set_bit generic_set_bit
156 #ifndef PLATFORM__CLEAR_BIT
157 # define __clear_bit generic_clear_bit
161 # define ffs generic_ffs
165 # define fls generic_fls
168 static inline unsigned fls_long(unsigned long l)
176 * __ffs64 - find first set bit in a 64 bit word
177 * @word: The 64 bit word
179 * On 64 bit arches this is a synomyn for __ffs
180 * The result is not defined if no bits are set, so check that @word
181 * is non-zero before calling this.
183 static inline unsigned long __ffs64(u64 word)
185 #if BITS_PER_LONG == 32
186 if (((u32)word) == 0UL)
187 return __ffs((u32)(word >> 32)) + 32;
188 #elif BITS_PER_LONG != 64
189 #error BITS_PER_LONG not 32 or 64
191 return __ffs((unsigned long)word);
195 * __set_bit - Set a bit in memory
196 * @nr: the bit to set
197 * @addr: the address to start counting from
199 * Unlike set_bit(), this function is non-atomic and may be reordered.
200 * If it's called on the same region of memory simultaneously, the effect
201 * may be that only one operation succeeds.
203 static inline void generic_set_bit(int nr, volatile unsigned long *addr)
205 unsigned long mask = BIT_MASK(nr);
206 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
211 static inline void generic_clear_bit(int nr, volatile unsigned long *addr)
213 unsigned long mask = BIT_MASK(nr);
214 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);