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> |
Matthew Wilcox | c68a2aa | 2016-12-16 11:52:43 -0500 | [diff] [blame] | 7 | #include <linux/kernel.h> |
Arnaldo Carvalho de Melo | fb72014 | 2010-04-30 19:31:12 -0300 | [diff] [blame] | 8 | |
Borislav Petkov | d944c4e | 2014-04-25 21:31:02 +0200 | [diff] [blame] | 9 | #define DECLARE_BITMAP(name,bits) \ |
| 10 | unsigned long name[BITS_TO_LONGS(bits)] |
| 11 | |
Arnaldo Carvalho de Melo | fb72014 | 2010-04-30 19:31:12 -0300 | [diff] [blame] | 12 | int __bitmap_weight(const unsigned long *bitmap, int bits); |
Jiri Olsa | 850f812 | 2012-01-27 15:34:23 +0100 | [diff] [blame] | 13 | void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1, |
| 14 | const unsigned long *bitmap2, int bits); |
Jiri Olsa | 741c74f | 2016-08-01 20:02:31 +0200 | [diff] [blame] | 15 | int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1, |
| 16 | const unsigned long *bitmap2, unsigned int bits); |
Arnaldo Carvalho de Melo | fb72014 | 2010-04-30 19:31:12 -0300 | [diff] [blame] | 17 | |
Arnaldo Carvalho de Melo | 64af4e0 | 2016-01-08 11:26:43 -0300 | [diff] [blame] | 18 | #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1))) |
| 19 | |
Arnaldo Carvalho de Melo | fb72014 | 2010-04-30 19:31:12 -0300 | [diff] [blame] | 20 | #define BITMAP_LAST_WORD_MASK(nbits) \ |
| 21 | ( \ |
| 22 | ((nbits) % BITS_PER_LONG) ? \ |
| 23 | (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \ |
| 24 | ) |
| 25 | |
| 26 | #define small_const_nbits(nbits) \ |
| 27 | (__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG) |
| 28 | |
| 29 | static inline void bitmap_zero(unsigned long *dst, int nbits) |
| 30 | { |
| 31 | if (small_const_nbits(nbits)) |
| 32 | *dst = 0UL; |
| 33 | else { |
| 34 | int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long); |
| 35 | memset(dst, 0, len); |
| 36 | } |
| 37 | } |
| 38 | |
Matthew Wilcox | b328daf | 2016-12-14 15:08:26 -0800 | [diff] [blame] | 39 | static inline void bitmap_fill(unsigned long *dst, unsigned int nbits) |
| 40 | { |
| 41 | unsigned int nlongs = BITS_TO_LONGS(nbits); |
| 42 | if (!small_const_nbits(nbits)) { |
| 43 | unsigned int len = (nlongs - 1) * sizeof(unsigned long); |
| 44 | memset(dst, 0xff, len); |
| 45 | } |
| 46 | dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits); |
| 47 | } |
| 48 | |
| 49 | static inline int bitmap_empty(const unsigned long *src, unsigned nbits) |
| 50 | { |
| 51 | if (small_const_nbits(nbits)) |
| 52 | return ! (*src & BITMAP_LAST_WORD_MASK(nbits)); |
| 53 | |
| 54 | return find_first_bit(src, nbits) == nbits; |
| 55 | } |
| 56 | |
| 57 | static inline int bitmap_full(const unsigned long *src, unsigned int nbits) |
| 58 | { |
| 59 | if (small_const_nbits(nbits)) |
| 60 | return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits)); |
| 61 | |
| 62 | return find_first_zero_bit(src, nbits) == nbits; |
| 63 | } |
| 64 | |
Arnaldo Carvalho de Melo | fb72014 | 2010-04-30 19:31:12 -0300 | [diff] [blame] | 65 | static inline int bitmap_weight(const unsigned long *src, int nbits) |
| 66 | { |
| 67 | if (small_const_nbits(nbits)) |
| 68 | return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits)); |
| 69 | return __bitmap_weight(src, nbits); |
| 70 | } |
| 71 | |
Jiri Olsa | 850f812 | 2012-01-27 15:34:23 +0100 | [diff] [blame] | 72 | static inline void bitmap_or(unsigned long *dst, const unsigned long *src1, |
| 73 | const unsigned long *src2, int nbits) |
| 74 | { |
| 75 | if (small_const_nbits(nbits)) |
| 76 | *dst = *src1 | *src2; |
| 77 | else |
| 78 | __bitmap_or(dst, src1, src2, nbits); |
| 79 | } |
| 80 | |
Jiri Olsa | 416c419 | 2014-10-26 23:44:03 +0100 | [diff] [blame] | 81 | /** |
| 82 | * test_and_set_bit - Set a bit and return its old value |
| 83 | * @nr: Bit to set |
| 84 | * @addr: Address to count from |
| 85 | */ |
| 86 | static inline int test_and_set_bit(int nr, unsigned long *addr) |
| 87 | { |
| 88 | unsigned long mask = BIT_MASK(nr); |
| 89 | unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr); |
| 90 | unsigned long old; |
| 91 | |
| 92 | old = *p; |
| 93 | *p = old | mask; |
| 94 | |
| 95 | return (old & mask) != 0; |
| 96 | } |
| 97 | |
Jiri Olsa | 98c0329 | 2016-08-02 13:33:02 +0200 | [diff] [blame] | 98 | /** |
| 99 | * bitmap_alloc - Allocate bitmap |
| 100 | * @nr: Bit to set |
| 101 | */ |
| 102 | static inline unsigned long *bitmap_alloc(int nbits) |
| 103 | { |
| 104 | return calloc(1, BITS_TO_LONGS(nbits) * sizeof(unsigned long)); |
| 105 | } |
| 106 | |
Jiri Olsa | 820d12b | 2016-08-01 20:02:30 +0200 | [diff] [blame] | 107 | /* |
| 108 | * bitmap_scnprintf - print bitmap list into buffer |
| 109 | * @bitmap: bitmap |
| 110 | * @nbits: size of bitmap |
| 111 | * @buf: buffer to store output |
| 112 | * @size: size of @buf |
| 113 | */ |
| 114 | size_t bitmap_scnprintf(unsigned long *bitmap, int nbits, |
| 115 | char *buf, size_t size); |
| 116 | |
Jiri Olsa | 741c74f | 2016-08-01 20:02:31 +0200 | [diff] [blame] | 117 | /** |
| 118 | * bitmap_and - Do logical and on bitmaps |
| 119 | * @dst: resulting bitmap |
| 120 | * @src1: operand 1 |
| 121 | * @src2: operand 2 |
| 122 | * @nbits: size of bitmap |
| 123 | */ |
| 124 | static inline int bitmap_and(unsigned long *dst, const unsigned long *src1, |
| 125 | const unsigned long *src2, unsigned int nbits) |
| 126 | { |
| 127 | if (small_const_nbits(nbits)) |
| 128 | return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0; |
| 129 | return __bitmap_and(dst, src1, src2, nbits); |
| 130 | } |
| 131 | |
Arnaldo Carvalho de Melo | fb72014 | 2010-04-30 19:31:12 -0300 | [diff] [blame] | 132 | #endif /* _PERF_BITOPS_H */ |