blob: 40bd21488032b0d68998ddfe04fbadac9dc1edf9 [file] [log] [blame]
Arnaldo Carvalho de Melofb720142010-04-30 19:31:12 -03001#ifndef _PERF_BITOPS_H
2#define _PERF_BITOPS_H
3
4#include <string.h>
5#include <linux/bitops.h>
6
Borislav Petkovd944c4e2014-04-25 21:31:02 +02007#define DECLARE_BITMAP(name,bits) \
8 unsigned long name[BITS_TO_LONGS(bits)]
9
Arnaldo Carvalho de Melofb720142010-04-30 19:31:12 -030010int __bitmap_weight(const unsigned long *bitmap, int bits);
Jiri Olsa850f8122012-01-27 15:34:23 +010011void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
12 const unsigned long *bitmap2, int bits);
Arnaldo Carvalho de Melofb720142010-04-30 19:31:12 -030013
14#define BITMAP_LAST_WORD_MASK(nbits) \
15( \
16 ((nbits) % BITS_PER_LONG) ? \
17 (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \
18)
19
20#define small_const_nbits(nbits) \
21 (__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG)
22
23static inline void bitmap_zero(unsigned long *dst, int nbits)
24{
25 if (small_const_nbits(nbits))
26 *dst = 0UL;
27 else {
28 int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
29 memset(dst, 0, len);
30 }
31}
32
33static inline int bitmap_weight(const unsigned long *src, int nbits)
34{
35 if (small_const_nbits(nbits))
36 return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
37 return __bitmap_weight(src, nbits);
38}
39
Jiri Olsa850f8122012-01-27 15:34:23 +010040static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
41 const unsigned long *src2, int nbits)
42{
43 if (small_const_nbits(nbits))
44 *dst = *src1 | *src2;
45 else
46 __bitmap_or(dst, src1, src2, nbits);
47}
48
Jiri Olsa416c4192014-10-26 23:44:03 +010049/**
50 * test_and_set_bit - Set a bit and return its old value
51 * @nr: Bit to set
52 * @addr: Address to count from
53 */
54static inline int test_and_set_bit(int nr, unsigned long *addr)
55{
56 unsigned long mask = BIT_MASK(nr);
57 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
58 unsigned long old;
59
60 old = *p;
61 *p = old | mask;
62
63 return (old & mask) != 0;
64}
65
Arnaldo Carvalho de Melofb720142010-04-30 19:31:12 -030066#endif /* _PERF_BITOPS_H */