1 /* SPDX-License-Identifier: GPL-2.0 */ 1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __LINUX_FIND_H_ 2 #ifndef __LINUX_FIND_H_ 3 #define __LINUX_FIND_H_ 3 #define __LINUX_FIND_H_ 4 4 5 #ifndef __LINUX_BITMAP_H 5 #ifndef __LINUX_BITMAP_H 6 #error only <linux/bitmap.h> can be included d 6 #error only <linux/bitmap.h> can be included directly 7 #endif 7 #endif 8 8 9 #include <linux/bitops.h> 9 #include <linux/bitops.h> 10 10 11 unsigned long _find_next_bit(const unsigned lo 11 unsigned long _find_next_bit(const unsigned long *addr1, unsigned long nbits, 12 unsigned long 12 unsigned long start); 13 unsigned long _find_next_and_bit(const unsigne 13 unsigned long _find_next_and_bit(const unsigned long *addr1, const unsigned long *addr2, 14 unsign 14 unsigned long nbits, unsigned long start); 15 unsigned long _find_next_andnot_bit(const unsi 15 unsigned long _find_next_andnot_bit(const unsigned long *addr1, const unsigned long *addr2, 16 unsign 16 unsigned long nbits, unsigned long start); 17 unsigned long _find_next_or_bit(const unsigned 17 unsigned long _find_next_or_bit(const unsigned long *addr1, const unsigned long *addr2, 18 unsign 18 unsigned long nbits, unsigned long start); 19 unsigned long _find_next_zero_bit(const unsign 19 unsigned long _find_next_zero_bit(const unsigned long *addr, unsigned long nbits, 20 unsig 20 unsigned long start); 21 extern unsigned long _find_first_bit(const uns 21 extern unsigned long _find_first_bit(const unsigned long *addr, unsigned long size); 22 unsigned long __find_nth_bit(const unsigned lo 22 unsigned long __find_nth_bit(const unsigned long *addr, unsigned long size, unsigned long n); 23 unsigned long __find_nth_and_bit(const unsigne 23 unsigned long __find_nth_and_bit(const unsigned long *addr1, const unsigned long *addr2, 24 unsigned long 24 unsigned long size, unsigned long n); 25 unsigned long __find_nth_andnot_bit(const unsi 25 unsigned long __find_nth_andnot_bit(const unsigned long *addr1, const unsigned long *addr2, 26 unsign 26 unsigned long size, unsigned long n); 27 unsigned long __find_nth_and_andnot_bit(const 27 unsigned long __find_nth_and_andnot_bit(const unsigned long *addr1, const unsigned long *addr2, 28 const 28 const unsigned long *addr3, unsigned long size, 29 unsign 29 unsigned long n); 30 extern unsigned long _find_first_and_bit(const 30 extern unsigned long _find_first_and_bit(const unsigned long *addr1, 31 const 31 const unsigned long *addr2, unsigned long size); 32 unsigned long _find_first_and_and_bit(const un << 33 const un << 34 extern unsigned long _find_first_zero_bit(cons 32 extern unsigned long _find_first_zero_bit(const unsigned long *addr, unsigned long size); 35 extern unsigned long _find_last_bit(const unsi 33 extern unsigned long _find_last_bit(const unsigned long *addr, unsigned long size); 36 34 37 #ifdef __BIG_ENDIAN 35 #ifdef __BIG_ENDIAN 38 unsigned long _find_first_zero_bit_le(const un 36 unsigned long _find_first_zero_bit_le(const unsigned long *addr, unsigned long size); 39 unsigned long _find_next_zero_bit_le(const un 37 unsigned long _find_next_zero_bit_le(const unsigned long *addr, unsigned 40 long s 38 long size, unsigned long offset); 41 unsigned long _find_next_bit_le(const unsigned 39 unsigned long _find_next_bit_le(const unsigned long *addr, unsigned 42 long size, uns 40 long size, unsigned long offset); 43 #endif 41 #endif 44 42 45 #ifndef find_next_bit 43 #ifndef find_next_bit 46 /** 44 /** 47 * find_next_bit - find the next set bit in a 45 * find_next_bit - find the next set bit in a memory region 48 * @addr: The address to base the search on 46 * @addr: The address to base the search on 49 * @size: The bitmap size in bits 47 * @size: The bitmap size in bits 50 * @offset: The bitnumber to start searching a 48 * @offset: The bitnumber to start searching at 51 * 49 * 52 * Returns the bit number for the next set bit 50 * Returns the bit number for the next set bit 53 * If no bits are set, returns @size. 51 * If no bits are set, returns @size. 54 */ 52 */ 55 static inline 53 static inline 56 unsigned long find_next_bit(const unsigned lon 54 unsigned long find_next_bit(const unsigned long *addr, unsigned long size, 57 unsigned long offs 55 unsigned long offset) 58 { 56 { 59 if (small_const_nbits(size)) { 57 if (small_const_nbits(size)) { 60 unsigned long val; 58 unsigned long val; 61 59 62 if (unlikely(offset >= size)) 60 if (unlikely(offset >= size)) 63 return size; 61 return size; 64 62 65 val = *addr & GENMASK(size - 1 63 val = *addr & GENMASK(size - 1, offset); 66 return val ? __ffs(val) : size 64 return val ? __ffs(val) : size; 67 } 65 } 68 66 69 return _find_next_bit(addr, size, offs 67 return _find_next_bit(addr, size, offset); 70 } 68 } 71 #endif 69 #endif 72 70 73 #ifndef find_next_and_bit 71 #ifndef find_next_and_bit 74 /** 72 /** 75 * find_next_and_bit - find the next set bit i 73 * find_next_and_bit - find the next set bit in both memory regions 76 * @addr1: The first address to base the searc 74 * @addr1: The first address to base the search on 77 * @addr2: The second address to base the sear 75 * @addr2: The second address to base the search on 78 * @size: The bitmap size in bits 76 * @size: The bitmap size in bits 79 * @offset: The bitnumber to start searching a 77 * @offset: The bitnumber to start searching at 80 * 78 * 81 * Returns the bit number for the next set bit 79 * Returns the bit number for the next set bit 82 * If no bits are set, returns @size. 80 * If no bits are set, returns @size. 83 */ 81 */ 84 static inline 82 static inline 85 unsigned long find_next_and_bit(const unsigned 83 unsigned long find_next_and_bit(const unsigned long *addr1, 86 const unsigned long *addr2, un 84 const unsigned long *addr2, unsigned long size, 87 unsigned long offset) 85 unsigned long offset) 88 { 86 { 89 if (small_const_nbits(size)) { 87 if (small_const_nbits(size)) { 90 unsigned long val; 88 unsigned long val; 91 89 92 if (unlikely(offset >= size)) 90 if (unlikely(offset >= size)) 93 return size; 91 return size; 94 92 95 val = *addr1 & *addr2 & GENMAS 93 val = *addr1 & *addr2 & GENMASK(size - 1, offset); 96 return val ? __ffs(val) : size 94 return val ? __ffs(val) : size; 97 } 95 } 98 96 99 return _find_next_and_bit(addr1, addr2 97 return _find_next_and_bit(addr1, addr2, size, offset); 100 } 98 } 101 #endif 99 #endif 102 100 103 #ifndef find_next_andnot_bit 101 #ifndef find_next_andnot_bit 104 /** 102 /** 105 * find_next_andnot_bit - find the next set bi 103 * find_next_andnot_bit - find the next set bit in *addr1 excluding all the bits 106 * in *addr2 104 * in *addr2 107 * @addr1: The first address to base the searc 105 * @addr1: The first address to base the search on 108 * @addr2: The second address to base the sear 106 * @addr2: The second address to base the search on 109 * @size: The bitmap size in bits 107 * @size: The bitmap size in bits 110 * @offset: The bitnumber to start searching a 108 * @offset: The bitnumber to start searching at 111 * 109 * 112 * Returns the bit number for the next set bit 110 * Returns the bit number for the next set bit 113 * If no bits are set, returns @size. 111 * If no bits are set, returns @size. 114 */ 112 */ 115 static inline 113 static inline 116 unsigned long find_next_andnot_bit(const unsig 114 unsigned long find_next_andnot_bit(const unsigned long *addr1, 117 const unsigned long *addr2, un 115 const unsigned long *addr2, unsigned long size, 118 unsigned long offset) 116 unsigned long offset) 119 { 117 { 120 if (small_const_nbits(size)) { 118 if (small_const_nbits(size)) { 121 unsigned long val; 119 unsigned long val; 122 120 123 if (unlikely(offset >= size)) 121 if (unlikely(offset >= size)) 124 return size; 122 return size; 125 123 126 val = *addr1 & ~*addr2 & GENMA 124 val = *addr1 & ~*addr2 & GENMASK(size - 1, offset); 127 return val ? __ffs(val) : size 125 return val ? __ffs(val) : size; 128 } 126 } 129 127 130 return _find_next_andnot_bit(addr1, ad 128 return _find_next_andnot_bit(addr1, addr2, size, offset); 131 } 129 } 132 #endif 130 #endif 133 131 134 #ifndef find_next_or_bit 132 #ifndef find_next_or_bit 135 /** 133 /** 136 * find_next_or_bit - find the next set bit in 134 * find_next_or_bit - find the next set bit in either memory regions 137 * @addr1: The first address to base the searc 135 * @addr1: The first address to base the search on 138 * @addr2: The second address to base the sear 136 * @addr2: The second address to base the search on 139 * @size: The bitmap size in bits 137 * @size: The bitmap size in bits 140 * @offset: The bitnumber to start searching a 138 * @offset: The bitnumber to start searching at 141 * 139 * 142 * Returns the bit number for the next set bit 140 * Returns the bit number for the next set bit 143 * If no bits are set, returns @size. 141 * If no bits are set, returns @size. 144 */ 142 */ 145 static inline 143 static inline 146 unsigned long find_next_or_bit(const unsigned 144 unsigned long find_next_or_bit(const unsigned long *addr1, 147 const unsigned long *addr2, un 145 const unsigned long *addr2, unsigned long size, 148 unsigned long offset) 146 unsigned long offset) 149 { 147 { 150 if (small_const_nbits(size)) { 148 if (small_const_nbits(size)) { 151 unsigned long val; 149 unsigned long val; 152 150 153 if (unlikely(offset >= size)) 151 if (unlikely(offset >= size)) 154 return size; 152 return size; 155 153 156 val = (*addr1 | *addr2) & GENM 154 val = (*addr1 | *addr2) & GENMASK(size - 1, offset); 157 return val ? __ffs(val) : size 155 return val ? __ffs(val) : size; 158 } 156 } 159 157 160 return _find_next_or_bit(addr1, addr2, 158 return _find_next_or_bit(addr1, addr2, size, offset); 161 } 159 } 162 #endif 160 #endif 163 161 164 #ifndef find_next_zero_bit 162 #ifndef find_next_zero_bit 165 /** 163 /** 166 * find_next_zero_bit - find the next cleared 164 * find_next_zero_bit - find the next cleared bit in a memory region 167 * @addr: The address to base the search on 165 * @addr: The address to base the search on 168 * @size: The bitmap size in bits 166 * @size: The bitmap size in bits 169 * @offset: The bitnumber to start searching a 167 * @offset: The bitnumber to start searching at 170 * 168 * 171 * Returns the bit number of the next zero bit 169 * Returns the bit number of the next zero bit 172 * If no bits are zero, returns @size. 170 * If no bits are zero, returns @size. 173 */ 171 */ 174 static inline 172 static inline 175 unsigned long find_next_zero_bit(const unsigne 173 unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size, 176 unsigned long 174 unsigned long offset) 177 { 175 { 178 if (small_const_nbits(size)) { 176 if (small_const_nbits(size)) { 179 unsigned long val; 177 unsigned long val; 180 178 181 if (unlikely(offset >= size)) 179 if (unlikely(offset >= size)) 182 return size; 180 return size; 183 181 184 val = *addr | ~GENMASK(size - 182 val = *addr | ~GENMASK(size - 1, offset); 185 return val == ~0UL ? size : ff 183 return val == ~0UL ? size : ffz(val); 186 } 184 } 187 185 188 return _find_next_zero_bit(addr, size, 186 return _find_next_zero_bit(addr, size, offset); 189 } 187 } 190 #endif 188 #endif 191 189 192 #ifndef find_first_bit 190 #ifndef find_first_bit 193 /** 191 /** 194 * find_first_bit - find the first set bit in 192 * find_first_bit - find the first set bit in a memory region 195 * @addr: The address to start the search at 193 * @addr: The address to start the search at 196 * @size: The maximum number of bits to search 194 * @size: The maximum number of bits to search 197 * 195 * 198 * Returns the bit number of the first set bit 196 * Returns the bit number of the first set bit. 199 * If no bits are set, returns @size. 197 * If no bits are set, returns @size. 200 */ 198 */ 201 static inline 199 static inline 202 unsigned long find_first_bit(const unsigned lo 200 unsigned long find_first_bit(const unsigned long *addr, unsigned long size) 203 { 201 { 204 if (small_const_nbits(size)) { 202 if (small_const_nbits(size)) { 205 unsigned long val = *addr & GE 203 unsigned long val = *addr & GENMASK(size - 1, 0); 206 204 207 return val ? __ffs(val) : size 205 return val ? __ffs(val) : size; 208 } 206 } 209 207 210 return _find_first_bit(addr, size); 208 return _find_first_bit(addr, size); 211 } 209 } 212 #endif 210 #endif 213 211 214 /** 212 /** 215 * find_nth_bit - find N'th set bit in a memor 213 * find_nth_bit - find N'th set bit in a memory region 216 * @addr: The address to start the search at 214 * @addr: The address to start the search at 217 * @size: The maximum number of bits to search 215 * @size: The maximum number of bits to search 218 * @n: The number of set bit, which position i 216 * @n: The number of set bit, which position is needed, counting from 0 219 * 217 * 220 * The following is semantically equivalent: 218 * The following is semantically equivalent: 221 * idx = find_nth_bit(addr, size, 0); 219 * idx = find_nth_bit(addr, size, 0); 222 * idx = find_first_bit(addr, size); 220 * idx = find_first_bit(addr, size); 223 * 221 * 224 * Returns the bit number of the N'th set bit. 222 * Returns the bit number of the N'th set bit. 225 * If no such, returns >= @size. !! 223 * If no such, returns @size. 226 */ 224 */ 227 static inline 225 static inline 228 unsigned long find_nth_bit(const unsigned long 226 unsigned long find_nth_bit(const unsigned long *addr, unsigned long size, unsigned long n) 229 { 227 { 230 if (n >= size) 228 if (n >= size) 231 return size; 229 return size; 232 230 233 if (small_const_nbits(size)) { 231 if (small_const_nbits(size)) { 234 unsigned long val = *addr & G 232 unsigned long val = *addr & GENMASK(size - 1, 0); 235 233 236 return val ? fns(val, n) : siz 234 return val ? fns(val, n) : size; 237 } 235 } 238 236 239 return __find_nth_bit(addr, size, n); 237 return __find_nth_bit(addr, size, n); 240 } 238 } 241 239 242 /** 240 /** 243 * find_nth_and_bit - find N'th set bit in 2 m 241 * find_nth_and_bit - find N'th set bit in 2 memory regions 244 * @addr1: The 1st address to start the search 242 * @addr1: The 1st address to start the search at 245 * @addr2: The 2nd address to start the search 243 * @addr2: The 2nd address to start the search at 246 * @size: The maximum number of bits to search 244 * @size: The maximum number of bits to search 247 * @n: The number of set bit, which position i 245 * @n: The number of set bit, which position is needed, counting from 0 248 * 246 * 249 * Returns the bit number of the N'th set bit. 247 * Returns the bit number of the N'th set bit. 250 * If no such, returns @size. 248 * If no such, returns @size. 251 */ 249 */ 252 static inline 250 static inline 253 unsigned long find_nth_and_bit(const unsigned 251 unsigned long find_nth_and_bit(const unsigned long *addr1, const unsigned long *addr2, 254 unsigned long 252 unsigned long size, unsigned long n) 255 { 253 { 256 if (n >= size) 254 if (n >= size) 257 return size; 255 return size; 258 256 259 if (small_const_nbits(size)) { 257 if (small_const_nbits(size)) { 260 unsigned long val = *addr1 & 258 unsigned long val = *addr1 & *addr2 & GENMASK(size - 1, 0); 261 259 262 return val ? fns(val, n) : siz 260 return val ? fns(val, n) : size; 263 } 261 } 264 262 265 return __find_nth_and_bit(addr1, addr2 263 return __find_nth_and_bit(addr1, addr2, size, n); 266 } 264 } 267 265 268 /** 266 /** 269 * find_nth_andnot_bit - find N'th set bit in 267 * find_nth_andnot_bit - find N'th set bit in 2 memory regions, 270 * flipping bits in 2nd 268 * flipping bits in 2nd region 271 * @addr1: The 1st address to start the search 269 * @addr1: The 1st address to start the search at 272 * @addr2: The 2nd address to start the search 270 * @addr2: The 2nd address to start the search at 273 * @size: The maximum number of bits to search 271 * @size: The maximum number of bits to search 274 * @n: The number of set bit, which position i 272 * @n: The number of set bit, which position is needed, counting from 0 275 * 273 * 276 * Returns the bit number of the N'th set bit. 274 * Returns the bit number of the N'th set bit. 277 * If no such, returns @size. 275 * If no such, returns @size. 278 */ 276 */ 279 static inline 277 static inline 280 unsigned long find_nth_andnot_bit(const unsign 278 unsigned long find_nth_andnot_bit(const unsigned long *addr1, const unsigned long *addr2, 281 unsigned long 279 unsigned long size, unsigned long n) 282 { 280 { 283 if (n >= size) 281 if (n >= size) 284 return size; 282 return size; 285 283 286 if (small_const_nbits(size)) { 284 if (small_const_nbits(size)) { 287 unsigned long val = *addr1 & 285 unsigned long val = *addr1 & (~*addr2) & GENMASK(size - 1, 0); 288 286 289 return val ? fns(val, n) : siz 287 return val ? fns(val, n) : size; 290 } 288 } 291 289 292 return __find_nth_andnot_bit(addr1, ad 290 return __find_nth_andnot_bit(addr1, addr2, size, n); 293 } 291 } 294 292 295 /** 293 /** 296 * find_nth_and_andnot_bit - find N'th set bit 294 * find_nth_and_andnot_bit - find N'th set bit in 2 memory regions, 297 * excluding those s 295 * excluding those set in 3rd region 298 * @addr1: The 1st address to start the search 296 * @addr1: The 1st address to start the search at 299 * @addr2: The 2nd address to start the search 297 * @addr2: The 2nd address to start the search at 300 * @addr3: The 3rd address to start the search 298 * @addr3: The 3rd address to start the search at 301 * @size: The maximum number of bits to search 299 * @size: The maximum number of bits to search 302 * @n: The number of set bit, which position i 300 * @n: The number of set bit, which position is needed, counting from 0 303 * 301 * 304 * Returns the bit number of the N'th set bit. 302 * Returns the bit number of the N'th set bit. 305 * If no such, returns @size. 303 * If no such, returns @size. 306 */ 304 */ 307 static __always_inline 305 static __always_inline 308 unsigned long find_nth_and_andnot_bit(const un 306 unsigned long find_nth_and_andnot_bit(const unsigned long *addr1, 309 const 307 const unsigned long *addr2, 310 const 308 const unsigned long *addr3, 311 unsign 309 unsigned long size, unsigned long n) 312 { 310 { 313 if (n >= size) 311 if (n >= size) 314 return size; 312 return size; 315 313 316 if (small_const_nbits(size)) { 314 if (small_const_nbits(size)) { 317 unsigned long val = *addr1 & 315 unsigned long val = *addr1 & *addr2 & (~*addr3) & GENMASK(size - 1, 0); 318 316 319 return val ? fns(val, n) : siz 317 return val ? fns(val, n) : size; 320 } 318 } 321 319 322 return __find_nth_and_andnot_bit(addr1 320 return __find_nth_and_andnot_bit(addr1, addr2, addr3, size, n); 323 } 321 } 324 322 325 #ifndef find_first_and_bit 323 #ifndef find_first_and_bit 326 /** 324 /** 327 * find_first_and_bit - find the first set bit 325 * find_first_and_bit - find the first set bit in both memory regions 328 * @addr1: The first address to base the searc 326 * @addr1: The first address to base the search on 329 * @addr2: The second address to base the sear 327 * @addr2: The second address to base the search on 330 * @size: The bitmap size in bits 328 * @size: The bitmap size in bits 331 * 329 * 332 * Returns the bit number for the next set bit 330 * Returns the bit number for the next set bit 333 * If no bits are set, returns @size. 331 * If no bits are set, returns @size. 334 */ 332 */ 335 static inline 333 static inline 336 unsigned long find_first_and_bit(const unsigne 334 unsigned long find_first_and_bit(const unsigned long *addr1, 337 const unsigne 335 const unsigned long *addr2, 338 unsigned long 336 unsigned long size) 339 { 337 { 340 if (small_const_nbits(size)) { 338 if (small_const_nbits(size)) { 341 unsigned long val = *addr1 & * 339 unsigned long val = *addr1 & *addr2 & GENMASK(size - 1, 0); 342 340 343 return val ? __ffs(val) : size 341 return val ? __ffs(val) : size; 344 } 342 } 345 343 346 return _find_first_and_bit(addr1, addr 344 return _find_first_and_bit(addr1, addr2, size); 347 } 345 } 348 #endif 346 #endif 349 347 350 /** << 351 * find_first_and_and_bit - find the first set << 352 * @addr1: The first address to base the searc << 353 * @addr2: The second address to base the sear << 354 * @addr3: The third address to base the searc << 355 * @size: The bitmap size in bits << 356 * << 357 * Returns the bit number for the first set bi << 358 * If no bits are set, returns @size. << 359 */ << 360 static inline << 361 unsigned long find_first_and_and_bit(const uns << 362 const uns << 363 const uns << 364 unsigned << 365 { << 366 if (small_const_nbits(size)) { << 367 unsigned long val = *addr1 & * << 368 << 369 return val ? __ffs(val) : size << 370 } << 371 << 372 return _find_first_and_and_bit(addr1, << 373 } << 374 << 375 #ifndef find_first_zero_bit 348 #ifndef find_first_zero_bit 376 /** 349 /** 377 * find_first_zero_bit - find the first cleare 350 * find_first_zero_bit - find the first cleared bit in a memory region 378 * @addr: The address to start the search at 351 * @addr: The address to start the search at 379 * @size: The maximum number of bits to search 352 * @size: The maximum number of bits to search 380 * 353 * 381 * Returns the bit number of the first cleared 354 * Returns the bit number of the first cleared bit. 382 * If no bits are zero, returns @size. 355 * If no bits are zero, returns @size. 383 */ 356 */ 384 static inline 357 static inline 385 unsigned long find_first_zero_bit(const unsign 358 unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size) 386 { 359 { 387 if (small_const_nbits(size)) { 360 if (small_const_nbits(size)) { 388 unsigned long val = *addr | ~G 361 unsigned long val = *addr | ~GENMASK(size - 1, 0); 389 362 390 return val == ~0UL ? size : ff 363 return val == ~0UL ? size : ffz(val); 391 } 364 } 392 365 393 return _find_first_zero_bit(addr, size 366 return _find_first_zero_bit(addr, size); 394 } 367 } 395 #endif 368 #endif 396 369 397 #ifndef find_last_bit 370 #ifndef find_last_bit 398 /** 371 /** 399 * find_last_bit - find the last set bit in a 372 * find_last_bit - find the last set bit in a memory region 400 * @addr: The address to start the search at 373 * @addr: The address to start the search at 401 * @size: The number of bits to search 374 * @size: The number of bits to search 402 * 375 * 403 * Returns the bit number of the last set bit, 376 * Returns the bit number of the last set bit, or size. 404 */ 377 */ 405 static inline 378 static inline 406 unsigned long find_last_bit(const unsigned lon 379 unsigned long find_last_bit(const unsigned long *addr, unsigned long size) 407 { 380 { 408 if (small_const_nbits(size)) { 381 if (small_const_nbits(size)) { 409 unsigned long val = *addr & GE 382 unsigned long val = *addr & GENMASK(size - 1, 0); 410 383 411 return val ? __fls(val) : size 384 return val ? __fls(val) : size; 412 } 385 } 413 386 414 return _find_last_bit(addr, size); 387 return _find_last_bit(addr, size); 415 } 388 } 416 #endif 389 #endif 417 390 418 /** 391 /** 419 * find_next_and_bit_wrap - find the next set 392 * find_next_and_bit_wrap - find the next set bit in both memory regions 420 * @addr1: The first address to base the searc 393 * @addr1: The first address to base the search on 421 * @addr2: The second address to base the sear 394 * @addr2: The second address to base the search on 422 * @size: The bitmap size in bits 395 * @size: The bitmap size in bits 423 * @offset: The bitnumber to start searching a 396 * @offset: The bitnumber to start searching at 424 * 397 * 425 * Returns the bit number for the next set bit 398 * Returns the bit number for the next set bit, or first set bit up to @offset 426 * If no bits are set, returns @size. 399 * If no bits are set, returns @size. 427 */ 400 */ 428 static inline 401 static inline 429 unsigned long find_next_and_bit_wrap(const uns 402 unsigned long find_next_and_bit_wrap(const unsigned long *addr1, 430 const 403 const unsigned long *addr2, 431 unsign 404 unsigned long size, unsigned long offset) 432 { 405 { 433 unsigned long bit = find_next_and_bit( 406 unsigned long bit = find_next_and_bit(addr1, addr2, size, offset); 434 407 435 if (bit < size || offset == 0) !! 408 if (bit < size) 436 return bit; 409 return bit; 437 410 438 bit = find_first_and_bit(addr1, addr2, 411 bit = find_first_and_bit(addr1, addr2, offset); 439 return bit < offset ? bit : size; 412 return bit < offset ? bit : size; 440 } 413 } 441 414 442 /** 415 /** 443 * find_next_bit_wrap - find the next set bit !! 416 * find_next_bit_wrap - find the next set bit in both memory regions 444 * @addr: The address to base the search on !! 417 * @addr: The first address to base the search on 445 * @size: The bitmap size in bits 418 * @size: The bitmap size in bits 446 * @offset: The bitnumber to start searching a 419 * @offset: The bitnumber to start searching at 447 * 420 * 448 * Returns the bit number for the next set bit 421 * Returns the bit number for the next set bit, or first set bit up to @offset 449 * If no bits are set, returns @size. 422 * If no bits are set, returns @size. 450 */ 423 */ 451 static inline 424 static inline 452 unsigned long find_next_bit_wrap(const unsigne 425 unsigned long find_next_bit_wrap(const unsigned long *addr, 453 unsign 426 unsigned long size, unsigned long offset) 454 { 427 { 455 unsigned long bit = find_next_bit(addr 428 unsigned long bit = find_next_bit(addr, size, offset); 456 429 457 if (bit < size || offset == 0) !! 430 if (bit < size) 458 return bit; 431 return bit; 459 432 460 bit = find_first_bit(addr, offset); 433 bit = find_first_bit(addr, offset); 461 return bit < offset ? bit : size; 434 return bit < offset ? bit : size; 462 } 435 } 463 436 464 /* 437 /* 465 * Helper for for_each_set_bit_wrap(). Make su 438 * Helper for for_each_set_bit_wrap(). Make sure you're doing right thing 466 * before using it alone. 439 * before using it alone. 467 */ 440 */ 468 static inline 441 static inline 469 unsigned long __for_each_wrap(const unsigned l 442 unsigned long __for_each_wrap(const unsigned long *bitmap, unsigned long size, 470 unsigned long 443 unsigned long start, unsigned long n) 471 { 444 { 472 unsigned long bit; 445 unsigned long bit; 473 446 474 /* If not wrapped around */ 447 /* If not wrapped around */ 475 if (n > start) { 448 if (n > start) { 476 /* and have a bit, just return 449 /* and have a bit, just return it. */ 477 bit = find_next_bit(bitmap, si 450 bit = find_next_bit(bitmap, size, n); 478 if (bit < size) 451 if (bit < size) 479 return bit; 452 return bit; 480 453 481 /* Otherwise, wrap around and 454 /* Otherwise, wrap around and ... */ 482 n = 0; 455 n = 0; 483 } 456 } 484 457 485 /* Search the other part. */ 458 /* Search the other part. */ 486 bit = find_next_bit(bitmap, start, n); 459 bit = find_next_bit(bitmap, start, n); 487 return bit < start ? bit : size; 460 return bit < start ? bit : size; 488 } 461 } 489 462 490 /** 463 /** 491 * find_next_clump8 - find next 8-bit clump wi 464 * find_next_clump8 - find next 8-bit clump with set bits in a memory region 492 * @clump: location to store copy of found clu 465 * @clump: location to store copy of found clump 493 * @addr: address to base the search on 466 * @addr: address to base the search on 494 * @size: bitmap size in number of bits 467 * @size: bitmap size in number of bits 495 * @offset: bit offset at which to start searc 468 * @offset: bit offset at which to start searching 496 * 469 * 497 * Returns the bit offset for the next set clu 470 * Returns the bit offset for the next set clump; the found clump value is 498 * copied to the location pointed by @clump. I 471 * copied to the location pointed by @clump. If no bits are set, returns @size. 499 */ 472 */ 500 extern unsigned long find_next_clump8(unsigned 473 extern unsigned long find_next_clump8(unsigned long *clump, 501 const un 474 const unsigned long *addr, 502 unsigned 475 unsigned long size, unsigned long offset); 503 476 504 #define find_first_clump8(clump, bits, size) \ 477 #define find_first_clump8(clump, bits, size) \ 505 find_next_clump8((clump), (bits), (siz 478 find_next_clump8((clump), (bits), (size), 0) 506 479 507 #if defined(__LITTLE_ENDIAN) 480 #if defined(__LITTLE_ENDIAN) 508 481 509 static inline unsigned long find_next_zero_bit 482 static inline unsigned long find_next_zero_bit_le(const void *addr, 510 unsigned long size, unsigned l 483 unsigned long size, unsigned long offset) 511 { 484 { 512 return find_next_zero_bit(addr, size, 485 return find_next_zero_bit(addr, size, offset); 513 } 486 } 514 487 515 static inline unsigned long find_next_bit_le(c 488 static inline unsigned long find_next_bit_le(const void *addr, 516 unsigned long size, unsigned l 489 unsigned long size, unsigned long offset) 517 { 490 { 518 return find_next_bit(addr, size, offse 491 return find_next_bit(addr, size, offset); 519 } 492 } 520 493 521 static inline unsigned long find_first_zero_bi 494 static inline unsigned long find_first_zero_bit_le(const void *addr, 522 unsigned long size) 495 unsigned long size) 523 { 496 { 524 return find_first_zero_bit(addr, size) 497 return find_first_zero_bit(addr, size); 525 } 498 } 526 499 527 #elif defined(__BIG_ENDIAN) 500 #elif defined(__BIG_ENDIAN) 528 501 529 #ifndef find_next_zero_bit_le 502 #ifndef find_next_zero_bit_le 530 static inline 503 static inline 531 unsigned long find_next_zero_bit_le(const void 504 unsigned long find_next_zero_bit_le(const void *addr, unsigned 532 long size, unsigned long offse 505 long size, unsigned long offset) 533 { 506 { 534 if (small_const_nbits(size)) { 507 if (small_const_nbits(size)) { 535 unsigned long val = *(const un 508 unsigned long val = *(const unsigned long *)addr; 536 509 537 if (unlikely(offset >= size)) 510 if (unlikely(offset >= size)) 538 return size; 511 return size; 539 512 540 val = swab(val) | ~GENMASK(siz 513 val = swab(val) | ~GENMASK(size - 1, offset); 541 return val == ~0UL ? size : ff 514 return val == ~0UL ? size : ffz(val); 542 } 515 } 543 516 544 return _find_next_zero_bit_le(addr, si 517 return _find_next_zero_bit_le(addr, size, offset); 545 } 518 } 546 #endif 519 #endif 547 520 548 #ifndef find_first_zero_bit_le 521 #ifndef find_first_zero_bit_le 549 static inline 522 static inline 550 unsigned long find_first_zero_bit_le(const voi 523 unsigned long find_first_zero_bit_le(const void *addr, unsigned long size) 551 { 524 { 552 if (small_const_nbits(size)) { 525 if (small_const_nbits(size)) { 553 unsigned long val = swab(*(con 526 unsigned long val = swab(*(const unsigned long *)addr) | ~GENMASK(size - 1, 0); 554 527 555 return val == ~0UL ? size : ff 528 return val == ~0UL ? size : ffz(val); 556 } 529 } 557 530 558 return _find_first_zero_bit_le(addr, s 531 return _find_first_zero_bit_le(addr, size); 559 } 532 } 560 #endif 533 #endif 561 534 562 #ifndef find_next_bit_le 535 #ifndef find_next_bit_le 563 static inline 536 static inline 564 unsigned long find_next_bit_le(const void *add 537 unsigned long find_next_bit_le(const void *addr, unsigned 565 long size, unsigned long offse 538 long size, unsigned long offset) 566 { 539 { 567 if (small_const_nbits(size)) { 540 if (small_const_nbits(size)) { 568 unsigned long val = *(const un 541 unsigned long val = *(const unsigned long *)addr; 569 542 570 if (unlikely(offset >= size)) 543 if (unlikely(offset >= size)) 571 return size; 544 return size; 572 545 573 val = swab(val) & GENMASK(size 546 val = swab(val) & GENMASK(size - 1, offset); 574 return val ? __ffs(val) : size 547 return val ? __ffs(val) : size; 575 } 548 } 576 549 577 return _find_next_bit_le(addr, size, o 550 return _find_next_bit_le(addr, size, offset); 578 } 551 } 579 #endif 552 #endif 580 553 581 #else 554 #else 582 #error "Please fix <asm/byteorder.h>" 555 #error "Please fix <asm/byteorder.h>" 583 #endif 556 #endif 584 557 585 #define for_each_set_bit(bit, addr, size) \ 558 #define for_each_set_bit(bit, addr, size) \ 586 for ((bit) = 0; (bit) = find_next_bit( 559 for ((bit) = 0; (bit) = find_next_bit((addr), (size), (bit)), (bit) < (size); (bit)++) 587 560 588 #define for_each_and_bit(bit, addr1, addr2, si 561 #define for_each_and_bit(bit, addr1, addr2, size) \ 589 for ((bit) = 0; 562 for ((bit) = 0; \ 590 (bit) = find_next_and_bit((addr1) 563 (bit) = find_next_and_bit((addr1), (addr2), (size), (bit)), (bit) < (size);\ 591 (bit)++) 564 (bit)++) 592 565 593 #define for_each_andnot_bit(bit, addr1, addr2, 566 #define for_each_andnot_bit(bit, addr1, addr2, size) \ 594 for ((bit) = 0; 567 for ((bit) = 0; \ 595 (bit) = find_next_andnot_bit((add 568 (bit) = find_next_andnot_bit((addr1), (addr2), (size), (bit)), (bit) < (size);\ 596 (bit)++) 569 (bit)++) 597 570 598 #define for_each_or_bit(bit, addr1, addr2, siz 571 #define for_each_or_bit(bit, addr1, addr2, size) \ 599 for ((bit) = 0; 572 for ((bit) = 0; \ 600 (bit) = find_next_or_bit((addr1), 573 (bit) = find_next_or_bit((addr1), (addr2), (size), (bit)), (bit) < (size);\ 601 (bit)++) 574 (bit)++) 602 575 603 /* same as for_each_set_bit() but use bit as v 576 /* same as for_each_set_bit() but use bit as value to start with */ 604 #define for_each_set_bit_from(bit, addr, size) 577 #define for_each_set_bit_from(bit, addr, size) \ 605 for (; (bit) = find_next_bit((addr), ( 578 for (; (bit) = find_next_bit((addr), (size), (bit)), (bit) < (size); (bit)++) 606 579 607 #define for_each_clear_bit(bit, addr, size) \ 580 #define for_each_clear_bit(bit, addr, size) \ 608 for ((bit) = 0; 581 for ((bit) = 0; \ 609 (bit) = find_next_zero_bit((addr) 582 (bit) = find_next_zero_bit((addr), (size), (bit)), (bit) < (size); \ 610 (bit)++) 583 (bit)++) 611 584 612 /* same as for_each_clear_bit() but use bit as 585 /* same as for_each_clear_bit() but use bit as value to start with */ 613 #define for_each_clear_bit_from(bit, addr, siz 586 #define for_each_clear_bit_from(bit, addr, size) \ 614 for (; (bit) = find_next_zero_bit((add 587 for (; (bit) = find_next_zero_bit((addr), (size), (bit)), (bit) < (size); (bit)++) 615 588 616 /** 589 /** 617 * for_each_set_bitrange - iterate over all se 590 * for_each_set_bitrange - iterate over all set bit ranges [b; e) 618 * @b: bit offset of start of current bitrange 591 * @b: bit offset of start of current bitrange (first set bit) 619 * @e: bit offset of end of current bitrange ( 592 * @e: bit offset of end of current bitrange (first unset bit) 620 * @addr: bitmap address to base the search on 593 * @addr: bitmap address to base the search on 621 * @size: bitmap size in number of bits 594 * @size: bitmap size in number of bits 622 */ 595 */ 623 #define for_each_set_bitrange(b, e, addr, size 596 #define for_each_set_bitrange(b, e, addr, size) \ 624 for ((b) = 0; 597 for ((b) = 0; \ 625 (b) = find_next_bit((addr), (size 598 (b) = find_next_bit((addr), (size), b), \ 626 (e) = find_next_zero_bit((addr), 599 (e) = find_next_zero_bit((addr), (size), (b) + 1), \ 627 (b) < (size); 600 (b) < (size); \ 628 (b) = (e) + 1) 601 (b) = (e) + 1) 629 602 630 /** 603 /** 631 * for_each_set_bitrange_from - iterate over a 604 * for_each_set_bitrange_from - iterate over all set bit ranges [b; e) 632 * @b: bit offset of start of current bitrange 605 * @b: bit offset of start of current bitrange (first set bit); must be initialized 633 * @e: bit offset of end of current bitrange ( 606 * @e: bit offset of end of current bitrange (first unset bit) 634 * @addr: bitmap address to base the search on 607 * @addr: bitmap address to base the search on 635 * @size: bitmap size in number of bits 608 * @size: bitmap size in number of bits 636 */ 609 */ 637 #define for_each_set_bitrange_from(b, e, addr, 610 #define for_each_set_bitrange_from(b, e, addr, size) \ 638 for (; 611 for (; \ 639 (b) = find_next_bit((addr), (size 612 (b) = find_next_bit((addr), (size), (b)), \ 640 (e) = find_next_zero_bit((addr), 613 (e) = find_next_zero_bit((addr), (size), (b) + 1), \ 641 (b) < (size); 614 (b) < (size); \ 642 (b) = (e) + 1) 615 (b) = (e) + 1) 643 616 644 /** 617 /** 645 * for_each_clear_bitrange - iterate over all 618 * for_each_clear_bitrange - iterate over all unset bit ranges [b; e) 646 * @b: bit offset of start of current bitrange 619 * @b: bit offset of start of current bitrange (first unset bit) 647 * @e: bit offset of end of current bitrange ( 620 * @e: bit offset of end of current bitrange (first set bit) 648 * @addr: bitmap address to base the search on 621 * @addr: bitmap address to base the search on 649 * @size: bitmap size in number of bits 622 * @size: bitmap size in number of bits 650 */ 623 */ 651 #define for_each_clear_bitrange(b, e, addr, si 624 #define for_each_clear_bitrange(b, e, addr, size) \ 652 for ((b) = 0; 625 for ((b) = 0; \ 653 (b) = find_next_zero_bit((addr), 626 (b) = find_next_zero_bit((addr), (size), (b)), \ 654 (e) = find_next_bit((addr), (size 627 (e) = find_next_bit((addr), (size), (b) + 1), \ 655 (b) < (size); 628 (b) < (size); \ 656 (b) = (e) + 1) 629 (b) = (e) + 1) 657 630 658 /** 631 /** 659 * for_each_clear_bitrange_from - iterate over 632 * for_each_clear_bitrange_from - iterate over all unset bit ranges [b; e) 660 * @b: bit offset of start of current bitrange 633 * @b: bit offset of start of current bitrange (first set bit); must be initialized 661 * @e: bit offset of end of current bitrange ( 634 * @e: bit offset of end of current bitrange (first unset bit) 662 * @addr: bitmap address to base the search on 635 * @addr: bitmap address to base the search on 663 * @size: bitmap size in number of bits 636 * @size: bitmap size in number of bits 664 */ 637 */ 665 #define for_each_clear_bitrange_from(b, e, add 638 #define for_each_clear_bitrange_from(b, e, addr, size) \ 666 for (; 639 for (; \ 667 (b) = find_next_zero_bit((addr), 640 (b) = find_next_zero_bit((addr), (size), (b)), \ 668 (e) = find_next_bit((addr), (size 641 (e) = find_next_bit((addr), (size), (b) + 1), \ 669 (b) < (size); 642 (b) < (size); \ 670 (b) = (e) + 1) 643 (b) = (e) + 1) 671 644 672 /** 645 /** 673 * for_each_set_bit_wrap - iterate over all se 646 * for_each_set_bit_wrap - iterate over all set bits starting from @start, and 674 * wrapping around the end of bitmap. 647 * wrapping around the end of bitmap. 675 * @bit: offset for current iteration 648 * @bit: offset for current iteration 676 * @addr: bitmap address to base the search on 649 * @addr: bitmap address to base the search on 677 * @size: bitmap size in number of bits 650 * @size: bitmap size in number of bits 678 * @start: Starting bit for bitmap traversing, 651 * @start: Starting bit for bitmap traversing, wrapping around the bitmap end 679 */ 652 */ 680 #define for_each_set_bit_wrap(bit, addr, size, 653 #define for_each_set_bit_wrap(bit, addr, size, start) \ 681 for ((bit) = find_next_bit_wrap((addr) 654 for ((bit) = find_next_bit_wrap((addr), (size), (start)); \ 682 (bit) < (size); 655 (bit) < (size); \ 683 (bit) = __for_each_wrap((addr), ( 656 (bit) = __for_each_wrap((addr), (size), (start), (bit) + 1)) 684 657 685 /** 658 /** 686 * for_each_set_clump8 - iterate over bitmap f 659 * for_each_set_clump8 - iterate over bitmap for each 8-bit clump with set bits 687 * @start: bit offset to start search and to s 660 * @start: bit offset to start search and to store the current iteration offset 688 * @clump: location to store copy of current 8 661 * @clump: location to store copy of current 8-bit clump 689 * @bits: bitmap address to base the search on 662 * @bits: bitmap address to base the search on 690 * @size: bitmap size in number of bits 663 * @size: bitmap size in number of bits 691 */ 664 */ 692 #define for_each_set_clump8(start, clump, bits 665 #define for_each_set_clump8(start, clump, bits, size) \ 693 for ((start) = find_first_clump8(&(clu 666 for ((start) = find_first_clump8(&(clump), (bits), (size)); \ 694 (start) < (size); \ 667 (start) < (size); \ 695 (start) = find_next_clump8(&(clum 668 (start) = find_next_clump8(&(clump), (bits), (size), (start) + 8)) 696 669 697 #endif /*__LINUX_FIND_H_ */ 670 #endif /*__LINUX_FIND_H_ */ 698 671
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.