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
21 #include "xfs_trans.h"
22 #include "xfs_buf_item.h"
25 * XFS bit manipulation routines, used in non-realtime code.
28 #ifndef HAVE_ARCH_HIGHBIT
30 * Index of high bit number in byte, -1 for none set, 0..7 otherwise.
32 static const char xfs_highbit
[256] = {
33 -1, 0, 1, 1, 2, 2, 2, 2, /* 00 .. 07 */
34 3, 3, 3, 3, 3, 3, 3, 3, /* 08 .. 0f */
35 4, 4, 4, 4, 4, 4, 4, 4, /* 10 .. 17 */
36 4, 4, 4, 4, 4, 4, 4, 4, /* 18 .. 1f */
37 5, 5, 5, 5, 5, 5, 5, 5, /* 20 .. 27 */
38 5, 5, 5, 5, 5, 5, 5, 5, /* 28 .. 2f */
39 5, 5, 5, 5, 5, 5, 5, 5, /* 30 .. 37 */
40 5, 5, 5, 5, 5, 5, 5, 5, /* 38 .. 3f */
41 6, 6, 6, 6, 6, 6, 6, 6, /* 40 .. 47 */
42 6, 6, 6, 6, 6, 6, 6, 6, /* 48 .. 4f */
43 6, 6, 6, 6, 6, 6, 6, 6, /* 50 .. 57 */
44 6, 6, 6, 6, 6, 6, 6, 6, /* 58 .. 5f */
45 6, 6, 6, 6, 6, 6, 6, 6, /* 60 .. 67 */
46 6, 6, 6, 6, 6, 6, 6, 6, /* 68 .. 6f */
47 6, 6, 6, 6, 6, 6, 6, 6, /* 70 .. 77 */
48 6, 6, 6, 6, 6, 6, 6, 6, /* 78 .. 7f */
49 7, 7, 7, 7, 7, 7, 7, 7, /* 80 .. 87 */
50 7, 7, 7, 7, 7, 7, 7, 7, /* 88 .. 8f */
51 7, 7, 7, 7, 7, 7, 7, 7, /* 90 .. 97 */
52 7, 7, 7, 7, 7, 7, 7, 7, /* 98 .. 9f */
53 7, 7, 7, 7, 7, 7, 7, 7, /* a0 .. a7 */
54 7, 7, 7, 7, 7, 7, 7, 7, /* a8 .. af */
55 7, 7, 7, 7, 7, 7, 7, 7, /* b0 .. b7 */
56 7, 7, 7, 7, 7, 7, 7, 7, /* b8 .. bf */
57 7, 7, 7, 7, 7, 7, 7, 7, /* c0 .. c7 */
58 7, 7, 7, 7, 7, 7, 7, 7, /* c8 .. cf */
59 7, 7, 7, 7, 7, 7, 7, 7, /* d0 .. d7 */
60 7, 7, 7, 7, 7, 7, 7, 7, /* d8 .. df */
61 7, 7, 7, 7, 7, 7, 7, 7, /* e0 .. e7 */
62 7, 7, 7, 7, 7, 7, 7, 7, /* e8 .. ef */
63 7, 7, 7, 7, 7, 7, 7, 7, /* f0 .. f7 */
64 7, 7, 7, 7, 7, 7, 7, 7, /* f8 .. ff */
69 * Count of bits set in byte, 0..8.
71 static const char xfs_countbit
[256] = {
72 0, 1, 1, 2, 1, 2, 2, 3, /* 00 .. 07 */
73 1, 2, 2, 3, 2, 3, 3, 4, /* 08 .. 0f */
74 1, 2, 2, 3, 2, 3, 3, 4, /* 10 .. 17 */
75 2, 3, 3, 4, 3, 4, 4, 5, /* 18 .. 1f */
76 1, 2, 2, 3, 2, 3, 3, 4, /* 20 .. 27 */
77 2, 3, 3, 4, 3, 4, 4, 5, /* 28 .. 2f */
78 2, 3, 3, 4, 3, 4, 4, 5, /* 30 .. 37 */
79 3, 4, 4, 5, 4, 5, 5, 6, /* 38 .. 3f */
80 1, 2, 2, 3, 2, 3, 3, 4, /* 40 .. 47 */
81 2, 3, 3, 4, 3, 4, 4, 5, /* 48 .. 4f */
82 2, 3, 3, 4, 3, 4, 4, 5, /* 50 .. 57 */
83 3, 4, 4, 5, 4, 5, 5, 6, /* 58 .. 5f */
84 2, 3, 3, 4, 3, 4, 4, 5, /* 60 .. 67 */
85 3, 4, 4, 5, 4, 5, 5, 6, /* 68 .. 6f */
86 3, 4, 4, 5, 4, 5, 5, 6, /* 70 .. 77 */
87 4, 5, 5, 6, 5, 6, 6, 7, /* 78 .. 7f */
88 1, 2, 2, 3, 2, 3, 3, 4, /* 80 .. 87 */
89 2, 3, 3, 4, 3, 4, 4, 5, /* 88 .. 8f */
90 2, 3, 3, 4, 3, 4, 4, 5, /* 90 .. 97 */
91 3, 4, 4, 5, 4, 5, 5, 6, /* 98 .. 9f */
92 2, 3, 3, 4, 3, 4, 4, 5, /* a0 .. a7 */
93 3, 4, 4, 5, 4, 5, 5, 6, /* a8 .. af */
94 3, 4, 4, 5, 4, 5, 5, 6, /* b0 .. b7 */
95 4, 5, 5, 6, 5, 6, 6, 7, /* b8 .. bf */
96 2, 3, 3, 4, 3, 4, 4, 5, /* c0 .. c7 */
97 3, 4, 4, 5, 4, 5, 5, 6, /* c8 .. cf */
98 3, 4, 4, 5, 4, 5, 5, 6, /* d0 .. d7 */
99 4, 5, 5, 6, 5, 6, 6, 7, /* d8 .. df */
100 3, 4, 4, 5, 4, 5, 5, 6, /* e0 .. e7 */
101 4, 5, 5, 6, 5, 6, 6, 7, /* e8 .. ef */
102 4, 5, 5, 6, 5, 6, 6, 7, /* f0 .. f7 */
103 5, 6, 6, 7, 6, 7, 7, 8, /* f8 .. ff */
107 * xfs_highbit32: get high bit set out of 32-bit argument, -1 if none set.
113 #ifdef HAVE_ARCH_HIGHBIT
123 else if (v
& 0x0000ffff)
130 return i
+ xfs_highbit
[(v
>> i
) & 0xff];
135 * xfs_lowbit64: get low bit set out of 64-bit argument, -1 if none set.
141 __uint32_t w
= (__uint32_t
)v
;
144 if (w
) { /* lower bits */
146 } else { /* upper bits */
147 w
= (__uint32_t
)(v
>> 32);
148 if (w
&& (n
= ffs(w
)))
155 * xfs_highbit64: get high bit set out of 64-bit argument, -1 if none set.
161 __uint32_t h
= (__uint32_t
)(v
>> 32);
164 return xfs_highbit32(h
) + 32;
165 return xfs_highbit32((__uint32_t
)v
);
170 * Count the number of bits set in the bitmap starting with bit
171 * start_bit. Size is the size of the bitmap in words.
173 * Do the counting by mapping a byte value to the number of set
174 * bits for that value using the xfs_countbit array, i.e.
175 * xfs_countbit[0] == 0, xfs_countbit[1] == 1, xfs_countbit[2] == 1,
176 * xfs_countbit[3] == 2, etc.
179 xfs_count_bits(uint
*map
, uint size
, uint start_bit
)
182 register unsigned char *bytep
;
183 register unsigned char *end_map
;
187 end_map
= (char*)(map
+ size
);
188 bytep
= (char*)(map
+ (start_bit
& ~0x7));
189 byte_bit
= start_bit
& 0x7;
192 * If the caller fell off the end of the map, return 0.
194 if (bytep
>= end_map
) {
199 * If start_bit is not byte aligned, then process the
200 * first byte separately.
204 * Shift off the bits we don't want to look at,
205 * before indexing into xfs_countbit.
207 bits
+= xfs_countbit
[(*bytep
>> byte_bit
)];
212 * Count the bits in each byte until the end of the bitmap.
214 while (bytep
< end_map
) {
215 bits
+= xfs_countbit
[*bytep
];
223 * Count the number of contiguous bits set in the bitmap starting with bit
224 * start_bit. Size is the size of the bitmap in words.
227 xfs_contig_bits(uint
*map
, uint size
, uint start_bit
)
229 uint
* p
= ((unsigned int *) map
) + (start_bit
>> BIT_TO_WORD_SHIFT
);
233 size
<<= BIT_TO_WORD_SHIFT
;
235 ASSERT(start_bit
< size
);
236 size
-= start_bit
& ~(NBWORD
- 1);
237 start_bit
&= (NBWORD
- 1);
240 /* set to one first offset bits prior to start */
241 tmp
|= (~0U >> (NBWORD
-start_bit
));
248 if ((tmp
= *p
++) != ~0U)
253 return result
- start_bit
;
255 return result
+ ffz(tmp
) - start_bit
;
259 * This takes the bit number to start looking from and
260 * returns the next set bit from there. It returns -1
261 * if there are no more bits set or the start bit is
262 * beyond the end of the bitmap.
264 * Size is the number of words, not bytes, in the bitmap.
266 int xfs_next_bit(uint
*map
, uint size
, uint start_bit
)
268 uint
* p
= ((unsigned int *) map
) + (start_bit
>> BIT_TO_WORD_SHIFT
);
269 uint result
= start_bit
& ~(NBWORD
- 1);
272 size
<<= BIT_TO_WORD_SHIFT
;
274 if (start_bit
>= size
)
277 start_bit
&= (NBWORD
- 1);
280 /* set to zero first offset bits prior to start */
281 tmp
&= (~0U << start_bit
);
288 if ((tmp
= *p
++) != 0U)
295 return result
+ ffs(tmp
) - 1;