find_next_bit.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. #ifdef CONFIG_GENERIC_FIND_BIT_LE
  149. /* include/linux/byteorder does not support "unsigned long" type */
  150. static inline unsigned long ext2_swabp(const unsigned long * x)
  151. {
  152. #if BITS_PER_LONG == 64
  153. return (unsigned long) __swab64p((u64 *) x);
  154. #elif BITS_PER_LONG == 32
  155. return (unsigned long) __swab32p((u32 *) x);
  156. #else
  157. #error BITS_PER_LONG not defined
  158. #endif
  159. }
  160. /* include/linux/byteorder doesn't support "unsigned long" type */
  161. static inline unsigned long ext2_swab(const unsigned long y)
  162. {
  163. #if BITS_PER_LONG == 64
  164. return (unsigned long) __swab64((u64) y);
  165. #elif BITS_PER_LONG == 32
  166. return (unsigned long) __swab32((u32) y);
  167. #else
  168. #error BITS_PER_LONG not defined
  169. #endif
  170. }
  171. unsigned long find_next_zero_bit_le(const void *addr, unsigned
  172. long size, unsigned long offset)
  173. {
  174. const unsigned long *p = addr;
  175. unsigned long result = offset & ~(BITS_PER_LONG - 1);
  176. unsigned long tmp;
  177. if (offset >= size)
  178. return size;
  179. p += BITOP_WORD(offset);
  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(find_next_zero_bit_le);
  211. unsigned long find_next_bit_le(const void *addr, unsigned
  212. long size, unsigned long offset)
  213. {
  214. const unsigned long *p = addr;
  215. unsigned long result = offset & ~(BITS_PER_LONG - 1);
  216. unsigned long tmp;
  217. if (offset >= size)
  218. return size;
  219. p += BITOP_WORD(offset);
  220. size -= result;
  221. offset &= (BITS_PER_LONG - 1UL);
  222. if (offset) {
  223. tmp = ext2_swabp(p++);
  224. tmp &= (~0UL << offset);
  225. if (size < BITS_PER_LONG)
  226. goto found_first;
  227. if (tmp)
  228. goto found_middle;
  229. size -= BITS_PER_LONG;
  230. result += BITS_PER_LONG;
  231. }
  232. while (size & ~(BITS_PER_LONG - 1)) {
  233. tmp = *(p++);
  234. if (tmp)
  235. goto found_middle_swap;
  236. result += BITS_PER_LONG;
  237. size -= BITS_PER_LONG;
  238. }
  239. if (!size)
  240. return result;
  241. tmp = ext2_swabp(p);
  242. found_first:
  243. tmp &= (~0UL >> (BITS_PER_LONG - size));
  244. if (tmp == 0UL) /* Are any bits set? */
  245. return result + size; /* Nope. */
  246. found_middle:
  247. return result + __ffs(tmp);
  248. found_middle_swap:
  249. return result + __ffs(ext2_swab(tmp));
  250. }
  251. EXPORT_SYMBOL(find_next_bit_le);
  252. #endif /* CONFIG_GENERIC_FIND_BIT_LE */
  253. #endif /* __BIG_ENDIAN */