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/osdep.h"
13 #include "qemu/bitops.h"
14 #include "qemu/bitmap.h"
15 #include "qemu/atomic.h"
18 * bitmaps provide an array of bits, implemented using an
19 * array of unsigned longs. The number of valid bits in a
20 * given bitmap does _not_ need to be an exact multiple of
23 * The possible unused bits in the last, partially used word
24 * of a bitmap are 'don't care'. The implementation makes
25 * no particular effort to keep them zero. It ensures that
26 * their value will not affect the results of any operation.
27 * The bitmap operations that return Boolean (bitmap_empty,
28 * for example) or scalar (bitmap_weight, for example) results
29 * carefully filter out these unused bits from impacting their
32 * These operations actually hold to a slightly stronger rule:
33 * if you don't input any bitmaps to these ops that have some
34 * unused bits set, then they won't output any set unused bits
37 * The byte ordering of bitmaps is more natural on little
38 * endian architectures.
41 int slow_bitmap_empty(const unsigned long *bitmap
, long bits
)
43 long k
, lim
= bits
/BITS_PER_LONG
;
45 for (k
= 0; k
< lim
; ++k
) {
50 if (bits
% BITS_PER_LONG
) {
51 if (bitmap
[k
] & BITMAP_LAST_WORD_MASK(bits
)) {
59 int slow_bitmap_full(const unsigned long *bitmap
, long bits
)
61 long k
, lim
= bits
/BITS_PER_LONG
;
63 for (k
= 0; k
< lim
; ++k
) {
69 if (bits
% BITS_PER_LONG
) {
70 if (~bitmap
[k
] & BITMAP_LAST_WORD_MASK(bits
)) {
78 int slow_bitmap_equal(const unsigned long *bitmap1
,
79 const unsigned long *bitmap2
, long bits
)
81 long k
, lim
= bits
/BITS_PER_LONG
;
83 for (k
= 0; k
< lim
; ++k
) {
84 if (bitmap1
[k
] != bitmap2
[k
]) {
89 if (bits
% BITS_PER_LONG
) {
90 if ((bitmap1
[k
] ^ bitmap2
[k
]) & BITMAP_LAST_WORD_MASK(bits
)) {
98 void slow_bitmap_complement(unsigned long *dst
, const unsigned long *src
,
101 long k
, lim
= bits
/BITS_PER_LONG
;
103 for (k
= 0; k
< lim
; ++k
) {
107 if (bits
% BITS_PER_LONG
) {
108 dst
[k
] = ~src
[k
] & BITMAP_LAST_WORD_MASK(bits
);
112 int slow_bitmap_and(unsigned long *dst
, const unsigned long *bitmap1
,
113 const unsigned long *bitmap2
, long bits
)
116 long nr
= BITS_TO_LONGS(bits
);
117 unsigned long result
= 0;
119 for (k
= 0; k
< nr
; k
++) {
120 result
|= (dst
[k
] = bitmap1
[k
] & bitmap2
[k
]);
125 void slow_bitmap_or(unsigned long *dst
, const unsigned long *bitmap1
,
126 const unsigned long *bitmap2
, long bits
)
129 long nr
= BITS_TO_LONGS(bits
);
131 for (k
= 0; k
< nr
; k
++) {
132 dst
[k
] = bitmap1
[k
] | bitmap2
[k
];
136 void slow_bitmap_xor(unsigned long *dst
, const unsigned long *bitmap1
,
137 const unsigned long *bitmap2
, long bits
)
140 long nr
= BITS_TO_LONGS(bits
);
142 for (k
= 0; k
< nr
; k
++) {
143 dst
[k
] = bitmap1
[k
] ^ bitmap2
[k
];
147 int slow_bitmap_andnot(unsigned long *dst
, const unsigned long *bitmap1
,
148 const unsigned long *bitmap2
, long bits
)
151 long nr
= BITS_TO_LONGS(bits
);
152 unsigned long result
= 0;
154 for (k
= 0; k
< nr
; k
++) {
155 result
|= (dst
[k
] = bitmap1
[k
] & ~bitmap2
[k
]);
160 void bitmap_set(unsigned long *map
, long start
, long nr
)
162 unsigned long *p
= map
+ BIT_WORD(start
);
163 const long size
= start
+ nr
;
164 int bits_to_set
= BITS_PER_LONG
- (start
% BITS_PER_LONG
);
165 unsigned long mask_to_set
= BITMAP_FIRST_WORD_MASK(start
);
167 assert(start
>= 0 && nr
>= 0);
169 while (nr
- bits_to_set
>= 0) {
172 bits_to_set
= BITS_PER_LONG
;
177 mask_to_set
&= BITMAP_LAST_WORD_MASK(size
);
182 void bitmap_set_atomic(unsigned long *map
, long start
, long nr
)
184 unsigned long *p
= map
+ BIT_WORD(start
);
185 const long size
= start
+ nr
;
186 int bits_to_set
= BITS_PER_LONG
- (start
% BITS_PER_LONG
);
187 unsigned long mask_to_set
= BITMAP_FIRST_WORD_MASK(start
);
189 assert(start
>= 0 && nr
>= 0);
192 if (nr
- bits_to_set
> 0) {
193 atomic_or(p
, mask_to_set
);
195 bits_to_set
= BITS_PER_LONG
;
201 if (bits_to_set
== BITS_PER_LONG
) {
202 while (nr
>= BITS_PER_LONG
) {
211 mask_to_set
&= BITMAP_LAST_WORD_MASK(size
);
212 atomic_or(p
, mask_to_set
);
214 /* If we avoided the full barrier in atomic_or(), issue a
215 * barrier to account for the assignments in the while loop.
221 void bitmap_clear(unsigned long *map
, long start
, long nr
)
223 unsigned long *p
= map
+ BIT_WORD(start
);
224 const long size
= start
+ nr
;
225 int bits_to_clear
= BITS_PER_LONG
- (start
% BITS_PER_LONG
);
226 unsigned long mask_to_clear
= BITMAP_FIRST_WORD_MASK(start
);
228 assert(start
>= 0 && nr
>= 0);
230 while (nr
- bits_to_clear
>= 0) {
231 *p
&= ~mask_to_clear
;
233 bits_to_clear
= BITS_PER_LONG
;
234 mask_to_clear
= ~0UL;
238 mask_to_clear
&= BITMAP_LAST_WORD_MASK(size
);
239 *p
&= ~mask_to_clear
;
243 bool bitmap_test_and_clear_atomic(unsigned long *map
, long start
, long nr
)
245 unsigned long *p
= map
+ BIT_WORD(start
);
246 const long size
= start
+ nr
;
247 int bits_to_clear
= BITS_PER_LONG
- (start
% BITS_PER_LONG
);
248 unsigned long mask_to_clear
= BITMAP_FIRST_WORD_MASK(start
);
249 unsigned long dirty
= 0;
250 unsigned long old_bits
;
252 assert(start
>= 0 && nr
>= 0);
255 if (nr
- bits_to_clear
> 0) {
256 old_bits
= atomic_fetch_and(p
, ~mask_to_clear
);
257 dirty
|= old_bits
& mask_to_clear
;
259 bits_to_clear
= BITS_PER_LONG
;
260 mask_to_clear
= ~0UL;
265 if (bits_to_clear
== BITS_PER_LONG
) {
266 while (nr
>= BITS_PER_LONG
) {
268 old_bits
= atomic_xchg(p
, 0);
278 mask_to_clear
&= BITMAP_LAST_WORD_MASK(size
);
279 old_bits
= atomic_fetch_and(p
, ~mask_to_clear
);
280 dirty
|= old_bits
& mask_to_clear
;
290 void bitmap_copy_and_clear_atomic(unsigned long *dst
, unsigned long *src
,
294 *dst
= atomic_xchg(src
, 0);
301 #define ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
304 * bitmap_find_next_zero_area - find a contiguous aligned zero area
305 * @map: The address to base the search on
306 * @size: The bitmap size in bits
307 * @start: The bitnumber to start searching at
308 * @nr: The number of zeroed bits we're looking for
309 * @align_mask: Alignment mask for zero area
311 * The @align_mask should be one less than a power of 2; the effect is that
312 * the bit offset of all zero areas this function finds is multiples of that
313 * power of 2. A @align_mask of 0 means no alignment is required.
315 unsigned long bitmap_find_next_zero_area(unsigned long *map
,
319 unsigned long align_mask
)
321 unsigned long index
, end
, i
;
323 index
= find_next_zero_bit(map
, size
, start
);
325 /* Align allocation */
326 index
= ALIGN_MASK(index
, align_mask
);
332 i
= find_next_bit(map
, end
, index
);
340 int slow_bitmap_intersects(const unsigned long *bitmap1
,
341 const unsigned long *bitmap2
, long bits
)
343 long k
, lim
= bits
/BITS_PER_LONG
;
345 for (k
= 0; k
< lim
; ++k
) {
346 if (bitmap1
[k
] & bitmap2
[k
]) {
351 if (bits
% BITS_PER_LONG
) {
352 if ((bitmap1
[k
] & bitmap2
[k
]) & BITMAP_LAST_WORD_MASK(bits
)) {
359 long slow_bitmap_count_one(const unsigned long *bitmap
, long nbits
)
361 long k
, lim
= nbits
/ BITS_PER_LONG
, result
= 0;
363 for (k
= 0; k
< lim
; k
++) {
364 result
+= ctpopl(bitmap
[k
]);
367 if (nbits
% BITS_PER_LONG
) {
368 result
+= ctpopl(bitmap
[k
] & BITMAP_LAST_WORD_MASK(nbits
));
374 static void bitmap_to_from_le(unsigned long *dst
,
375 const unsigned long *src
, long nbits
)
377 long len
= BITS_TO_LONGS(nbits
);
379 #ifdef HOST_WORDS_BIGENDIAN
382 for (index
= 0; index
< len
; index
++) {
383 # if HOST_LONG_BITS == 64
384 dst
[index
] = bswap64(src
[index
]);
386 dst
[index
] = bswap32(src
[index
]);
390 memcpy(dst
, src
, len
* sizeof(unsigned long));
394 void bitmap_from_le(unsigned long *dst
, const unsigned long *src
,
397 bitmap_to_from_le(dst
, src
, nbits
);
400 void bitmap_to_le(unsigned long *dst
, const unsigned long *src
,
403 bitmap_to_from_le(dst
, src
, nbits
);
407 * Copy "src" bitmap with a positive offset and put it into the "dst"
408 * bitmap. The caller needs to make sure the bitmap size of "src"
409 * is bigger than (shift + nbits).
411 void bitmap_copy_with_src_offset(unsigned long *dst
, const unsigned long *src
,
412 unsigned long shift
, unsigned long nbits
)
414 unsigned long left_mask
, right_mask
, last_mask
;
416 /* Proper shift src pointer to the first word to copy from */
417 src
+= BIT_WORD(shift
);
418 shift
%= BITS_PER_LONG
;
422 bitmap_copy(dst
, src
, nbits
);
426 right_mask
= (1ul << shift
) - 1;
427 left_mask
= ~right_mask
;
429 while (nbits
>= BITS_PER_LONG
) {
430 *dst
= (*src
& left_mask
) >> shift
;
431 *dst
|= (src
[1] & right_mask
) << (BITS_PER_LONG
- shift
);
434 nbits
-= BITS_PER_LONG
;
437 if (nbits
> BITS_PER_LONG
- shift
) {
438 *dst
= (*src
& left_mask
) >> shift
;
439 nbits
-= BITS_PER_LONG
- shift
;
440 last_mask
= (1ul << nbits
) - 1;
441 *dst
|= (src
[1] & last_mask
) << (BITS_PER_LONG
- shift
);
443 last_mask
= (1ul << nbits
) - 1;
444 *dst
= (*src
>> shift
) & last_mask
;
449 * Copy "src" bitmap into the "dst" bitmap with an offset in the
450 * "dst". The caller needs to make sure the bitmap size of "dst" is
451 * bigger than (shift + nbits).
453 void bitmap_copy_with_dst_offset(unsigned long *dst
, const unsigned long *src
,
454 unsigned long shift
, unsigned long nbits
)
456 unsigned long left_mask
, right_mask
, last_mask
;
458 /* Proper shift dst pointer to the first word to copy from */
459 dst
+= BIT_WORD(shift
);
460 shift
%= BITS_PER_LONG
;
464 bitmap_copy(dst
, src
, nbits
);
468 right_mask
= (1ul << (BITS_PER_LONG
- shift
)) - 1;
469 left_mask
= ~right_mask
;
471 *dst
&= (1ul << shift
) - 1;
472 while (nbits
>= BITS_PER_LONG
) {
473 *dst
|= (*src
& right_mask
) << shift
;
474 dst
[1] = (*src
& left_mask
) >> (BITS_PER_LONG
- shift
);
477 nbits
-= BITS_PER_LONG
;
480 if (nbits
> BITS_PER_LONG
- shift
) {
481 *dst
|= (*src
& right_mask
) << shift
;
482 nbits
-= BITS_PER_LONG
- shift
;
483 last_mask
= ((1ul << nbits
) - 1) << (BITS_PER_LONG
- shift
);
484 dst
[1] = (*src
& last_mask
) >> (BITS_PER_LONG
- shift
);
486 last_mask
= (1ul << nbits
) - 1;
487 *dst
|= (*src
& last_mask
) << shift
;