find_bit.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* bit search implementation
  3. *
  4. * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. *
  7. * Copyright (C) 2008 IBM Corporation
  8. * 'find_last_bit' is written by Rusty Russell <rusty@rustcorp.com.au>
  9. * (Inspired by David Howell's find_next_bit implementation)
  10. *
  11. * Rewritten by Yury Norov <yury.norov@gmail.com> to decrease
  12. * size and improve performance, 2015.
  13. */
  14. #include <linux/bitops.h>
  15. #include <linux/bitmap.h>
  16. #include <linux/export.h>
  17. #include <linux/math.h>
  18. #include <linux/minmax.h>
  19. #include <linux/swab.h>
  20. #include <linux/random.h>
  21. /*
  22. * Common helper for find_bit() function family
  23. * @FETCH: The expression that fetches and pre-processes each word of bitmap(s)
  24. * @MUNGE: The expression that post-processes a word containing found bit (may be empty)
  25. * @size: The bitmap size in bits
  26. */
  27. #define FIND_FIRST_BIT(FETCH, MUNGE, size) \
  28. ({ \
  29. unsigned long idx, val, sz = (size); \
  30. \
  31. for (idx = 0; idx * BITS_PER_LONG < sz; idx++) { \
  32. val = (FETCH); \
  33. if (val) { \
  34. sz = min(idx * BITS_PER_LONG + __ffs(MUNGE(val)), sz); \
  35. break; \
  36. } \
  37. } \
  38. \
  39. sz; \
  40. })
  41. /*
  42. * Common helper for find_next_bit() function family
  43. * @FETCH: The expression that fetches and pre-processes each word of bitmap(s)
  44. * @MUNGE: The expression that post-processes a word containing found bit (may be empty)
  45. * @size: The bitmap size in bits
  46. * @start: The bitnumber to start searching at
  47. */
  48. #define FIND_NEXT_BIT(FETCH, MUNGE, size, start) \
  49. ({ \
  50. unsigned long mask, idx, tmp, sz = (size), __start = (start); \
  51. \
  52. if (unlikely(__start >= sz)) \
  53. goto out; \
  54. \
  55. mask = MUNGE(BITMAP_FIRST_WORD_MASK(__start)); \
  56. idx = __start / BITS_PER_LONG; \
  57. \
  58. for (tmp = (FETCH) & mask; !tmp; tmp = (FETCH)) { \
  59. if ((idx + 1) * BITS_PER_LONG >= sz) \
  60. goto out; \
  61. idx++; \
  62. } \
  63. \
  64. sz = min(idx * BITS_PER_LONG + __ffs(MUNGE(tmp)), sz); \
  65. out: \
  66. sz; \
  67. })
  68. #define FIND_NTH_BIT(FETCH, size, num) \
  69. ({ \
  70. unsigned long sz = (size), nr = (num), idx, w, tmp = 0; \
  71. \
  72. for (idx = 0; (idx + 1) * BITS_PER_LONG <= sz; idx++) { \
  73. if (idx * BITS_PER_LONG + nr >= sz) \
  74. goto out; \
  75. \
  76. tmp = (FETCH); \
  77. w = hweight_long(tmp); \
  78. if (w > nr) \
  79. goto found; \
  80. \
  81. nr -= w; \
  82. } \
  83. \
  84. if (sz % BITS_PER_LONG) \
  85. tmp = (FETCH) & BITMAP_LAST_WORD_MASK(sz); \
  86. found: \
  87. sz = idx * BITS_PER_LONG + fns(tmp, nr); \
  88. out: \
  89. sz; \
  90. })
  91. #ifndef find_first_bit
  92. /*
  93. * Find the first set bit in a memory region.
  94. */
  95. unsigned long _find_first_bit(const unsigned long *addr, unsigned long size)
  96. {
  97. return FIND_FIRST_BIT(addr[idx], /* nop */, size);
  98. }
  99. EXPORT_SYMBOL(_find_first_bit);
  100. #endif
  101. #ifndef find_first_and_bit
  102. /*
  103. * Find the first set bit in two memory regions.
  104. */
  105. unsigned long _find_first_and_bit(const unsigned long *addr1,
  106. const unsigned long *addr2,
  107. unsigned long size)
  108. {
  109. return FIND_FIRST_BIT(addr1[idx] & addr2[idx], /* nop */, size);
  110. }
  111. EXPORT_SYMBOL(_find_first_and_bit);
  112. #endif
  113. /*
  114. * Find the first bit set in 1st memory region and unset in 2nd.
  115. */
  116. unsigned long _find_first_andnot_bit(const unsigned long *addr1,
  117. const unsigned long *addr2,
  118. unsigned long size)
  119. {
  120. return FIND_FIRST_BIT(addr1[idx] & ~addr2[idx], /* nop */, size);
  121. }
  122. EXPORT_SYMBOL(_find_first_andnot_bit);
  123. /*
  124. * Find the first set bit in three memory regions.
  125. */
  126. unsigned long _find_first_and_and_bit(const unsigned long *addr1,
  127. const unsigned long *addr2,
  128. const unsigned long *addr3,
  129. unsigned long size)
  130. {
  131. return FIND_FIRST_BIT(addr1[idx] & addr2[idx] & addr3[idx], /* nop */, size);
  132. }
  133. EXPORT_SYMBOL(_find_first_and_and_bit);
  134. #ifndef find_first_zero_bit
  135. /*
  136. * Find the first cleared bit in a memory region.
  137. */
  138. unsigned long _find_first_zero_bit(const unsigned long *addr, unsigned long size)
  139. {
  140. return FIND_FIRST_BIT(~addr[idx], /* nop */, size);
  141. }
  142. EXPORT_SYMBOL(_find_first_zero_bit);
  143. #endif
  144. #ifndef find_next_bit
  145. unsigned long _find_next_bit(const unsigned long *addr, unsigned long nbits, unsigned long start)
  146. {
  147. return FIND_NEXT_BIT(addr[idx], /* nop */, nbits, start);
  148. }
  149. EXPORT_SYMBOL(_find_next_bit);
  150. #endif
  151. unsigned long __find_nth_bit(const unsigned long *addr, unsigned long size, unsigned long n)
  152. {
  153. return FIND_NTH_BIT(addr[idx], size, n);
  154. }
  155. EXPORT_SYMBOL(__find_nth_bit);
  156. unsigned long __find_nth_and_bit(const unsigned long *addr1, const unsigned long *addr2,
  157. unsigned long size, unsigned long n)
  158. {
  159. return FIND_NTH_BIT(addr1[idx] & addr2[idx], size, n);
  160. }
  161. EXPORT_SYMBOL(__find_nth_and_bit);
  162. unsigned long __find_nth_andnot_bit(const unsigned long *addr1, const unsigned long *addr2,
  163. unsigned long size, unsigned long n)
  164. {
  165. return FIND_NTH_BIT(addr1[idx] & ~addr2[idx], size, n);
  166. }
  167. EXPORT_SYMBOL(__find_nth_andnot_bit);
  168. unsigned long __find_nth_and_andnot_bit(const unsigned long *addr1,
  169. const unsigned long *addr2,
  170. const unsigned long *addr3,
  171. unsigned long size, unsigned long n)
  172. {
  173. return FIND_NTH_BIT(addr1[idx] & addr2[idx] & ~addr3[idx], size, n);
  174. }
  175. EXPORT_SYMBOL(__find_nth_and_andnot_bit);
  176. #ifndef find_next_and_bit
  177. unsigned long _find_next_and_bit(const unsigned long *addr1, const unsigned long *addr2,
  178. unsigned long nbits, unsigned long start)
  179. {
  180. return FIND_NEXT_BIT(addr1[idx] & addr2[idx], /* nop */, nbits, start);
  181. }
  182. EXPORT_SYMBOL(_find_next_and_bit);
  183. #endif
  184. #ifndef find_next_andnot_bit
  185. unsigned long _find_next_andnot_bit(const unsigned long *addr1, const unsigned long *addr2,
  186. unsigned long nbits, unsigned long start)
  187. {
  188. return FIND_NEXT_BIT(addr1[idx] & ~addr2[idx], /* nop */, nbits, start);
  189. }
  190. EXPORT_SYMBOL(_find_next_andnot_bit);
  191. #endif
  192. #ifndef find_next_or_bit
  193. unsigned long _find_next_or_bit(const unsigned long *addr1, const unsigned long *addr2,
  194. unsigned long nbits, unsigned long start)
  195. {
  196. return FIND_NEXT_BIT(addr1[idx] | addr2[idx], /* nop */, nbits, start);
  197. }
  198. EXPORT_SYMBOL(_find_next_or_bit);
  199. #endif
  200. #ifndef find_next_zero_bit
  201. unsigned long _find_next_zero_bit(const unsigned long *addr, unsigned long nbits,
  202. unsigned long start)
  203. {
  204. return FIND_NEXT_BIT(~addr[idx], /* nop */, nbits, start);
  205. }
  206. EXPORT_SYMBOL(_find_next_zero_bit);
  207. #endif
  208. #ifndef find_last_bit
  209. unsigned long _find_last_bit(const unsigned long *addr, unsigned long size)
  210. {
  211. if (size) {
  212. unsigned long val = BITMAP_LAST_WORD_MASK(size);
  213. unsigned long idx = (size-1) / BITS_PER_LONG;
  214. do {
  215. val &= addr[idx];
  216. if (val)
  217. return idx * BITS_PER_LONG + __fls(val);
  218. val = ~0ul;
  219. } while (idx--);
  220. }
  221. return size;
  222. }
  223. EXPORT_SYMBOL(_find_last_bit);
  224. #endif
  225. unsigned long find_next_clump8(unsigned long *clump, const unsigned long *addr,
  226. unsigned long size, unsigned long offset)
  227. {
  228. offset = find_next_bit(addr, size, offset);
  229. if (offset == size)
  230. return size;
  231. offset = round_down(offset, 8);
  232. *clump = bitmap_get_value8(addr, offset);
  233. return offset;
  234. }
  235. EXPORT_SYMBOL(find_next_clump8);
  236. #ifdef __BIG_ENDIAN
  237. #ifndef find_first_zero_bit_le
  238. /*
  239. * Find the first cleared bit in an LE memory region.
  240. */
  241. unsigned long _find_first_zero_bit_le(const unsigned long *addr, unsigned long size)
  242. {
  243. return FIND_FIRST_BIT(~addr[idx], swab, size);
  244. }
  245. EXPORT_SYMBOL(_find_first_zero_bit_le);
  246. #endif
  247. #ifndef find_next_zero_bit_le
  248. unsigned long _find_next_zero_bit_le(const unsigned long *addr,
  249. unsigned long size, unsigned long offset)
  250. {
  251. return FIND_NEXT_BIT(~addr[idx], swab, size, offset);
  252. }
  253. EXPORT_SYMBOL(_find_next_zero_bit_le);
  254. #endif
  255. #ifndef find_next_bit_le
  256. unsigned long _find_next_bit_le(const unsigned long *addr,
  257. unsigned long size, unsigned long offset)
  258. {
  259. return FIND_NEXT_BIT(addr[idx], swab, size, offset);
  260. }
  261. EXPORT_SYMBOL(_find_next_bit_le);
  262. #endif
  263. #endif /* __BIG_ENDIAN */
  264. /**
  265. * find_random_bit - find a set bit at random position
  266. * @addr: The address to base the search on
  267. * @size: The bitmap size in bits
  268. *
  269. * Returns: a position of a random set bit; >= @size otherwise
  270. */
  271. unsigned long find_random_bit(const unsigned long *addr, unsigned long size)
  272. {
  273. int w = bitmap_weight(addr, size);
  274. switch (w) {
  275. case 0:
  276. return size;
  277. case 1:
  278. /* Performance trick for single-bit bitmaps */
  279. return find_first_bit(addr, size);
  280. default:
  281. return find_nth_bit(addr, size, get_random_u32_below(w));
  282. }
  283. }
  284. EXPORT_SYMBOL(find_random_bit);