find_bit.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. /*
  24. * This is a common helper function for find_next_bit and
  25. * find_next_zero_bit. The difference is the "invert" argument, which
  26. * is XORed with each fetched word before searching it for one bits.
  27. */
  28. static unsigned long _find_next_bit(const unsigned long *addr,
  29. unsigned long nbits, unsigned long start, unsigned long invert)
  30. {
  31. unsigned long tmp;
  32. if (!nbits || start >= nbits)
  33. return nbits;
  34. tmp = addr[start / BITS_PER_LONG] ^ invert;
  35. /* Handle 1st word. */
  36. tmp &= BITMAP_FIRST_WORD_MASK(start);
  37. start = round_down(start, BITS_PER_LONG);
  38. while (!tmp) {
  39. start += BITS_PER_LONG;
  40. if (start >= nbits)
  41. return nbits;
  42. tmp = addr[start / BITS_PER_LONG] ^ invert;
  43. }
  44. return min(start + __ffs(tmp), nbits);
  45. }
  46. #endif
  47. #ifndef find_next_bit
  48. /*
  49. * Find the next set bit in a memory region.
  50. */
  51. unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
  52. unsigned long offset)
  53. {
  54. return _find_next_bit(addr, size, offset, 0UL);
  55. }
  56. EXPORT_SYMBOL(find_next_bit);
  57. #endif
  58. #ifndef find_next_zero_bit
  59. unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
  60. unsigned long offset)
  61. {
  62. return _find_next_bit(addr, size, offset, ~0UL);
  63. }
  64. EXPORT_SYMBOL(find_next_zero_bit);
  65. #endif
  66. #ifndef find_first_bit
  67. /*
  68. * Find the first set bit in a memory region.
  69. */
  70. unsigned long find_first_bit(const unsigned long *addr, unsigned long size)
  71. {
  72. unsigned long idx;
  73. for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
  74. if (addr[idx])
  75. return min(idx * BITS_PER_LONG + __ffs(addr[idx]), size);
  76. }
  77. return size;
  78. }
  79. EXPORT_SYMBOL(find_first_bit);
  80. #endif
  81. #ifndef find_first_zero_bit
  82. /*
  83. * Find the first cleared bit in a memory region.
  84. */
  85. unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size)
  86. {
  87. unsigned long idx;
  88. for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
  89. if (addr[idx] != ~0UL)
  90. return min(idx * BITS_PER_LONG + ffz(addr[idx]), size);
  91. }
  92. return size;
  93. }
  94. EXPORT_SYMBOL(find_first_zero_bit);
  95. #endif
  96. #ifndef find_last_bit
  97. unsigned long find_last_bit(const unsigned long *addr, unsigned long size)
  98. {
  99. if (size) {
  100. unsigned long val = BITMAP_LAST_WORD_MASK(size);
  101. unsigned long idx = (size-1) / BITS_PER_LONG;
  102. do {
  103. val &= addr[idx];
  104. if (val)
  105. return idx * BITS_PER_LONG + __fls(val);
  106. val = ~0ul;
  107. } while (idx--);
  108. }
  109. return size;
  110. }
  111. EXPORT_SYMBOL(find_last_bit);
  112. #endif
  113. #ifdef __BIG_ENDIAN
  114. /* include/linux/byteorder does not support "unsigned long" type */
  115. static inline unsigned long ext2_swab(const unsigned long y)
  116. {
  117. #if BITS_PER_LONG == 64
  118. return (unsigned long) __swab64((u64) y);
  119. #elif BITS_PER_LONG == 32
  120. return (unsigned long) __swab32((u32) y);
  121. #else
  122. #error BITS_PER_LONG not defined
  123. #endif
  124. }
  125. #if !defined(find_next_bit_le) || !defined(find_next_zero_bit_le)
  126. static unsigned long _find_next_bit_le(const unsigned long *addr,
  127. unsigned long nbits, unsigned long start, unsigned long invert)
  128. {
  129. unsigned long tmp;
  130. if (!nbits || start >= nbits)
  131. return nbits;
  132. tmp = addr[start / BITS_PER_LONG] ^ invert;
  133. /* Handle 1st word. */
  134. tmp &= ext2_swab(BITMAP_FIRST_WORD_MASK(start));
  135. start = round_down(start, BITS_PER_LONG);
  136. while (!tmp) {
  137. start += BITS_PER_LONG;
  138. if (start >= nbits)
  139. return nbits;
  140. tmp = addr[start / BITS_PER_LONG] ^ invert;
  141. }
  142. return min(start + __ffs(ext2_swab(tmp)), nbits);
  143. }
  144. #endif
  145. #ifndef find_next_zero_bit_le
  146. unsigned long find_next_zero_bit_le(const void *addr, unsigned
  147. long size, unsigned long offset)
  148. {
  149. return _find_next_bit_le(addr, size, offset, ~0UL);
  150. }
  151. EXPORT_SYMBOL(find_next_zero_bit_le);
  152. #endif
  153. #ifndef find_next_bit_le
  154. unsigned long find_next_bit_le(const void *addr, unsigned
  155. long size, unsigned long offset)
  156. {
  157. return _find_next_bit_le(addr, size, offset, 0UL);
  158. }
  159. EXPORT_SYMBOL(find_next_bit_le);
  160. #endif
  161. #endif /* __BIG_ENDIAN */