1 #ifndef _PERF_BITOPS_H 2 #define _PERF_BITOPS_H 3 4 #include <string.h> 5 #include <linux/bitops.h> 6 #include <stdlib.h> 7 #include <linux/kernel.h> 8 9 #define DECLARE_BITMAP(name,bits) \ 10 unsigned long name[BITS_TO_LONGS(bits)] 11 12 int __bitmap_weight(const unsigned long *bitmap, int bits); 13 void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1, 14 const unsigned long *bitmap2, int bits); 15 int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1, 16 const unsigned long *bitmap2, unsigned int bits); 17 18 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1))) 19 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 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 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 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 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 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 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 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 132 #endif /* _PERF_BITOPS_H */ 133