xfs_bit.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_log_format.h"
  20. /*
  21. * XFS bit manipulation routines, used in non-realtime code.
  22. */
  23. /*
  24. * Return whether bitmap is empty.
  25. * Size is number of words in the bitmap, which is padded to word boundary
  26. * Returns 1 for empty, 0 for non-empty.
  27. */
  28. int
  29. xfs_bitmap_empty(uint *map, uint size)
  30. {
  31. uint i;
  32. uint ret = 0;
  33. for (i = 0; i < size; i++) {
  34. ret |= map[i];
  35. }
  36. return (ret == 0);
  37. }
  38. /*
  39. * Count the number of contiguous bits set in the bitmap starting with bit
  40. * start_bit. Size is the size of the bitmap in words.
  41. */
  42. int
  43. xfs_contig_bits(uint *map, uint size, uint start_bit)
  44. {
  45. uint * p = ((unsigned int *) map) + (start_bit >> BIT_TO_WORD_SHIFT);
  46. uint result = 0;
  47. uint tmp;
  48. size <<= BIT_TO_WORD_SHIFT;
  49. ASSERT(start_bit < size);
  50. size -= start_bit & ~(NBWORD - 1);
  51. start_bit &= (NBWORD - 1);
  52. if (start_bit) {
  53. tmp = *p++;
  54. /* set to one first offset bits prior to start */
  55. tmp |= (~0U >> (NBWORD-start_bit));
  56. if (tmp != ~0U)
  57. goto found;
  58. result += NBWORD;
  59. size -= NBWORD;
  60. }
  61. while (size) {
  62. if ((tmp = *p++) != ~0U)
  63. goto found;
  64. result += NBWORD;
  65. size -= NBWORD;
  66. }
  67. return result - start_bit;
  68. found:
  69. return result + ffz(tmp) - start_bit;
  70. }
  71. /*
  72. * This takes the bit number to start looking from and
  73. * returns the next set bit from there. It returns -1
  74. * if there are no more bits set or the start bit is
  75. * beyond the end of the bitmap.
  76. *
  77. * Size is the number of words, not bytes, in the bitmap.
  78. */
  79. int xfs_next_bit(uint *map, uint size, uint start_bit)
  80. {
  81. uint * p = ((unsigned int *) map) + (start_bit >> BIT_TO_WORD_SHIFT);
  82. uint result = start_bit & ~(NBWORD - 1);
  83. uint tmp;
  84. size <<= BIT_TO_WORD_SHIFT;
  85. if (start_bit >= size)
  86. return -1;
  87. size -= result;
  88. start_bit &= (NBWORD - 1);
  89. if (start_bit) {
  90. tmp = *p++;
  91. /* set to zero first offset bits prior to start */
  92. tmp &= (~0U << start_bit);
  93. if (tmp != 0U)
  94. goto found;
  95. result += NBWORD;
  96. size -= NBWORD;
  97. }
  98. while (size) {
  99. if ((tmp = *p++) != 0U)
  100. goto found;
  101. result += NBWORD;
  102. size -= NBWORD;
  103. }
  104. return -1;
  105. found:
  106. return result + ffs(tmp) - 1;
  107. }