1
2#ifndef _PERF_BITOPS_H
3#define _PERF_BITOPS_H
4
5#include <string.h>
6#include <linux/bitops.h>
7#include <stdlib.h>
8#include <linux/kernel.h>
9
10#define DECLARE_BITMAP(name,bits) \
11 unsigned long name[BITS_TO_LONGS(bits)]
12
13int __bitmap_weight(const unsigned long *bitmap, int bits);
14void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
15 const unsigned long *bitmap2, int bits);
16int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
17 const unsigned long *bitmap2, unsigned int bits);
18void bitmap_clear(unsigned long *map, unsigned int start, int len);
19
20#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
21
22#define BITMAP_LAST_WORD_MASK(nbits) \
23( \
24 ((nbits) % BITS_PER_LONG) ? \
25 (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \
26)
27
28#define small_const_nbits(nbits) \
29 (__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG)
30
31static inline void bitmap_zero(unsigned long *dst, int nbits)
32{
33 if (small_const_nbits(nbits))
34 *dst = 0UL;
35 else {
36 int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
37 memset(dst, 0, len);
38 }
39}
40
41static inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
42{
43 unsigned int nlongs = BITS_TO_LONGS(nbits);
44 if (!small_const_nbits(nbits)) {
45 unsigned int len = (nlongs - 1) * sizeof(unsigned long);
46 memset(dst, 0xff, len);
47 }
48 dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits);
49}
50
51static inline int bitmap_empty(const unsigned long *src, unsigned nbits)
52{
53 if (small_const_nbits(nbits))
54 return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
55
56 return find_first_bit(src, nbits) == nbits;
57}
58
59static inline int bitmap_full(const unsigned long *src, unsigned int nbits)
60{
61 if (small_const_nbits(nbits))
62 return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
63
64 return find_first_zero_bit(src, nbits) == nbits;
65}
66
67static inline int bitmap_weight(const unsigned long *src, int nbits)
68{
69 if (small_const_nbits(nbits))
70 return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
71 return __bitmap_weight(src, nbits);
72}
73
74static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
75 const unsigned long *src2, int nbits)
76{
77 if (small_const_nbits(nbits))
78 *dst = *src1 | *src2;
79 else
80 __bitmap_or(dst, src1, src2, nbits);
81}
82
83
84
85
86
87
88static inline int test_and_set_bit(int nr, unsigned long *addr)
89{
90 unsigned long mask = BIT_MASK(nr);
91 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
92 unsigned long old;
93
94 old = *p;
95 *p = old | mask;
96
97 return (old & mask) != 0;
98}
99
100
101
102
103
104
105static inline int test_and_clear_bit(int nr, unsigned long *addr)
106{
107 unsigned long mask = BIT_MASK(nr);
108 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
109 unsigned long old;
110
111 old = *p;
112 *p = old & ~mask;
113
114 return (old & mask) != 0;
115}
116
117
118
119
120
121static inline unsigned long *bitmap_alloc(int nbits)
122{
123 return calloc(1, BITS_TO_LONGS(nbits) * sizeof(unsigned long));
124}
125
126
127
128
129
130
131
132
133size_t bitmap_scnprintf(unsigned long *bitmap, int nbits,
134 char *buf, size_t size);
135
136
137
138
139
140
141
142
143static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
144 const unsigned long *src2, unsigned int nbits)
145{
146 if (small_const_nbits(nbits))
147 return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
148 return __bitmap_and(dst, src1, src2, nbits);
149}
150
151#endif
152