4 * Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com>
6 * Mostly inspired by (stolen from) linux/bitmap.h and linux/bitops.h
8 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
9 * See the COPYING.LIB file in the top-level directory.
17 #include "qemu/bitops.h"
20 * The available bitmap operations and their rough meaning in the
21 * case that the bitmap is a single unsigned long are thus:
23 * Note that nbits should be always a compile time evaluable constant.
24 * Otherwise many inlines will generate horrible code.
26 * bitmap_zero(dst, nbits) *dst = 0UL
27 * bitmap_fill(dst, nbits) *dst = ~0UL
28 * bitmap_copy(dst, src, nbits) *dst = *src
29 * bitmap_and(dst, src1, src2, nbits) *dst = *src1 & *src2
30 * bitmap_or(dst, src1, src2, nbits) *dst = *src1 | *src2
31 * bitmap_xor(dst, src1, src2, nbits) *dst = *src1 ^ *src2
32 * bitmap_andnot(dst, src1, src2, nbits) *dst = *src1 & ~(*src2)
33 * bitmap_complement(dst, src, nbits) *dst = ~(*src)
34 * bitmap_equal(src1, src2, nbits) Are *src1 and *src2 equal?
35 * bitmap_intersects(src1, src2, nbits) Do *src1 and *src2 overlap?
36 * bitmap_empty(src, nbits) Are all bits zero in *src?
37 * bitmap_full(src, nbits) Are all bits set in *src?
38 * bitmap_set(dst, pos, nbits) Set specified bit area
39 * bitmap_set_atomic(dst, pos, nbits) Set specified bit area with atomic ops
40 * bitmap_clear(dst, pos, nbits) Clear specified bit area
41 * bitmap_test_and_clear_atomic(dst, pos, nbits) Test and clear area
42 * bitmap_find_next_zero_area(buf, len, pos, n, mask) Find bit free area
46 * Also the following operations apply to bitmaps.
48 * set_bit(bit, addr) *addr |= bit
49 * clear_bit(bit, addr) *addr &= ~bit
50 * change_bit(bit, addr) *addr ^= bit
51 * test_bit(bit, addr) Is bit set in *addr?
52 * test_and_set_bit(bit, addr) Set bit and return old value
53 * test_and_clear_bit(bit, addr) Clear bit and return old value
54 * test_and_change_bit(bit, addr) Change bit and return old value
55 * find_first_zero_bit(addr, nbits) Position first zero bit in *addr
56 * find_first_bit(addr, nbits) Position first set bit in *addr
57 * find_next_zero_bit(addr, nbits, bit) Position next zero bit in *addr >= bit
58 * find_next_bit(addr, nbits, bit) Position next set bit in *addr >= bit
61 #define BITMAP_LAST_WORD_MASK(nbits) \
63 ((nbits) % BITS_PER_LONG) ? \
64 (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \
67 #define DECLARE_BITMAP(name,bits) \
68 unsigned long name[BITS_TO_LONGS(bits)]
70 #define small_nbits(nbits) \
71 ((nbits) <= BITS_PER_LONG)
73 int slow_bitmap_empty(const unsigned long *bitmap
, long bits
);
74 int slow_bitmap_full(const unsigned long *bitmap
, long bits
);
75 int slow_bitmap_equal(const unsigned long *bitmap1
,
76 const unsigned long *bitmap2
, long bits
);
77 void slow_bitmap_complement(unsigned long *dst
, const unsigned long *src
,
79 void slow_bitmap_shift_right(unsigned long *dst
,
80 const unsigned long *src
, int shift
, long bits
);
81 void slow_bitmap_shift_left(unsigned long *dst
,
82 const unsigned long *src
, int shift
, long bits
);
83 int slow_bitmap_and(unsigned long *dst
, const unsigned long *bitmap1
,
84 const unsigned long *bitmap2
, long bits
);
85 void slow_bitmap_or(unsigned long *dst
, const unsigned long *bitmap1
,
86 const unsigned long *bitmap2
, long bits
);
87 void slow_bitmap_xor(unsigned long *dst
, const unsigned long *bitmap1
,
88 const unsigned long *bitmap2
, long bits
);
89 int slow_bitmap_andnot(unsigned long *dst
, const unsigned long *bitmap1
,
90 const unsigned long *bitmap2
, long bits
);
91 int slow_bitmap_intersects(const unsigned long *bitmap1
,
92 const unsigned long *bitmap2
, long bits
);
94 static inline unsigned long *bitmap_try_new(long nbits
)
96 long len
= BITS_TO_LONGS(nbits
) * sizeof(unsigned long);
97 return g_try_malloc0(len
);
100 static inline unsigned long *bitmap_new(long nbits
)
102 unsigned long *ptr
= bitmap_try_new(nbits
);
109 static inline void bitmap_zero(unsigned long *dst
, long nbits
)
111 if (small_nbits(nbits
)) {
114 long len
= BITS_TO_LONGS(nbits
) * sizeof(unsigned long);
119 static inline void bitmap_fill(unsigned long *dst
, long nbits
)
121 size_t nlongs
= BITS_TO_LONGS(nbits
);
122 if (!small_nbits(nbits
)) {
123 long len
= (nlongs
- 1) * sizeof(unsigned long);
124 memset(dst
, 0xff, len
);
126 dst
[nlongs
- 1] = BITMAP_LAST_WORD_MASK(nbits
);
129 static inline void bitmap_copy(unsigned long *dst
, const unsigned long *src
,
132 if (small_nbits(nbits
)) {
135 long len
= BITS_TO_LONGS(nbits
) * sizeof(unsigned long);
136 memcpy(dst
, src
, len
);
140 static inline int bitmap_and(unsigned long *dst
, const unsigned long *src1
,
141 const unsigned long *src2
, long nbits
)
143 if (small_nbits(nbits
)) {
144 return (*dst
= *src1
& *src2
) != 0;
146 return slow_bitmap_and(dst
, src1
, src2
, nbits
);
149 static inline void bitmap_or(unsigned long *dst
, const unsigned long *src1
,
150 const unsigned long *src2
, long nbits
)
152 if (small_nbits(nbits
)) {
153 *dst
= *src1
| *src2
;
155 slow_bitmap_or(dst
, src1
, src2
, nbits
);
159 static inline void bitmap_xor(unsigned long *dst
, const unsigned long *src1
,
160 const unsigned long *src2
, long nbits
)
162 if (small_nbits(nbits
)) {
163 *dst
= *src1
^ *src2
;
165 slow_bitmap_xor(dst
, src1
, src2
, nbits
);
169 static inline int bitmap_andnot(unsigned long *dst
, const unsigned long *src1
,
170 const unsigned long *src2
, long nbits
)
172 if (small_nbits(nbits
)) {
173 return (*dst
= *src1
& ~(*src2
)) != 0;
175 return slow_bitmap_andnot(dst
, src1
, src2
, nbits
);
178 static inline void bitmap_complement(unsigned long *dst
,
179 const unsigned long *src
,
182 if (small_nbits(nbits
)) {
183 *dst
= ~(*src
) & BITMAP_LAST_WORD_MASK(nbits
);
185 slow_bitmap_complement(dst
, src
, nbits
);
189 static inline int bitmap_equal(const unsigned long *src1
,
190 const unsigned long *src2
, long nbits
)
192 if (small_nbits(nbits
)) {
193 return ! ((*src1
^ *src2
) & BITMAP_LAST_WORD_MASK(nbits
));
195 return slow_bitmap_equal(src1
, src2
, nbits
);
199 static inline int bitmap_empty(const unsigned long *src
, long nbits
)
201 if (small_nbits(nbits
)) {
202 return ! (*src
& BITMAP_LAST_WORD_MASK(nbits
));
204 return slow_bitmap_empty(src
, nbits
);
208 static inline int bitmap_full(const unsigned long *src
, long nbits
)
210 if (small_nbits(nbits
)) {
211 return ! (~(*src
) & BITMAP_LAST_WORD_MASK(nbits
));
213 return slow_bitmap_full(src
, nbits
);
217 static inline int bitmap_intersects(const unsigned long *src1
,
218 const unsigned long *src2
, long nbits
)
220 if (small_nbits(nbits
)) {
221 return ((*src1
& *src2
) & BITMAP_LAST_WORD_MASK(nbits
)) != 0;
223 return slow_bitmap_intersects(src1
, src2
, nbits
);
227 void bitmap_set(unsigned long *map
, long i
, long len
);
228 void bitmap_set_atomic(unsigned long *map
, long i
, long len
);
229 void bitmap_clear(unsigned long *map
, long start
, long nr
);
230 bool bitmap_test_and_clear_atomic(unsigned long *map
, long start
, long nr
);
231 unsigned long bitmap_find_next_zero_area(unsigned long *map
,
235 unsigned long align_mask
);
237 static inline unsigned long *bitmap_zero_extend(unsigned long *old
,
238 long old_nbits
, long new_nbits
)
240 long new_len
= BITS_TO_LONGS(new_nbits
) * sizeof(unsigned long);
241 unsigned long *new = g_realloc(old
, new_len
);
242 bitmap_clear(new, old_nbits
, new_nbits
- old_nbits
);
246 #endif /* BITMAP_H */