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