Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / fs / xfs / xfs_bit.c
blobb009112b545b7b4c60eac19a1c04cf69b563314a
1 /*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
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
18 #include "xfs.h"
19 #include "xfs_bit.h"
20 #include "xfs_log.h"
21 #include "xfs_trans.h"
22 #include "xfs_buf_item.h"
25 * XFS bit manipulation routines, used in non-realtime code.
28 <<<<<<< HEAD:fs/xfs/xfs_bit.c
29 =======
30 #ifndef HAVE_ARCH_HIGHBIT
32 * Index of high bit number in byte, -1 for none set, 0..7 otherwise.
34 static const char xfs_highbit[256] = {
35 -1, 0, 1, 1, 2, 2, 2, 2, /* 00 .. 07 */
36 3, 3, 3, 3, 3, 3, 3, 3, /* 08 .. 0f */
37 4, 4, 4, 4, 4, 4, 4, 4, /* 10 .. 17 */
38 4, 4, 4, 4, 4, 4, 4, 4, /* 18 .. 1f */
39 5, 5, 5, 5, 5, 5, 5, 5, /* 20 .. 27 */
40 5, 5, 5, 5, 5, 5, 5, 5, /* 28 .. 2f */
41 5, 5, 5, 5, 5, 5, 5, 5, /* 30 .. 37 */
42 5, 5, 5, 5, 5, 5, 5, 5, /* 38 .. 3f */
43 6, 6, 6, 6, 6, 6, 6, 6, /* 40 .. 47 */
44 6, 6, 6, 6, 6, 6, 6, 6, /* 48 .. 4f */
45 6, 6, 6, 6, 6, 6, 6, 6, /* 50 .. 57 */
46 6, 6, 6, 6, 6, 6, 6, 6, /* 58 .. 5f */
47 6, 6, 6, 6, 6, 6, 6, 6, /* 60 .. 67 */
48 6, 6, 6, 6, 6, 6, 6, 6, /* 68 .. 6f */
49 6, 6, 6, 6, 6, 6, 6, 6, /* 70 .. 77 */
50 6, 6, 6, 6, 6, 6, 6, 6, /* 78 .. 7f */
51 7, 7, 7, 7, 7, 7, 7, 7, /* 80 .. 87 */
52 7, 7, 7, 7, 7, 7, 7, 7, /* 88 .. 8f */
53 7, 7, 7, 7, 7, 7, 7, 7, /* 90 .. 97 */
54 7, 7, 7, 7, 7, 7, 7, 7, /* 98 .. 9f */
55 7, 7, 7, 7, 7, 7, 7, 7, /* a0 .. a7 */
56 7, 7, 7, 7, 7, 7, 7, 7, /* a8 .. af */
57 7, 7, 7, 7, 7, 7, 7, 7, /* b0 .. b7 */
58 7, 7, 7, 7, 7, 7, 7, 7, /* b8 .. bf */
59 7, 7, 7, 7, 7, 7, 7, 7, /* c0 .. c7 */
60 7, 7, 7, 7, 7, 7, 7, 7, /* c8 .. cf */
61 7, 7, 7, 7, 7, 7, 7, 7, /* d0 .. d7 */
62 7, 7, 7, 7, 7, 7, 7, 7, /* d8 .. df */
63 7, 7, 7, 7, 7, 7, 7, 7, /* e0 .. e7 */
64 7, 7, 7, 7, 7, 7, 7, 7, /* e8 .. ef */
65 7, 7, 7, 7, 7, 7, 7, 7, /* f0 .. f7 */
66 7, 7, 7, 7, 7, 7, 7, 7, /* f8 .. ff */
68 #endif
71 * xfs_highbit32: get high bit set out of 32-bit argument, -1 if none set.
73 inline int
74 xfs_highbit32(
75 __uint32_t v)
77 #ifdef HAVE_ARCH_HIGHBIT
78 return highbit32(v);
79 #else
80 int i;
82 if (v & 0xffff0000)
83 if (v & 0xff000000)
84 i = 24;
85 else
86 i = 16;
87 else if (v & 0x0000ffff)
88 if (v & 0x0000ff00)
89 i = 8;
90 else
91 i = 0;
92 else
93 return -1;
94 return i + xfs_highbit[(v >> i) & 0xff];
95 #endif
99 * xfs_lowbit64: get low bit set out of 64-bit argument, -1 if none set.
102 xfs_lowbit64(
103 __uint64_t v)
105 __uint32_t w = (__uint32_t)v;
106 int n = 0;
108 if (w) { /* lower bits */
109 n = ffs(w);
110 } else { /* upper bits */
111 w = (__uint32_t)(v >> 32);
112 if (w && (n = ffs(w)))
113 n += 32;
115 return n - 1;
119 * xfs_highbit64: get high bit set out of 64-bit argument, -1 if none set.
122 xfs_highbit64(
123 __uint64_t v)
125 __uint32_t h = (__uint32_t)(v >> 32);
127 if (h)
128 return xfs_highbit32(h) + 32;
129 return xfs_highbit32((__uint32_t)v);
133 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/xfs/xfs_bit.c
135 * Return whether bitmap is empty.
136 * Size is number of words in the bitmap, which is padded to word boundary
137 * Returns 1 for empty, 0 for non-empty.
140 xfs_bitmap_empty(uint *map, uint size)
142 uint i;
143 uint ret = 0;
145 for (i = 0; i < size; i++) {
146 ret |= map[i];
149 return (ret == 0);
153 * Count the number of contiguous bits set in the bitmap starting with bit
154 * start_bit. Size is the size of the bitmap in words.
157 xfs_contig_bits(uint *map, uint size, uint start_bit)
159 uint * p = ((unsigned int *) map) + (start_bit >> BIT_TO_WORD_SHIFT);
160 uint result = 0;
161 uint tmp;
163 size <<= BIT_TO_WORD_SHIFT;
165 ASSERT(start_bit < size);
166 size -= start_bit & ~(NBWORD - 1);
167 start_bit &= (NBWORD - 1);
168 if (start_bit) {
169 tmp = *p++;
170 /* set to one first offset bits prior to start */
171 tmp |= (~0U >> (NBWORD-start_bit));
172 if (tmp != ~0U)
173 goto found;
174 result += NBWORD;
175 size -= NBWORD;
177 while (size) {
178 if ((tmp = *p++) != ~0U)
179 goto found;
180 result += NBWORD;
181 size -= NBWORD;
183 return result - start_bit;
184 found:
185 return result + ffz(tmp) - start_bit;
189 * This takes the bit number to start looking from and
190 * returns the next set bit from there. It returns -1
191 * if there are no more bits set or the start bit is
192 * beyond the end of the bitmap.
194 * Size is the number of words, not bytes, in the bitmap.
196 int xfs_next_bit(uint *map, uint size, uint start_bit)
198 uint * p = ((unsigned int *) map) + (start_bit >> BIT_TO_WORD_SHIFT);
199 uint result = start_bit & ~(NBWORD - 1);
200 uint tmp;
202 size <<= BIT_TO_WORD_SHIFT;
204 if (start_bit >= size)
205 return -1;
206 size -= result;
207 start_bit &= (NBWORD - 1);
208 if (start_bit) {
209 tmp = *p++;
210 /* set to zero first offset bits prior to start */
211 tmp &= (~0U << start_bit);
212 if (tmp != 0U)
213 goto found;
214 result += NBWORD;
215 size -= NBWORD;
217 while (size) {
218 if ((tmp = *p++) != 0U)
219 goto found;
220 result += NBWORD;
221 size -= NBWORD;
223 return -1;
224 found:
225 return result + ffs(tmp) - 1;