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 | |
Jagan Teki | 79d4d5f | 2015-10-21 17:30:34 +0530 | [diff] [blame] | 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) |
Heiko Schocher | 69bab55 | 2015-09-07 13:43:52 +0200 | [diff] [blame] | 11 | |
wdenk | b15cbc0 | 2000-08-21 15:05:47 +0000 | [diff] [blame] | 12 | /* |
Jagan Teki | 8aceb8f | 2015-10-21 16:46:51 +0530 | [diff] [blame] | 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 | /* |
wdenk | b15cbc0 | 2000-08-21 15:05:47 +0000 | [diff] [blame] | 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 | |
Simon Kagstrom | 2258cec | 2009-08-24 09:10:12 +0200 | [diff] [blame] | 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 | |
wdenk | b15cbc0 | 2000-08-21 15:05:47 +0000 | [diff] [blame] | 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 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 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); |
wdenk | b15cbc0 | 2000-08-21 15:05:47 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static inline unsigned int generic_hweight16(unsigned int w) |
| 110 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 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); |
wdenk | b15cbc0 | 2000-08-21 15:05:47 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static inline unsigned int generic_hweight8(unsigned int w) |
| 118 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 119 | unsigned int res = (w & 0x55) + ((w >> 1) & 0x55); |
| 120 | res = (res & 0x33) + ((res >> 2) & 0x33); |
| 121 | return (res & 0x0F) + ((res >> 4) & 0x0F); |
wdenk | b15cbc0 | 2000-08-21 15:05:47 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | #include <asm/bitops.h> |
| 125 | |
Simon Kagstrom | 13edaf3 | 2009-08-24 09:09:50 +0200 | [diff] [blame] | 126 | /* linux/include/asm-generic/bitops/non-atomic.h */ |
| 127 | |
Simon Kagstrom | 95a3c73 | 2009-09-17 15:15:52 +0200 | [diff] [blame] | 128 | #ifndef PLATFORM__SET_BIT |
Simon Kagstrom | 13edaf3 | 2009-08-24 09:09:50 +0200 | [diff] [blame] | 129 | # define __set_bit generic_set_bit |
| 130 | #endif |
| 131 | |
Simon Kagstrom | 95a3c73 | 2009-09-17 15:15:52 +0200 | [diff] [blame] | 132 | #ifndef PLATFORM__CLEAR_BIT |
Simon Kagstrom | 13edaf3 | 2009-08-24 09:09:50 +0200 | [diff] [blame] | 133 | # define __clear_bit generic_clear_bit |
| 134 | #endif |
| 135 | |
Simon Kagstrom | 95a3c73 | 2009-09-17 15:15:52 +0200 | [diff] [blame] | 136 | #ifndef PLATFORM_FFS |
Simon Kagstrom | 2258cec | 2009-08-24 09:10:12 +0200 | [diff] [blame] | 137 | # define ffs generic_ffs |
| 138 | #endif |
| 139 | |
Simon Kagstrom | 95a3c73 | 2009-09-17 15:15:52 +0200 | [diff] [blame] | 140 | #ifndef PLATFORM_FLS |
Simon Kagstrom | 2258cec | 2009-08-24 09:10:12 +0200 | [diff] [blame] | 141 | # define fls generic_fls |
| 142 | #endif |
| 143 | |
Fabio Estevam | 5e39d08 | 2015-11-05 12:43:38 -0200 | [diff] [blame] | 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 | |
Simon Kagstrom | 13edaf3 | 2009-08-24 09:09:50 +0200 | [diff] [blame] | 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 | } |
wdenk | b15cbc0 | 2000-08-21 15:05:47 +0000 | [diff] [blame] | 194 | |
| 195 | #endif |