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