find_next_bit.c 6.4 KB

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