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)
19 #ifdef CONFIG_GENERIC_FIND_NEXT_BIT
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 * This implementation of find_{first,next}_zero_bit was stolen from
65 * Linus' asm-alpha/bitops.h.
67 unsigned long find_next_zero_bit(const unsigned long *addr
, unsigned long size
,
70 const unsigned long *p
= addr
+ BITOP_WORD(offset
);
71 unsigned long result
= offset
& ~(BITS_PER_LONG
-1);
77 offset
%= BITS_PER_LONG
;
80 tmp
|= ~0UL >> (BITS_PER_LONG
- offset
);
81 if (size
< BITS_PER_LONG
)
85 size
-= BITS_PER_LONG
;
86 result
+= BITS_PER_LONG
;
88 while (size
& ~(BITS_PER_LONG
-1)) {
91 result
+= BITS_PER_LONG
;
92 size
-= BITS_PER_LONG
;
100 if (tmp
== ~0UL) /* Are any bits zero? */
101 return result
+ size
; /* Nope. */
103 return result
+ ffz(tmp
);
105 EXPORT_SYMBOL(find_next_zero_bit
);
106 #endif /* CONFIG_GENERIC_FIND_NEXT_BIT */
108 #ifdef CONFIG_GENERIC_FIND_FIRST_BIT
110 * Find the first set bit in a memory region.
112 unsigned long find_first_bit(const unsigned long *addr
, unsigned long size
)
114 const unsigned long *p
= addr
;
115 unsigned long result
= 0;
118 while (size
& ~(BITS_PER_LONG
-1)) {
121 result
+= BITS_PER_LONG
;
122 size
-= BITS_PER_LONG
;
127 tmp
= (*p
) & (~0UL >> (BITS_PER_LONG
- size
));
128 if (tmp
== 0UL) /* Are any bits set? */
129 return result
+ size
; /* Nope. */
131 return result
+ __ffs(tmp
);
133 EXPORT_SYMBOL(find_first_bit
);
136 * Find the first cleared bit in a memory region.
138 unsigned long find_first_zero_bit(const unsigned long *addr
, unsigned long size
)
140 const unsigned long *p
= addr
;
141 unsigned long result
= 0;
144 while (size
& ~(BITS_PER_LONG
-1)) {
147 result
+= BITS_PER_LONG
;
148 size
-= BITS_PER_LONG
;
153 tmp
= (*p
) | (~0UL << size
);
154 if (tmp
== ~0UL) /* Are any bits zero? */
155 return result
+ size
; /* Nope. */
157 return result
+ ffz(tmp
);
159 EXPORT_SYMBOL(find_first_zero_bit
);
160 #endif /* CONFIG_GENERIC_FIND_FIRST_BIT */
163 #ifdef CONFIG_GENERIC_FIND_BIT_LE
165 /* include/linux/byteorder does not support "unsigned long" type */
166 static inline unsigned long ext2_swabp(const unsigned long * x
)
168 #if BITS_PER_LONG == 64
169 return (unsigned long) __swab64p((u64
*) x
);
170 #elif BITS_PER_LONG == 32
171 return (unsigned long) __swab32p((u32
*) x
);
173 #error BITS_PER_LONG not defined
177 /* include/linux/byteorder doesn't support "unsigned long" type */
178 static inline unsigned long ext2_swab(const unsigned long y
)
180 #if BITS_PER_LONG == 64
181 return (unsigned long) __swab64((u64
) y
);
182 #elif BITS_PER_LONG == 32
183 return (unsigned long) __swab32((u32
) y
);
185 #error BITS_PER_LONG not defined
189 unsigned long find_next_zero_bit_le(const void *addr
, unsigned
190 long size
, unsigned long offset
)
192 const unsigned long *p
= addr
;
193 unsigned long result
= offset
& ~(BITS_PER_LONG
- 1);
198 p
+= BITOP_WORD(offset
);
200 offset
&= (BITS_PER_LONG
- 1UL);
202 tmp
= ext2_swabp(p
++);
203 tmp
|= (~0UL >> (BITS_PER_LONG
- offset
));
204 if (size
< BITS_PER_LONG
)
208 size
-= BITS_PER_LONG
;
209 result
+= BITS_PER_LONG
;
212 while (size
& ~(BITS_PER_LONG
- 1)) {
214 goto found_middle_swap
;
215 result
+= BITS_PER_LONG
;
216 size
-= BITS_PER_LONG
;
223 if (tmp
== ~0UL) /* Are any bits zero? */
224 return result
+ size
; /* Nope. Skip ffz */
226 return result
+ ffz(tmp
);
229 return result
+ ffz(ext2_swab(tmp
));
231 EXPORT_SYMBOL(find_next_zero_bit_le
);
233 unsigned long find_next_bit_le(const void *addr
, unsigned
234 long size
, unsigned long offset
)
236 const unsigned long *p
= addr
;
237 unsigned long result
= offset
& ~(BITS_PER_LONG
- 1);
242 p
+= BITOP_WORD(offset
);
244 offset
&= (BITS_PER_LONG
- 1UL);
246 tmp
= ext2_swabp(p
++);
247 tmp
&= (~0UL << offset
);
248 if (size
< BITS_PER_LONG
)
252 size
-= BITS_PER_LONG
;
253 result
+= BITS_PER_LONG
;
256 while (size
& ~(BITS_PER_LONG
- 1)) {
259 goto found_middle_swap
;
260 result
+= BITS_PER_LONG
;
261 size
-= BITS_PER_LONG
;
267 tmp
&= (~0UL >> (BITS_PER_LONG
- size
));
268 if (tmp
== 0UL) /* Are any bits set? */
269 return result
+ size
; /* Nope. */
271 return result
+ __ffs(tmp
);
274 return result
+ __ffs(ext2_swab(tmp
));
276 EXPORT_SYMBOL(find_next_bit_le
);
278 #endif /* CONFIG_GENERIC_FIND_BIT_LE */
279 #endif /* __BIG_ENDIAN */