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