7cbd23e56d4884ed72909bee27cdd705ac5901d9
[linux-2.6-microblaze.git] / tools / include / linux / bitmap.h
1 /* SPDX-License-Identifier: GPL-2.0 */
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
13 int __bitmap_weight(const unsigned long *bitmap, int bits);
14 void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
15                  const unsigned long *bitmap2, int bits);
16 int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
17                  const unsigned long *bitmap2, unsigned int bits);
18 int __bitmap_equal(const unsigned long *bitmap1,
19                    const unsigned long *bitmap2, unsigned int bits);
20 void bitmap_clear(unsigned long *map, unsigned int start, int len);
21
22 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
23
24 #define BITMAP_LAST_WORD_MASK(nbits)                                    \
25 (                                                                       \
26         ((nbits) % BITS_PER_LONG) ?                                     \
27                 (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL               \
28 )
29
30 #define small_const_nbits(nbits) \
31         (__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG)
32
33 static inline void bitmap_zero(unsigned long *dst, unsigned int nbits)
34 {
35         if (small_const_nbits(nbits))
36                 *dst = 0UL;
37         else {
38                 int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
39                 memset(dst, 0, len);
40         }
41 }
42
43 static inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
44 {
45         unsigned int nlongs = BITS_TO_LONGS(nbits);
46         if (!small_const_nbits(nbits)) {
47                 unsigned int len = (nlongs - 1) * sizeof(unsigned long);
48                 memset(dst, 0xff,  len);
49         }
50         dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits);
51 }
52
53 static inline int bitmap_empty(const unsigned long *src, unsigned nbits)
54 {
55         if (small_const_nbits(nbits))
56                 return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
57
58         return find_first_bit(src, nbits) == nbits;
59 }
60
61 static inline int bitmap_full(const unsigned long *src, unsigned int nbits)
62 {
63         if (small_const_nbits(nbits))
64                 return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
65
66         return find_first_zero_bit(src, nbits) == nbits;
67 }
68
69 static inline int bitmap_weight(const unsigned long *src, unsigned int nbits)
70 {
71         if (small_const_nbits(nbits))
72                 return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
73         return __bitmap_weight(src, nbits);
74 }
75
76 static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
77                              const unsigned long *src2, unsigned int nbits)
78 {
79         if (small_const_nbits(nbits))
80                 *dst = *src1 | *src2;
81         else
82                 __bitmap_or(dst, src1, src2, nbits);
83 }
84
85 /**
86  * test_and_set_bit - Set a bit and return its old value
87  * @nr: Bit to set
88  * @addr: Address to count from
89  */
90 static inline int test_and_set_bit(int nr, unsigned long *addr)
91 {
92         unsigned long mask = BIT_MASK(nr);
93         unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
94         unsigned long old;
95
96         old = *p;
97         *p = old | mask;
98
99         return (old & mask) != 0;
100 }
101
102 /**
103  * test_and_clear_bit - Clear a bit and return its old value
104  * @nr: Bit to clear
105  * @addr: Address to count from
106  */
107 static inline int test_and_clear_bit(int nr, unsigned long *addr)
108 {
109         unsigned long mask = BIT_MASK(nr);
110         unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
111         unsigned long old;
112
113         old = *p;
114         *p = old & ~mask;
115
116         return (old & mask) != 0;
117 }
118
119 /**
120  * bitmap_alloc - Allocate bitmap
121  * @nbits: Number of bits
122  */
123 static inline unsigned long *bitmap_alloc(int nbits)
124 {
125         return calloc(1, BITS_TO_LONGS(nbits) * sizeof(unsigned long));
126 }
127
128 /*
129  * bitmap_free - Free bitmap
130  * @bitmap: pointer to bitmap
131  */
132 static inline void bitmap_free(unsigned long *bitmap)
133 {
134         free(bitmap);
135 }
136
137 /*
138  * bitmap_scnprintf - print bitmap list into buffer
139  * @bitmap: bitmap
140  * @nbits: size of bitmap
141  * @buf: buffer to store output
142  * @size: size of @buf
143  */
144 size_t bitmap_scnprintf(unsigned long *bitmap, unsigned int nbits,
145                         char *buf, size_t size);
146
147 /**
148  * bitmap_and - Do logical and on bitmaps
149  * @dst: resulting bitmap
150  * @src1: operand 1
151  * @src2: operand 2
152  * @nbits: size of bitmap
153  */
154 static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
155                              const unsigned long *src2, unsigned int nbits)
156 {
157         if (small_const_nbits(nbits))
158                 return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
159         return __bitmap_and(dst, src1, src2, nbits);
160 }
161
162 #ifdef __LITTLE_ENDIAN
163 #define BITMAP_MEM_ALIGNMENT 8
164 #else
165 #define BITMAP_MEM_ALIGNMENT (8 * sizeof(unsigned long))
166 #endif
167 #define BITMAP_MEM_MASK (BITMAP_MEM_ALIGNMENT - 1)
168 #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
169
170 static inline int bitmap_equal(const unsigned long *src1,
171                         const unsigned long *src2, unsigned int nbits)
172 {
173         if (small_const_nbits(nbits))
174                 return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
175         if (__builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
176             IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
177                 return !memcmp(src1, src2, nbits / 8);
178         return __bitmap_equal(src1, src2, nbits);
179 }
180
181 #endif /* _PERF_BITOPS_H */