1 #include <linux/types.h>
2 #include <linux/module.h>
3 #include <asm/byteorder.h>
4 #include <asm/bitops.h>
7 * find_next_bit - find the next set bit in a memory region
8 * @addr: The address to base the search on
9 * @offset: The bitnumber to start searching at
10 * @size: The maximum size to search
12 unsigned long find_next_bit(const unsigned long *addr
, unsigned long size
,
15 const unsigned long *p
= addr
+ BITOP_WORD(offset
);
16 unsigned long result
= offset
& ~(BITS_PER_LONG
-1);
22 offset
%= BITS_PER_LONG
;
25 tmp
&= (~0UL << offset
);
26 if (size
< BITS_PER_LONG
)
30 size
-= BITS_PER_LONG
;
31 result
+= BITS_PER_LONG
;
33 while (size
& ~(BITS_PER_LONG
-1)) {
36 result
+= BITS_PER_LONG
;
37 size
-= BITS_PER_LONG
;
44 tmp
&= (~0UL >> (BITS_PER_LONG
- size
));
45 if (tmp
== 0UL) /* Are any bits set? */
46 return result
+ size
; /* Nope. */
48 return result
+ __ffs(tmp
);
50 EXPORT_SYMBOL(find_next_bit
);
53 * This implementation of find_{first,next}_zero_bit was stolen from
54 * Linus' asm-alpha/bitops.h.
56 unsigned long find_next_zero_bit(const unsigned long *addr
, unsigned long size
,
59 const unsigned long *p
= addr
+ BITOP_WORD(offset
);
60 unsigned long result
= offset
& ~(BITS_PER_LONG
-1);
66 offset
%= BITS_PER_LONG
;
69 tmp
|= ~0UL >> (BITS_PER_LONG
- offset
);
70 if (size
< BITS_PER_LONG
)
74 size
-= BITS_PER_LONG
;
75 result
+= BITS_PER_LONG
;
77 while (size
& ~(BITS_PER_LONG
-1)) {
80 result
+= BITS_PER_LONG
;
81 size
-= BITS_PER_LONG
;
89 if (tmp
== ~0UL) /* Are any bits zero? */
90 return result
+ size
; /* Nope. */
92 return result
+ ffz(tmp
);
94 EXPORT_SYMBOL(find_next_zero_bit
);
96 static inline unsigned int ext2_ilog2(unsigned int x
)
100 asm("cntlzw %0,%1": "=r"(lz
):"r"(x
));
104 static inline unsigned int ext2_ffz(unsigned int x
)
109 rc
= ext2_ilog2(x
& -x
);
113 unsigned long find_next_zero_le_bit(const unsigned long *addr
,
114 unsigned long size
, unsigned long offset
)
116 const unsigned int *p
= ((const unsigned int *)addr
) + (offset
>> 5);
117 unsigned int result
= offset
& ~31;
125 tmp
= cpu_to_le32p(p
++);
126 tmp
|= ~0U >> (32 - offset
); /* bug or feature ? */
135 if ((tmp
= cpu_to_le32p(p
++)) != ~0)
142 tmp
= cpu_to_le32p(p
);
145 if (tmp
== ~0) /* Are any bits zero? */
146 return result
+ size
; /* Nope. */
148 return result
+ ext2_ffz(tmp
);
150 EXPORT_SYMBOL(find_next_zero_le_bit
);