5 #include <linux/bitops.h>
8 #define DECLARE_BITMAP(name,bits) \
9 unsigned long name[BITS_TO_LONGS(bits)]
11 void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
12 const unsigned long *bitmap2, int bits);
14 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
16 #define BITMAP_LAST_WORD_MASK(nbits) \
18 ((nbits) % BITS_PER_LONG) ? \
19 (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \
22 #define small_const_nbits(nbits) \
23 (__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG)
25 static inline int __bitmap_weight(const unsigned long *bitmap, int bits)
27 int k, w = 0, lim = bits/BITS_PER_LONG;
29 for (k = 0; k < lim; k++)
30 w += hweight_long(bitmap[k]);
32 if (bits % BITS_PER_LONG)
33 w += hweight_long(bitmap[k] & BITMAP_LAST_WORD_MASK(bits));
38 static inline int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
39 const unsigned long *bitmap2, unsigned int bits)
42 unsigned int lim = bits/BITS_PER_LONG;
43 unsigned long result = 0;
45 for (k = 0; k < lim; k++)
46 result |= (dst[k] = bitmap1[k] & bitmap2[k]);
47 if (bits % BITS_PER_LONG)
48 result |= (dst[k] = bitmap1[k] & bitmap2[k] &
49 BITMAP_LAST_WORD_MASK(bits));
53 static inline void bitmap_complement(unsigned long *dst, const unsigned long *src,
56 unsigned int k, lim = bits/BITS_PER_LONG;
57 for (k = 0; k < lim; ++k)
60 if (bits % BITS_PER_LONG)
64 static inline void bitmap_zero(unsigned long *dst, int nbits)
66 memset(dst, 0, BITS_TO_LONGS(nbits) * sizeof(unsigned long));
69 static inline int bitmap_weight(const unsigned long *src, int nbits)
71 if (small_const_nbits(nbits))
72 return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
73 return __bitmap_weight(src, nbits);
76 static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
77 const unsigned long *src2, int nbits)
79 if (small_const_nbits(nbits))
82 __bitmap_or(dst, src1, src2, nbits);
85 static inline unsigned long *bitmap_alloc(int nbits)
87 return calloc(1, BITS_TO_LONGS(nbits) * sizeof(unsigned long));
90 static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
91 const unsigned long *src2, unsigned int nbits)
93 if (small_const_nbits(nbits))
94 return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
95 return __bitmap_and(dst, src1, src2, nbits);
98 static inline unsigned long _find_next_bit(const unsigned long *addr,
99 unsigned long nbits, unsigned long start, unsigned long invert)
103 if (!nbits || start >= nbits)
106 tmp = addr[start / BITS_PER_LONG] ^ invert;
108 /* Handle 1st word. */
109 tmp &= BITMAP_FIRST_WORD_MASK(start);
110 start = round_down(start, BITS_PER_LONG);
113 start += BITS_PER_LONG;
117 tmp = addr[start / BITS_PER_LONG] ^ invert;
120 return min(start + __ffs(tmp), nbits);
123 static inline unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
124 unsigned long offset)
126 return _find_next_bit(addr, size, offset, 0UL);
129 static inline unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
130 unsigned long offset)
132 return _find_next_bit(addr, size, offset, ~0UL);
135 #define find_first_bit(addr, size) find_next_bit((addr), (size), 0)
136 #define find_first_zero_bit(addr, size) find_next_zero_bit((addr), (size), 0)
138 #endif /* _PERF_BITOPS_H */