2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "xfs_log_format.h"
23 * XFS bit manipulation routines, used in non-realtime code.
27 * Return whether bitmap is empty.
28 * Size is number of words in the bitmap, which is padded to word boundary
29 * Returns 1 for empty, 0 for non-empty.
32 xfs_bitmap_empty(uint
*map
, uint size
)
36 for (i
= 0; i
< size
; i
++) {
45 * Count the number of contiguous bits set in the bitmap starting with bit
46 * start_bit. Size is the size of the bitmap in words.
49 xfs_contig_bits(uint
*map
, uint size
, uint start_bit
)
51 uint
* p
= ((unsigned int *) map
) + (start_bit
>> BIT_TO_WORD_SHIFT
);
55 size
<<= BIT_TO_WORD_SHIFT
;
57 ASSERT(start_bit
< size
);
58 size
-= start_bit
& ~(NBWORD
- 1);
59 start_bit
&= (NBWORD
- 1);
62 /* set to one first offset bits prior to start */
63 tmp
|= (~0U >> (NBWORD
-start_bit
));
70 if ((tmp
= *p
++) != ~0U)
75 return result
- start_bit
;
77 return result
+ ffz(tmp
) - start_bit
;
81 * This takes the bit number to start looking from and
82 * returns the next set bit from there. It returns -1
83 * if there are no more bits set or the start bit is
84 * beyond the end of the bitmap.
86 * Size is the number of words, not bytes, in the bitmap.
88 int xfs_next_bit(uint
*map
, uint size
, uint start_bit
)
90 uint
* p
= ((unsigned int *) map
) + (start_bit
>> BIT_TO_WORD_SHIFT
);
91 uint result
= start_bit
& ~(NBWORD
- 1);
94 size
<<= BIT_TO_WORD_SHIFT
;
96 if (start_bit
>= size
)
99 start_bit
&= (NBWORD
- 1);
102 /* set to zero first offset bits prior to start */
103 tmp
&= (~0U << start_bit
);
110 if ((tmp
= *p
++) != 0U)
117 return result
+ ffs(tmp
) - 1;