find_next_bit.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* find_next_bit.c: fallback find next bit implementation
  2. *
  3. * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/bitops.h>
  12. #include <linux/module.h>
  13. #include <asm/types.h>
  14. #include <asm/byteorder.h>
  15. #define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
  16. #undef find_next_bit
  17. #undef find_next_zero_bit
  18. /**
  19. * find_next_bit - find the next set bit in a memory region
  20. * @addr: The address to base the search on
  21. * @offset: The bitnumber to start searching at
  22. * @size: The maximum size to search
  23. */
  24. unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
  25. unsigned long offset)
  26. {
  27. const unsigned long *p = addr + BITOP_WORD(offset);
  28. unsigned long result = offset & ~(BITS_PER_LONG-1);
  29. unsigned long tmp;
  30. if (offset >= size)
  31. return size;
  32. size -= result;
  33. offset %= BITS_PER_LONG;
  34. if (offset) {
  35. tmp = *(p++);
  36. tmp &= (~0UL << offset);
  37. if (size < BITS_PER_LONG)
  38. goto found_first;
  39. if (tmp)
  40. goto found_middle;
  41. size -= BITS_PER_LONG;
  42. result += BITS_PER_LONG;
  43. }
  44. while (size & ~(BITS_PER_LONG-1)) {
  45. if ((tmp = *(p++)))
  46. goto found_middle;
  47. result += BITS_PER_LONG;
  48. size -= BITS_PER_LONG;
  49. }
  50. if (!size)
  51. return result;
  52. tmp = *p;
  53. found_first:
  54. tmp &= (~0UL >> (BITS_PER_LONG - size));
  55. if (tmp == 0UL) /* Are any bits set? */
  56. return result + size; /* Nope. */
  57. found_middle:
  58. return result + __ffs(tmp);
  59. }
  60. EXPORT_SYMBOL(find_next_bit);
  61. /*
  62. * This implementation of find_{first,next}_zero_bit was stolen from
  63. * Linus' asm-alpha/bitops.h.
  64. */
  65. unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
  66. unsigned long offset)
  67. {
  68. const unsigned long *p = addr + BITOP_WORD(offset);
  69. unsigned long result = offset & ~(BITS_PER_LONG-1);
  70. unsigned long tmp;
  71. if (offset >= size)
  72. return size;
  73. size -= result;
  74. offset %= BITS_PER_LONG;
  75. if (offset) {
  76. tmp = *(p++);
  77. tmp |= ~0UL >> (BITS_PER_LONG - offset);
  78. if (size < BITS_PER_LONG)
  79. goto found_first;
  80. if (~tmp)
  81. goto found_middle;
  82. size -= BITS_PER_LONG;
  83. result += BITS_PER_LONG;
  84. }
  85. while (size & ~(BITS_PER_LONG-1)) {
  86. if (~(tmp = *(p++)))
  87. goto found_middle;
  88. result += BITS_PER_LONG;
  89. size -= BITS_PER_LONG;
  90. }
  91. if (!size)
  92. return result;
  93. tmp = *p;
  94. found_first:
  95. tmp |= ~0UL << size;
  96. if (tmp == ~0UL) /* Are any bits zero? */
  97. return result + size; /* Nope. */
  98. found_middle:
  99. return result + ffz(tmp);
  100. }
  101. EXPORT_SYMBOL(find_next_zero_bit);
  102. #ifdef __BIG_ENDIAN
  103. /* include/linux/byteorder does not support "unsigned long" type */
  104. static inline unsigned long ext2_swabp(const unsigned long * x)
  105. {
  106. #if BITS_PER_LONG == 64
  107. return (unsigned long) __swab64p((u64 *) x);
  108. #elif BITS_PER_LONG == 32
  109. return (unsigned long) __swab32p((u32 *) x);
  110. #else
  111. #error BITS_PER_LONG not defined
  112. #endif
  113. }
  114. /* include/linux/byteorder doesn't 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. unsigned long generic_find_next_zero_le_bit(const unsigned long *addr, unsigned
  126. long size, unsigned long offset)
  127. {
  128. const unsigned long *p = addr + BITOP_WORD(offset);
  129. unsigned long result = offset & ~(BITS_PER_LONG - 1);
  130. unsigned long tmp;
  131. if (offset >= size)
  132. return size;
  133. size -= result;
  134. offset &= (BITS_PER_LONG - 1UL);
  135. if (offset) {
  136. tmp = ext2_swabp(p++);
  137. tmp |= (~0UL >> (BITS_PER_LONG - offset));
  138. if (size < BITS_PER_LONG)
  139. goto found_first;
  140. if (~tmp)
  141. goto found_middle;
  142. size -= BITS_PER_LONG;
  143. result += BITS_PER_LONG;
  144. }
  145. while (size & ~(BITS_PER_LONG - 1)) {
  146. if (~(tmp = *(p++)))
  147. goto found_middle_swap;
  148. result += BITS_PER_LONG;
  149. size -= BITS_PER_LONG;
  150. }
  151. if (!size)
  152. return result;
  153. tmp = ext2_swabp(p);
  154. found_first:
  155. tmp |= ~0UL << size;
  156. if (tmp == ~0UL) /* Are any bits zero? */
  157. return result + size; /* Nope. Skip ffz */
  158. found_middle:
  159. return result + ffz(tmp);
  160. found_middle_swap:
  161. return result + ffz(ext2_swab(tmp));
  162. }
  163. EXPORT_SYMBOL(generic_find_next_zero_le_bit);
  164. unsigned long generic_find_next_le_bit(const unsigned long *addr, unsigned
  165. long size, unsigned long offset)
  166. {
  167. const unsigned long *p = addr + BITOP_WORD(offset);
  168. unsigned long result = offset & ~(BITS_PER_LONG - 1);
  169. unsigned long tmp;
  170. if (offset >= size)
  171. return size;
  172. size -= result;
  173. offset &= (BITS_PER_LONG - 1UL);
  174. if (offset) {
  175. tmp = ext2_swabp(p++);
  176. tmp &= (~0UL << offset);
  177. if (size < BITS_PER_LONG)
  178. goto found_first;
  179. if (tmp)
  180. goto found_middle;
  181. size -= BITS_PER_LONG;
  182. result += BITS_PER_LONG;
  183. }
  184. while (size & ~(BITS_PER_LONG - 1)) {
  185. tmp = *(p++);
  186. if (tmp)
  187. goto found_middle_swap;
  188. result += BITS_PER_LONG;
  189. size -= BITS_PER_LONG;
  190. }
  191. if (!size)
  192. return result;
  193. tmp = ext2_swabp(p);
  194. found_first:
  195. tmp &= (~0UL >> (BITS_PER_LONG - size));
  196. if (tmp == 0UL) /* Are any bits set? */
  197. return result + size; /* Nope. */
  198. found_middle:
  199. return result + __ffs(tmp);
  200. found_middle_swap:
  201. return result + __ffs(ext2_swab(tmp));
  202. }
  203. EXPORT_SYMBOL(generic_find_next_le_bit);
  204. #endif /* __BIG_ENDIAN */