1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
7 #include "xfs_log_format.h"
11 * XFS bit manipulation routines, used in non-realtime code.
15 * Return whether bitmap is empty.
16 * Size is number of words in the bitmap, which is padded to word boundary
17 * Returns 1 for empty, 0 for non-empty.
20 xfs_bitmap_empty(uint
*map
, uint size
)
24 for (i
= 0; i
< size
; i
++) {
33 * Count the number of contiguous bits set in the bitmap starting with bit
34 * start_bit. Size is the size of the bitmap in words.
37 xfs_contig_bits(uint
*map
, uint size
, uint start_bit
)
39 uint
* p
= ((unsigned int *) map
) + (start_bit
>> BIT_TO_WORD_SHIFT
);
43 size
<<= BIT_TO_WORD_SHIFT
;
45 ASSERT(start_bit
< size
);
46 size
-= start_bit
& ~(NBWORD
- 1);
47 start_bit
&= (NBWORD
- 1);
50 /* set to one first offset bits prior to start */
51 tmp
|= (~0U >> (NBWORD
-start_bit
));
58 if ((tmp
= *p
++) != ~0U)
63 return result
- start_bit
;
65 return result
+ ffz(tmp
) - start_bit
;
69 * This takes the bit number to start looking from and
70 * returns the next set bit from there. It returns -1
71 * if there are no more bits set or the start bit is
72 * beyond the end of the bitmap.
74 * Size is the number of words, not bytes, in the bitmap.
76 int xfs_next_bit(uint
*map
, uint size
, uint start_bit
)
78 uint
* p
= ((unsigned int *) map
) + (start_bit
>> BIT_TO_WORD_SHIFT
);
79 uint result
= start_bit
& ~(NBWORD
- 1);
82 size
<<= BIT_TO_WORD_SHIFT
;
84 if (start_bit
>= size
)
87 start_bit
&= (NBWORD
- 1);
90 /* set to zero first offset bits prior to start */
91 tmp
&= (~0U << start_bit
);
98 if ((tmp
= *p
++) != 0U)
105 return result
+ ffs(tmp
) - 1;