aboutsummaryrefslogtreecommitdiffstats
path: root/lib/find_next_bit.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/find_next_bit.c')
-rw-r--r--lib/find_next_bit.c69
1 files changed, 60 insertions, 9 deletions
diff --git a/lib/find_next_bit.c b/lib/find_next_bit.c
index 78ccd73a8841..24c59ded47a0 100644
--- a/lib/find_next_bit.c
+++ b/lib/find_next_bit.c
@@ -16,14 +16,12 @@
16 16
17#define BITOP_WORD(nr) ((nr) / BITS_PER_LONG) 17#define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
18 18
19/** 19#ifdef CONFIG_GENERIC_FIND_NEXT_BIT
20 * find_next_bit - find the next set bit in a memory region 20/*
21 * @addr: The address to base the search on 21 * Find the next set bit in a memory region.
22 * @offset: The bitnumber to start searching at
23 * @size: The maximum size to search
24 */ 22 */
25unsigned long find_next_bit(const unsigned long *addr, unsigned long size, 23unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
26 unsigned long offset) 24 unsigned long offset)
27{ 25{
28 const unsigned long *p = addr + BITOP_WORD(offset); 26 const unsigned long *p = addr + BITOP_WORD(offset);
29 unsigned long result = offset & ~(BITS_PER_LONG-1); 27 unsigned long result = offset & ~(BITS_PER_LONG-1);
@@ -60,7 +58,6 @@ found_first:
60found_middle: 58found_middle:
61 return result + __ffs(tmp); 59 return result + __ffs(tmp);
62} 60}
63
64EXPORT_SYMBOL(find_next_bit); 61EXPORT_SYMBOL(find_next_bit);
65 62
66/* 63/*
@@ -68,7 +65,7 @@ EXPORT_SYMBOL(find_next_bit);
68 * Linus' asm-alpha/bitops.h. 65 * Linus' asm-alpha/bitops.h.
69 */ 66 */
70unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size, 67unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
71 unsigned long offset) 68 unsigned long offset)
72{ 69{
73 const unsigned long *p = addr + BITOP_WORD(offset); 70 const unsigned long *p = addr + BITOP_WORD(offset);
74 unsigned long result = offset & ~(BITS_PER_LONG-1); 71 unsigned long result = offset & ~(BITS_PER_LONG-1);
@@ -105,8 +102,62 @@ found_first:
105found_middle: 102found_middle:
106 return result + ffz(tmp); 103 return result + ffz(tmp);
107} 104}
108
109EXPORT_SYMBOL(find_next_zero_bit); 105EXPORT_SYMBOL(find_next_zero_bit);
106#endif /* CONFIG_GENERIC_FIND_NEXT_BIT */
107
108#ifdef CONFIG_GENERIC_FIND_FIRST_BIT
109/*
110 * Find the first set bit in a memory region.
111 */
112unsigned long find_first_bit(const unsigned long *addr, unsigned long size)
113{
114 const unsigned long *p = addr;
115 unsigned long result = 0;
116 unsigned long tmp;
117
118 while (size & ~(BITS_PER_LONG-1)) {
119 if ((tmp = *(p++)))
120 goto found;
121 result += BITS_PER_LONG;
122 size -= BITS_PER_LONG;
123 }
124 if (!size)
125 return result;
126
127 tmp = (*p) & (~0UL >> (BITS_PER_LONG - size));
128 if (tmp == 0UL) /* Are any bits set? */
129 return result + size; /* Nope. */
130found:
131 return result + __ffs(tmp);
132}
133EXPORT_SYMBOL(find_first_bit);
134
135/*
136 * Find the first cleared bit in a memory region.
137 */
138unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size)
139{
140 const unsigned long *p = addr;
141 unsigned long result = 0;
142 unsigned long tmp;
143
144 while (size & ~(BITS_PER_LONG-1)) {
145 if (~(tmp = *(p++)))
146 goto found;
147 result += BITS_PER_LONG;
148 size -= BITS_PER_LONG;
149 }
150 if (!size)
151 return result;
152
153 tmp = (*p) | (~0UL << size);
154 if (tmp == ~0UL) /* Are any bits zero? */
155 return result + size; /* Nope. */
156found:
157 return result + ffz(tmp);
158}
159EXPORT_SYMBOL(find_first_zero_bit);
160#endif /* CONFIG_GENERIC_FIND_FIRST_BIT */
110 161
111#ifdef __BIG_ENDIAN 162#ifdef __BIG_ENDIAN
112 163