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)
20 * Find the next set bit in a memory region.
22 unsigned long __find_next_bit(const unsigned long *addr
,
23 unsigned long size
, unsigned long offset
)
25 const unsigned long *p
= addr
+ BITOP_WORD(offset
);
26 unsigned long result
= offset
& ~(BITS_PER_LONG
-1);
32 offset
%= BITS_PER_LONG
;
35 tmp
&= (~0UL << offset
);
36 if (size
< BITS_PER_LONG
)
40 size
-= BITS_PER_LONG
;
41 result
+= BITS_PER_LONG
;
43 while (size
& ~(BITS_PER_LONG
-1)) {
46 result
+= BITS_PER_LONG
;
47 size
-= BITS_PER_LONG
;
54 tmp
&= (~0UL >> (BITS_PER_LONG
- size
));
55 if (tmp
== 0UL) /* Are any bits set? */
56 return result
+ size
; /* Nope. */
58 return result
+ __ffs(tmp
);
60 EXPORT_SYMBOL(__find_next_bit
);
63 * This implementation of find_{first,next}_zero_bit was stolen from
64 * Linus' asm-alpha/bitops.h.
66 unsigned long __find_next_zero_bit(const unsigned long *addr
,
67 unsigned long size
, unsigned long offset
)
69 const unsigned long *p
= addr
+ BITOP_WORD(offset
);
70 unsigned long result
= offset
& ~(BITS_PER_LONG
-1);
76 offset
%= BITS_PER_LONG
;
79 tmp
|= ~0UL >> (BITS_PER_LONG
- offset
);
80 if (size
< BITS_PER_LONG
)
84 size
-= BITS_PER_LONG
;
85 result
+= BITS_PER_LONG
;
87 while (size
& ~(BITS_PER_LONG
-1)) {
90 result
+= BITS_PER_LONG
;
91 size
-= BITS_PER_LONG
;
99 if (tmp
== ~0UL) /* Are any bits zero? */
100 return result
+ size
; /* Nope. */
102 return result
+ ffz(tmp
);
104 EXPORT_SYMBOL(__find_next_zero_bit
);
108 /* include/linux/byteorder does not support "unsigned long" type */
109 static inline unsigned long ext2_swabp(const unsigned long * x
)
111 #if BITS_PER_LONG == 64
112 return (unsigned long) __swab64p((u64
*) x
);
113 #elif BITS_PER_LONG == 32
114 return (unsigned long) __swab32p((u32
*) x
);
116 #error BITS_PER_LONG not defined
120 /* include/linux/byteorder doesn't support "unsigned long" type */
121 static inline unsigned long ext2_swab(const unsigned long y
)
123 #if BITS_PER_LONG == 64
124 return (unsigned long) __swab64((u64
) y
);
125 #elif BITS_PER_LONG == 32
126 return (unsigned long) __swab32((u32
) y
);
128 #error BITS_PER_LONG not defined
132 unsigned long generic_find_next_zero_le_bit(const unsigned long *addr
, unsigned
133 long size
, unsigned long offset
)
135 const unsigned long *p
= addr
+ BITOP_WORD(offset
);
136 unsigned long result
= offset
& ~(BITS_PER_LONG
- 1);
142 offset
&= (BITS_PER_LONG
- 1UL);
144 tmp
= ext2_swabp(p
++);
145 tmp
|= (~0UL >> (BITS_PER_LONG
- offset
));
146 if (size
< BITS_PER_LONG
)
150 size
-= BITS_PER_LONG
;
151 result
+= BITS_PER_LONG
;
154 while (size
& ~(BITS_PER_LONG
- 1)) {
156 goto found_middle_swap
;
157 result
+= BITS_PER_LONG
;
158 size
-= BITS_PER_LONG
;
165 if (tmp
== ~0UL) /* Are any bits zero? */
166 return result
+ size
; /* Nope. Skip ffz */
168 return result
+ ffz(tmp
);
171 return result
+ ffz(ext2_swab(tmp
));
174 EXPORT_SYMBOL(generic_find_next_zero_le_bit
);
176 unsigned long generic_find_next_le_bit(const unsigned long *addr
, unsigned
177 long size
, unsigned long offset
)
179 const unsigned long *p
= addr
+ BITOP_WORD(offset
);
180 unsigned long result
= offset
& ~(BITS_PER_LONG
- 1);
186 offset
&= (BITS_PER_LONG
- 1UL);
188 tmp
= ext2_swabp(p
++);
189 tmp
&= (~0UL << offset
);
190 if (size
< BITS_PER_LONG
)
194 size
-= BITS_PER_LONG
;
195 result
+= BITS_PER_LONG
;
198 while (size
& ~(BITS_PER_LONG
- 1)) {
201 goto found_middle_swap
;
202 result
+= BITS_PER_LONG
;
203 size
-= BITS_PER_LONG
;
209 tmp
&= (~0UL >> (BITS_PER_LONG
- size
));
210 if (tmp
== 0UL) /* Are any bits set? */
211 return result
+ size
; /* Nope. */
213 return result
+ __ffs(tmp
);
216 return result
+ __ffs(ext2_swab(tmp
));
218 EXPORT_SYMBOL(generic_find_next_le_bit
);
219 #endif /* __BIG_ENDIAN */