1 #ifndef _LINUX_BITOPS_H 2 #define _LINUX_BITOPS_H 3 4 #include <asm/types.h> 5 #include <asm-generic/bitsperlong.h> 6 #include <linux/compiler.h> 7 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) 11 12 /* 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. 16 */ 17 #define GENMASK(h, l) \ 18 (((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h)))) 19 20 #define GENMASK_ULL(h, l) \ 21 (((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h)))) 22 23 /* 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). 27 */ 28 29 static inline int generic_ffs(int x) 30 { 31 int r = 1; 32 33 if (!x) 34 return 0; 35 if (!(x & 0xffff)) { 36 x >>= 16; 37 r += 16; 38 } 39 if (!(x & 0xff)) { 40 x >>= 8; 41 r += 8; 42 } 43 if (!(x & 0xf)) { 44 x >>= 4; 45 r += 4; 46 } 47 if (!(x & 3)) { 48 x >>= 2; 49 r += 2; 50 } 51 if (!(x & 1)) { 52 x >>= 1; 53 r += 1; 54 } 55 return r; 56 } 57 58 /** 59 * fls - find last (most-significant) bit set 60 * @x: the word to search 61 * 62 * This is defined the same way as ffs. 63 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. 64 */ 65 static inline int generic_fls(int x) 66 { 67 int r = 32; 68 69 if (!x) 70 return 0; 71 if (!(x & 0xffff0000u)) { 72 x <<= 16; 73 r -= 16; 74 } 75 if (!(x & 0xff000000u)) { 76 x <<= 8; 77 r -= 8; 78 } 79 if (!(x & 0xf0000000u)) { 80 x <<= 4; 81 r -= 4; 82 } 83 if (!(x & 0xc0000000u)) { 84 x <<= 2; 85 r -= 2; 86 } 87 if (!(x & 0x80000000u)) { 88 x <<= 1; 89 r -= 1; 90 } 91 return r; 92 } 93 94 95 /* 96 * hweightN: returns the hamming weight (i.e. the number 97 * of bits set) of a N-bit word 98 */ 99 100 static inline unsigned int generic_hweight32(unsigned int w) 101 { 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); 107 } 108 109 static inline unsigned int generic_hweight16(unsigned int w) 110 { 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); 115 } 116 117 static inline unsigned int generic_hweight8(unsigned int w) 118 { 119 unsigned int res = (w & 0x55) + ((w >> 1) & 0x55); 120 res = (res & 0x33) + ((res >> 2) & 0x33); 121 return (res & 0x0F) + ((res >> 4) & 0x0F); 122 } 123 124 #include <asm/bitops.h> 125 126 /* linux/include/asm-generic/bitops/non-atomic.h */ 127 128 #ifndef PLATFORM__SET_BIT 129 # define __set_bit generic_set_bit 130 #endif 131 132 #ifndef PLATFORM__CLEAR_BIT 133 # define __clear_bit generic_clear_bit 134 #endif 135 136 #ifndef PLATFORM_FFS 137 # define ffs generic_ffs 138 #endif 139 140 #ifndef PLATFORM_FLS 141 # define fls generic_fls 142 #endif 143 144 static inline unsigned fls_long(unsigned long l) 145 { 146 if (sizeof(l) == 4) 147 return fls(l); 148 return fls64(l); 149 } 150 151 /** 152 * __ffs64 - find first set bit in a 64 bit word 153 * @word: The 64 bit word 154 * 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. 158 */ 159 static inline unsigned long __ffs64(u64 word) 160 { 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 166 #endif 167 return __ffs((unsigned long)word); 168 } 169 170 /** 171 * __set_bit - Set a bit in memory 172 * @nr: the bit to set 173 * @addr: the address to start counting from 174 * 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. 178 */ 179 static inline void generic_set_bit(int nr, volatile unsigned long *addr) 180 { 181 unsigned long mask = BIT_MASK(nr); 182 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr); 183 184 *p |= mask; 185 } 186 187 static inline void generic_clear_bit(int nr, volatile unsigned long *addr) 188 { 189 unsigned long mask = BIT_MASK(nr); 190 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr); 191 192 *p &= ~mask; 193 } 194 195 #endif 196