find_next_bit.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. /*
  17. * Find the next set bit in a memory region.
  18. */
  19. unsigned long __find_next_bit(const unsigned long *addr,
  20. unsigned long size, unsigned long offset)
  21. {
  22. const unsigned long *p = addr + BITOP_WORD(offset);
  23. unsigned long result = offset & ~(BITS_PER_LONG-1);
  24. unsigned long tmp;
  25. if (offset >= size)
  26. return size;
  27. size -= result;
  28. offset %= BITS_PER_LONG;
  29. if (offset) {
  30. tmp = *(p++);
  31. tmp &= (~0UL << offset);
  32. if (size < BITS_PER_LONG)
  33. goto found_first;
  34. if (tmp)
  35. goto found_middle;
  36. size -= BITS_PER_LONG;
  37. result += BITS_PER_LONG;
  38. }
  39. while (size & ~(BITS_PER_LONG-1)) {
  40. if ((tmp = *(p++)))
  41. goto found_middle;
  42. result += BITS_PER_LONG;
  43. size -= BITS_PER_LONG;
  44. }
  45. if (!size)
  46. return result;
  47. tmp = *p;
  48. found_first:
  49. tmp &= (~0UL >> (BITS_PER_LONG - size));
  50. if (tmp == 0UL) /* Are any bits set? */
  51. return result + size; /* Nope. */
  52. found_middle:
  53. return result + __ffs(tmp);
  54. }
  55. EXPORT_SYMBOL(__find_next_bit);
  56. /*
  57. * This implementation of find_{first,next}_zero_bit was stolen from
  58. * Linus' asm-alpha/bitops.h.
  59. */
  60. unsigned long __find_next_zero_bit(const unsigned long *addr,
  61. unsigned long size, unsigned long offset)
  62. {
  63. const unsigned long *p = addr + BITOP_WORD(offset);
  64. unsigned long result = offset & ~(BITS_PER_LONG-1);
  65. unsigned long tmp;
  66. if (offset >= size)
  67. return size;
  68. size -= result;
  69. offset %= BITS_PER_LONG;
  70. if (offset) {
  71. tmp = *(p++);
  72. tmp |= ~0UL >> (BITS_PER_LONG - offset);
  73. if (size < BITS_PER_LONG)
  74. goto found_first;
  75. if (~tmp)
  76. goto found_middle;
  77. size -= BITS_PER_LONG;
  78. result += BITS_PER_LONG;
  79. }
  80. while (size & ~(BITS_PER_LONG-1)) {
  81. if (~(tmp = *(p++)))
  82. goto found_middle;
  83. result += BITS_PER_LONG;
  84. size -= BITS_PER_LONG;
  85. }
  86. if (!size)
  87. return result;
  88. tmp = *p;
  89. found_first:
  90. tmp |= ~0UL << size;
  91. if (tmp == ~0UL) /* Are any bits zero? */
  92. return result + size; /* Nope. */
  93. found_middle:
  94. return result + ffz(tmp);
  95. }
  96. EXPORT_SYMBOL(__find_next_zero_bit);
  97. #ifdef __BIG_ENDIAN
  98. /* include/linux/byteorder does not support "unsigned long" type */
  99. static inline unsigned long ext2_swabp(const unsigned long * x)
  100. {
  101. #if BITS_PER_LONG == 64
  102. return (unsigned long) __swab64p((u64 *) x);
  103. #elif BITS_PER_LONG == 32
  104. return (unsigned long) __swab32p((u32 *) x);
  105. #else
  106. #error BITS_PER_LONG not defined
  107. #endif
  108. }
  109. /* include/linux/byteorder doesn't support "unsigned long" type */
  110. static inline unsigned long ext2_swab(const unsigned long y)
  111. {
  112. #if BITS_PER_LONG == 64
  113. return (unsigned long) __swab64((u64) y);
  114. #elif BITS_PER_LONG == 32
  115. return (unsigned long) __swab32((u32) y);
  116. #else
  117. #error BITS_PER_LONG not defined
  118. #endif
  119. }
  120. unsigned long generic_find_next_zero_le_bit(const unsigned long *addr, unsigned
  121. long size, unsigned long offset)
  122. {
  123. const unsigned long *p = addr + BITOP_WORD(offset);
  124. unsigned long result = offset & ~(BITS_PER_LONG - 1);
  125. unsigned long tmp;
  126. if (offset >= size)
  127. return size;
  128. size -= result;
  129. offset &= (BITS_PER_LONG - 1UL);
  130. if (offset) {
  131. tmp = ext2_swabp(p++);
  132. tmp |= (~0UL >> (BITS_PER_LONG - offset));
  133. if (size < BITS_PER_LONG)
  134. goto found_first;
  135. if (~tmp)
  136. goto found_middle;
  137. size -= BITS_PER_LONG;
  138. result += BITS_PER_LONG;
  139. }
  140. while (size & ~(BITS_PER_LONG - 1)) {
  141. if (~(tmp = *(p++)))
  142. goto found_middle_swap;
  143. result += BITS_PER_LONG;
  144. size -= BITS_PER_LONG;
  145. }
  146. if (!size)
  147. return result;
  148. tmp = ext2_swabp(p);
  149. found_first:
  150. tmp |= ~0UL << size;
  151. if (tmp == ~0UL) /* Are any bits zero? */
  152. return result + size; /* Nope. Skip ffz */
  153. found_middle:
  154. return result + ffz(tmp);
  155. found_middle_swap:
  156. return result + ffz(ext2_swab(tmp));
  157. }
  158. EXPORT_SYMBOL(generic_find_next_zero_le_bit);
  159. unsigned long generic_find_next_le_bit(const unsigned long *addr, unsigned
  160. long size, unsigned long offset)
  161. {
  162. const unsigned long *p = addr + BITOP_WORD(offset);
  163. unsigned long result = offset & ~(BITS_PER_LONG - 1);
  164. unsigned long tmp;
  165. if (offset >= size)
  166. return size;
  167. size -= result;
  168. offset &= (BITS_PER_LONG - 1UL);
  169. if (offset) {
  170. tmp = ext2_swabp(p++);
  171. tmp &= (~0UL << offset);
  172. if (size < BITS_PER_LONG)
  173. goto found_first;
  174. if (tmp)
  175. goto found_middle;
  176. size -= BITS_PER_LONG;
  177. result += BITS_PER_LONG;
  178. }
  179. while (size & ~(BITS_PER_LONG - 1)) {
  180. tmp = *(p++);
  181. if (tmp)
  182. goto found_middle_swap;
  183. result += BITS_PER_LONG;
  184. size -= BITS_PER_LONG;
  185. }
  186. if (!size)
  187. return result;
  188. tmp = ext2_swabp(p);
  189. found_first:
  190. tmp &= (~0UL >> (BITS_PER_LONG - size));
  191. if (tmp == 0UL) /* Are any bits set? */
  192. return result + size; /* Nope. */
  193. found_middle:
  194. return result + __ffs(tmp);
  195. found_middle_swap:
  196. return result + __ffs(ext2_swab(tmp));
  197. }
  198. EXPORT_SYMBOL(generic_find_next_le_bit);
  199. #endif /* __BIG_ENDIAN */