1 /* find_next_bit.c: fallback find next bit implementation
3 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #include <linux/bitops.h>
13 #include <linux/module.h>
14 #include <asm/types.h>
16 #define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
19 * find_next_bit - find the next set bit in a memory region
20 * @addr: The address to base the search on
21 * @offset: The bitnumber to start searching at
22 * @size: The maximum size to search
24 unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
27 const unsigned long *p = addr + BITOP_WORD(offset);
28 unsigned long result = offset & ~(BITS_PER_LONG-1);
34 offset %= BITS_PER_LONG;
37 tmp &= (~0UL << offset);
38 if (size < BITS_PER_LONG)
42 size -= BITS_PER_LONG;
43 result += BITS_PER_LONG;
45 while (size & ~(BITS_PER_LONG-1)) {
48 result += BITS_PER_LONG;
49 size -= BITS_PER_LONG;
56 tmp &= (~0UL >> (BITS_PER_LONG - size));
57 if (tmp == 0UL) /* Are any bits set? */
58 return result + size; /* Nope. */
60 return result + __ffs(tmp);
63 EXPORT_SYMBOL(find_next_bit);
66 * This implementation of find_{first,next}_zero_bit was stolen from
67 * Linus' asm-alpha/bitops.h.
69 unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
72 const unsigned long *p = addr + BITOP_WORD(offset);
73 unsigned long result = offset & ~(BITS_PER_LONG-1);
79 offset %= BITS_PER_LONG;
82 tmp |= ~0UL >> (BITS_PER_LONG - offset);
83 if (size < BITS_PER_LONG)
87 size -= BITS_PER_LONG;
88 result += BITS_PER_LONG;
90 while (size & ~(BITS_PER_LONG-1)) {
93 result += BITS_PER_LONG;
94 size -= BITS_PER_LONG;
102 if (tmp == ~0UL) /* Are any bits zero? */
103 return result + size; /* Nope. */
105 return result + ffz(tmp);
108 EXPORT_SYMBOL(find_next_zero_bit);