wdenk | b15cbc0 | 2000-08-21 15:05:47 +0000 | [diff] [blame] | 1 | #ifndef _LINUX_BITOPS_H |
| 2 | #define _LINUX_BITOPS_H |
| 3 | |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 4 | #ifndef USE_HOSTCC |
| 5 | |
Simon Kagstrom | 02f9990 | 2009-08-24 09:09:50 +0200 | [diff] [blame] | 6 | #include <asm/types.h> |
Stefan Roese | e1b27d2 | 2017-02-20 16:50:26 +0100 | [diff] [blame] | 7 | #include <asm-generic/bitsperlong.h> |
Fabio Estevam | de4d2e9 | 2015-11-05 12:43:38 -0200 | [diff] [blame] | 8 | #include <linux/compiler.h> |
Vignesh Raghavendra | 016e6d6 | 2019-12-09 10:25:31 +0530 | [diff] [blame] | 9 | #include <linux/kernel.h> |
wdenk | b15cbc0 | 2000-08-21 15:05:47 +0000 | [diff] [blame] | 10 | |
Masahiro Yamada | ed3986c | 2017-11-22 02:38:11 +0900 | [diff] [blame] | 11 | #ifdef __KERNEL__ |
Jagan Teki | 6734528 | 2015-10-21 17:30:34 +0530 | [diff] [blame] | 12 | #define BIT(nr) (1UL << (nr)) |
Masahiro Yamada | ed3986c | 2017-11-22 02:38:11 +0900 | [diff] [blame] | 13 | #define BIT_ULL(nr) (1ULL << (nr)) |
Jagan Teki | 6734528 | 2015-10-21 17:30:34 +0530 | [diff] [blame] | 14 | #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG)) |
| 15 | #define BIT_WORD(nr) ((nr) / BITS_PER_LONG) |
Masahiro Yamada | ed3986c | 2017-11-22 02:38:11 +0900 | [diff] [blame] | 16 | #define BIT_ULL_MASK(nr) (1ULL << ((nr) % BITS_PER_LONG_LONG)) |
| 17 | #define BIT_ULL_WORD(nr) ((nr) / BITS_PER_LONG_LONG) |
| 18 | #define BITS_PER_BYTE 8 |
| 19 | #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long)) |
| 20 | #endif |
Heiko Schocher | 92a3188 | 2015-09-07 13:43:52 +0200 | [diff] [blame] | 21 | |
wdenk | b15cbc0 | 2000-08-21 15:05:47 +0000 | [diff] [blame] | 22 | /* |
Jagan Teki | 89b5c81 | 2015-10-21 16:46:51 +0530 | [diff] [blame] | 23 | * Create a contiguous bitmask starting at bit position @l and ending at |
| 24 | * position @h. For example |
| 25 | * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000. |
| 26 | */ |
Vignesh R | e519c61 | 2019-02-05 11:29:11 +0530 | [diff] [blame] | 27 | #ifdef CONFIG_SANDBOX |
| 28 | #define GENMASK(h, l) \ |
| 29 | (((~0UL) << (l)) & (~0UL >> (CONFIG_SANDBOX_BITS_PER_LONG - 1 - (h)))) |
| 30 | #else |
Jagan Teki | 89b5c81 | 2015-10-21 16:46:51 +0530 | [diff] [blame] | 31 | #define GENMASK(h, l) \ |
| 32 | (((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h)))) |
Vignesh R | e519c61 | 2019-02-05 11:29:11 +0530 | [diff] [blame] | 33 | #endif |
Jagan Teki | 89b5c81 | 2015-10-21 16:46:51 +0530 | [diff] [blame] | 34 | |
| 35 | #define GENMASK_ULL(h, l) \ |
| 36 | (((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h)))) |
| 37 | |
| 38 | /* |
wdenk | b15cbc0 | 2000-08-21 15:05:47 +0000 | [diff] [blame] | 39 | * ffs: find first bit set. This is defined the same way as |
| 40 | * the libc and compiler builtin ffs routines, therefore |
| 41 | * differs in spirit from the above ffz (man ffs). |
| 42 | */ |
| 43 | |
| 44 | static inline int generic_ffs(int x) |
| 45 | { |
| 46 | int r = 1; |
| 47 | |
| 48 | if (!x) |
| 49 | return 0; |
| 50 | if (!(x & 0xffff)) { |
| 51 | x >>= 16; |
| 52 | r += 16; |
| 53 | } |
| 54 | if (!(x & 0xff)) { |
| 55 | x >>= 8; |
| 56 | r += 8; |
| 57 | } |
| 58 | if (!(x & 0xf)) { |
| 59 | x >>= 4; |
| 60 | r += 4; |
| 61 | } |
| 62 | if (!(x & 3)) { |
| 63 | x >>= 2; |
| 64 | r += 2; |
| 65 | } |
| 66 | if (!(x & 1)) { |
| 67 | x >>= 1; |
| 68 | r += 1; |
| 69 | } |
| 70 | return r; |
| 71 | } |
| 72 | |
Simon Kagstrom | 52d6122 | 2009-08-24 09:10:12 +0200 | [diff] [blame] | 73 | /** |
| 74 | * fls - find last (most-significant) bit set |
| 75 | * @x: the word to search |
| 76 | * |
| 77 | * This is defined the same way as ffs. |
| 78 | * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. |
| 79 | */ |
| 80 | static inline int generic_fls(int x) |
| 81 | { |
| 82 | int r = 32; |
| 83 | |
| 84 | if (!x) |
| 85 | return 0; |
| 86 | if (!(x & 0xffff0000u)) { |
| 87 | x <<= 16; |
| 88 | r -= 16; |
| 89 | } |
| 90 | if (!(x & 0xff000000u)) { |
| 91 | x <<= 8; |
| 92 | r -= 8; |
| 93 | } |
| 94 | if (!(x & 0xf0000000u)) { |
| 95 | x <<= 4; |
| 96 | r -= 4; |
| 97 | } |
| 98 | if (!(x & 0xc0000000u)) { |
| 99 | x <<= 2; |
| 100 | r -= 2; |
| 101 | } |
| 102 | if (!(x & 0x80000000u)) { |
| 103 | x <<= 1; |
| 104 | r -= 1; |
| 105 | } |
| 106 | return r; |
| 107 | } |
| 108 | |
| 109 | |
wdenk | b15cbc0 | 2000-08-21 15:05:47 +0000 | [diff] [blame] | 110 | /* |
| 111 | * hweightN: returns the hamming weight (i.e. the number |
| 112 | * of bits set) of a N-bit word |
| 113 | */ |
| 114 | |
| 115 | static inline unsigned int generic_hweight32(unsigned int w) |
| 116 | { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 117 | unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555); |
| 118 | res = (res & 0x33333333) + ((res >> 2) & 0x33333333); |
| 119 | res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F); |
| 120 | res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF); |
| 121 | return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF); |
wdenk | b15cbc0 | 2000-08-21 15:05:47 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static inline unsigned int generic_hweight16(unsigned int w) |
| 125 | { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 126 | unsigned int res = (w & 0x5555) + ((w >> 1) & 0x5555); |
| 127 | res = (res & 0x3333) + ((res >> 2) & 0x3333); |
| 128 | res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F); |
| 129 | return (res & 0x00FF) + ((res >> 8) & 0x00FF); |
wdenk | b15cbc0 | 2000-08-21 15:05:47 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static inline unsigned int generic_hweight8(unsigned int w) |
| 133 | { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 134 | unsigned int res = (w & 0x55) + ((w >> 1) & 0x55); |
| 135 | res = (res & 0x33) + ((res >> 2) & 0x33); |
| 136 | return (res & 0x0F) + ((res >> 4) & 0x0F); |
wdenk | b15cbc0 | 2000-08-21 15:05:47 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Vignesh Raghavendra | 016e6d6 | 2019-12-09 10:25:31 +0530 | [diff] [blame] | 139 | static inline unsigned long generic_hweight64(__u64 w) |
| 140 | { |
| 141 | return generic_hweight32((unsigned int)(w >> 32)) + |
| 142 | generic_hweight32((unsigned int)w); |
| 143 | } |
| 144 | |
| 145 | static inline unsigned long hweight_long(unsigned long w) |
| 146 | { |
| 147 | return sizeof(w) == 4 ? generic_hweight32(w) : generic_hweight64(w); |
| 148 | } |
| 149 | |
wdenk | b15cbc0 | 2000-08-21 15:05:47 +0000 | [diff] [blame] | 150 | #include <asm/bitops.h> |
| 151 | |
Simon Kagstrom | 02f9990 | 2009-08-24 09:09:50 +0200 | [diff] [blame] | 152 | /* linux/include/asm-generic/bitops/non-atomic.h */ |
| 153 | |
Simon Kagstrom | 0413cfe | 2009-09-17 15:15:52 +0200 | [diff] [blame] | 154 | #ifndef PLATFORM__SET_BIT |
Simon Kagstrom | 02f9990 | 2009-08-24 09:09:50 +0200 | [diff] [blame] | 155 | # define __set_bit generic_set_bit |
| 156 | #endif |
| 157 | |
Simon Kagstrom | 0413cfe | 2009-09-17 15:15:52 +0200 | [diff] [blame] | 158 | #ifndef PLATFORM__CLEAR_BIT |
Simon Kagstrom | 02f9990 | 2009-08-24 09:09:50 +0200 | [diff] [blame] | 159 | # define __clear_bit generic_clear_bit |
| 160 | #endif |
| 161 | |
Simon Kagstrom | 0413cfe | 2009-09-17 15:15:52 +0200 | [diff] [blame] | 162 | #ifndef PLATFORM_FFS |
Simon Kagstrom | 52d6122 | 2009-08-24 09:10:12 +0200 | [diff] [blame] | 163 | # define ffs generic_ffs |
| 164 | #endif |
| 165 | |
Simon Kagstrom | 0413cfe | 2009-09-17 15:15:52 +0200 | [diff] [blame] | 166 | #ifndef PLATFORM_FLS |
Simon Kagstrom | 52d6122 | 2009-08-24 09:10:12 +0200 | [diff] [blame] | 167 | # define fls generic_fls |
| 168 | #endif |
| 169 | |
Fabio Estevam | de4d2e9 | 2015-11-05 12:43:38 -0200 | [diff] [blame] | 170 | static inline unsigned fls_long(unsigned long l) |
| 171 | { |
| 172 | if (sizeof(l) == 4) |
| 173 | return fls(l); |
| 174 | return fls64(l); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * __ffs64 - find first set bit in a 64 bit word |
| 179 | * @word: The 64 bit word |
| 180 | * |
| 181 | * On 64 bit arches this is a synomyn for __ffs |
| 182 | * The result is not defined if no bits are set, so check that @word |
| 183 | * is non-zero before calling this. |
| 184 | */ |
| 185 | static inline unsigned long __ffs64(u64 word) |
| 186 | { |
| 187 | #if BITS_PER_LONG == 32 |
| 188 | if (((u32)word) == 0UL) |
| 189 | return __ffs((u32)(word >> 32)) + 32; |
| 190 | #elif BITS_PER_LONG != 64 |
| 191 | #error BITS_PER_LONG not 32 or 64 |
| 192 | #endif |
| 193 | return __ffs((unsigned long)word); |
| 194 | } |
| 195 | |
Simon Kagstrom | 02f9990 | 2009-08-24 09:09:50 +0200 | [diff] [blame] | 196 | /** |
| 197 | * __set_bit - Set a bit in memory |
| 198 | * @nr: the bit to set |
| 199 | * @addr: the address to start counting from |
| 200 | * |
| 201 | * Unlike set_bit(), this function is non-atomic and may be reordered. |
| 202 | * If it's called on the same region of memory simultaneously, the effect |
| 203 | * may be that only one operation succeeds. |
| 204 | */ |
| 205 | static inline void generic_set_bit(int nr, volatile unsigned long *addr) |
| 206 | { |
| 207 | unsigned long mask = BIT_MASK(nr); |
| 208 | unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr); |
| 209 | |
| 210 | *p |= mask; |
| 211 | } |
| 212 | |
| 213 | static inline void generic_clear_bit(int nr, volatile unsigned long *addr) |
| 214 | { |
| 215 | unsigned long mask = BIT_MASK(nr); |
| 216 | unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr); |
| 217 | |
| 218 | *p &= ~mask; |
| 219 | } |
wdenk | b15cbc0 | 2000-08-21 15:05:47 +0000 | [diff] [blame] | 220 | |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 221 | #endif /* !USE_HOSTCC */ |
| 222 | |
wdenk | b15cbc0 | 2000-08-21 15:05:47 +0000 | [diff] [blame] | 223 | #endif |