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 <arch/arch.h>
20 #define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
23 * Find the next set bit in a memory region.
25 unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
28 const unsigned long *p = addr + BITOP_WORD(offset);
29 unsigned long result = offset & ~(BITS_PER_LONG-1);
35 offset %= BITS_PER_LONG;
38 tmp &= (~0UL << offset);
39 if (size < BITS_PER_LONG)
43 size -= BITS_PER_LONG;
44 result += BITS_PER_LONG;
46 while (size & ~(BITS_PER_LONG-1)) {
49 result += BITS_PER_LONG;
50 size -= BITS_PER_LONG;
57 tmp &= (~0UL >> (BITS_PER_LONG - size));
58 if (tmp == 0UL) /* Are any bits set? */
59 return result + size; /* Nope. */
61 return result + __ffs(tmp);
65 * This implementation of find_{first,next}_zero_bit was stolen from
66 * Linus' asm-alpha/bitops.h.
68 unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
71 const unsigned long *p = addr + BITOP_WORD(offset);
72 unsigned long result = offset & ~(BITS_PER_LONG-1);
78 offset %= BITS_PER_LONG;
81 tmp |= ~0UL >> (BITS_PER_LONG - offset);
82 if (size < BITS_PER_LONG)
86 size -= BITS_PER_LONG;
87 result += BITS_PER_LONG;
89 while (size & ~(BITS_PER_LONG-1)) {
92 result += BITS_PER_LONG;
93 size -= BITS_PER_LONG;
101 if (tmp == ~0UL) /* Are any bits zero? */
102 return result + size; /* Nope. */
104 return result + ffz(tmp);
108 * Find the first set bit in a memory region.
110 unsigned long find_first_bit(const unsigned long *addr, unsigned long size)
112 const unsigned long *p = addr;
113 unsigned long result = 0;
116 while (size & ~(BITS_PER_LONG-1)) {
119 result += BITS_PER_LONG;
120 size -= BITS_PER_LONG;
125 tmp = (*p) & (~0UL >> (BITS_PER_LONG - size));
126 if (tmp == 0UL) /* Are any bits set? */
127 return result + size; /* Nope. */
129 return result + __ffs(tmp);
133 * Find the first cleared bit in a memory region.
135 unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size)
137 const unsigned long *p = addr;
138 unsigned long result = 0;
141 while (size & ~(BITS_PER_LONG-1)) {
144 result += BITS_PER_LONG;
145 size -= BITS_PER_LONG;
150 tmp = (*p) | (~0UL << size);
151 if (tmp == ~0UL) /* Are any bits zero? */
152 return result + size; /* Nope. */
154 return result + ffz(tmp);