find_next_bit.c 6.4 KB

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