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/types.h>
13 #include <linux/bitops.h>
15 #define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
18 * Find the next set bit in a memory region.
20 unsigned long find_next_bit(const unsigned long *addr
, unsigned long size
,
23 const unsigned long *p
= addr
+ BITOP_WORD(offset
);
24 unsigned long result
= offset
& ~(BITS_PER_LONG
-1);
30 offset
%= BITS_PER_LONG
;
33 tmp
&= (~0UL << offset
);
34 if (size
< BITS_PER_LONG
)
38 size
-= BITS_PER_LONG
;
39 result
+= BITS_PER_LONG
;
41 while (size
& ~(BITS_PER_LONG
-1)) {
44 result
+= BITS_PER_LONG
;
45 size
-= BITS_PER_LONG
;
52 tmp
&= (~0UL >> (BITS_PER_LONG
- size
));
53 if (tmp
== 0UL) /* Are any bits set? */
54 return result
+ size
; /* Nope. */
56 return result
+ __ffs(tmp
);