Commit | Line | Data |
---|---|---|
fb72014d ACM |
1 | #ifndef _PERF_BITOPS_H |
2 | #define _PERF_BITOPS_H | |
3 | ||
4 | #include <string.h> | |
5 | #include <linux/bitops.h> | |
98c03296 | 6 | #include <stdlib.h> |
fb72014d | 7 | |
d944c4ee BP |
8 | #define DECLARE_BITMAP(name,bits) \ |
9 | unsigned long name[BITS_TO_LONGS(bits)] | |
10 | ||
fb72014d | 11 | int __bitmap_weight(const unsigned long *bitmap, int bits); |
850f8127 JO |
12 | void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1, |
13 | const unsigned long *bitmap2, int bits); | |
741c74f5 JO |
14 | int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1, |
15 | const unsigned long *bitmap2, unsigned int bits); | |
fb72014d | 16 | |
64af4e0d ACM |
17 | #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1))) |
18 | ||
fb72014d ACM |
19 | #define BITMAP_LAST_WORD_MASK(nbits) \ |
20 | ( \ | |
21 | ((nbits) % BITS_PER_LONG) ? \ | |
22 | (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \ | |
23 | ) | |
24 | ||
25 | #define small_const_nbits(nbits) \ | |
26 | (__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG) | |
27 | ||
28 | static inline void bitmap_zero(unsigned long *dst, int nbits) | |
29 | { | |
30 | if (small_const_nbits(nbits)) | |
31 | *dst = 0UL; | |
32 | else { | |
33 | int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long); | |
34 | memset(dst, 0, len); | |
35 | } | |
36 | } | |
37 | ||
38 | static inline int bitmap_weight(const unsigned long *src, int nbits) | |
39 | { | |
40 | if (small_const_nbits(nbits)) | |
41 | return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits)); | |
42 | return __bitmap_weight(src, nbits); | |
43 | } | |
44 | ||
850f8127 JO |
45 | static inline void bitmap_or(unsigned long *dst, const unsigned long *src1, |
46 | const unsigned long *src2, int nbits) | |
47 | { | |
48 | if (small_const_nbits(nbits)) | |
49 | *dst = *src1 | *src2; | |
50 | else | |
51 | __bitmap_or(dst, src1, src2, nbits); | |
52 | } | |
53 | ||
416c419c JO |
54 | /** |
55 | * test_and_set_bit - Set a bit and return its old value | |
56 | * @nr: Bit to set | |
57 | * @addr: Address to count from | |
58 | */ | |
59 | static inline int test_and_set_bit(int nr, unsigned long *addr) | |
60 | { | |
61 | unsigned long mask = BIT_MASK(nr); | |
62 | unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr); | |
63 | unsigned long old; | |
64 | ||
65 | old = *p; | |
66 | *p = old | mask; | |
67 | ||
68 | return (old & mask) != 0; | |
69 | } | |
70 | ||
98c03296 JO |
71 | /** |
72 | * bitmap_alloc - Allocate bitmap | |
73 | * @nr: Bit to set | |
74 | */ | |
75 | static inline unsigned long *bitmap_alloc(int nbits) | |
76 | { | |
77 | return calloc(1, BITS_TO_LONGS(nbits) * sizeof(unsigned long)); | |
78 | } | |
79 | ||
820d12b7 JO |
80 | /* |
81 | * bitmap_scnprintf - print bitmap list into buffer | |
82 | * @bitmap: bitmap | |
83 | * @nbits: size of bitmap | |
84 | * @buf: buffer to store output | |
85 | * @size: size of @buf | |
86 | */ | |
87 | size_t bitmap_scnprintf(unsigned long *bitmap, int nbits, | |
88 | char *buf, size_t size); | |
89 | ||
741c74f5 JO |
90 | /** |
91 | * bitmap_and - Do logical and on bitmaps | |
92 | * @dst: resulting bitmap | |
93 | * @src1: operand 1 | |
94 | * @src2: operand 2 | |
95 | * @nbits: size of bitmap | |
96 | */ | |
97 | static inline int bitmap_and(unsigned long *dst, const unsigned long *src1, | |
98 | const unsigned long *src2, unsigned int nbits) | |
99 | { | |
100 | if (small_const_nbits(nbits)) | |
101 | return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0; | |
102 | return __bitmap_and(dst, src1, src2, nbits); | |
103 | } | |
104 | ||
fb72014d | 105 | #endif /* _PERF_BITOPS_H */ |