blob: 5820e072b890269984dcfc7a5dec1cb10ed61930 [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)
Alexander van Heukelum6fd92b62008-03-09 21:01:04 +010018#undef find_next_bit
19#undef find_next_zero_bit
Akinobu Mitac7f612c2006-03-26 01:39:11 -080020
21/**
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
26 */
27unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
28 unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Akinobu Mitac7f612c2006-03-26 01:39:11 -080030 const unsigned long *p = addr + BITOP_WORD(offset);
31 unsigned long result = offset & ~(BITS_PER_LONG-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 unsigned long tmp;
33
Akinobu Mitac7f612c2006-03-26 01:39:11 -080034 if (offset >= size)
35 return size;
36 size -= result;
37 offset %= BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 if (offset) {
Akinobu Mitac7f612c2006-03-26 01:39:11 -080039 tmp = *(p++);
40 tmp &= (~0UL << offset);
41 if (size < BITS_PER_LONG)
42 goto found_first;
43 if (tmp)
44 goto found_middle;
45 size -= BITS_PER_LONG;
46 result += BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 }
Akinobu Mitac7f612c2006-03-26 01:39:11 -080048 while (size & ~(BITS_PER_LONG-1)) {
49 if ((tmp = *(p++)))
50 goto found_middle;
51 result += BITS_PER_LONG;
52 size -= BITS_PER_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 }
Akinobu Mitac7f612c2006-03-26 01:39:11 -080054 if (!size)
55 return result;
56 tmp = *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Akinobu Mitac7f612c2006-03-26 01:39:11 -080058found_first:
59 tmp &= (~0UL >> (BITS_PER_LONG - size));
60 if (tmp == 0UL) /* Are any bits set? */
61 return result + size; /* Nope. */
62found_middle:
63 return result + __ffs(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
David Howells40234402006-01-08 01:01:19 -080065
66EXPORT_SYMBOL(find_next_bit);
Akinobu Mitac7f612c2006-03-26 01:39:11 -080067
68/*
69 * This implementation of find_{first,next}_zero_bit was stolen from
70 * Linus' asm-alpha/bitops.h.
71 */
72unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
73 unsigned long offset)
74{
75 const unsigned long *p = addr + BITOP_WORD(offset);
76 unsigned long result = offset & ~(BITS_PER_LONG-1);
77 unsigned long tmp;
78
79 if (offset >= size)
80 return size;
81 size -= result;
82 offset %= BITS_PER_LONG;
83 if (offset) {
84 tmp = *(p++);
85 tmp |= ~0UL >> (BITS_PER_LONG - offset);
86 if (size < BITS_PER_LONG)
87 goto found_first;
88 if (~tmp)
89 goto found_middle;
90 size -= BITS_PER_LONG;
91 result += BITS_PER_LONG;
92 }
93 while (size & ~(BITS_PER_LONG-1)) {
94 if (~(tmp = *(p++)))
95 goto found_middle;
96 result += BITS_PER_LONG;
97 size -= BITS_PER_LONG;
98 }
99 if (!size)
100 return result;
101 tmp = *p;
102
103found_first:
104 tmp |= ~0UL << size;
105 if (tmp == ~0UL) /* Are any bits zero? */
106 return result + size; /* Nope. */
107found_middle:
108 return result + ffz(tmp);
109}
110
111EXPORT_SYMBOL(find_next_zero_bit);
Akinobu Mita930ae742006-03-26 01:39:15 -0800112
113#ifdef __BIG_ENDIAN
114
115/* include/linux/byteorder does not support "unsigned long" type */
116static inline unsigned long ext2_swabp(const unsigned long * x)
117{
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);
122#else
123#error BITS_PER_LONG not defined
124#endif
125}
126
127/* include/linux/byteorder doesn't support "unsigned long" type */
128static inline unsigned long ext2_swab(const unsigned long y)
129{
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);
134#else
135#error BITS_PER_LONG not defined
136#endif
137}
138
139unsigned long generic_find_next_zero_le_bit(const unsigned long *addr, unsigned
140 long size, unsigned long offset)
141{
142 const unsigned long *p = addr + BITOP_WORD(offset);
143 unsigned long result = offset & ~(BITS_PER_LONG - 1);
144 unsigned long tmp;
145
146 if (offset >= size)
147 return size;
148 size -= result;
149 offset &= (BITS_PER_LONG - 1UL);
150 if (offset) {
151 tmp = ext2_swabp(p++);
152 tmp |= (~0UL >> (BITS_PER_LONG - offset));
153 if (size < BITS_PER_LONG)
154 goto found_first;
155 if (~tmp)
156 goto found_middle;
157 size -= BITS_PER_LONG;
158 result += BITS_PER_LONG;
159 }
160
161 while (size & ~(BITS_PER_LONG - 1)) {
162 if (~(tmp = *(p++)))
163 goto found_middle_swap;
164 result += BITS_PER_LONG;
165 size -= BITS_PER_LONG;
166 }
167 if (!size)
168 return result;
169 tmp = ext2_swabp(p);
170found_first:
171 tmp |= ~0UL << size;
172 if (tmp == ~0UL) /* Are any bits zero? */
173 return result + size; /* Nope. Skip ffz */
174found_middle:
175 return result + ffz(tmp);
176
177found_middle_swap:
178 return result + ffz(ext2_swab(tmp));
179}
180
181EXPORT_SYMBOL(generic_find_next_zero_le_bit);
182
Aneesh Kumar K.Vaa02ad62008-01-28 23:58:27 -0500183unsigned long generic_find_next_le_bit(const unsigned long *addr, unsigned
184 long size, unsigned long offset)
185{
186 const unsigned long *p = addr + BITOP_WORD(offset);
187 unsigned long result = offset & ~(BITS_PER_LONG - 1);
188 unsigned long tmp;
189
190 if (offset >= size)
191 return size;
192 size -= result;
193 offset &= (BITS_PER_LONG - 1UL);
194 if (offset) {
195 tmp = ext2_swabp(p++);
196 tmp &= (~0UL << offset);
197 if (size < BITS_PER_LONG)
198 goto found_first;
199 if (tmp)
200 goto found_middle;
201 size -= BITS_PER_LONG;
202 result += BITS_PER_LONG;
203 }
204
205 while (size & ~(BITS_PER_LONG - 1)) {
206 tmp = *(p++);
207 if (tmp)
208 goto found_middle_swap;
209 result += BITS_PER_LONG;
210 size -= BITS_PER_LONG;
211 }
212 if (!size)
213 return result;
214 tmp = ext2_swabp(p);
215found_first:
216 tmp &= (~0UL >> (BITS_PER_LONG - size));
217 if (tmp == 0UL) /* Are any bits set? */
218 return result + size; /* Nope. */
219found_middle:
220 return result + __ffs(tmp);
221
222found_middle_swap:
223 return result + __ffs(ext2_swab(tmp));
224}
225EXPORT_SYMBOL(generic_find_next_le_bit);
Akinobu Mita930ae742006-03-26 01:39:15 -0800226#endif /* __BIG_ENDIAN */