1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
7 #include "xfs_log_format.h"
10 * XFS bit manipulation routines, used in non-realtime code.
14 * Return whether bitmap is empty.
15 * Size is number of words in the bitmap, which is padded to word boundary
16 * Returns 1 for empty, 0 for non-empty.
19 xfs_bitmap_empty(uint *map, uint size)
23 for (i = 0; i < size; i++) {
32 * Count the number of contiguous bits set in the bitmap starting with bit
33 * start_bit. Size is the size of the bitmap in words.
36 xfs_contig_bits(uint *map, uint size, uint start_bit)
38 uint * p = ((unsigned int *) map) + (start_bit >> BIT_TO_WORD_SHIFT);
42 size <<= BIT_TO_WORD_SHIFT;
44 ASSERT(start_bit < size);
45 size -= start_bit & ~(NBWORD - 1);
46 start_bit &= (NBWORD - 1);
49 /* set to one first offset bits prior to start */
50 tmp |= (~0U >> (NBWORD-start_bit));
57 if ((tmp = *p++) != ~0U)
62 return result - start_bit;
64 return result + ffz(tmp) - start_bit;
68 * This takes the bit number to start looking from and
69 * returns the next set bit from there. It returns -1
70 * if there are no more bits set or the start bit is
71 * beyond the end of the bitmap.
73 * Size is the number of words, not bytes, in the bitmap.
75 int xfs_next_bit(uint *map, uint size, uint start_bit)
77 uint * p = ((unsigned int *) map) + (start_bit >> BIT_TO_WORD_SHIFT);
78 uint result = start_bit & ~(NBWORD - 1);
81 size <<= BIT_TO_WORD_SHIFT;
83 if (start_bit >= size)
86 start_bit &= (NBWORD - 1);
89 /* set to zero first offset bits prior to start */
90 tmp &= (~0U << start_bit);
97 if ((tmp = *p++) != 0U)
104 return result + ffs(tmp) - 1;