Blame view
lib/find_next_bit.c
6.38 KB
1da177e4c Linux-2.6.12-rc2 |
1 2 3 4 5 6 7 8 9 10 11 12 |
/* find_next_bit.c: fallback find next bit implementation * * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved. * Written by David Howells (dhowells@redhat.com) * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. */ #include <linux/bitops.h> |
8bc3bcc93 lib: reduce the u... |
13 |
#include <linux/export.h> |
c7f612cdf [PATCH] bitops: g... |
14 |
#include <asm/types.h> |
930ae745f [PATCH] bitops: g... |
15 |
#include <asm/byteorder.h> |
1da177e4c Linux-2.6.12-rc2 |
16 |
|
c7f612cdf [PATCH] bitops: g... |
17 |
#define BITOP_WORD(nr) ((nr) / BITS_PER_LONG) |
19de85ef5 bitops: add #ifnd... |
18 |
#ifndef find_next_bit |
64970b68d x86, generic: opt... |
19 20 |
/* * Find the next set bit in a memory region. |
c7f612cdf [PATCH] bitops: g... |
21 |
*/ |
fee4b19fb bitops: remove "o... |
22 23 |
unsigned long find_next_bit(const unsigned long *addr, unsigned long size, unsigned long offset) |
1da177e4c Linux-2.6.12-rc2 |
24 |
{ |
c7f612cdf [PATCH] bitops: g... |
25 26 |
const unsigned long *p = addr + BITOP_WORD(offset); unsigned long result = offset & ~(BITS_PER_LONG-1); |
1da177e4c Linux-2.6.12-rc2 |
27 |
unsigned long tmp; |
c7f612cdf [PATCH] bitops: g... |
28 29 30 31 |
if (offset >= size) return size; size -= result; offset %= BITS_PER_LONG; |
1da177e4c Linux-2.6.12-rc2 |
32 |
if (offset) { |
c7f612cdf [PATCH] bitops: g... |
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
tmp = *(p++); tmp &= (~0UL << offset); if (size < BITS_PER_LONG) goto found_first; if (tmp) goto found_middle; size -= BITS_PER_LONG; result += BITS_PER_LONG; } while (size & ~(BITS_PER_LONG-1)) { if ((tmp = *(p++))) goto found_middle; result += BITS_PER_LONG; size -= BITS_PER_LONG; |
1da177e4c Linux-2.6.12-rc2 |
47 |
} |
c7f612cdf [PATCH] bitops: g... |
48 49 50 |
if (!size) return result; tmp = *p; |
1da177e4c Linux-2.6.12-rc2 |
51 |
|
c7f612cdf [PATCH] bitops: g... |
52 53 54 55 56 57 58 |
found_first: tmp &= (~0UL >> (BITS_PER_LONG - size)); if (tmp == 0UL) /* Are any bits set? */ return result + size; /* Nope. */ found_middle: return result + __ffs(tmp); } |
fee4b19fb bitops: remove "o... |
59 |
EXPORT_SYMBOL(find_next_bit); |
19de85ef5 bitops: add #ifnd... |
60 |
#endif |
1da177e4c Linux-2.6.12-rc2 |
61 |
|
19de85ef5 bitops: add #ifnd... |
62 |
#ifndef find_next_zero_bit |
c7f612cdf [PATCH] bitops: g... |
63 64 65 66 |
/* * This implementation of find_{first,next}_zero_bit was stolen from * Linus' asm-alpha/bitops.h. */ |
fee4b19fb bitops: remove "o... |
67 68 |
unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size, unsigned long offset) |
c7f612cdf [PATCH] bitops: g... |
69 70 71 72 |
{ const unsigned long *p = addr + BITOP_WORD(offset); unsigned long result = offset & ~(BITS_PER_LONG-1); unsigned long tmp; |
1da177e4c Linux-2.6.12-rc2 |
73 |
|
c7f612cdf [PATCH] bitops: g... |
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
if (offset >= size) return size; size -= result; offset %= BITS_PER_LONG; if (offset) { tmp = *(p++); tmp |= ~0UL >> (BITS_PER_LONG - offset); if (size < BITS_PER_LONG) goto found_first; if (~tmp) goto found_middle; size -= BITS_PER_LONG; result += BITS_PER_LONG; } while (size & ~(BITS_PER_LONG-1)) { if (~(tmp = *(p++))) goto found_middle; result += BITS_PER_LONG; size -= BITS_PER_LONG; |
1da177e4c Linux-2.6.12-rc2 |
93 |
} |
c7f612cdf [PATCH] bitops: g... |
94 95 96 |
if (!size) return result; tmp = *p; |
1da177e4c Linux-2.6.12-rc2 |
97 |
|
c7f612cdf [PATCH] bitops: g... |
98 99 100 101 102 103 |
found_first: tmp |= ~0UL << size; if (tmp == ~0UL) /* Are any bits zero? */ return result + size; /* Nope. */ found_middle: return result + ffz(tmp); |
1da177e4c Linux-2.6.12-rc2 |
104 |
} |
fee4b19fb bitops: remove "o... |
105 |
EXPORT_SYMBOL(find_next_zero_bit); |
19de85ef5 bitops: add #ifnd... |
106 |
#endif |
77b9bd9c4 x86: generic vers... |
107 |
|
19de85ef5 bitops: add #ifnd... |
108 |
#ifndef find_first_bit |
77b9bd9c4 x86: generic vers... |
109 110 111 |
/* * Find the first set bit in a memory region. */ |
fee4b19fb bitops: remove "o... |
112 |
unsigned long find_first_bit(const unsigned long *addr, unsigned long size) |
77b9bd9c4 x86: generic vers... |
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
{ const unsigned long *p = addr; unsigned long result = 0; unsigned long tmp; while (size & ~(BITS_PER_LONG-1)) { if ((tmp = *(p++))) goto found; result += BITS_PER_LONG; size -= BITS_PER_LONG; } if (!size) return result; tmp = (*p) & (~0UL >> (BITS_PER_LONG - size)); if (tmp == 0UL) /* Are any bits set? */ return result + size; /* Nope. */ found: return result + __ffs(tmp); } |
fee4b19fb bitops: remove "o... |
133 |
EXPORT_SYMBOL(find_first_bit); |
19de85ef5 bitops: add #ifnd... |
134 |
#endif |
77b9bd9c4 x86: generic vers... |
135 |
|
19de85ef5 bitops: add #ifnd... |
136 |
#ifndef find_first_zero_bit |
77b9bd9c4 x86: generic vers... |
137 138 139 |
/* * Find the first cleared bit in a memory region. */ |
fee4b19fb bitops: remove "o... |
140 |
unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size) |
77b9bd9c4 x86: generic vers... |
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
{ const unsigned long *p = addr; unsigned long result = 0; unsigned long tmp; while (size & ~(BITS_PER_LONG-1)) { if (~(tmp = *(p++))) goto found; result += BITS_PER_LONG; size -= BITS_PER_LONG; } if (!size) return result; tmp = (*p) | (~0UL << size); if (tmp == ~0UL) /* Are any bits zero? */ return result + size; /* Nope. */ found: return result + ffz(tmp); } |
fee4b19fb bitops: remove "o... |
161 |
EXPORT_SYMBOL(find_first_zero_bit); |
19de85ef5 bitops: add #ifnd... |
162 |
#endif |
930ae745f [PATCH] bitops: g... |
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
#ifdef __BIG_ENDIAN /* include/linux/byteorder does not support "unsigned long" type */ static inline unsigned long ext2_swabp(const unsigned long * x) { #if BITS_PER_LONG == 64 return (unsigned long) __swab64p((u64 *) x); #elif BITS_PER_LONG == 32 return (unsigned long) __swab32p((u32 *) x); #else #error BITS_PER_LONG not defined #endif } /* include/linux/byteorder doesn't support "unsigned long" type */ static inline unsigned long ext2_swab(const unsigned long y) { #if BITS_PER_LONG == 64 return (unsigned long) __swab64((u64) y); #elif BITS_PER_LONG == 32 return (unsigned long) __swab32((u32) y); #else #error BITS_PER_LONG not defined #endif } |
19de85ef5 bitops: add #ifnd... |
189 |
#ifndef find_next_zero_bit_le |
a56560b3b asm-generic: chan... |
190 |
unsigned long find_next_zero_bit_le(const void *addr, unsigned |
930ae745f [PATCH] bitops: g... |
191 192 |
long size, unsigned long offset) { |
a56560b3b asm-generic: chan... |
193 |
const unsigned long *p = addr; |
930ae745f [PATCH] bitops: g... |
194 195 196 197 198 |
unsigned long result = offset & ~(BITS_PER_LONG - 1); unsigned long tmp; if (offset >= size) return size; |
a56560b3b asm-generic: chan... |
199 |
p += BITOP_WORD(offset); |
930ae745f [PATCH] bitops: g... |
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
size -= result; offset &= (BITS_PER_LONG - 1UL); if (offset) { tmp = ext2_swabp(p++); tmp |= (~0UL >> (BITS_PER_LONG - offset)); if (size < BITS_PER_LONG) goto found_first; if (~tmp) goto found_middle; size -= BITS_PER_LONG; result += BITS_PER_LONG; } while (size & ~(BITS_PER_LONG - 1)) { if (~(tmp = *(p++))) goto found_middle_swap; result += BITS_PER_LONG; size -= BITS_PER_LONG; } if (!size) return result; tmp = ext2_swabp(p); found_first: tmp |= ~0UL << size; if (tmp == ~0UL) /* Are any bits zero? */ return result + size; /* Nope. Skip ffz */ found_middle: return result + ffz(tmp); found_middle_swap: return result + ffz(ext2_swab(tmp)); } |
c4945b9ed asm-generic: rena... |
232 |
EXPORT_SYMBOL(find_next_zero_bit_le); |
19de85ef5 bitops: add #ifnd... |
233 |
#endif |
930ae745f [PATCH] bitops: g... |
234 |
|
19de85ef5 bitops: add #ifnd... |
235 |
#ifndef find_next_bit_le |
a56560b3b asm-generic: chan... |
236 |
unsigned long find_next_bit_le(const void *addr, unsigned |
aa02ad67d ext4: Add ext4_fi... |
237 238 |
long size, unsigned long offset) { |
a56560b3b asm-generic: chan... |
239 |
const unsigned long *p = addr; |
aa02ad67d ext4: Add ext4_fi... |
240 241 242 243 244 |
unsigned long result = offset & ~(BITS_PER_LONG - 1); unsigned long tmp; if (offset >= size) return size; |
a56560b3b asm-generic: chan... |
245 |
p += BITOP_WORD(offset); |
aa02ad67d ext4: Add ext4_fi... |
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
size -= result; offset &= (BITS_PER_LONG - 1UL); if (offset) { tmp = ext2_swabp(p++); tmp &= (~0UL << offset); if (size < BITS_PER_LONG) goto found_first; if (tmp) goto found_middle; size -= BITS_PER_LONG; result += BITS_PER_LONG; } while (size & ~(BITS_PER_LONG - 1)) { tmp = *(p++); if (tmp) goto found_middle_swap; result += BITS_PER_LONG; size -= BITS_PER_LONG; } if (!size) return result; tmp = ext2_swabp(p); found_first: tmp &= (~0UL >> (BITS_PER_LONG - size)); if (tmp == 0UL) /* Are any bits set? */ return result + size; /* Nope. */ found_middle: return result + __ffs(tmp); found_middle_swap: return result + __ffs(ext2_swab(tmp)); } |
c4945b9ed asm-generic: rena... |
279 |
EXPORT_SYMBOL(find_next_bit_le); |
19de85ef5 bitops: add #ifnd... |
280 |
#endif |
0664996b7 bitops: introduce... |
281 |
|
930ae745f [PATCH] bitops: g... |
282 |
#endif /* __BIG_ENDIAN */ |