1 /* find_next_bit.c: fallback find next bit implementation
3 * Copied from lib/find_next_bit.c to tools/lib/next_bit.c
5 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
6 * Written by David Howells (dhowells@redhat.com)
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
14 #include <linux/bitops.h>
15 #include <asm/types.h>
16 #include <asm/byteorder.h>
18 #define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
22 * Find the next set bit in a memory region.
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
);
64 #ifndef find_first_bit
66 * Find the first set bit in a memory region.
68 unsigned long find_first_bit(const unsigned long *addr
, unsigned long size
)
70 const unsigned long *p
= addr
;
71 unsigned long result
= 0;
74 while (size
& ~(BITS_PER_LONG
-1)) {
77 result
+= BITS_PER_LONG
;
78 size
-= BITS_PER_LONG
;
83 tmp
= (*p
) & (~0UL >> (BITS_PER_LONG
- size
));
84 if (tmp
== 0UL) /* Are any bits set? */
85 return result
+ size
; /* Nope. */
87 return result
+ __ffs(tmp
);