1 /* find_next_bit.c: fallback find next bit implementation
3 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #include <linux/bitops.h>
13 #include <linux/module.h>
14 #include <asm/types.h>
15 #include <asm/byteorder.h>
17 #define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
21 * Find the next set bit in a memory region.
23 unsigned long find_next_bit(const unsigned long *addr
, unsigned long size
,
26 const unsigned long *p
= addr
+ BITOP_WORD(offset
);
27 unsigned long result
= offset
& ~(BITS_PER_LONG
-1);
33 offset
%= BITS_PER_LONG
;
36 tmp
&= (~0UL << offset
);
37 if (size
< BITS_PER_LONG
)
41 size
-= BITS_PER_LONG
;
42 result
+= BITS_PER_LONG
;
44 while (size
& ~(BITS_PER_LONG
-1)) {
47 result
+= BITS_PER_LONG
;
48 size
-= BITS_PER_LONG
;
55 tmp
&= (~0UL >> (BITS_PER_LONG
- size
));
56 if (tmp
== 0UL) /* Are any bits set? */
57 return result
+ size
; /* Nope. */
59 return result
+ __ffs(tmp
);
61 EXPORT_SYMBOL(find_next_bit
);
64 #ifndef find_next_zero_bit
66 * This implementation of find_{first,next}_zero_bit was stolen from
67 * Linus' asm-alpha/bitops.h.
69 unsigned long find_next_zero_bit(const unsigned long *addr
, unsigned long size
,
72 const unsigned long *p
= addr
+ BITOP_WORD(offset
);
73 unsigned long result
= offset
& ~(BITS_PER_LONG
-1);
79 offset
%= BITS_PER_LONG
;
82 tmp
|= ~0UL >> (BITS_PER_LONG
- offset
);
83 if (size
< BITS_PER_LONG
)
87 size
-= BITS_PER_LONG
;
88 result
+= BITS_PER_LONG
;
90 while (size
& ~(BITS_PER_LONG
-1)) {
93 result
+= BITS_PER_LONG
;
94 size
-= BITS_PER_LONG
;
102 if (tmp
== ~0UL) /* Are any bits zero? */
103 return result
+ size
; /* Nope. */
105 return result
+ ffz(tmp
);
107 EXPORT_SYMBOL(find_next_zero_bit
);
110 #ifndef find_first_bit
112 * Find the first set bit in a memory region.
114 unsigned long find_first_bit(const unsigned long *addr
, unsigned long size
)
116 const unsigned long *p
= addr
;
117 unsigned long result
= 0;
120 while (size
& ~(BITS_PER_LONG
-1)) {
123 result
+= BITS_PER_LONG
;
124 size
-= BITS_PER_LONG
;
129 tmp
= (*p
) & (~0UL >> (BITS_PER_LONG
- size
));
130 if (tmp
== 0UL) /* Are any bits set? */
131 return result
+ size
; /* Nope. */
133 return result
+ __ffs(tmp
);
135 EXPORT_SYMBOL(find_first_bit
);
138 #ifndef find_first_zero_bit
140 * Find the first cleared bit in a memory region.
142 unsigned long find_first_zero_bit(const unsigned long *addr
, unsigned long size
)
144 const unsigned long *p
= addr
;
145 unsigned long result
= 0;
148 while (size
& ~(BITS_PER_LONG
-1)) {
151 result
+= BITS_PER_LONG
;
152 size
-= BITS_PER_LONG
;
157 tmp
= (*p
) | (~0UL << size
);
158 if (tmp
== ~0UL) /* Are any bits zero? */
159 return result
+ size
; /* Nope. */
161 return result
+ ffz(tmp
);
163 EXPORT_SYMBOL(find_first_zero_bit
);
168 /* include/linux/byteorder does not support "unsigned long" type */
169 static inline unsigned long ext2_swabp(const unsigned long * x
)
171 #if BITS_PER_LONG == 64
172 return (unsigned long) __swab64p((u64
*) x
);
173 #elif BITS_PER_LONG == 32
174 return (unsigned long) __swab32p((u32
*) x
);
176 #error BITS_PER_LONG not defined
180 /* include/linux/byteorder doesn't support "unsigned long" type */
181 static inline unsigned long ext2_swab(const unsigned long y
)
183 #if BITS_PER_LONG == 64
184 return (unsigned long) __swab64((u64
) y
);
185 #elif BITS_PER_LONG == 32
186 return (unsigned long) __swab32((u32
) y
);
188 #error BITS_PER_LONG not defined
192 #ifndef find_next_zero_bit_le
193 unsigned long find_next_zero_bit_le(const void *addr
, unsigned
194 long size
, unsigned long offset
)
196 const unsigned long *p
= addr
;
197 unsigned long result
= offset
& ~(BITS_PER_LONG
- 1);
202 p
+= BITOP_WORD(offset
);
204 offset
&= (BITS_PER_LONG
- 1UL);
206 tmp
= ext2_swabp(p
++);
207 tmp
|= (~0UL >> (BITS_PER_LONG
- offset
));
208 if (size
< BITS_PER_LONG
)
212 size
-= BITS_PER_LONG
;
213 result
+= BITS_PER_LONG
;
216 while (size
& ~(BITS_PER_LONG
- 1)) {
218 goto found_middle_swap
;
219 result
+= BITS_PER_LONG
;
220 size
-= BITS_PER_LONG
;
227 if (tmp
== ~0UL) /* Are any bits zero? */
228 return result
+ size
; /* Nope. Skip ffz */
230 return result
+ ffz(tmp
);
233 return result
+ ffz(ext2_swab(tmp
));
235 EXPORT_SYMBOL(find_next_zero_bit_le
);
238 #ifndef find_next_bit_le
239 unsigned long find_next_bit_le(const void *addr
, unsigned
240 long size
, unsigned long offset
)
242 const unsigned long *p
= addr
;
243 unsigned long result
= offset
& ~(BITS_PER_LONG
- 1);
248 p
+= BITOP_WORD(offset
);
250 offset
&= (BITS_PER_LONG
- 1UL);
252 tmp
= ext2_swabp(p
++);
253 tmp
&= (~0UL << offset
);
254 if (size
< BITS_PER_LONG
)
258 size
-= BITS_PER_LONG
;
259 result
+= BITS_PER_LONG
;
262 while (size
& ~(BITS_PER_LONG
- 1)) {
265 goto found_middle_swap
;
266 result
+= BITS_PER_LONG
;
267 size
-= BITS_PER_LONG
;
273 tmp
&= (~0UL >> (BITS_PER_LONG
- size
));
274 if (tmp
== 0UL) /* Are any bits set? */
275 return result
+ size
; /* Nope. */
277 return result
+ __ffs(tmp
);
280 return result
+ __ffs(ext2_swab(tmp
));
282 EXPORT_SYMBOL(find_next_bit_le
);
285 #endif /* __BIG_ENDIAN */