Arnaldo Carvalho de Melo | fb72014 | 2010-04-30 19:31:12 -0300 | [diff] [blame] | 1 | #ifndef _PERF_BITOPS_H |
| 2 | #define _PERF_BITOPS_H |
| 3 | |
| 4 | #include <string.h> |
| 5 | #include <linux/bitops.h> |
| 6 | |
Borislav Petkov | d944c4e | 2014-04-25 21:31:02 +0200 | [diff] [blame] | 7 | #define DECLARE_BITMAP(name,bits) \ |
| 8 | unsigned long name[BITS_TO_LONGS(bits)] |
| 9 | |
Arnaldo Carvalho de Melo | fb72014 | 2010-04-30 19:31:12 -0300 | [diff] [blame] | 10 | int __bitmap_weight(const unsigned long *bitmap, int bits); |
Jiri Olsa | 850f812 | 2012-01-27 15:34:23 +0100 | [diff] [blame] | 11 | void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1, |
| 12 | const unsigned long *bitmap2, int bits); |
Arnaldo Carvalho de Melo | fb72014 | 2010-04-30 19:31:12 -0300 | [diff] [blame] | 13 | |
Arnaldo Carvalho de Melo | 64af4e0 | 2016-01-08 11:26:43 -0300 | [diff] [blame] | 14 | #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1))) |
| 15 | |
Arnaldo Carvalho de Melo | fb72014 | 2010-04-30 19:31:12 -0300 | [diff] [blame] | 16 | #define BITMAP_LAST_WORD_MASK(nbits) \ |
| 17 | ( \ |
| 18 | ((nbits) % BITS_PER_LONG) ? \ |
| 19 | (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \ |
| 20 | ) |
| 21 | |
| 22 | #define small_const_nbits(nbits) \ |
| 23 | (__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG) |
| 24 | |
| 25 | static inline void bitmap_zero(unsigned long *dst, int nbits) |
| 26 | { |
| 27 | if (small_const_nbits(nbits)) |
| 28 | *dst = 0UL; |
| 29 | else { |
| 30 | int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long); |
| 31 | memset(dst, 0, len); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | static inline int bitmap_weight(const unsigned long *src, int nbits) |
| 36 | { |
| 37 | if (small_const_nbits(nbits)) |
| 38 | return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits)); |
| 39 | return __bitmap_weight(src, nbits); |
| 40 | } |
| 41 | |
Jiri Olsa | 850f812 | 2012-01-27 15:34:23 +0100 | [diff] [blame] | 42 | static inline void bitmap_or(unsigned long *dst, const unsigned long *src1, |
| 43 | const unsigned long *src2, int nbits) |
| 44 | { |
| 45 | if (small_const_nbits(nbits)) |
| 46 | *dst = *src1 | *src2; |
| 47 | else |
| 48 | __bitmap_or(dst, src1, src2, nbits); |
| 49 | } |
| 50 | |
Jiri Olsa | 416c419 | 2014-10-26 23:44:03 +0100 | [diff] [blame] | 51 | /** |
| 52 | * test_and_set_bit - Set a bit and return its old value |
| 53 | * @nr: Bit to set |
| 54 | * @addr: Address to count from |
| 55 | */ |
| 56 | static inline int test_and_set_bit(int nr, unsigned long *addr) |
| 57 | { |
| 58 | unsigned long mask = BIT_MASK(nr); |
| 59 | unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr); |
| 60 | unsigned long old; |
| 61 | |
| 62 | old = *p; |
| 63 | *p = old | mask; |
| 64 | |
| 65 | return (old & mask) != 0; |
| 66 | } |
| 67 | |
Arnaldo Carvalho de Melo | fb72014 | 2010-04-30 19:31:12 -0300 | [diff] [blame] | 68 | #endif /* _PERF_BITOPS_H */ |