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.
16 #include <linux/bitops.h>
17 #include <asm/types.h>
18 #include <asm/byteorder.h>
20 #define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
22 #ifdef CONFIG_GENERIC_FIND_NEXT_BIT
24 * Find the next set bit in a memory region.
26 unsigned long find_next_bit(const unsigned long *addr
, unsigned long size
,
29 const unsigned long *p
= addr
+ BITOP_WORD(offset
);
30 unsigned long result
= offset
& ~(BITS_PER_LONG
-1);
36 offset
%= BITS_PER_LONG
;
39 tmp
&= (~0UL << offset
);
40 if (size
< BITS_PER_LONG
)
44 size
-= BITS_PER_LONG
;
45 result
+= BITS_PER_LONG
;
47 while (size
& ~(BITS_PER_LONG
-1)) {
50 result
+= BITS_PER_LONG
;
51 size
-= BITS_PER_LONG
;
58 tmp
&= (~0UL >> (BITS_PER_LONG
- size
));
59 if (tmp
== 0UL) /* Are any bits set? */
60 return result
+ size
; /* Nope. */
62 return result
+ __ffs(tmp
);
64 EXPORT_SYMBOL(find_next_bit
);
67 * This implementation of find_{first,next}_zero_bit was stolen from
68 * Linus' asm-alpha/bitops.h.
70 unsigned long find_next_zero_bit(const unsigned long *addr
, unsigned long size
,
73 const unsigned long *p
= addr
+ BITOP_WORD(offset
);
74 unsigned long result
= offset
& ~(BITS_PER_LONG
-1);
80 offset
%= BITS_PER_LONG
;
83 tmp
|= ~0UL >> (BITS_PER_LONG
- offset
);
84 if (size
< BITS_PER_LONG
)
88 size
-= BITS_PER_LONG
;
89 result
+= BITS_PER_LONG
;
91 while (size
& ~(BITS_PER_LONG
-1)) {
94 result
+= BITS_PER_LONG
;
95 size
-= BITS_PER_LONG
;
103 if (tmp
== ~0UL) /* Are any bits zero? */
104 return result
+ size
; /* Nope. */
106 return result
+ ffz(tmp
);
108 EXPORT_SYMBOL(find_next_zero_bit
);
109 #endif /* CONFIG_GENERIC_FIND_NEXT_BIT */