xfs_bit.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_bit.h"
  20. #include "xfs_log.h"
  21. #include "xfs_trans.h"
  22. #include "xfs_buf_item.h"
  23. /*
  24. * XFS bit manipulation routines, used in non-realtime code.
  25. */
  26. #ifndef HAVE_ARCH_HIGHBIT
  27. /*
  28. * Index of high bit number in byte, -1 for none set, 0..7 otherwise.
  29. */
  30. static const char xfs_highbit[256] = {
  31. -1, 0, 1, 1, 2, 2, 2, 2, /* 00 .. 07 */
  32. 3, 3, 3, 3, 3, 3, 3, 3, /* 08 .. 0f */
  33. 4, 4, 4, 4, 4, 4, 4, 4, /* 10 .. 17 */
  34. 4, 4, 4, 4, 4, 4, 4, 4, /* 18 .. 1f */
  35. 5, 5, 5, 5, 5, 5, 5, 5, /* 20 .. 27 */
  36. 5, 5, 5, 5, 5, 5, 5, 5, /* 28 .. 2f */
  37. 5, 5, 5, 5, 5, 5, 5, 5, /* 30 .. 37 */
  38. 5, 5, 5, 5, 5, 5, 5, 5, /* 38 .. 3f */
  39. 6, 6, 6, 6, 6, 6, 6, 6, /* 40 .. 47 */
  40. 6, 6, 6, 6, 6, 6, 6, 6, /* 48 .. 4f */
  41. 6, 6, 6, 6, 6, 6, 6, 6, /* 50 .. 57 */
  42. 6, 6, 6, 6, 6, 6, 6, 6, /* 58 .. 5f */
  43. 6, 6, 6, 6, 6, 6, 6, 6, /* 60 .. 67 */
  44. 6, 6, 6, 6, 6, 6, 6, 6, /* 68 .. 6f */
  45. 6, 6, 6, 6, 6, 6, 6, 6, /* 70 .. 77 */
  46. 6, 6, 6, 6, 6, 6, 6, 6, /* 78 .. 7f */
  47. 7, 7, 7, 7, 7, 7, 7, 7, /* 80 .. 87 */
  48. 7, 7, 7, 7, 7, 7, 7, 7, /* 88 .. 8f */
  49. 7, 7, 7, 7, 7, 7, 7, 7, /* 90 .. 97 */
  50. 7, 7, 7, 7, 7, 7, 7, 7, /* 98 .. 9f */
  51. 7, 7, 7, 7, 7, 7, 7, 7, /* a0 .. a7 */
  52. 7, 7, 7, 7, 7, 7, 7, 7, /* a8 .. af */
  53. 7, 7, 7, 7, 7, 7, 7, 7, /* b0 .. b7 */
  54. 7, 7, 7, 7, 7, 7, 7, 7, /* b8 .. bf */
  55. 7, 7, 7, 7, 7, 7, 7, 7, /* c0 .. c7 */
  56. 7, 7, 7, 7, 7, 7, 7, 7, /* c8 .. cf */
  57. 7, 7, 7, 7, 7, 7, 7, 7, /* d0 .. d7 */
  58. 7, 7, 7, 7, 7, 7, 7, 7, /* d8 .. df */
  59. 7, 7, 7, 7, 7, 7, 7, 7, /* e0 .. e7 */
  60. 7, 7, 7, 7, 7, 7, 7, 7, /* e8 .. ef */
  61. 7, 7, 7, 7, 7, 7, 7, 7, /* f0 .. f7 */
  62. 7, 7, 7, 7, 7, 7, 7, 7, /* f8 .. ff */
  63. };
  64. #endif
  65. /*
  66. * xfs_highbit32: get high bit set out of 32-bit argument, -1 if none set.
  67. */
  68. inline int
  69. xfs_highbit32(
  70. __uint32_t v)
  71. {
  72. #ifdef HAVE_ARCH_HIGHBIT
  73. return highbit32(v);
  74. #else
  75. int i;
  76. if (v & 0xffff0000)
  77. if (v & 0xff000000)
  78. i = 24;
  79. else
  80. i = 16;
  81. else if (v & 0x0000ffff)
  82. if (v & 0x0000ff00)
  83. i = 8;
  84. else
  85. i = 0;
  86. else
  87. return -1;
  88. return i + xfs_highbit[(v >> i) & 0xff];
  89. #endif
  90. }
  91. /*
  92. * xfs_lowbit64: get low bit set out of 64-bit argument, -1 if none set.
  93. */
  94. int
  95. xfs_lowbit64(
  96. __uint64_t v)
  97. {
  98. __uint32_t w = (__uint32_t)v;
  99. int n = 0;
  100. if (w) { /* lower bits */
  101. n = ffs(w);
  102. } else { /* upper bits */
  103. w = (__uint32_t)(v >> 32);
  104. if (w && (n = ffs(w)))
  105. n += 32;
  106. }
  107. return n - 1;
  108. }
  109. /*
  110. * xfs_highbit64: get high bit set out of 64-bit argument, -1 if none set.
  111. */
  112. int
  113. xfs_highbit64(
  114. __uint64_t v)
  115. {
  116. __uint32_t h = (__uint32_t)(v >> 32);
  117. if (h)
  118. return xfs_highbit32(h) + 32;
  119. return xfs_highbit32((__uint32_t)v);
  120. }
  121. /*
  122. * Return whether bitmap is empty.
  123. * Size is number of words in the bitmap, which is padded to word boundary
  124. * Returns 1 for empty, 0 for non-empty.
  125. */
  126. int
  127. xfs_bitmap_empty(uint *map, uint size)
  128. {
  129. uint i;
  130. uint ret = 0;
  131. for (i = 0; i < size; i++) {
  132. ret |= map[i];
  133. }
  134. return (ret == 0);
  135. }
  136. /*
  137. * Count the number of contiguous bits set in the bitmap starting with bit
  138. * start_bit. Size is the size of the bitmap in words.
  139. */
  140. int
  141. xfs_contig_bits(uint *map, uint size, uint start_bit)
  142. {
  143. uint * p = ((unsigned int *) map) + (start_bit >> BIT_TO_WORD_SHIFT);
  144. uint result = 0;
  145. uint tmp;
  146. size <<= BIT_TO_WORD_SHIFT;
  147. ASSERT(start_bit < size);
  148. size -= start_bit & ~(NBWORD - 1);
  149. start_bit &= (NBWORD - 1);
  150. if (start_bit) {
  151. tmp = *p++;
  152. /* set to one first offset bits prior to start */
  153. tmp |= (~0U >> (NBWORD-start_bit));
  154. if (tmp != ~0U)
  155. goto found;
  156. result += NBWORD;
  157. size -= NBWORD;
  158. }
  159. while (size) {
  160. if ((tmp = *p++) != ~0U)
  161. goto found;
  162. result += NBWORD;
  163. size -= NBWORD;
  164. }
  165. return result - start_bit;
  166. found:
  167. return result + ffz(tmp) - start_bit;
  168. }
  169. /*
  170. * This takes the bit number to start looking from and
  171. * returns the next set bit from there. It returns -1
  172. * if there are no more bits set or the start bit is
  173. * beyond the end of the bitmap.
  174. *
  175. * Size is the number of words, not bytes, in the bitmap.
  176. */
  177. int xfs_next_bit(uint *map, uint size, uint start_bit)
  178. {
  179. uint * p = ((unsigned int *) map) + (start_bit >> BIT_TO_WORD_SHIFT);
  180. uint result = start_bit & ~(NBWORD - 1);
  181. uint tmp;
  182. size <<= BIT_TO_WORD_SHIFT;
  183. if (start_bit >= size)
  184. return -1;
  185. size -= result;
  186. start_bit &= (NBWORD - 1);
  187. if (start_bit) {
  188. tmp = *p++;
  189. /* set to zero first offset bits prior to start */
  190. tmp &= (~0U << start_bit);
  191. if (tmp != 0U)
  192. goto found;
  193. result += NBWORD;
  194. size -= NBWORD;
  195. }
  196. while (size) {
  197. if ((tmp = *p++) != 0U)
  198. goto found;
  199. result += NBWORD;
  200. size -= NBWORD;
  201. }
  202. return -1;
  203. found:
  204. return result + ffs(tmp) - 1;
  205. }