find_bit.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* bit search implementation
  2. *
  3. * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * Copyright (C) 2008 IBM Corporation
  7. * 'find_last_bit' is written by Rusty Russell <rusty@rustcorp.com.au>
  8. * (Inspired by David Howell's find_next_bit implementation)
  9. *
  10. * Rewritten by Yury Norov <yury.norov@gmail.com> to decrease
  11. * size and improve performance, 2015.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version
  16. * 2 of the License, or (at your option) any later version.
  17. */
  18. #include <linux/bitops.h>
  19. #include <linux/bitmap.h>
  20. #include <linux/export.h>
  21. #include <linux/kernel.h>
  22. #if !defined(find_next_bit) || !defined(find_next_zero_bit) || \
  23. !defined(find_next_and_bit)
  24. /*
  25. * This is a common helper function for find_next_bit, find_next_zero_bit, and
  26. * find_next_and_bit. The differences are:
  27. * - The "invert" argument, which is XORed with each fetched word before
  28. * searching it for one bits.
  29. * - The optional "addr2", which is anded with "addr1" if present.
  30. */
  31. static inline unsigned long _find_next_bit(const unsigned long *addr1,
  32. const unsigned long *addr2, unsigned long nbits,
  33. unsigned long start, unsigned long invert)
  34. {
  35. unsigned long tmp;
  36. if (unlikely(start >= nbits))
  37. return nbits;
  38. tmp = addr1[start / BITS_PER_LONG];
  39. if (addr2)
  40. tmp &= addr2[start / BITS_PER_LONG];
  41. tmp ^= invert;
  42. /* Handle 1st word. */
  43. tmp &= BITMAP_FIRST_WORD_MASK(start);
  44. start = round_down(start, BITS_PER_LONG);
  45. while (!tmp) {
  46. start += BITS_PER_LONG;
  47. if (start >= nbits)
  48. return nbits;
  49. tmp = addr1[start / BITS_PER_LONG];
  50. if (addr2)
  51. tmp &= addr2[start / BITS_PER_LONG];
  52. tmp ^= invert;
  53. }
  54. return min(start + __ffs(tmp), nbits);
  55. }
  56. #endif
  57. #ifndef find_next_bit
  58. /*
  59. * Find the next set bit in a memory region.
  60. */
  61. unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
  62. unsigned long offset)
  63. {
  64. return _find_next_bit(addr, NULL, size, offset, 0UL);
  65. }
  66. EXPORT_SYMBOL(find_next_bit);
  67. #endif
  68. #ifndef find_next_zero_bit
  69. unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
  70. unsigned long offset)
  71. {
  72. return _find_next_bit(addr, NULL, size, offset, ~0UL);
  73. }
  74. EXPORT_SYMBOL(find_next_zero_bit);
  75. #endif
  76. #if !defined(find_next_and_bit)
  77. unsigned long find_next_and_bit(const unsigned long *addr1,
  78. const unsigned long *addr2, unsigned long size,
  79. unsigned long offset)
  80. {
  81. return _find_next_bit(addr1, addr2, size, offset, 0UL);
  82. }
  83. EXPORT_SYMBOL(find_next_and_bit);
  84. #endif
  85. #ifndef find_first_bit
  86. /*
  87. * Find the first set bit in a memory region.
  88. */
  89. unsigned long find_first_bit(const unsigned long *addr, unsigned long size)
  90. {
  91. unsigned long idx;
  92. for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
  93. if (addr[idx])
  94. return min(idx * BITS_PER_LONG + __ffs(addr[idx]), size);
  95. }
  96. return size;
  97. }
  98. EXPORT_SYMBOL(find_first_bit);
  99. #endif
  100. #ifndef find_first_zero_bit
  101. /*
  102. * Find the first cleared bit in a memory region.
  103. */
  104. unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size)
  105. {
  106. unsigned long idx;
  107. for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
  108. if (addr[idx] != ~0UL)
  109. return min(idx * BITS_PER_LONG + ffz(addr[idx]), size);
  110. }
  111. return size;
  112. }
  113. EXPORT_SYMBOL(find_first_zero_bit);
  114. #endif
  115. #ifndef find_last_bit
  116. unsigned long find_last_bit(const unsigned long *addr, unsigned long size)
  117. {
  118. if (size) {
  119. unsigned long val = BITMAP_LAST_WORD_MASK(size);
  120. unsigned long idx = (size-1) / BITS_PER_LONG;
  121. do {
  122. val &= addr[idx];
  123. if (val)
  124. return idx * BITS_PER_LONG + __fls(val);
  125. val = ~0ul;
  126. } while (idx--);
  127. }
  128. return size;
  129. }
  130. EXPORT_SYMBOL(find_last_bit);
  131. #endif
  132. #ifdef __BIG_ENDIAN
  133. /* include/linux/byteorder does not support "unsigned long" type */
  134. static inline unsigned long ext2_swab(const unsigned long y)
  135. {
  136. #if BITS_PER_LONG == 64
  137. return (unsigned long) __swab64((u64) y);
  138. #elif BITS_PER_LONG == 32
  139. return (unsigned long) __swab32((u32) y);
  140. #else
  141. #error BITS_PER_LONG not defined
  142. #endif
  143. }
  144. #if !defined(find_next_bit_le) || !defined(find_next_zero_bit_le)
  145. static inline unsigned long _find_next_bit_le(const unsigned long *addr1,
  146. const unsigned long *addr2, unsigned long nbits,
  147. unsigned long start, unsigned long invert)
  148. {
  149. unsigned long tmp;
  150. if (unlikely(start >= nbits))
  151. return nbits;
  152. tmp = addr1[start / BITS_PER_LONG];
  153. if (addr2)
  154. tmp &= addr2[start / BITS_PER_LONG];
  155. tmp ^= invert;
  156. /* Handle 1st word. */
  157. tmp &= ext2_swab(BITMAP_FIRST_WORD_MASK(start));
  158. start = round_down(start, BITS_PER_LONG);
  159. while (!tmp) {
  160. start += BITS_PER_LONG;
  161. if (start >= nbits)
  162. return nbits;
  163. tmp = addr1[start / BITS_PER_LONG];
  164. if (addr2)
  165. tmp &= addr2[start / BITS_PER_LONG];
  166. tmp ^= invert;
  167. }
  168. return min(start + __ffs(ext2_swab(tmp)), nbits);
  169. }
  170. #endif
  171. #ifndef find_next_zero_bit_le
  172. unsigned long find_next_zero_bit_le(const void *addr, unsigned
  173. long size, unsigned long offset)
  174. {
  175. return _find_next_bit_le(addr, NULL, size, offset, ~0UL);
  176. }
  177. EXPORT_SYMBOL(find_next_zero_bit_le);
  178. #endif
  179. #ifndef find_next_bit_le
  180. unsigned long find_next_bit_le(const void *addr, unsigned
  181. long size, unsigned long offset)
  182. {
  183. return _find_next_bit_le(addr, NULL, size, offset, 0UL);
  184. }
  185. EXPORT_SYMBOL(find_next_bit_le);
  186. #endif
  187. #endif /* __BIG_ENDIAN */