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)))
23 #define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
25 static inline void bitmap_zero(unsigned long *dst, unsigned int nbits)
27 if (small_const_nbits(nbits))
30 int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
35 static inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
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);
42 dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits);
45 static inline int bitmap_empty(const unsigned long *src, unsigned nbits)
47 if (small_const_nbits(nbits))
48 return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
50 return find_first_bit(src, nbits) == nbits;
53 static inline int bitmap_full(const unsigned long *src, unsigned int nbits)
55 if (small_const_nbits(nbits))
56 return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
58 return find_first_zero_bit(src, nbits) == nbits;
61 static inline int bitmap_weight(const unsigned long *src, unsigned int nbits)
63 if (small_const_nbits(nbits))
64 return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
65 return __bitmap_weight(src, nbits);
68 static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
69 const unsigned long *src2, unsigned int nbits)
71 if (small_const_nbits(nbits))
74 __bitmap_or(dst, src1, src2, nbits);
78 * test_and_set_bit - Set a bit and return its old value
80 * @addr: Address to count from
82 static inline int test_and_set_bit(int nr, unsigned long *addr)
84 unsigned long mask = BIT_MASK(nr);
85 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
91 return (old & mask) != 0;
95 * test_and_clear_bit - Clear a bit and return its old value
97 * @addr: Address to count from
99 static inline int test_and_clear_bit(int nr, unsigned long *addr)
101 unsigned long mask = BIT_MASK(nr);
102 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
108 return (old & mask) != 0;
112 * bitmap_alloc - Allocate bitmap
113 * @nbits: Number of bits
115 static inline unsigned long *bitmap_alloc(int nbits)
117 return calloc(1, BITS_TO_LONGS(nbits) * sizeof(unsigned long));
121 * bitmap_free - Free bitmap
122 * @bitmap: pointer to bitmap
124 static inline void bitmap_free(unsigned long *bitmap)
130 * bitmap_scnprintf - print bitmap list into buffer
132 * @nbits: size of bitmap
133 * @buf: buffer to store output
134 * @size: size of @buf
136 size_t bitmap_scnprintf(unsigned long *bitmap, unsigned int nbits,
137 char *buf, size_t size);
140 * bitmap_and - Do logical and on bitmaps
141 * @dst: resulting bitmap
144 * @nbits: size of bitmap
146 static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
147 const unsigned long *src2, unsigned int nbits)
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);
154 #ifdef __LITTLE_ENDIAN
155 #define BITMAP_MEM_ALIGNMENT 8
157 #define BITMAP_MEM_ALIGNMENT (8 * sizeof(unsigned long))
159 #define BITMAP_MEM_MASK (BITMAP_MEM_ALIGNMENT - 1)
160 #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
162 static inline int bitmap_equal(const unsigned long *src1,
163 const unsigned long *src2, unsigned int nbits)
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);
173 #endif /* _PERF_BITOPS_H */