]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - tools/include/linux/bitmap.h
5 #include <linux/bitops.h>
7 #include <linux/kernel.h>
9 #define DECLARE_BITMAP(name,bits) \
10 unsigned long name[BITS_TO_LONGS(bits)]
12 int __bitmap_weight(const unsigned long *bitmap
, int bits
);
13 void __bitmap_or(unsigned long *dst
, const unsigned long *bitmap1
,
14 const unsigned long *bitmap2
, int bits
);
15 int __bitmap_and(unsigned long *dst
, const unsigned long *bitmap1
,
16 const unsigned long *bitmap2
, unsigned int bits
);
18 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
20 #define BITMAP_LAST_WORD_MASK(nbits) \
22 ((nbits) % BITS_PER_LONG) ? \
23 (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \
26 #define small_const_nbits(nbits) \
27 (__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG)
29 static inline void bitmap_zero(unsigned long *dst
, int nbits
)
31 if (small_const_nbits(nbits
))
34 int len
= BITS_TO_LONGS(nbits
) * sizeof(unsigned long);
39 static inline void bitmap_fill(unsigned long *dst
, unsigned int nbits
)
41 unsigned int nlongs
= BITS_TO_LONGS(nbits
);
42 if (!small_const_nbits(nbits
)) {
43 unsigned int len
= (nlongs
- 1) * sizeof(unsigned long);
44 memset(dst
, 0xff, len
);
46 dst
[nlongs
- 1] = BITMAP_LAST_WORD_MASK(nbits
);
49 static inline int bitmap_empty(const unsigned long *src
, unsigned nbits
)
51 if (small_const_nbits(nbits
))
52 return ! (*src
& BITMAP_LAST_WORD_MASK(nbits
));
54 return find_first_bit(src
, nbits
) == nbits
;
57 static inline int bitmap_full(const unsigned long *src
, unsigned int nbits
)
59 if (small_const_nbits(nbits
))
60 return ! (~(*src
) & BITMAP_LAST_WORD_MASK(nbits
));
62 return find_first_zero_bit(src
, nbits
) == nbits
;
65 static inline int bitmap_weight(const unsigned long *src
, int nbits
)
67 if (small_const_nbits(nbits
))
68 return hweight_long(*src
& BITMAP_LAST_WORD_MASK(nbits
));
69 return __bitmap_weight(src
, nbits
);
72 static inline void bitmap_or(unsigned long *dst
, const unsigned long *src1
,
73 const unsigned long *src2
, int nbits
)
75 if (small_const_nbits(nbits
))
78 __bitmap_or(dst
, src1
, src2
, nbits
);
82 * test_and_set_bit - Set a bit and return its old value
84 * @addr: Address to count from
86 static inline int test_and_set_bit(int nr
, unsigned long *addr
)
88 unsigned long mask
= BIT_MASK(nr
);
89 unsigned long *p
= ((unsigned long *)addr
) + BIT_WORD(nr
);
95 return (old
& mask
) != 0;
99 * bitmap_alloc - Allocate bitmap
102 static inline unsigned long *bitmap_alloc(int nbits
)
104 return calloc(1, BITS_TO_LONGS(nbits
) * sizeof(unsigned long));
108 * bitmap_scnprintf - print bitmap list into buffer
110 * @nbits: size of bitmap
111 * @buf: buffer to store output
112 * @size: size of @buf
114 size_t bitmap_scnprintf(unsigned long *bitmap
, int nbits
,
115 char *buf
, size_t size
);
118 * bitmap_and - Do logical and on bitmaps
119 * @dst: resulting bitmap
122 * @nbits: size of bitmap
124 static inline int bitmap_and(unsigned long *dst
, const unsigned long *src1
,
125 const unsigned long *src2
, unsigned int nbits
)
127 if (small_const_nbits(nbits
))
128 return (*dst
= *src1
& *src2
& BITMAP_LAST_WORD_MASK(nbits
)) != 0;
129 return __bitmap_and(dst
, src1
, src2
, nbits
);
132 #endif /* _PERF_BITOPS_H */