1 #ifndef _LINUX_BITOPS_H
2 #define _LINUX_BITOPS_H
5 #include <asm-generic/bitsperlong.h>
6 #include <linux/compiler.h>
8 #define BIT(nr) (1UL << (nr))
9 #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
10 #define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
13 * Create a contiguous bitmask starting at bit position @l and ending at
14 * position @h. For example
15 * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
17 #define GENMASK(h, l) \
18 (((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
20 #define GENMASK_ULL(h, l) \
21 (((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
24 * ffs: find first bit set. This is defined the same way as
25 * the libc and compiler builtin ffs routines, therefore
26 * differs in spirit from the above ffz (man ffs).
29 static inline int generic_ffs(int x)
59 * fls - find last (most-significant) bit set
60 * @x: the word to search
62 * This is defined the same way as ffs.
63 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
65 static inline int generic_fls(int x)
71 if (!(x & 0xffff0000u)) {
75 if (!(x & 0xff000000u)) {
79 if (!(x & 0xf0000000u)) {
83 if (!(x & 0xc0000000u)) {
87 if (!(x & 0x80000000u)) {
96 * hweightN: returns the hamming weight (i.e. the number
97 * of bits set) of a N-bit word
100 static inline unsigned int generic_hweight32(unsigned int w)
102 unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
103 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
104 res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
105 res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
106 return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
109 static inline unsigned int generic_hweight16(unsigned int w)
111 unsigned int res = (w & 0x5555) + ((w >> 1) & 0x5555);
112 res = (res & 0x3333) + ((res >> 2) & 0x3333);
113 res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F);
114 return (res & 0x00FF) + ((res >> 8) & 0x00FF);
117 static inline unsigned int generic_hweight8(unsigned int w)
119 unsigned int res = (w & 0x55) + ((w >> 1) & 0x55);
120 res = (res & 0x33) + ((res >> 2) & 0x33);
121 return (res & 0x0F) + ((res >> 4) & 0x0F);
124 #include <asm/bitops.h>
126 /* linux/include/asm-generic/bitops/non-atomic.h */
128 #ifndef PLATFORM__SET_BIT
129 # define __set_bit generic_set_bit
132 #ifndef PLATFORM__CLEAR_BIT
133 # define __clear_bit generic_clear_bit
137 # define ffs generic_ffs
141 # define fls generic_fls
144 static inline unsigned fls_long(unsigned long l)
152 * __ffs64 - find first set bit in a 64 bit word
153 * @word: The 64 bit word
155 * On 64 bit arches this is a synomyn for __ffs
156 * The result is not defined if no bits are set, so check that @word
157 * is non-zero before calling this.
159 static inline unsigned long __ffs64(u64 word)
161 #if BITS_PER_LONG == 32
162 if (((u32)word) == 0UL)
163 return __ffs((u32)(word >> 32)) + 32;
164 #elif BITS_PER_LONG != 64
165 #error BITS_PER_LONG not 32 or 64
167 return __ffs((unsigned long)word);
171 * __set_bit - Set a bit in memory
172 * @nr: the bit to set
173 * @addr: the address to start counting from
175 * Unlike set_bit(), this function is non-atomic and may be reordered.
176 * If it's called on the same region of memory simultaneously, the effect
177 * may be that only one operation succeeds.
179 static inline void generic_set_bit(int nr, volatile unsigned long *addr)
181 unsigned long mask = BIT_MASK(nr);
182 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
187 static inline void generic_clear_bit(int nr, volatile unsigned long *addr)
189 unsigned long mask = BIT_MASK(nr);
190 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);