find.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __LINUX_FIND_H_
  3. #define __LINUX_FIND_H_
  4. #ifndef __LINUX_BITMAP_H
  5. #error only <linux/bitmap.h> can be included directly
  6. #endif
  7. #include <linux/bitops.h>
  8. unsigned long _find_next_bit(const unsigned long *addr1, unsigned long nbits,
  9. unsigned long start);
  10. unsigned long _find_next_and_bit(const unsigned long *addr1, const unsigned long *addr2,
  11. unsigned long nbits, unsigned long start);
  12. unsigned long _find_next_andnot_bit(const unsigned long *addr1, const unsigned long *addr2,
  13. unsigned long nbits, unsigned long start);
  14. unsigned long _find_next_zero_bit(const unsigned long *addr, unsigned long nbits,
  15. unsigned long start);
  16. extern unsigned long _find_first_bit(const unsigned long *addr, unsigned long size);
  17. unsigned long __find_nth_bit(const unsigned long *addr, unsigned long size, unsigned long n);
  18. unsigned long __find_nth_and_bit(const unsigned long *addr1, const unsigned long *addr2,
  19. unsigned long size, unsigned long n);
  20. unsigned long __find_nth_andnot_bit(const unsigned long *addr1, const unsigned long *addr2,
  21. unsigned long size, unsigned long n);
  22. extern unsigned long _find_first_and_bit(const unsigned long *addr1,
  23. const unsigned long *addr2, unsigned long size);
  24. extern unsigned long _find_first_zero_bit(const unsigned long *addr, unsigned long size);
  25. extern unsigned long _find_last_bit(const unsigned long *addr, unsigned long size);
  26. #ifdef __BIG_ENDIAN
  27. unsigned long _find_first_zero_bit_le(const unsigned long *addr, unsigned long size);
  28. unsigned long _find_next_zero_bit_le(const unsigned long *addr, unsigned
  29. long size, unsigned long offset);
  30. unsigned long _find_next_bit_le(const unsigned long *addr, unsigned
  31. long size, unsigned long offset);
  32. #endif
  33. #ifndef find_next_bit
  34. /**
  35. * find_next_bit - find the next set bit in a memory region
  36. * @addr: The address to base the search on
  37. * @size: The bitmap size in bits
  38. * @offset: The bitnumber to start searching at
  39. *
  40. * Returns the bit number for the next set bit
  41. * If no bits are set, returns @size.
  42. */
  43. static inline
  44. unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
  45. unsigned long offset)
  46. {
  47. if (small_const_nbits(size)) {
  48. unsigned long val;
  49. if (unlikely(offset >= size))
  50. return size;
  51. val = *addr & GENMASK(size - 1, offset);
  52. return val ? __ffs(val) : size;
  53. }
  54. return _find_next_bit(addr, size, offset);
  55. }
  56. #endif
  57. #ifndef find_next_and_bit
  58. /**
  59. * find_next_and_bit - find the next set bit in both memory regions
  60. * @addr1: The first address to base the search on
  61. * @addr2: The second address to base the search on
  62. * @size: The bitmap size in bits
  63. * @offset: The bitnumber to start searching at
  64. *
  65. * Returns the bit number for the next set bit
  66. * If no bits are set, returns @size.
  67. */
  68. static inline
  69. unsigned long find_next_and_bit(const unsigned long *addr1,
  70. const unsigned long *addr2, unsigned long size,
  71. unsigned long offset)
  72. {
  73. if (small_const_nbits(size)) {
  74. unsigned long val;
  75. if (unlikely(offset >= size))
  76. return size;
  77. val = *addr1 & *addr2 & GENMASK(size - 1, offset);
  78. return val ? __ffs(val) : size;
  79. }
  80. return _find_next_and_bit(addr1, addr2, size, offset);
  81. }
  82. #endif
  83. #ifndef find_next_andnot_bit
  84. /**
  85. * find_next_andnot_bit - find the next set bit in *addr1 excluding all the bits
  86. * in *addr2
  87. * @addr1: The first address to base the search on
  88. * @addr2: The second address to base the search on
  89. * @size: The bitmap size in bits
  90. * @offset: The bitnumber to start searching at
  91. *
  92. * Returns the bit number for the next set bit
  93. * If no bits are set, returns @size.
  94. */
  95. static inline
  96. unsigned long find_next_andnot_bit(const unsigned long *addr1,
  97. const unsigned long *addr2, unsigned long size,
  98. unsigned long offset)
  99. {
  100. if (small_const_nbits(size)) {
  101. unsigned long val;
  102. if (unlikely(offset >= size))
  103. return size;
  104. val = *addr1 & ~*addr2 & GENMASK(size - 1, offset);
  105. return val ? __ffs(val) : size;
  106. }
  107. return _find_next_andnot_bit(addr1, addr2, size, offset);
  108. }
  109. #endif
  110. #ifndef find_next_zero_bit
  111. /**
  112. * find_next_zero_bit - find the next cleared bit in a memory region
  113. * @addr: The address to base the search on
  114. * @size: The bitmap size in bits
  115. * @offset: The bitnumber to start searching at
  116. *
  117. * Returns the bit number of the next zero bit
  118. * If no bits are zero, returns @size.
  119. */
  120. static inline
  121. unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
  122. unsigned long offset)
  123. {
  124. if (small_const_nbits(size)) {
  125. unsigned long val;
  126. if (unlikely(offset >= size))
  127. return size;
  128. val = *addr | ~GENMASK(size - 1, offset);
  129. return val == ~0UL ? size : ffz(val);
  130. }
  131. return _find_next_zero_bit(addr, size, offset);
  132. }
  133. #endif
  134. #ifndef find_first_bit
  135. /**
  136. * find_first_bit - find the first set bit in a memory region
  137. * @addr: The address to start the search at
  138. * @size: The maximum number of bits to search
  139. *
  140. * Returns the bit number of the first set bit.
  141. * If no bits are set, returns @size.
  142. */
  143. static inline
  144. unsigned long find_first_bit(const unsigned long *addr, unsigned long size)
  145. {
  146. if (small_const_nbits(size)) {
  147. unsigned long val = *addr & GENMASK(size - 1, 0);
  148. return val ? __ffs(val) : size;
  149. }
  150. return _find_first_bit(addr, size);
  151. }
  152. #endif
  153. /**
  154. * find_nth_bit - find N'th set bit in a memory region
  155. * @addr: The address to start the search at
  156. * @size: The maximum number of bits to search
  157. * @n: The number of set bit, which position is needed, counting from 0
  158. *
  159. * The following is semantically equivalent:
  160. * idx = find_nth_bit(addr, size, 0);
  161. * idx = find_first_bit(addr, size);
  162. *
  163. * Returns the bit number of the N'th set bit.
  164. * If no such, returns @size.
  165. */
  166. static inline
  167. unsigned long find_nth_bit(const unsigned long *addr, unsigned long size, unsigned long n)
  168. {
  169. if (n >= size)
  170. return size;
  171. if (small_const_nbits(size)) {
  172. unsigned long val = *addr & GENMASK(size - 1, 0);
  173. return val ? fns(val, n) : size;
  174. }
  175. return __find_nth_bit(addr, size, n);
  176. }
  177. /**
  178. * find_nth_and_bit - find N'th set bit in 2 memory regions
  179. * @addr1: The 1st address to start the search at
  180. * @addr2: The 2nd address to start the search at
  181. * @size: The maximum number of bits to search
  182. * @n: The number of set bit, which position is needed, counting from 0
  183. *
  184. * Returns the bit number of the N'th set bit.
  185. * If no such, returns @size.
  186. */
  187. static inline
  188. unsigned long find_nth_and_bit(const unsigned long *addr1, const unsigned long *addr2,
  189. unsigned long size, unsigned long n)
  190. {
  191. if (n >= size)
  192. return size;
  193. if (small_const_nbits(size)) {
  194. unsigned long val = *addr1 & *addr2 & GENMASK(size - 1, 0);
  195. return val ? fns(val, n) : size;
  196. }
  197. return __find_nth_and_bit(addr1, addr2, size, n);
  198. }
  199. /**
  200. * find_nth_andnot_bit - find N'th set bit in 2 memory regions,
  201. * flipping bits in 2nd region
  202. * @addr1: The 1st address to start the search at
  203. * @addr2: The 2nd address to start the search at
  204. * @size: The maximum number of bits to search
  205. * @n: The number of set bit, which position is needed, counting from 0
  206. *
  207. * Returns the bit number of the N'th set bit.
  208. * If no such, returns @size.
  209. */
  210. static inline
  211. unsigned long find_nth_andnot_bit(const unsigned long *addr1, const unsigned long *addr2,
  212. unsigned long size, unsigned long n)
  213. {
  214. if (n >= size)
  215. return size;
  216. if (small_const_nbits(size)) {
  217. unsigned long val = *addr1 & (~*addr2) & GENMASK(size - 1, 0);
  218. return val ? fns(val, n) : size;
  219. }
  220. return __find_nth_andnot_bit(addr1, addr2, size, n);
  221. }
  222. #ifndef find_first_and_bit
  223. /**
  224. * find_first_and_bit - find the first set bit in both memory regions
  225. * @addr1: The first address to base the search on
  226. * @addr2: The second address to base the search on
  227. * @size: The bitmap size in bits
  228. *
  229. * Returns the bit number for the next set bit
  230. * If no bits are set, returns @size.
  231. */
  232. static inline
  233. unsigned long find_first_and_bit(const unsigned long *addr1,
  234. const unsigned long *addr2,
  235. unsigned long size)
  236. {
  237. if (small_const_nbits(size)) {
  238. unsigned long val = *addr1 & *addr2 & GENMASK(size - 1, 0);
  239. return val ? __ffs(val) : size;
  240. }
  241. return _find_first_and_bit(addr1, addr2, size);
  242. }
  243. #endif
  244. #ifndef find_first_zero_bit
  245. /**
  246. * find_first_zero_bit - find the first cleared bit in a memory region
  247. * @addr: The address to start the search at
  248. * @size: The maximum number of bits to search
  249. *
  250. * Returns the bit number of the first cleared bit.
  251. * If no bits are zero, returns @size.
  252. */
  253. static inline
  254. unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size)
  255. {
  256. if (small_const_nbits(size)) {
  257. unsigned long val = *addr | ~GENMASK(size - 1, 0);
  258. return val == ~0UL ? size : ffz(val);
  259. }
  260. return _find_first_zero_bit(addr, size);
  261. }
  262. #endif
  263. #ifndef find_last_bit
  264. /**
  265. * find_last_bit - find the last set bit in a memory region
  266. * @addr: The address to start the search at
  267. * @size: The number of bits to search
  268. *
  269. * Returns the bit number of the last set bit, or size.
  270. */
  271. static inline
  272. unsigned long find_last_bit(const unsigned long *addr, unsigned long size)
  273. {
  274. if (small_const_nbits(size)) {
  275. unsigned long val = *addr & GENMASK(size - 1, 0);
  276. return val ? __fls(val) : size;
  277. }
  278. return _find_last_bit(addr, size);
  279. }
  280. #endif
  281. /**
  282. * find_next_and_bit_wrap - find the next set bit in both memory regions
  283. * @addr1: The first address to base the search on
  284. * @addr2: The second address to base the search on
  285. * @size: The bitmap size in bits
  286. * @offset: The bitnumber to start searching at
  287. *
  288. * Returns the bit number for the next set bit, or first set bit up to @offset
  289. * If no bits are set, returns @size.
  290. */
  291. static inline
  292. unsigned long find_next_and_bit_wrap(const unsigned long *addr1,
  293. const unsigned long *addr2,
  294. unsigned long size, unsigned long offset)
  295. {
  296. unsigned long bit = find_next_and_bit(addr1, addr2, size, offset);
  297. if (bit < size)
  298. return bit;
  299. bit = find_first_and_bit(addr1, addr2, offset);
  300. return bit < offset ? bit : size;
  301. }
  302. /**
  303. * find_next_bit_wrap - find the next set bit in both memory regions
  304. * @addr: The first address to base the search on
  305. * @size: The bitmap size in bits
  306. * @offset: The bitnumber to start searching at
  307. *
  308. * Returns the bit number for the next set bit, or first set bit up to @offset
  309. * If no bits are set, returns @size.
  310. */
  311. static inline
  312. unsigned long find_next_bit_wrap(const unsigned long *addr,
  313. unsigned long size, unsigned long offset)
  314. {
  315. unsigned long bit = find_next_bit(addr, size, offset);
  316. if (bit < size)
  317. return bit;
  318. bit = find_first_bit(addr, offset);
  319. return bit < offset ? bit : size;
  320. }
  321. /*
  322. * Helper for for_each_set_bit_wrap(). Make sure you're doing right thing
  323. * before using it alone.
  324. */
  325. static inline
  326. unsigned long __for_each_wrap(const unsigned long *bitmap, unsigned long size,
  327. unsigned long start, unsigned long n)
  328. {
  329. unsigned long bit;
  330. /* If not wrapped around */
  331. if (n > start) {
  332. /* and have a bit, just return it. */
  333. bit = find_next_bit(bitmap, size, n);
  334. if (bit < size)
  335. return bit;
  336. /* Otherwise, wrap around and ... */
  337. n = 0;
  338. }
  339. /* Search the other part. */
  340. bit = find_next_bit(bitmap, start, n);
  341. return bit < start ? bit : size;
  342. }
  343. /**
  344. * find_next_clump8 - find next 8-bit clump with set bits in a memory region
  345. * @clump: location to store copy of found clump
  346. * @addr: address to base the search on
  347. * @size: bitmap size in number of bits
  348. * @offset: bit offset at which to start searching
  349. *
  350. * Returns the bit offset for the next set clump; the found clump value is
  351. * copied to the location pointed by @clump. If no bits are set, returns @size.
  352. */
  353. extern unsigned long find_next_clump8(unsigned long *clump,
  354. const unsigned long *addr,
  355. unsigned long size, unsigned long offset);
  356. #define find_first_clump8(clump, bits, size) \
  357. find_next_clump8((clump), (bits), (size), 0)
  358. #if defined(__LITTLE_ENDIAN)
  359. static inline unsigned long find_next_zero_bit_le(const void *addr,
  360. unsigned long size, unsigned long offset)
  361. {
  362. return find_next_zero_bit(addr, size, offset);
  363. }
  364. static inline unsigned long find_next_bit_le(const void *addr,
  365. unsigned long size, unsigned long offset)
  366. {
  367. return find_next_bit(addr, size, offset);
  368. }
  369. static inline unsigned long find_first_zero_bit_le(const void *addr,
  370. unsigned long size)
  371. {
  372. return find_first_zero_bit(addr, size);
  373. }
  374. #elif defined(__BIG_ENDIAN)
  375. #ifndef find_next_zero_bit_le
  376. static inline
  377. unsigned long find_next_zero_bit_le(const void *addr, unsigned
  378. long size, unsigned long offset)
  379. {
  380. if (small_const_nbits(size)) {
  381. unsigned long val = *(const unsigned long *)addr;
  382. if (unlikely(offset >= size))
  383. return size;
  384. val = swab(val) | ~GENMASK(size - 1, offset);
  385. return val == ~0UL ? size : ffz(val);
  386. }
  387. return _find_next_zero_bit_le(addr, size, offset);
  388. }
  389. #endif
  390. #ifndef find_first_zero_bit_le
  391. static inline
  392. unsigned long find_first_zero_bit_le(const void *addr, unsigned long size)
  393. {
  394. if (small_const_nbits(size)) {
  395. unsigned long val = swab(*(const unsigned long *)addr) | ~GENMASK(size - 1, 0);
  396. return val == ~0UL ? size : ffz(val);
  397. }
  398. return _find_first_zero_bit_le(addr, size);
  399. }
  400. #endif
  401. #ifndef find_next_bit_le
  402. static inline
  403. unsigned long find_next_bit_le(const void *addr, unsigned
  404. long size, unsigned long offset)
  405. {
  406. if (small_const_nbits(size)) {
  407. unsigned long val = *(const unsigned long *)addr;
  408. if (unlikely(offset >= size))
  409. return size;
  410. val = swab(val) & GENMASK(size - 1, offset);
  411. return val ? __ffs(val) : size;
  412. }
  413. return _find_next_bit_le(addr, size, offset);
  414. }
  415. #endif
  416. #else
  417. #error "Please fix <asm/byteorder.h>"
  418. #endif
  419. #define for_each_set_bit(bit, addr, size) \
  420. for ((bit) = 0; (bit) = find_next_bit((addr), (size), (bit)), (bit) < (size); (bit)++)
  421. #define for_each_and_bit(bit, addr1, addr2, size) \
  422. for ((bit) = 0; \
  423. (bit) = find_next_and_bit((addr1), (addr2), (size), (bit)), (bit) < (size);\
  424. (bit)++)
  425. #define for_each_andnot_bit(bit, addr1, addr2, size) \
  426. for ((bit) = 0; \
  427. (bit) = find_next_andnot_bit((addr1), (addr2), (size), (bit)), (bit) < (size);\
  428. (bit)++)
  429. /* same as for_each_set_bit() but use bit as value to start with */
  430. #define for_each_set_bit_from(bit, addr, size) \
  431. for (; (bit) = find_next_bit((addr), (size), (bit)), (bit) < (size); (bit)++)
  432. #define for_each_clear_bit(bit, addr, size) \
  433. for ((bit) = 0; \
  434. (bit) = find_next_zero_bit((addr), (size), (bit)), (bit) < (size); \
  435. (bit)++)
  436. /* same as for_each_clear_bit() but use bit as value to start with */
  437. #define for_each_clear_bit_from(bit, addr, size) \
  438. for (; (bit) = find_next_zero_bit((addr), (size), (bit)), (bit) < (size); (bit)++)
  439. /**
  440. * for_each_set_bitrange - iterate over all set bit ranges [b; e)
  441. * @b: bit offset of start of current bitrange (first set bit)
  442. * @e: bit offset of end of current bitrange (first unset bit)
  443. * @addr: bitmap address to base the search on
  444. * @size: bitmap size in number of bits
  445. */
  446. #define for_each_set_bitrange(b, e, addr, size) \
  447. for ((b) = 0; \
  448. (b) = find_next_bit((addr), (size), b), \
  449. (e) = find_next_zero_bit((addr), (size), (b) + 1), \
  450. (b) < (size); \
  451. (b) = (e) + 1)
  452. /**
  453. * for_each_set_bitrange_from - iterate over all set bit ranges [b; e)
  454. * @b: bit offset of start of current bitrange (first set bit); must be initialized
  455. * @e: bit offset of end of current bitrange (first unset bit)
  456. * @addr: bitmap address to base the search on
  457. * @size: bitmap size in number of bits
  458. */
  459. #define for_each_set_bitrange_from(b, e, addr, size) \
  460. for (; \
  461. (b) = find_next_bit((addr), (size), (b)), \
  462. (e) = find_next_zero_bit((addr), (size), (b) + 1), \
  463. (b) < (size); \
  464. (b) = (e) + 1)
  465. /**
  466. * for_each_clear_bitrange - iterate over all unset bit ranges [b; e)
  467. * @b: bit offset of start of current bitrange (first unset bit)
  468. * @e: bit offset of end of current bitrange (first set bit)
  469. * @addr: bitmap address to base the search on
  470. * @size: bitmap size in number of bits
  471. */
  472. #define for_each_clear_bitrange(b, e, addr, size) \
  473. for ((b) = 0; \
  474. (b) = find_next_zero_bit((addr), (size), (b)), \
  475. (e) = find_next_bit((addr), (size), (b) + 1), \
  476. (b) < (size); \
  477. (b) = (e) + 1)
  478. /**
  479. * for_each_clear_bitrange_from - iterate over all unset bit ranges [b; e)
  480. * @b: bit offset of start of current bitrange (first set bit); must be initialized
  481. * @e: bit offset of end of current bitrange (first unset bit)
  482. * @addr: bitmap address to base the search on
  483. * @size: bitmap size in number of bits
  484. */
  485. #define for_each_clear_bitrange_from(b, e, addr, size) \
  486. for (; \
  487. (b) = find_next_zero_bit((addr), (size), (b)), \
  488. (e) = find_next_bit((addr), (size), (b) + 1), \
  489. (b) < (size); \
  490. (b) = (e) + 1)
  491. /**
  492. * for_each_set_bit_wrap - iterate over all set bits starting from @start, and
  493. * wrapping around the end of bitmap.
  494. * @bit: offset for current iteration
  495. * @addr: bitmap address to base the search on
  496. * @size: bitmap size in number of bits
  497. * @start: Starting bit for bitmap traversing, wrapping around the bitmap end
  498. */
  499. #define for_each_set_bit_wrap(bit, addr, size, start) \
  500. for ((bit) = find_next_bit_wrap((addr), (size), (start)); \
  501. (bit) < (size); \
  502. (bit) = __for_each_wrap((addr), (size), (start), (bit) + 1))
  503. /**
  504. * for_each_set_clump8 - iterate over bitmap for each 8-bit clump with set bits
  505. * @start: bit offset to start search and to store the current iteration offset
  506. * @clump: location to store copy of current 8-bit clump
  507. * @bits: bitmap address to base the search on
  508. * @size: bitmap size in number of bits
  509. */
  510. #define for_each_set_clump8(start, clump, bits, size) \
  511. for ((start) = find_first_clump8(&(clump), (bits), (size)); \
  512. (start) < (size); \
  513. (start) = find_next_clump8(&(clump), (bits), (size), (start) + 8))
  514. #endif /*__LINUX_FIND_H_ */