5 #include <linux/bitops.h>
8 #define DECLARE_BITMAP(name,bits) \
9 unsigned long name[BITS_TO_LONGS(bits)]
11 int __bitmap_weight(const unsigned long *bitmap, int bits);
12 void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
13 const unsigned long *bitmap2, int bits);
14 int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
15 const unsigned long *bitmap2, unsigned int bits);
17 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
19 #define BITMAP_LAST_WORD_MASK(nbits) \
21 ((nbits) % BITS_PER_LONG) ? \
22 (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \
25 #define small_const_nbits(nbits) \
26 (__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG)
28 static inline void bitmap_zero(unsigned long *dst, int nbits)
30 if (small_const_nbits(nbits))
33 int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
38 static inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
40 unsigned int nlongs = BITS_TO_LONGS(nbits);
41 if (!small_const_nbits(nbits)) {
42 unsigned int len = (nlongs - 1) * sizeof(unsigned long);
43 memset(dst, 0xff, len);
45 dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits);
48 static inline int bitmap_empty(const unsigned long *src, unsigned nbits)
50 if (small_const_nbits(nbits))
51 return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
53 return find_first_bit(src, nbits) == nbits;
56 static inline int bitmap_full(const unsigned long *src, unsigned int nbits)
58 if (small_const_nbits(nbits))
59 return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
61 return find_first_zero_bit(src, nbits) == nbits;
64 static inline int bitmap_weight(const unsigned long *src, int nbits)
66 if (small_const_nbits(nbits))
67 return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
68 return __bitmap_weight(src, nbits);
71 static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
72 const unsigned long *src2, int nbits)
74 if (small_const_nbits(nbits))
77 __bitmap_or(dst, src1, src2, nbits);
81 * test_and_set_bit - Set a bit and return its old value
83 * @addr: Address to count from
85 static inline int test_and_set_bit(int nr, unsigned long *addr)
87 unsigned long mask = BIT_MASK(nr);
88 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
94 return (old & mask) != 0;
98 * bitmap_alloc - Allocate bitmap
101 static inline unsigned long *bitmap_alloc(int nbits)
103 return calloc(1, BITS_TO_LONGS(nbits) * sizeof(unsigned long));
107 * bitmap_scnprintf - print bitmap list into buffer
109 * @nbits: size of bitmap
110 * @buf: buffer to store output
111 * @size: size of @buf
113 size_t bitmap_scnprintf(unsigned long *bitmap, int nbits,
114 char *buf, size_t size);
117 * bitmap_and - Do logical and on bitmaps
118 * @dst: resulting bitmap
121 * @nbits: size of bitmap
123 static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
124 const unsigned long *src2, unsigned int nbits)
126 if (small_const_nbits(nbits))
127 return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
128 return __bitmap_and(dst, src1, src2, nbits);
131 #endif /* _PERF_BITOPS_H */