bitops.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include <linux/types.h>
  2. #include <linux/module.h>
  3. #include <asm/byteorder.h>
  4. #include <asm/bitops.h>
  5. /**
  6. * find_next_bit - find the next set bit in a memory region
  7. * @addr: The address to base the search on
  8. * @offset: The bitnumber to start searching at
  9. * @size: The maximum size to search
  10. */
  11. unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
  12. unsigned long offset)
  13. {
  14. const unsigned long *p = addr + BITOP_WORD(offset);
  15. unsigned long result = offset & ~(BITS_PER_LONG-1);
  16. unsigned long tmp;
  17. if (offset >= size)
  18. return size;
  19. size -= result;
  20. offset %= BITS_PER_LONG;
  21. if (offset) {
  22. tmp = *(p++);
  23. tmp &= (~0UL << offset);
  24. if (size < BITS_PER_LONG)
  25. goto found_first;
  26. if (tmp)
  27. goto found_middle;
  28. size -= BITS_PER_LONG;
  29. result += BITS_PER_LONG;
  30. }
  31. while (size & ~(BITS_PER_LONG-1)) {
  32. if ((tmp = *(p++)))
  33. goto found_middle;
  34. result += BITS_PER_LONG;
  35. size -= BITS_PER_LONG;
  36. }
  37. if (!size)
  38. return result;
  39. tmp = *p;
  40. found_first:
  41. tmp &= (~0UL >> (BITS_PER_LONG - size));
  42. if (tmp == 0UL) /* Are any bits set? */
  43. return result + size; /* Nope. */
  44. found_middle:
  45. return result + __ffs(tmp);
  46. }
  47. EXPORT_SYMBOL(find_next_bit);
  48. /*
  49. * This implementation of find_{first,next}_zero_bit was stolen from
  50. * Linus' asm-alpha/bitops.h.
  51. */
  52. unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
  53. unsigned long offset)
  54. {
  55. const unsigned long *p = addr + BITOP_WORD(offset);
  56. unsigned long result = offset & ~(BITS_PER_LONG-1);
  57. unsigned long tmp;
  58. if (offset >= size)
  59. return size;
  60. size -= result;
  61. offset %= BITS_PER_LONG;
  62. if (offset) {
  63. tmp = *(p++);
  64. tmp |= ~0UL >> (BITS_PER_LONG - offset);
  65. if (size < BITS_PER_LONG)
  66. goto found_first;
  67. if (~tmp)
  68. goto found_middle;
  69. size -= BITS_PER_LONG;
  70. result += BITS_PER_LONG;
  71. }
  72. while (size & ~(BITS_PER_LONG-1)) {
  73. if (~(tmp = *(p++)))
  74. goto found_middle;
  75. result += BITS_PER_LONG;
  76. size -= BITS_PER_LONG;
  77. }
  78. if (!size)
  79. return result;
  80. tmp = *p;
  81. found_first:
  82. tmp |= ~0UL << size;
  83. if (tmp == ~0UL) /* Are any bits zero? */
  84. return result + size; /* Nope. */
  85. found_middle:
  86. return result + ffz(tmp);
  87. }
  88. EXPORT_SYMBOL(find_next_zero_bit);
  89. static inline unsigned int ext2_ilog2(unsigned int x)
  90. {
  91. int lz;
  92. asm("cntlzw %0,%1": "=r"(lz):"r"(x));
  93. return 31 - lz;
  94. }
  95. static inline unsigned int ext2_ffz(unsigned int x)
  96. {
  97. u32 rc;
  98. if ((x = ~x) == 0)
  99. return 32;
  100. rc = ext2_ilog2(x & -x);
  101. return rc;
  102. }
  103. unsigned long find_next_zero_le_bit(const unsigned long *addr,
  104. unsigned long size, unsigned long offset)
  105. {
  106. const unsigned int *p = ((const unsigned int *)addr) + (offset >> 5);
  107. unsigned int result = offset & ~31;
  108. unsigned int tmp;
  109. if (offset >= size)
  110. return size;
  111. size -= result;
  112. offset &= 31;
  113. if (offset) {
  114. tmp = cpu_to_le32p(p++);
  115. tmp |= ~0U >> (32 - offset); /* bug or feature ? */
  116. if (size < 32)
  117. goto found_first;
  118. if (tmp != ~0)
  119. goto found_middle;
  120. size -= 32;
  121. result += 32;
  122. }
  123. while (size >= 32) {
  124. if ((tmp = cpu_to_le32p(p++)) != ~0)
  125. goto found_middle;
  126. result += 32;
  127. size -= 32;
  128. }
  129. if (!size)
  130. return result;
  131. tmp = cpu_to_le32p(p);
  132. found_first:
  133. tmp |= ~0 << size;
  134. if (tmp == ~0) /* Are any bits zero? */
  135. return result + size; /* Nope. */
  136. found_middle:
  137. return result + ext2_ffz(tmp);
  138. }
  139. EXPORT_SYMBOL(find_next_zero_le_bit);