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