• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_GENERIC_BITOPS_FIND_H_
3 #define _ASM_GENERIC_BITOPS_FIND_H_
4 
5 extern unsigned long _find_next_bit(const unsigned long *addr1,
6 		const unsigned long *addr2, unsigned long nbits,
7 		unsigned long start, unsigned long invert, unsigned long le);
8 extern unsigned long _find_first_bit(const unsigned long *addr, unsigned long size);
9 extern unsigned long _find_first_zero_bit(const unsigned long *addr, unsigned long size);
10 extern unsigned long _find_last_bit(const unsigned long *addr, unsigned long size);
11 
12 #ifndef find_next_bit
13 /**
14  * find_next_bit - find the next set bit in a memory region
15  * @addr: The address to base the search on
16  * @offset: The bitnumber to start searching at
17  * @size: The bitmap size in bits
18  *
19  * Returns the bit number for the next set bit
20  * If no bits are set, returns @size.
21  */
22 static inline
find_next_bit(const unsigned long * addr,unsigned long size,unsigned long offset)23 unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
24 			    unsigned long offset)
25 {
26 	if (small_const_nbits(size)) {
27 		unsigned long val;
28 
29 		if (unlikely(offset >= size))
30 			return size;
31 
32 		val = *addr & GENMASK(size - 1, offset);
33 		return val ? __ffs(val) : size;
34 	}
35 
36 	return _find_next_bit(addr, NULL, size, offset, 0UL, 0);
37 }
38 #endif
39 
40 #ifndef find_next_and_bit
41 /**
42  * find_next_and_bit - find the next set bit in both memory regions
43  * @addr1: The first address to base the search on
44  * @addr2: The second address to base the search on
45  * @offset: The bitnumber to start searching at
46  * @size: The bitmap size in bits
47  *
48  * Returns the bit number for the next set bit
49  * If no bits are set, returns @size.
50  */
51 static inline
find_next_and_bit(const unsigned long * addr1,const unsigned long * addr2,unsigned long size,unsigned long offset)52 unsigned long find_next_and_bit(const unsigned long *addr1,
53 		const unsigned long *addr2, unsigned long size,
54 		unsigned long offset)
55 {
56 	if (small_const_nbits(size)) {
57 		unsigned long val;
58 
59 		if (unlikely(offset >= size))
60 			return size;
61 
62 		val = *addr1 & *addr2 & GENMASK(size - 1, offset);
63 		return val ? __ffs(val) : size;
64 	}
65 
66 	return _find_next_bit(addr1, addr2, size, offset, 0UL, 0);
67 }
68 #endif
69 
70 #ifndef find_next_zero_bit
71 /**
72  * find_next_zero_bit - find the next cleared bit in a memory region
73  * @addr: The address to base the search on
74  * @offset: The bitnumber to start searching at
75  * @size: The bitmap size in bits
76  *
77  * Returns the bit number of the next zero bit
78  * If no bits are zero, returns @size.
79  */
80 static inline
find_next_zero_bit(const unsigned long * addr,unsigned long size,unsigned long offset)81 unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
82 				 unsigned long offset)
83 {
84 	if (small_const_nbits(size)) {
85 		unsigned long val;
86 
87 		if (unlikely(offset >= size))
88 			return size;
89 
90 		val = *addr | ~GENMASK(size - 1, offset);
91 		return val == ~0UL ? size : ffz(val);
92 	}
93 
94 	return _find_next_bit(addr, NULL, size, offset, ~0UL, 0);
95 }
96 #endif
97 
98 #ifdef CONFIG_GENERIC_FIND_FIRST_BIT
99 
100 #ifndef find_first_bit
101 /**
102  * find_first_bit - find the first set bit in a memory region
103  * @addr: The address to start the search at
104  * @size: The maximum number of bits to search
105  *
106  * Returns the bit number of the first set bit.
107  * If no bits are set, returns @size.
108  */
109 static inline
find_first_bit(const unsigned long * addr,unsigned long size)110 unsigned long find_first_bit(const unsigned long *addr, unsigned long size)
111 {
112 	if (small_const_nbits(size)) {
113 		unsigned long val = *addr & GENMASK(size - 1, 0);
114 
115 		return val ? __ffs(val) : size;
116 	}
117 
118 	return _find_first_bit(addr, size);
119 }
120 #endif
121 
122 #ifndef find_first_zero_bit
123 /**
124  * find_first_zero_bit - find the first cleared bit in a memory region
125  * @addr: The address to start the search at
126  * @size: The maximum number of bits to search
127  *
128  * Returns the bit number of the first cleared bit.
129  * If no bits are zero, returns @size.
130  */
131 static inline
find_first_zero_bit(const unsigned long * addr,unsigned long size)132 unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size)
133 {
134 	if (small_const_nbits(size)) {
135 		unsigned long val = *addr | ~GENMASK(size - 1, 0);
136 
137 		return val == ~0UL ? size : ffz(val);
138 	}
139 
140 	return _find_first_zero_bit(addr, size);
141 }
142 #endif
143 
144 #else /* CONFIG_GENERIC_FIND_FIRST_BIT */
145 
146 #ifndef find_first_bit
147 #define find_first_bit(addr, size) find_next_bit((addr), (size), 0)
148 #endif
149 #ifndef find_first_zero_bit
150 #define find_first_zero_bit(addr, size) find_next_zero_bit((addr), (size), 0)
151 #endif
152 
153 #endif /* CONFIG_GENERIC_FIND_FIRST_BIT */
154 
155 #ifndef find_last_bit
156 /**
157  * find_last_bit - find the last set bit in a memory region
158  * @addr: The address to start the search at
159  * @size: The number of bits to search
160  *
161  * Returns the bit number of the last set bit, or size.
162  */
163 static inline
find_last_bit(const unsigned long * addr,unsigned long size)164 unsigned long find_last_bit(const unsigned long *addr, unsigned long size)
165 {
166 	if (small_const_nbits(size)) {
167 		unsigned long val = *addr & GENMASK(size - 1, 0);
168 
169 		return val ? __fls(val) : size;
170 	}
171 
172 	return _find_last_bit(addr, size);
173 }
174 #endif
175 
176 /**
177  * find_next_clump8 - find next 8-bit clump with set bits in a memory region
178  * @clump: location to store copy of found clump
179  * @addr: address to base the search on
180  * @size: bitmap size in number of bits
181  * @offset: bit offset at which to start searching
182  *
183  * Returns the bit offset for the next set clump; the found clump value is
184  * copied to the location pointed by @clump. If no bits are set, returns @size.
185  */
186 extern unsigned long find_next_clump8(unsigned long *clump,
187 				      const unsigned long *addr,
188 				      unsigned long size, unsigned long offset);
189 
190 #define find_first_clump8(clump, bits, size) \
191 	find_next_clump8((clump), (bits), (size), 0)
192 
193 #endif /*_ASM_GENERIC_BITOPS_FIND_H_ */
194