blob: 16ce44a2b43eaac5730be0e3084ed44fd93e86e1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Dave Chinner239880e2013-10-23 10:50:10 +110019#include "xfs_log_format.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Nathan Scott7b718762005-11-02 14:58:39 +110021/*
22 * XFS bit manipulation routines, used in non-realtime code.
23 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/*
Eric Sandeen24ad33f2007-06-28 16:43:30 +100026 * Return whether bitmap is empty.
27 * Size is number of words in the bitmap, which is padded to word boundary
28 * Returns 1 for empty, 0 for non-empty.
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 */
30int
Eric Sandeen24ad33f2007-06-28 16:43:30 +100031xfs_bitmap_empty(uint *map, uint size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
Eric Sandeen24ad33f2007-06-28 16:43:30 +100033 uint i;
34 uint ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Eric Sandeen24ad33f2007-06-28 16:43:30 +100036 for (i = 0; i < size; i++) {
37 ret |= map[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 }
39
Eric Sandeen24ad33f2007-06-28 16:43:30 +100040 return (ret == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041}
42
43/*
44 * Count the number of contiguous bits set in the bitmap starting with bit
45 * start_bit. Size is the size of the bitmap in words.
46 */
47int
48xfs_contig_bits(uint *map, uint size, uint start_bit)
49{
50 uint * p = ((unsigned int *) map) + (start_bit >> BIT_TO_WORD_SHIFT);
51 uint result = 0;
52 uint tmp;
53
54 size <<= BIT_TO_WORD_SHIFT;
55
56 ASSERT(start_bit < size);
57 size -= start_bit & ~(NBWORD - 1);
58 start_bit &= (NBWORD - 1);
59 if (start_bit) {
60 tmp = *p++;
61 /* set to one first offset bits prior to start */
62 tmp |= (~0U >> (NBWORD-start_bit));
63 if (tmp != ~0U)
64 goto found;
65 result += NBWORD;
66 size -= NBWORD;
67 }
68 while (size) {
69 if ((tmp = *p++) != ~0U)
70 goto found;
71 result += NBWORD;
72 size -= NBWORD;
73 }
74 return result - start_bit;
75found:
76 return result + ffz(tmp) - start_bit;
77}
78
79/*
80 * This takes the bit number to start looking from and
81 * returns the next set bit from there. It returns -1
82 * if there are no more bits set or the start bit is
83 * beyond the end of the bitmap.
84 *
85 * Size is the number of words, not bytes, in the bitmap.
86 */
87int xfs_next_bit(uint *map, uint size, uint start_bit)
88{
89 uint * p = ((unsigned int *) map) + (start_bit >> BIT_TO_WORD_SHIFT);
90 uint result = start_bit & ~(NBWORD - 1);
91 uint tmp;
92
93 size <<= BIT_TO_WORD_SHIFT;
94
95 if (start_bit >= size)
96 return -1;
97 size -= result;
98 start_bit &= (NBWORD - 1);
99 if (start_bit) {
100 tmp = *p++;
101 /* set to zero first offset bits prior to start */
102 tmp &= (~0U << start_bit);
103 if (tmp != 0U)
104 goto found;
105 result += NBWORD;
106 size -= NBWORD;
107 }
108 while (size) {
109 if ((tmp = *p++) != 0U)
110 goto found;
111 result += NBWORD;
112 size -= NBWORD;
113 }
114 return -1;
115found:
116 return result + ffs(tmp) - 1;
117}