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);
21 int __bitmap_intersects(const unsigned long *bitmap1,
22 const unsigned long *bitmap2, unsigned int bits);
24 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
25 #define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
27 static inline void bitmap_zero(unsigned long *dst, unsigned int nbits)
29 if (small_const_nbits(nbits))
32 int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
37 static inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
39 unsigned int nlongs = BITS_TO_LONGS(nbits);
40 if (!small_const_nbits(nbits)) {
41 unsigned int len = (nlongs - 1) * sizeof(unsigned long);
42 memset(dst, 0xff, len);
44 dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits);
47 static inline int bitmap_empty(const unsigned long *src, unsigned nbits)
49 if (small_const_nbits(nbits))
50 return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
52 return find_first_bit(src, nbits) == nbits;
55 static inline int bitmap_full(const unsigned long *src, unsigned int nbits)
57 if (small_const_nbits(nbits))
58 return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
60 return find_first_zero_bit(src, nbits) == nbits;
63 static inline int bitmap_weight(const unsigned long *src, unsigned int nbits)
65 if (small_const_nbits(nbits))
66 return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
67 return __bitmap_weight(src, nbits);
70 static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
71 const unsigned long *src2, unsigned int nbits)
73 if (small_const_nbits(nbits))
76 __bitmap_or(dst, src1, src2, nbits);
80 * test_and_set_bit - Set a bit and return its old value
82 * @addr: Address to count from
84 static inline int test_and_set_bit(int nr, unsigned long *addr)
86 unsigned long mask = BIT_MASK(nr);
87 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
93 return (old & mask) != 0;
97 * test_and_clear_bit - Clear a bit and return its old value
99 * @addr: Address to count from
101 static inline int test_and_clear_bit(int nr, unsigned long *addr)
103 unsigned long mask = BIT_MASK(nr);
104 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
110 return (old & mask) != 0;
114 * bitmap_alloc - Allocate bitmap
115 * @nbits: Number of bits
117 static inline unsigned long *bitmap_alloc(int nbits)
119 return calloc(1, BITS_TO_LONGS(nbits) * sizeof(unsigned long));
123 * bitmap_free - Free bitmap
124 * @bitmap: pointer to bitmap
126 static inline void bitmap_free(unsigned long *bitmap)
132 * bitmap_scnprintf - print bitmap list into buffer
134 * @nbits: size of bitmap
135 * @buf: buffer to store output
136 * @size: size of @buf
138 size_t bitmap_scnprintf(unsigned long *bitmap, unsigned int nbits,
139 char *buf, size_t size);
142 * bitmap_and - Do logical and on bitmaps
143 * @dst: resulting bitmap
146 * @nbits: size of bitmap
148 static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
149 const unsigned long *src2, unsigned int nbits)
151 if (small_const_nbits(nbits))
152 return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
153 return __bitmap_and(dst, src1, src2, nbits);
156 #ifdef __LITTLE_ENDIAN
157 #define BITMAP_MEM_ALIGNMENT 8
159 #define BITMAP_MEM_ALIGNMENT (8 * sizeof(unsigned long))
161 #define BITMAP_MEM_MASK (BITMAP_MEM_ALIGNMENT - 1)
162 #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
164 static inline int bitmap_equal(const unsigned long *src1,
165 const unsigned long *src2, unsigned int nbits)
167 if (small_const_nbits(nbits))
168 return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
169 if (__builtin_constant_p(nbits & BITMAP_MEM_MASK) &&
170 IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT))
171 return !memcmp(src1, src2, nbits / 8);
172 return __bitmap_equal(src1, src2, nbits);
175 static inline int bitmap_intersects(const unsigned long *src1,
176 const unsigned long *src2, unsigned int nbits)
178 if (small_const_nbits(nbits))
179 return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
181 return __bitmap_intersects(src1, src2, nbits);
184 #endif /* _PERF_BITOPS_H */