4 * Stolen from linux/src/lib/bitmap.c
6 * Copyright (C) 2010 Corentin Chary
8 * This source code is licensed under the GNU General Public License,
12 #include "qemu/bitops.h"
13 #include "qemu/bitmap.h"
14 #include "qemu/atomic.h"
17 * bitmaps provide an array of bits, implemented using an
18 * array of unsigned longs. The number of valid bits in a
19 * given bitmap does _not_ need to be an exact multiple of
22 * The possible unused bits in the last, partially used word
23 * of a bitmap are 'don't care'. The implementation makes
24 * no particular effort to keep them zero. It ensures that
25 * their value will not affect the results of any operation.
26 * The bitmap operations that return Boolean (bitmap_empty,
27 * for example) or scalar (bitmap_weight, for example) results
28 * carefully filter out these unused bits from impacting their
31 * These operations actually hold to a slightly stronger rule:
32 * if you don't input any bitmaps to these ops that have some
33 * unused bits set, then they won't output any set unused bits
36 * The byte ordering of bitmaps is more natural on little
37 * endian architectures.
40 int slow_bitmap_empty(const unsigned long *bitmap
, long bits
)
42 long k
, lim
= bits
/BITS_PER_LONG
;
44 for (k
= 0; k
< lim
; ++k
) {
49 if (bits
% BITS_PER_LONG
) {
50 if (bitmap
[k
] & BITMAP_LAST_WORD_MASK(bits
)) {
58 int slow_bitmap_full(const unsigned long *bitmap
, long bits
)
60 long k
, lim
= bits
/BITS_PER_LONG
;
62 for (k
= 0; k
< lim
; ++k
) {
68 if (bits
% BITS_PER_LONG
) {
69 if (~bitmap
[k
] & BITMAP_LAST_WORD_MASK(bits
)) {
77 int slow_bitmap_equal(const unsigned long *bitmap1
,
78 const unsigned long *bitmap2
, long bits
)
80 long k
, lim
= bits
/BITS_PER_LONG
;
82 for (k
= 0; k
< lim
; ++k
) {
83 if (bitmap1
[k
] != bitmap2
[k
]) {
88 if (bits
% BITS_PER_LONG
) {
89 if ((bitmap1
[k
] ^ bitmap2
[k
]) & BITMAP_LAST_WORD_MASK(bits
)) {
97 void slow_bitmap_complement(unsigned long *dst
, const unsigned long *src
,
100 long k
, lim
= bits
/BITS_PER_LONG
;
102 for (k
= 0; k
< lim
; ++k
) {
106 if (bits
% BITS_PER_LONG
) {
107 dst
[k
] = ~src
[k
] & BITMAP_LAST_WORD_MASK(bits
);
111 int slow_bitmap_and(unsigned long *dst
, const unsigned long *bitmap1
,
112 const unsigned long *bitmap2
, long bits
)
115 long nr
= BITS_TO_LONGS(bits
);
116 unsigned long result
= 0;
118 for (k
= 0; k
< nr
; k
++) {
119 result
|= (dst
[k
] = bitmap1
[k
] & bitmap2
[k
]);
124 void slow_bitmap_or(unsigned long *dst
, const unsigned long *bitmap1
,
125 const unsigned long *bitmap2
, long bits
)
128 long nr
= BITS_TO_LONGS(bits
);
130 for (k
= 0; k
< nr
; k
++) {
131 dst
[k
] = bitmap1
[k
] | bitmap2
[k
];
135 void slow_bitmap_xor(unsigned long *dst
, const unsigned long *bitmap1
,
136 const unsigned long *bitmap2
, long bits
)
139 long nr
= BITS_TO_LONGS(bits
);
141 for (k
= 0; k
< nr
; k
++) {
142 dst
[k
] = bitmap1
[k
] ^ bitmap2
[k
];
146 int slow_bitmap_andnot(unsigned long *dst
, const unsigned long *bitmap1
,
147 const unsigned long *bitmap2
, long bits
)
150 long nr
= BITS_TO_LONGS(bits
);
151 unsigned long result
= 0;
153 for (k
= 0; k
< nr
; k
++) {
154 result
|= (dst
[k
] = bitmap1
[k
] & ~bitmap2
[k
]);
159 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG))
161 void bitmap_set(unsigned long *map
, long start
, long nr
)
163 unsigned long *p
= map
+ BIT_WORD(start
);
164 const long size
= start
+ nr
;
165 int bits_to_set
= BITS_PER_LONG
- (start
% BITS_PER_LONG
);
166 unsigned long mask_to_set
= BITMAP_FIRST_WORD_MASK(start
);
168 while (nr
- bits_to_set
>= 0) {
171 bits_to_set
= BITS_PER_LONG
;
176 mask_to_set
&= BITMAP_LAST_WORD_MASK(size
);
181 void bitmap_set_atomic(unsigned long *map
, long start
, long nr
)
183 unsigned long *p
= map
+ BIT_WORD(start
);
184 const long size
= start
+ nr
;
185 int bits_to_set
= BITS_PER_LONG
- (start
% BITS_PER_LONG
);
186 unsigned long mask_to_set
= BITMAP_FIRST_WORD_MASK(start
);
189 if (nr
- bits_to_set
> 0) {
190 atomic_or(p
, mask_to_set
);
192 bits_to_set
= BITS_PER_LONG
;
198 if (bits_to_set
== BITS_PER_LONG
) {
199 while (nr
>= BITS_PER_LONG
) {
208 mask_to_set
&= BITMAP_LAST_WORD_MASK(size
);
209 atomic_or(p
, mask_to_set
);
211 /* If we avoided the full barrier in atomic_or(), issue a
212 * barrier to account for the assignments in the while loop.
218 void bitmap_clear(unsigned long *map
, long start
, long nr
)
220 unsigned long *p
= map
+ BIT_WORD(start
);
221 const long size
= start
+ nr
;
222 int bits_to_clear
= BITS_PER_LONG
- (start
% BITS_PER_LONG
);
223 unsigned long mask_to_clear
= BITMAP_FIRST_WORD_MASK(start
);
225 while (nr
- bits_to_clear
>= 0) {
226 *p
&= ~mask_to_clear
;
228 bits_to_clear
= BITS_PER_LONG
;
229 mask_to_clear
= ~0UL;
233 mask_to_clear
&= BITMAP_LAST_WORD_MASK(size
);
234 *p
&= ~mask_to_clear
;
238 bool bitmap_test_and_clear_atomic(unsigned long *map
, long start
, long nr
)
240 unsigned long *p
= map
+ BIT_WORD(start
);
241 const long size
= start
+ nr
;
242 int bits_to_clear
= BITS_PER_LONG
- (start
% BITS_PER_LONG
);
243 unsigned long mask_to_clear
= BITMAP_FIRST_WORD_MASK(start
);
244 unsigned long dirty
= 0;
245 unsigned long old_bits
;
248 if (nr
- bits_to_clear
> 0) {
249 old_bits
= atomic_fetch_and(p
, ~mask_to_clear
);
250 dirty
|= old_bits
& mask_to_clear
;
252 bits_to_clear
= BITS_PER_LONG
;
253 mask_to_clear
= ~0UL;
258 if (bits_to_clear
== BITS_PER_LONG
) {
259 while (nr
>= BITS_PER_LONG
) {
261 old_bits
= atomic_xchg(p
, 0);
271 mask_to_clear
&= BITMAP_LAST_WORD_MASK(size
);
272 old_bits
= atomic_fetch_and(p
, ~mask_to_clear
);
273 dirty
|= old_bits
& mask_to_clear
;
283 #define ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
286 * bitmap_find_next_zero_area - find a contiguous aligned zero area
287 * @map: The address to base the search on
288 * @size: The bitmap size in bits
289 * @start: The bitnumber to start searching at
290 * @nr: The number of zeroed bits we're looking for
291 * @align_mask: Alignment mask for zero area
293 * The @align_mask should be one less than a power of 2; the effect is that
294 * the bit offset of all zero areas this function finds is multiples of that
295 * power of 2. A @align_mask of 0 means no alignment is required.
297 unsigned long bitmap_find_next_zero_area(unsigned long *map
,
301 unsigned long align_mask
)
303 unsigned long index
, end
, i
;
305 index
= find_next_zero_bit(map
, size
, start
);
307 /* Align allocation */
308 index
= ALIGN_MASK(index
, align_mask
);
314 i
= find_next_bit(map
, end
, index
);
322 int slow_bitmap_intersects(const unsigned long *bitmap1
,
323 const unsigned long *bitmap2
, long bits
)
325 long k
, lim
= bits
/BITS_PER_LONG
;
327 for (k
= 0; k
< lim
; ++k
) {
328 if (bitmap1
[k
] & bitmap2
[k
]) {
333 if (bits
% BITS_PER_LONG
) {
334 if ((bitmap1
[k
] & bitmap2
[k
]) & BITMAP_LAST_WORD_MASK(bits
)) {