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)
11 * Create a contiguous bitmask starting at bit position @l and ending at
12 * position @h. For example
13 * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
15 #define GENMASK(h, l) \
16 (((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
18 #define GENMASK_ULL(h, l) \
19 (((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
22 * ffs: find first bit set. This is defined the same way as
23 * the libc and compiler builtin ffs routines, therefore
24 * differs in spirit from the above ffz (man ffs).
27 static inline int generic_ffs(int x)
57 * fls - find last (most-significant) bit set
58 * @x: the word to search
60 * This is defined the same way as ffs.
61 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
63 static inline int generic_fls(int x)
69 if (!(x & 0xffff0000u)) {
73 if (!(x & 0xff000000u)) {
77 if (!(x & 0xf0000000u)) {
81 if (!(x & 0xc0000000u)) {
85 if (!(x & 0x80000000u)) {
94 * hweightN: returns the hamming weight (i.e. the number
95 * of bits set) of a N-bit word
98 static inline unsigned int generic_hweight32(unsigned int w)
100 unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
101 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
102 res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
103 res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
104 return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
107 static inline unsigned int generic_hweight16(unsigned int w)
109 unsigned int res = (w & 0x5555) + ((w >> 1) & 0x5555);
110 res = (res & 0x3333) + ((res >> 2) & 0x3333);
111 res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F);
112 return (res & 0x00FF) + ((res >> 8) & 0x00FF);
115 static inline unsigned int generic_hweight8(unsigned int w)
117 unsigned int res = (w & 0x55) + ((w >> 1) & 0x55);
118 res = (res & 0x33) + ((res >> 2) & 0x33);
119 return (res & 0x0F) + ((res >> 4) & 0x0F);
122 #include <asm/bitops.h>
124 /* linux/include/asm-generic/bitops/non-atomic.h */
126 #ifndef PLATFORM__SET_BIT
127 # define __set_bit generic_set_bit
130 #ifndef PLATFORM__CLEAR_BIT
131 # define __clear_bit generic_clear_bit
135 # define ffs generic_ffs
139 # define fls generic_fls
143 * __set_bit - Set a bit in memory
144 * @nr: the bit to set
145 * @addr: the address to start counting from
147 * Unlike set_bit(), this function is non-atomic and may be reordered.
148 * If it's called on the same region of memory simultaneously, the effect
149 * may be that only one operation succeeds.
151 static inline void generic_set_bit(int nr, volatile unsigned long *addr)
153 unsigned long mask = BIT_MASK(nr);
154 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
159 static inline void generic_clear_bit(int nr, volatile unsigned long *addr)
161 unsigned long mask = BIT_MASK(nr);
162 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);