blob: c093ba988003ab5068fa21eee579ea252ffc4756 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* find_next_bit.c: fallback find next bit implementation
2 *
3 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
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.
10 */
11
12#include <linux/bitops.h>
David Howells40234402006-01-08 01:01:19 -080013#include <linux/module.h>
Akinobu Mitac7f612c2006-03-26 01:39:11 -080014#include <asm/types.h>
Akinobu Mita930ae742006-03-26 01:39:15 -080015#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Akinobu Mitac7f612c2006-03-26 01:39:11 -080017#define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
18
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +020019#ifdef CONFIG_GENERIC_FIND_NEXT_BIT
Alexander van Heukelum64970b62008-03-11 16:17:19 +010020/*
21 * Find the next set bit in a memory region.
Akinobu Mitac7f612c2006-03-26 01:39:11 -080022 */
Thomas Gleixnerfee4b192008-04-29 12:01:02 +020023unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
24 unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
Akinobu Mitac7f612c2006-03-26 01:39:11 -080026 const unsigned long *p = addr + BITOP_WORD(offset);
27 unsigned long result = offset & ~(BITS_PER_LONG-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 unsigned long tmp;
29
Akinobu Mitac7f612c2006-03-26 01:39:11 -080030 if (offset >= size)
31 return size;
32 size -= result;
33 offset %= BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 if (offset) {
Akinobu Mitac7f612c2006-03-26 01:39:11 -080035 tmp = *(p++);
36 tmp &= (~0UL << offset);
37 if (size < BITS_PER_LONG)
38 goto found_first;
39 if (tmp)
40 goto found_middle;
41 size -= BITS_PER_LONG;
42 result += BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 }
Akinobu Mitac7f612c2006-03-26 01:39:11 -080044 while (size & ~(BITS_PER_LONG-1)) {
45 if ((tmp = *(p++)))
46 goto found_middle;
47 result += BITS_PER_LONG;
48 size -= BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 }
Akinobu Mitac7f612c2006-03-26 01:39:11 -080050 if (!size)
51 return result;
52 tmp = *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Akinobu Mitac7f612c2006-03-26 01:39:11 -080054found_first:
55 tmp &= (~0UL >> (BITS_PER_LONG - size));
56 if (tmp == 0UL) /* Are any bits set? */
57 return result + size; /* Nope. */
58found_middle:
59 return result + __ffs(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
Thomas Gleixnerfee4b192008-04-29 12:01:02 +020061EXPORT_SYMBOL(find_next_bit);
Akinobu Mitac7f612c2006-03-26 01:39:11 -080062
63/*
64 * This implementation of find_{first,next}_zero_bit was stolen from
65 * Linus' asm-alpha/bitops.h.
66 */
Thomas Gleixnerfee4b192008-04-29 12:01:02 +020067unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
68 unsigned long offset)
Akinobu Mitac7f612c2006-03-26 01:39:11 -080069{
70 const unsigned long *p = addr + BITOP_WORD(offset);
71 unsigned long result = offset & ~(BITS_PER_LONG-1);
72 unsigned long tmp;
73
74 if (offset >= size)
75 return size;
76 size -= result;
77 offset %= BITS_PER_LONG;
78 if (offset) {
79 tmp = *(p++);
80 tmp |= ~0UL >> (BITS_PER_LONG - offset);
81 if (size < BITS_PER_LONG)
82 goto found_first;
83 if (~tmp)
84 goto found_middle;
85 size -= BITS_PER_LONG;
86 result += BITS_PER_LONG;
87 }
88 while (size & ~(BITS_PER_LONG-1)) {
89 if (~(tmp = *(p++)))
90 goto found_middle;
91 result += BITS_PER_LONG;
92 size -= BITS_PER_LONG;
93 }
94 if (!size)
95 return result;
96 tmp = *p;
97
98found_first:
99 tmp |= ~0UL << size;
100 if (tmp == ~0UL) /* Are any bits zero? */
101 return result + size; /* Nope. */
102found_middle:
103 return result + ffz(tmp);
104}
Thomas Gleixnerfee4b192008-04-29 12:01:02 +0200105EXPORT_SYMBOL(find_next_zero_bit);
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200106#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 */
Thomas Gleixnerfee4b192008-04-29 12:01:02 +0200112unsigned long find_first_bit(const unsigned long *addr, unsigned long size)
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200113{
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}
Thomas Gleixnerfee4b192008-04-29 12:01:02 +0200133EXPORT_SYMBOL(find_first_bit);
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200134
135/*
136 * Find the first cleared bit in a memory region.
137 */
Thomas Gleixnerfee4b192008-04-29 12:01:02 +0200138unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size)
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200139{
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}
Thomas Gleixnerfee4b192008-04-29 12:01:02 +0200159EXPORT_SYMBOL(find_first_zero_bit);
Alexander van Heukelum77b9bd92008-04-01 11:46:19 +0200160#endif /* CONFIG_GENERIC_FIND_FIRST_BIT */
Akinobu Mita930ae742006-03-26 01:39:15 -0800161
162#ifdef __BIG_ENDIAN
163
164/* include/linux/byteorder does not support "unsigned long" type */
165static inline unsigned long ext2_swabp(const unsigned long * x)
166{
167#if BITS_PER_LONG == 64
168 return (unsigned long) __swab64p((u64 *) x);
169#elif BITS_PER_LONG == 32
170 return (unsigned long) __swab32p((u32 *) x);
171#else
172#error BITS_PER_LONG not defined
173#endif
174}
175
176/* include/linux/byteorder doesn't support "unsigned long" type */
177static inline unsigned long ext2_swab(const unsigned long y)
178{
179#if BITS_PER_LONG == 64
180 return (unsigned long) __swab64((u64) y);
181#elif BITS_PER_LONG == 32
182 return (unsigned long) __swab32((u32) y);
183#else
184#error BITS_PER_LONG not defined
185#endif
186}
187
Akinobu Mitac4945b92011-03-23 16:41:47 -0700188unsigned long find_next_zero_bit_le(const unsigned long *addr, unsigned
Akinobu Mita930ae742006-03-26 01:39:15 -0800189 long size, unsigned long offset)
190{
191 const unsigned long *p = addr + BITOP_WORD(offset);
192 unsigned long result = offset & ~(BITS_PER_LONG - 1);
193 unsigned long tmp;
194
195 if (offset >= size)
196 return size;
197 size -= result;
198 offset &= (BITS_PER_LONG - 1UL);
199 if (offset) {
200 tmp = ext2_swabp(p++);
201 tmp |= (~0UL >> (BITS_PER_LONG - offset));
202 if (size < BITS_PER_LONG)
203 goto found_first;
204 if (~tmp)
205 goto found_middle;
206 size -= BITS_PER_LONG;
207 result += BITS_PER_LONG;
208 }
209
210 while (size & ~(BITS_PER_LONG - 1)) {
211 if (~(tmp = *(p++)))
212 goto found_middle_swap;
213 result += BITS_PER_LONG;
214 size -= BITS_PER_LONG;
215 }
216 if (!size)
217 return result;
218 tmp = ext2_swabp(p);
219found_first:
220 tmp |= ~0UL << size;
221 if (tmp == ~0UL) /* Are any bits zero? */
222 return result + size; /* Nope. Skip ffz */
223found_middle:
224 return result + ffz(tmp);
225
226found_middle_swap:
227 return result + ffz(ext2_swab(tmp));
228}
Akinobu Mitac4945b92011-03-23 16:41:47 -0700229EXPORT_SYMBOL(find_next_zero_bit_le);
Akinobu Mita930ae742006-03-26 01:39:15 -0800230
Akinobu Mitac4945b92011-03-23 16:41:47 -0700231unsigned long find_next_bit_le(const unsigned long *addr, unsigned
Aneesh Kumar K.Vaa02ad62008-01-28 23:58:27 -0500232 long size, unsigned long offset)
233{
234 const unsigned long *p = addr + BITOP_WORD(offset);
235 unsigned long result = offset & ~(BITS_PER_LONG - 1);
236 unsigned long tmp;
237
238 if (offset >= size)
239 return size;
240 size -= result;
241 offset &= (BITS_PER_LONG - 1UL);
242 if (offset) {
243 tmp = ext2_swabp(p++);
244 tmp &= (~0UL << offset);
245 if (size < BITS_PER_LONG)
246 goto found_first;
247 if (tmp)
248 goto found_middle;
249 size -= BITS_PER_LONG;
250 result += BITS_PER_LONG;
251 }
252
253 while (size & ~(BITS_PER_LONG - 1)) {
254 tmp = *(p++);
255 if (tmp)
256 goto found_middle_swap;
257 result += BITS_PER_LONG;
258 size -= BITS_PER_LONG;
259 }
260 if (!size)
261 return result;
262 tmp = ext2_swabp(p);
263found_first:
264 tmp &= (~0UL >> (BITS_PER_LONG - size));
265 if (tmp == 0UL) /* Are any bits set? */
266 return result + size; /* Nope. */
267found_middle:
268 return result + __ffs(tmp);
269
270found_middle_swap:
271 return result + __ffs(ext2_swab(tmp));
272}
Akinobu Mitac4945b92011-03-23 16:41:47 -0700273EXPORT_SYMBOL(find_next_bit_le);
Akinobu Mita930ae742006-03-26 01:39:15 -0800274#endif /* __BIG_ENDIAN */