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 #undef find_next_zero_bit
22 * find_next_bit - find the next set bit in a memory region
23 * @addr: The address to base the search on
24 * @offset: The bitnumber to start searching at
25 * @size: The maximum size to search
27 unsigned long find_next_bit(const unsigned long *addr
, unsigned long size
,
30 const unsigned long *p
= addr
+ BITOP_WORD(offset
);
31 unsigned long result
= offset
& ~(BITS_PER_LONG
-1);
37 offset
%= BITS_PER_LONG
;
40 tmp
&= (~0UL << offset
);
41 if (size
< BITS_PER_LONG
)
45 size
-= BITS_PER_LONG
;
46 result
+= BITS_PER_LONG
;
48 while (size
& ~(BITS_PER_LONG
-1)) {
51 result
+= BITS_PER_LONG
;
52 size
-= BITS_PER_LONG
;
59 tmp
&= (~0UL >> (BITS_PER_LONG
- size
));
60 if (tmp
== 0UL) /* Are any bits set? */
61 return result
+ size
; /* Nope. */
63 return result
+ __ffs(tmp
);
66 EXPORT_SYMBOL(find_next_bit
);
69 * This implementation of find_{first,next}_zero_bit was stolen from
70 * Linus' asm-alpha/bitops.h.
72 unsigned long find_next_zero_bit(const unsigned long *addr
, unsigned long size
,
75 const unsigned long *p
= addr
+ BITOP_WORD(offset
);
76 unsigned long result
= offset
& ~(BITS_PER_LONG
-1);
82 offset
%= BITS_PER_LONG
;
85 tmp
|= ~0UL >> (BITS_PER_LONG
- offset
);
86 if (size
< BITS_PER_LONG
)
90 size
-= BITS_PER_LONG
;
91 result
+= BITS_PER_LONG
;
93 while (size
& ~(BITS_PER_LONG
-1)) {
96 result
+= BITS_PER_LONG
;
97 size
-= BITS_PER_LONG
;
105 if (tmp
== ~0UL) /* Are any bits zero? */
106 return result
+ size
; /* Nope. */
108 return result
+ ffz(tmp
);
111 EXPORT_SYMBOL(find_next_zero_bit
);
115 /* include/linux/byteorder does not support "unsigned long" type */
116 static inline unsigned long ext2_swabp(const unsigned long * x
)
118 #if BITS_PER_LONG == 64
119 return (unsigned long) __swab64p((u64
*) x
);
120 #elif BITS_PER_LONG == 32
121 return (unsigned long) __swab32p((u32
*) x
);
123 #error BITS_PER_LONG not defined
127 /* include/linux/byteorder doesn't support "unsigned long" type */
128 static inline unsigned long ext2_swab(const unsigned long y
)
130 #if BITS_PER_LONG == 64
131 return (unsigned long) __swab64((u64
) y
);
132 #elif BITS_PER_LONG == 32
133 return (unsigned long) __swab32((u32
) y
);
135 #error BITS_PER_LONG not defined
139 unsigned long generic_find_next_zero_le_bit(const unsigned long *addr
, unsigned
140 long size
, unsigned long offset
)
142 const unsigned long *p
= addr
+ BITOP_WORD(offset
);
143 unsigned long result
= offset
& ~(BITS_PER_LONG
- 1);
149 offset
&= (BITS_PER_LONG
- 1UL);
151 tmp
= ext2_swabp(p
++);
152 tmp
|= (~0UL >> (BITS_PER_LONG
- offset
));
153 if (size
< BITS_PER_LONG
)
157 size
-= BITS_PER_LONG
;
158 result
+= BITS_PER_LONG
;
161 while (size
& ~(BITS_PER_LONG
- 1)) {
163 goto found_middle_swap
;
164 result
+= BITS_PER_LONG
;
165 size
-= BITS_PER_LONG
;
172 if (tmp
== ~0UL) /* Are any bits zero? */
173 return result
+ size
; /* Nope. Skip ffz */
175 return result
+ ffz(tmp
);
178 return result
+ ffz(ext2_swab(tmp
));
181 EXPORT_SYMBOL(generic_find_next_zero_le_bit
);
183 unsigned long generic_find_next_le_bit(const unsigned long *addr
, unsigned
184 long size
, unsigned long offset
)
186 const unsigned long *p
= addr
+ BITOP_WORD(offset
);
187 unsigned long result
= offset
& ~(BITS_PER_LONG
- 1);
193 offset
&= (BITS_PER_LONG
- 1UL);
195 tmp
= ext2_swabp(p
++);
196 tmp
&= (~0UL << offset
);
197 if (size
< BITS_PER_LONG
)
201 size
-= BITS_PER_LONG
;
202 result
+= BITS_PER_LONG
;
205 while (size
& ~(BITS_PER_LONG
- 1)) {
208 goto found_middle_swap
;
209 result
+= BITS_PER_LONG
;
210 size
-= BITS_PER_LONG
;
216 tmp
&= (~0UL >> (BITS_PER_LONG
- size
));
217 if (tmp
== 0UL) /* Are any bits set? */
218 return result
+ size
; /* Nope. */
220 return result
+ __ffs(tmp
);
223 return result
+ __ffs(ext2_swab(tmp
));
225 EXPORT_SYMBOL(generic_find_next_le_bit
);
226 #endif /* __BIG_ENDIAN */