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 void bitmap_clear(unsigned long *map, unsigned int start, int len);
20 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
22 #define BITMAP_LAST_WORD_MASK(nbits) \
24 ((nbits) % BITS_PER_LONG) ? \
25 (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \
28 #define small_const_nbits(nbits) \
29 (__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG)
31 static inline void bitmap_zero(unsigned long *dst, int nbits)
33 if (small_const_nbits(nbits))
36 int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
41 static inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
43 unsigned int nlongs = BITS_TO_LONGS(nbits);
44 if (!small_const_nbits(nbits)) {
45 unsigned int len = (nlongs - 1) * sizeof(unsigned long);
46 memset(dst, 0xff, len);
48 dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits);
51 static inline int bitmap_empty(const unsigned long *src, unsigned nbits)
53 if (small_const_nbits(nbits))
54 return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
56 return find_first_bit(src, nbits) == nbits;
59 static inline int bitmap_full(const unsigned long *src, unsigned int nbits)
61 if (small_const_nbits(nbits))
62 return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
64 return find_first_zero_bit(src, nbits) == nbits;
67 static inline int bitmap_weight(const unsigned long *src, int nbits)
69 if (small_const_nbits(nbits))
70 return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
71 return __bitmap_weight(src, nbits);
74 static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
75 const unsigned long *src2, int nbits)
77 if (small_const_nbits(nbits))
80 __bitmap_or(dst, src1, src2, nbits);
84 * test_and_set_bit - Set a bit and return its old value
86 * @addr: Address to count from
88 static inline int test_and_set_bit(int nr, unsigned long *addr)
90 unsigned long mask = BIT_MASK(nr);
91 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
97 return (old & mask) != 0;
101 * test_and_clear_bit - Clear a bit and return its old value
103 * @addr: Address to count from
105 static inline int test_and_clear_bit(int nr, unsigned long *addr)
107 unsigned long mask = BIT_MASK(nr);
108 unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
114 return (old & mask) != 0;
118 * bitmap_alloc - Allocate bitmap
119 * @nbits: Number of bits
121 static inline unsigned long *bitmap_alloc(int nbits)
123 return calloc(1, BITS_TO_LONGS(nbits) * sizeof(unsigned long));
127 * bitmap_scnprintf - print bitmap list into buffer
129 * @nbits: size of bitmap
130 * @buf: buffer to store output
131 * @size: size of @buf
133 size_t bitmap_scnprintf(unsigned long *bitmap, int nbits,
134 char *buf, size_t size);
137 * bitmap_and - Do logical and on bitmaps
138 * @dst: resulting bitmap
141 * @nbits: size of bitmap
143 static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
144 const unsigned long *src2, unsigned int nbits)
146 if (small_const_nbits(nbits))
147 return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
148 return __bitmap_and(dst, src1, src2, nbits);
151 #endif /* _PERF_BITOPS_H */