1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <linux/bitops.h>
22#include <linux/bitmap.h>
23#include <linux/kernel.h>
24
25#if !defined(find_next_bit) || !defined(find_next_zero_bit) || \
26 !defined(find_next_and_bit)
27
28
29
30
31
32
33
34
35static inline unsigned long _find_next_bit(const unsigned long *addr1,
36 const unsigned long *addr2, unsigned long nbits,
37 unsigned long start, unsigned long invert)
38{
39 unsigned long tmp;
40
41 if (unlikely(start >= nbits))
42 return nbits;
43
44 tmp = addr1[start / BITS_PER_LONG];
45 if (addr2)
46 tmp &= addr2[start / BITS_PER_LONG];
47 tmp ^= invert;
48
49
50 tmp &= BITMAP_FIRST_WORD_MASK(start);
51 start = round_down(start, BITS_PER_LONG);
52
53 while (!tmp) {
54 start += BITS_PER_LONG;
55 if (start >= nbits)
56 return nbits;
57
58 tmp = addr1[start / BITS_PER_LONG];
59 if (addr2)
60 tmp &= addr2[start / BITS_PER_LONG];
61 tmp ^= invert;
62 }
63
64 return min(start + __ffs(tmp), nbits);
65}
66#endif
67
68#ifndef find_next_bit
69
70
71
72unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
73 unsigned long offset)
74{
75 return _find_next_bit(addr, NULL, size, offset, 0UL);
76}
77#endif
78
79#ifndef find_first_bit
80
81
82
83unsigned long find_first_bit(const unsigned long *addr, unsigned long size)
84{
85 unsigned long idx;
86
87 for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
88 if (addr[idx])
89 return min(idx * BITS_PER_LONG + __ffs(addr[idx]), size);
90 }
91
92 return size;
93}
94#endif
95
96#ifndef find_first_zero_bit
97
98
99
100unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size)
101{
102 unsigned long idx;
103
104 for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
105 if (addr[idx] != ~0UL)
106 return min(idx * BITS_PER_LONG + ffz(addr[idx]), size);
107 }
108
109 return size;
110}
111#endif
112
113#ifndef find_next_zero_bit
114unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
115 unsigned long offset)
116{
117 return _find_next_bit(addr, NULL, size, offset, ~0UL);
118}
119#endif
120
121#ifndef find_next_and_bit
122unsigned long find_next_and_bit(const unsigned long *addr1,
123 const unsigned long *addr2, unsigned long size,
124 unsigned long offset)
125{
126 return _find_next_bit(addr1, addr2, size, offset, 0UL);
127}
128#endif
129