| .. | .. |
|---|
| 4 | 4 | #include <asm/types.h> |
|---|
| 5 | 5 | #include <linux/bits.h> |
|---|
| 6 | 6 | |
|---|
| 7 | | -#define BITS_PER_TYPE(type) (sizeof(type) * BITS_PER_BYTE) |
|---|
| 7 | +/* Set bits in the first 'n' bytes when loaded from memory */ |
|---|
| 8 | +#ifdef __LITTLE_ENDIAN |
|---|
| 9 | +# define aligned_byte_mask(n) ((1UL << 8*(n))-1) |
|---|
| 10 | +#else |
|---|
| 11 | +# define aligned_byte_mask(n) (~0xffUL << (BITS_PER_LONG - 8 - 8*(n))) |
|---|
| 12 | +#endif |
|---|
| 13 | + |
|---|
| 14 | +#define BITS_PER_TYPE(type) (sizeof(type) * BITS_PER_BYTE) |
|---|
| 8 | 15 | #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_TYPE(long)) |
|---|
| 16 | +#define BITS_TO_U64(nr) DIV_ROUND_UP(nr, BITS_PER_TYPE(u64)) |
|---|
| 17 | +#define BITS_TO_U32(nr) DIV_ROUND_UP(nr, BITS_PER_TYPE(u32)) |
|---|
| 18 | +#define BITS_TO_BYTES(nr) DIV_ROUND_UP(nr, BITS_PER_TYPE(char)) |
|---|
| 9 | 19 | |
|---|
| 10 | 20 | extern unsigned int __sw_hweight8(unsigned int w); |
|---|
| 11 | 21 | extern unsigned int __sw_hweight16(unsigned int w); |
|---|
| .. | .. |
|---|
| 39 | 49 | for ((bit) = find_next_zero_bit((addr), (size), (bit)); \ |
|---|
| 40 | 50 | (bit) < (size); \ |
|---|
| 41 | 51 | (bit) = find_next_zero_bit((addr), (size), (bit) + 1)) |
|---|
| 52 | + |
|---|
| 53 | +/** |
|---|
| 54 | + * for_each_set_clump8 - iterate over bitmap for each 8-bit clump with set bits |
|---|
| 55 | + * @start: bit offset to start search and to store the current iteration offset |
|---|
| 56 | + * @clump: location to store copy of current 8-bit clump |
|---|
| 57 | + * @bits: bitmap address to base the search on |
|---|
| 58 | + * @size: bitmap size in number of bits |
|---|
| 59 | + */ |
|---|
| 60 | +#define for_each_set_clump8(start, clump, bits, size) \ |
|---|
| 61 | + for ((start) = find_first_clump8(&(clump), (bits), (size)); \ |
|---|
| 62 | + (start) < (size); \ |
|---|
| 63 | + (start) = find_next_clump8(&(clump), (bits), (size), (start) + 8)) |
|---|
| 42 | 64 | |
|---|
| 43 | 65 | static inline int get_bitmask_order(unsigned int count) |
|---|
| 44 | 66 | { |
|---|
| .. | .. |
|---|
| 140 | 162 | * |
|---|
| 141 | 163 | * This is safe to use for 16- and 8-bit types as well. |
|---|
| 142 | 164 | */ |
|---|
| 143 | | -static inline __s32 sign_extend32(__u32 value, int index) |
|---|
| 165 | +static __always_inline __s32 sign_extend32(__u32 value, int index) |
|---|
| 144 | 166 | { |
|---|
| 145 | 167 | __u8 shift = 31 - index; |
|---|
| 146 | 168 | return (__s32)(value << shift) >> shift; |
|---|
| .. | .. |
|---|
| 151 | 173 | * @value: value to sign extend |
|---|
| 152 | 174 | * @index: 0 based bit index (0<=index<64) to sign bit |
|---|
| 153 | 175 | */ |
|---|
| 154 | | -static inline __s64 sign_extend64(__u64 value, int index) |
|---|
| 176 | +static __always_inline __s64 sign_extend64(__u64 value, int index) |
|---|
| 155 | 177 | { |
|---|
| 156 | 178 | __u8 shift = 63 - index; |
|---|
| 157 | 179 | return (__s64)(value << shift) >> shift; |
|---|
| .. | .. |
|---|
| 166 | 188 | |
|---|
| 167 | 189 | static inline int get_count_order(unsigned int count) |
|---|
| 168 | 190 | { |
|---|
| 169 | | - int order; |
|---|
| 191 | + if (count == 0) |
|---|
| 192 | + return -1; |
|---|
| 170 | 193 | |
|---|
| 171 | | - order = fls(count) - 1; |
|---|
| 172 | | - if (count & (count - 1)) |
|---|
| 173 | | - order++; |
|---|
| 174 | | - return order; |
|---|
| 194 | + return fls(--count); |
|---|
| 175 | 195 | } |
|---|
| 176 | 196 | |
|---|
| 177 | 197 | /** |
|---|
| .. | .. |
|---|
| 184 | 204 | { |
|---|
| 185 | 205 | if (l == 0UL) |
|---|
| 186 | 206 | return -1; |
|---|
| 187 | | - else if (l & (l - 1UL)) |
|---|
| 188 | | - return (int)fls_long(l); |
|---|
| 189 | | - else |
|---|
| 190 | | - return (int)fls_long(l) - 1; |
|---|
| 207 | + return (int)fls_long(--l); |
|---|
| 191 | 208 | } |
|---|
| 192 | 209 | |
|---|
| 193 | 210 | /** |
|---|
| .. | .. |
|---|
| 246 | 263 | new__ = (old__ & ~mask__) | bits__; \ |
|---|
| 247 | 264 | } while (cmpxchg(ptr, old__, new__) != old__); \ |
|---|
| 248 | 265 | \ |
|---|
| 249 | | - new__; \ |
|---|
| 266 | + old__; \ |
|---|
| 250 | 267 | }) |
|---|
| 251 | 268 | #endif |
|---|
| 252 | 269 | |
|---|
| 253 | 270 | #ifndef bit_clear_unless |
|---|
| 254 | | -#define bit_clear_unless(ptr, _clear, _test) \ |
|---|
| 271 | +#define bit_clear_unless(ptr, clear, test) \ |
|---|
| 255 | 272 | ({ \ |
|---|
| 256 | | - const typeof(*ptr) clear = (_clear), test = (_test); \ |
|---|
| 257 | | - typeof(*ptr) old, new; \ |
|---|
| 273 | + const typeof(*(ptr)) clear__ = (clear), test__ = (test);\ |
|---|
| 274 | + typeof(*(ptr)) old__, new__; \ |
|---|
| 258 | 275 | \ |
|---|
| 259 | 276 | do { \ |
|---|
| 260 | | - old = READ_ONCE(*ptr); \ |
|---|
| 261 | | - new = old & ~clear; \ |
|---|
| 262 | | - } while (!(old & test) && \ |
|---|
| 263 | | - cmpxchg(ptr, old, new) != old); \ |
|---|
| 277 | + old__ = READ_ONCE(*(ptr)); \ |
|---|
| 278 | + new__ = old__ & ~clear__; \ |
|---|
| 279 | + } while (!(old__ & test__) && \ |
|---|
| 280 | + cmpxchg(ptr, old__, new__) != old__); \ |
|---|
| 264 | 281 | \ |
|---|
| 265 | | - !(old & test); \ |
|---|
| 282 | + !(old__ & test__); \ |
|---|
| 266 | 283 | }) |
|---|
| 267 | 284 | #endif |
|---|
| 268 | 285 | |
|---|