1 /* SPDX-License-Identifier: GPL-2.0 */
6 #include <linux/bitops.h>
8 #include <linux/kernel.h>
10 #define DECLARE_BITMAP(name,bits) \
11 unsigned long name[BITS_TO_LONGS(bits)]
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);
22 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
24 #define BITMAP_LAST_WORD_MASK(nbits) \
26 ((nbits) % BITS_PER_LONG) ? \
27 (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \
30 #define small_const_nbits(nbits) \
31 (__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG)
33 static inline void bitmap_zero(unsigned long *dst, int nbits)
35 if (small_const_nbits(nbits))
38 int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
43 static inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
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);
50 dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits);
53 static inline int bitmap_empty(const unsigned long *src, unsigned nbits)
55 if (small_const_nbits(nbits))
56 return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
58 return find_first_bit(src, nbits) == nbits;
61 static inline int bitmap_full(const unsigned long *src, unsigned int nbits)
63 if (small_const_nbits(nbits))
64 return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
66 return find_first_zero_bit(src, nbits) == nbits;
69 static inline int bitmap_weight(const unsigned long *src, int nbits)
71 if (small_const_nbits(nbits))
72 return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
73 return __bitmap_weight(src, nbits);
76 static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
77 const unsigned long *src2, int nbits)
79 if (small_const_nbits(nbits))
82 __bitmap_or(dst, src1, src2, nbits);
86 * test_and_set_bit - Set a bit and return its old value
88 * @addr: Address to count from
90 static inline int test_and_set_bit(int nr, unsigned long *addr)
92 unsigned long mask = BIT_MASK(nr);
93 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
99 return (old & mask) != 0;
103 * test_and_clear_bit - Clear a bit and return its old value
105 * @addr: Address to count from
107 static inline int test_and_clear_bit(int nr, unsigned long *addr)
109 unsigned long mask = BIT_MASK(nr);
110 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
116 return (old & mask) != 0;
120 * bitmap_alloc - Allocate bitmap
121 * @nbits: Number of bits
123 static inline unsigned long *bitmap_alloc(int nbits)
125 return calloc(1, BITS_TO_LONGS(nbits) * sizeof(unsigned long));
129 * bitmap_free - Free bitmap
130 * @bitmap: pointer to bitmap
132 static inline void bitmap_free(unsigned long *bitmap)
138 * bitmap_scnprintf - print bitmap list into buffer
140 * @nbits: size of bitmap
141 * @buf: buffer to store output
142 * @size: size of @buf
144 size_t bitmap_scnprintf(unsigned long *bitmap, int nbits,
145 char *buf, size_t size);
148 * bitmap_and - Do logical and on bitmaps
149 * @dst: resulting bitmap
152 * @nbits: size of bitmap
154 static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
155 const unsigned long *src2, unsigned int nbits)
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);
162 #ifdef __LITTLE_ENDIAN
163 #define BITMAP_MEM_ALIGNMENT 8
165 #define BITMAP_MEM_ALIGNMENT (8 * sizeof(unsigned long))
167 #define BITMAP_MEM_MASK (BITMAP_MEM_ALIGNMENT - 1)
168 #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
170 static inline int bitmap_equal(const unsigned long *src1,
171 const unsigned long *src2, unsigned int nbits)
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);
181 #endif /* _PERF_BITOPS_H */