find_next_bit.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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, unsigned long size,
  21. 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, unsigned long size,
  62. 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, unsigned long size)
  104. {
  105. const unsigned long *p = addr;
  106. unsigned long result = 0;
  107. unsigned long tmp;
  108. while (size & ~(BITS_PER_LONG-1)) {
  109. if ((tmp = *(p++)))
  110. goto found;
  111. result += BITS_PER_LONG;
  112. size -= BITS_PER_LONG;
  113. }
  114. if (!size)
  115. return result;
  116. tmp = (*p) & (~0UL >> (BITS_PER_LONG - size));
  117. if (tmp == 0UL) /* Are any bits set? */
  118. return result + size; /* Nope. */
  119. found:
  120. return result + __ffs(tmp);
  121. }
  122. EXPORT_SYMBOL(find_first_bit);
  123. /*
  124. * Find the first cleared bit in a memory region.
  125. */
  126. unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size)
  127. {
  128. const unsigned long *p = addr;
  129. unsigned long result = 0;
  130. unsigned long tmp;
  131. while (size & ~(BITS_PER_LONG-1)) {
  132. if (~(tmp = *(p++)))
  133. goto found;
  134. result += BITS_PER_LONG;
  135. size -= BITS_PER_LONG;
  136. }
  137. if (!size)
  138. return result;
  139. tmp = (*p) | (~0UL << size);
  140. if (tmp == ~0UL) /* Are any bits zero? */
  141. return result + size; /* Nope. */
  142. found:
  143. return result + ffz(tmp);
  144. }
  145. EXPORT_SYMBOL(find_first_zero_bit);
  146. #endif /* CONFIG_GENERIC_FIND_FIRST_BIT */
  147. #ifdef __BIG_ENDIAN
  148. /* include/linux/byteorder does not support "unsigned long" type */
  149. static inline unsigned long ext2_swabp(const unsigned long * x)
  150. {
  151. #if BITS_PER_LONG == 64
  152. return (unsigned long) __swab64p((u64 *) x);
  153. #elif BITS_PER_LONG == 32
  154. return (unsigned long) __swab32p((u32 *) x);
  155. #else
  156. #error BITS_PER_LONG not defined
  157. #endif
  158. }
  159. /* include/linux/byteorder doesn't support "unsigned long" type */
  160. static inline unsigned long ext2_swab(const unsigned long y)
  161. {
  162. #if BITS_PER_LONG == 64
  163. return (unsigned long) __swab64((u64) y);
  164. #elif BITS_PER_LONG == 32
  165. return (unsigned long) __swab32((u32) y);
  166. #else
  167. #error BITS_PER_LONG not defined
  168. #endif
  169. }
  170. unsigned long generic_find_next_zero_le_bit(const unsigned long *addr, unsigned
  171. long size, unsigned long offset)
  172. {
  173. const unsigned long *p = addr + BITOP_WORD(offset);
  174. unsigned long result = offset & ~(BITS_PER_LONG - 1);
  175. unsigned long tmp;
  176. if (offset >= size)
  177. return size;
  178. size -= result;
  179. offset &= (BITS_PER_LONG - 1UL);
  180. if (offset) {
  181. tmp = ext2_swabp(p++);
  182. tmp |= (~0UL >> (BITS_PER_LONG - offset));
  183. if (size < BITS_PER_LONG)
  184. goto found_first;
  185. if (~tmp)
  186. goto found_middle;
  187. size -= BITS_PER_LONG;
  188. result += BITS_PER_LONG;
  189. }
  190. while (size & ~(BITS_PER_LONG - 1)) {
  191. if (~(tmp = *(p++)))
  192. goto found_middle_swap;
  193. result += BITS_PER_LONG;
  194. size -= BITS_PER_LONG;
  195. }
  196. if (!size)
  197. return result;
  198. tmp = ext2_swabp(p);
  199. found_first:
  200. tmp |= ~0UL << size;
  201. if (tmp == ~0UL) /* Are any bits zero? */
  202. return result + size; /* Nope. Skip ffz */
  203. found_middle:
  204. return result + ffz(tmp);
  205. found_middle_swap:
  206. return result + ffz(ext2_swab(tmp));
  207. }
  208. EXPORT_SYMBOL(generic_find_next_zero_le_bit);
  209. unsigned long generic_find_next_le_bit(const unsigned long *addr, unsigned
  210. long size, unsigned long offset)
  211. {
  212. const unsigned long *p = addr + BITOP_WORD(offset);
  213. unsigned long result = offset & ~(BITS_PER_LONG - 1);
  214. unsigned long tmp;
  215. if (offset >= size)
  216. return size;
  217. size -= result;
  218. offset &= (BITS_PER_LONG - 1UL);
  219. if (offset) {
  220. tmp = ext2_swabp(p++);
  221. tmp &= (~0UL << offset);
  222. if (size < BITS_PER_LONG)
  223. goto found_first;
  224. if (tmp)
  225. goto found_middle;
  226. size -= BITS_PER_LONG;
  227. result += BITS_PER_LONG;
  228. }
  229. while (size & ~(BITS_PER_LONG - 1)) {
  230. tmp = *(p++);
  231. if (tmp)
  232. goto found_middle_swap;
  233. result += BITS_PER_LONG;
  234. size -= BITS_PER_LONG;
  235. }
  236. if (!size)
  237. return result;
  238. tmp = ext2_swabp(p);
  239. found_first:
  240. tmp &= (~0UL >> (BITS_PER_LONG - size));
  241. if (tmp == 0UL) /* Are any bits set? */
  242. return result + size; /* Nope. */
  243. found_middle:
  244. return result + __ffs(tmp);
  245. found_middle_swap:
  246. return result + __ffs(ext2_swab(tmp));
  247. }
  248. EXPORT_SYMBOL(generic_find_next_le_bit);
  249. #endif /* __BIG_ENDIAN */