blob: d3f5784807b45a00fa2301727951f6fb3e325f35 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* find_next_bit.c: fallback find next bit implementation
2 *
3 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <linux/bitops.h>
David Howells40234402006-01-08 01:01:19 -080013#include <linux/module.h>
Akinobu Mitac7f612c2006-03-26 01:39:11 -080014#include <asm/types.h>
Akinobu Mita930ae742006-03-26 01:39:15 -080015#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Akinobu Mitac7f612c2006-03-26 01:39:11 -080017#define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
18
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +020019#ifdef CONFIG_GENERIC_FIND_NEXT_BIT
Alexander van Heukelum64970b62008-03-11 16:17:19 +010020/*
21 * Find the next set bit in a memory region.
Akinobu Mitac7f612c2006-03-26 01:39:11 -080022 */
Alexander van Heukelum64970b62008-03-11 16:17:19 +010023unsigned long __find_next_bit(const unsigned long *addr,
24 unsigned long size, unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
Akinobu Mitac7f612c2006-03-26 01:39:11 -080026 const unsigned long *p = addr + BITOP_WORD(offset);
27 unsigned long result = offset & ~(BITS_PER_LONG-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 unsigned long tmp;
29
Akinobu Mitac7f612c2006-03-26 01:39:11 -080030 if (offset >= size)
31 return size;
32 size -= result;
33 offset %= BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 if (offset) {
Akinobu Mitac7f612c2006-03-26 01:39:11 -080035 tmp = *(p++);
36 tmp &= (~0UL << offset);
37 if (size < BITS_PER_LONG)
38 goto found_first;
39 if (tmp)
40 goto found_middle;
41 size -= BITS_PER_LONG;
42 result += BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 }
Akinobu Mitac7f612c2006-03-26 01:39:11 -080044 while (size & ~(BITS_PER_LONG-1)) {
45 if ((tmp = *(p++)))
46 goto found_middle;
47 result += BITS_PER_LONG;
48 size -= BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 }
Akinobu Mitac7f612c2006-03-26 01:39:11 -080050 if (!size)
51 return result;
52 tmp = *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Akinobu Mitac7f612c2006-03-26 01:39:11 -080054found_first:
55 tmp &= (~0UL >> (BITS_PER_LONG - size));
56 if (tmp == 0UL) /* Are any bits set? */
57 return result + size; /* Nope. */
58found_middle:
59 return result + __ffs(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
Alexander van Heukelum64970b62008-03-11 16:17:19 +010061EXPORT_SYMBOL(__find_next_bit);
Akinobu Mitac7f612c2006-03-26 01:39:11 -080062
63/*
64 * This implementation of find_{first,next}_zero_bit was stolen from
65 * Linus' asm-alpha/bitops.h.
66 */
Alexander van Heukelum64970b62008-03-11 16:17:19 +010067unsigned long __find_next_zero_bit(const unsigned long *addr,
68 unsigned long size, unsigned long offset)
Akinobu Mitac7f612c2006-03-26 01:39:11 -080069{
70 const unsigned long *p = addr + BITOP_WORD(offset);
71 unsigned long result = offset & ~(BITS_PER_LONG-1);
72 unsigned long tmp;
73
74 if (offset >= size)
75 return size;
76 size -= result;
77 offset %= BITS_PER_LONG;
78 if (offset) {
79 tmp = *(p++);
80 tmp |= ~0UL >> (BITS_PER_LONG - offset);
81 if (size < BITS_PER_LONG)
82 goto found_first;
83 if (~tmp)
84 goto found_middle;
85 size -= BITS_PER_LONG;
86 result += BITS_PER_LONG;
87 }
88 while (size & ~(BITS_PER_LONG-1)) {
89 if (~(tmp = *(p++)))
90 goto found_middle;
91 result += BITS_PER_LONG;
92 size -= BITS_PER_LONG;
93 }
94 if (!size)
95 return result;
96 tmp = *p;
97
98found_first:
99 tmp |= ~0UL << size;
100 if (tmp == ~0UL) /* Are any bits zero? */
101 return result + size; /* Nope. */
102found_middle:
103 return result + ffz(tmp);
104}
Alexander van Heukelum64970b62008-03-11 16:17:19 +0100105EXPORT_SYMBOL(__find_next_zero_bit);
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200106#endif /* CONFIG_GENERIC_FIND_NEXT_BIT */
107
108#ifdef CONFIG_GENERIC_FIND_FIRST_BIT
109/*
110 * Find the first set bit in a memory region.
111 */
112unsigned long __find_first_bit(const unsigned long *addr,
113 unsigned long size)
114{
115 const unsigned long *p = addr;
116 unsigned long result = 0;
117 unsigned long tmp;
118
119 while (size & ~(BITS_PER_LONG-1)) {
120 if ((tmp = *(p++)))
121 goto found;
122 result += BITS_PER_LONG;
123 size -= BITS_PER_LONG;
124 }
125 if (!size)
126 return result;
127
128 tmp = (*p) & (~0UL >> (BITS_PER_LONG - size));
129 if (tmp == 0UL) /* Are any bits set? */
130 return result + size; /* Nope. */
131found:
132 return result + __ffs(tmp);
133}
134EXPORT_SYMBOL(__find_first_bit);
135
136/*
137 * Find the first cleared bit in a memory region.
138 */
139unsigned long __find_first_zero_bit(const unsigned long *addr,
140 unsigned long size)
141{
142 const unsigned long *p = addr;
143 unsigned long result = 0;
144 unsigned long tmp;
145
146 while (size & ~(BITS_PER_LONG-1)) {
147 if (~(tmp = *(p++)))
148 goto found;
149 result += BITS_PER_LONG;
150 size -= BITS_PER_LONG;
151 }
152 if (!size)
153 return result;
154
155 tmp = (*p) | (~0UL << size);
156 if (tmp == ~0UL) /* Are any bits zero? */
157 return result + size; /* Nope. */
158found:
159 return result + ffz(tmp);
160}
161EXPORT_SYMBOL(__find_first_zero_bit);
162#endif /* CONFIG_GENERIC_FIND_FIRST_BIT */
Akinobu Mita930ae742006-03-26 01:39:15 -0800163
164#ifdef __BIG_ENDIAN
165
166/* include/linux/byteorder does not support "unsigned long" type */
167static inline unsigned long ext2_swabp(const unsigned long * x)
168{
169#if BITS_PER_LONG == 64
170 return (unsigned long) __swab64p((u64 *) x);
171#elif BITS_PER_LONG == 32
172 return (unsigned long) __swab32p((u32 *) x);
173#else
174#error BITS_PER_LONG not defined
175#endif
176}
177
178/* include/linux/byteorder doesn't support "unsigned long" type */
179static inline unsigned long ext2_swab(const unsigned long y)
180{
181#if BITS_PER_LONG == 64
182 return (unsigned long) __swab64((u64) y);
183#elif BITS_PER_LONG == 32
184 return (unsigned long) __swab32((u32) y);
185#else
186#error BITS_PER_LONG not defined
187#endif
188}
189
190unsigned long generic_find_next_zero_le_bit(const unsigned long *addr, unsigned
191 long size, unsigned long offset)
192{
193 const unsigned long *p = addr + BITOP_WORD(offset);
194 unsigned long result = offset & ~(BITS_PER_LONG - 1);
195 unsigned long tmp;
196
197 if (offset >= size)
198 return size;
199 size -= result;
200 offset &= (BITS_PER_LONG - 1UL);
201 if (offset) {
202 tmp = ext2_swabp(p++);
203 tmp |= (~0UL >> (BITS_PER_LONG - offset));
204 if (size < BITS_PER_LONG)
205 goto found_first;
206 if (~tmp)
207 goto found_middle;
208 size -= BITS_PER_LONG;
209 result += BITS_PER_LONG;
210 }
211
212 while (size & ~(BITS_PER_LONG - 1)) {
213 if (~(tmp = *(p++)))
214 goto found_middle_swap;
215 result += BITS_PER_LONG;
216 size -= BITS_PER_LONG;
217 }
218 if (!size)
219 return result;
220 tmp = ext2_swabp(p);
221found_first:
222 tmp |= ~0UL << size;
223 if (tmp == ~0UL) /* Are any bits zero? */
224 return result + size; /* Nope. Skip ffz */
225found_middle:
226 return result + ffz(tmp);
227
228found_middle_swap:
229 return result + ffz(ext2_swab(tmp));
230}
231
232EXPORT_SYMBOL(generic_find_next_zero_le_bit);
233
Aneesh Kumar K.Vaa02ad62008-01-28 23:58:27 -0500234unsigned long generic_find_next_le_bit(const unsigned long *addr, unsigned
235 long size, unsigned long offset)
236{
237 const unsigned long *p = addr + BITOP_WORD(offset);
238 unsigned long result = offset & ~(BITS_PER_LONG - 1);
239 unsigned long tmp;
240
241 if (offset >= size)
242 return size;
243 size -= result;
244 offset &= (BITS_PER_LONG - 1UL);
245 if (offset) {
246 tmp = ext2_swabp(p++);
247 tmp &= (~0UL << offset);
248 if (size < BITS_PER_LONG)
249 goto found_first;
250 if (tmp)
251 goto found_middle;
252 size -= BITS_PER_LONG;
253 result += BITS_PER_LONG;
254 }
255
256 while (size & ~(BITS_PER_LONG - 1)) {
257 tmp = *(p++);
258 if (tmp)
259 goto found_middle_swap;
260 result += BITS_PER_LONG;
261 size -= BITS_PER_LONG;
262 }
263 if (!size)
264 return result;
265 tmp = ext2_swabp(p);
266found_first:
267 tmp &= (~0UL >> (BITS_PER_LONG - size));
268 if (tmp == 0UL) /* Are any bits set? */
269 return result + size; /* Nope. */
270found_middle:
271 return result + __ffs(tmp);
272
273found_middle_swap:
274 return result + __ffs(ext2_swab(tmp));
275}
276EXPORT_SYMBOL(generic_find_next_le_bit);
Akinobu Mita930ae742006-03-26 01:39:15 -0800277#endif /* __BIG_ENDIAN */