bitops.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "bitmap.h"
  2. #include "bitops.h"
  3. /*
  4. * This is a common helper function for find_next_bit, find_next_zero_bit, and
  5. * find_next_and_bit. The differences are:
  6. * - The "invert" argument, which is XORed with each fetched word before
  7. * searching it for one bits.
  8. * - The optional "addr2", which is anded with "addr1" if present.
  9. */
  10. static inline unsigned long _find_next_bit(const unsigned long *addr1,
  11. const unsigned long *addr2, unsigned long nbits,
  12. unsigned long start, unsigned long invert)
  13. {
  14. unsigned long tmp;
  15. if (unlikely(start >= nbits))
  16. return nbits;
  17. tmp = addr1[start / BITS_PER_LONG];
  18. if (addr2)
  19. tmp &= addr2[start / BITS_PER_LONG];
  20. tmp ^= invert;
  21. /* Handle 1st word. */
  22. tmp &= BITMAP_FIRST_WORD_MASK(start);
  23. start = round_down(start, BITS_PER_LONG);
  24. while (!tmp) {
  25. start += BITS_PER_LONG;
  26. if (start >= nbits)
  27. return nbits;
  28. tmp = addr1[start / BITS_PER_LONG];
  29. if (addr2)
  30. tmp &= addr2[start / BITS_PER_LONG];
  31. tmp ^= invert;
  32. }
  33. return min(start + __ffs(tmp), nbits);
  34. }
  35. /*
  36. * Find the next set bit in a memory region.
  37. */
  38. unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
  39. unsigned long offset)
  40. {
  41. return _find_next_bit(addr, NULL, size, offset, 0UL);
  42. }
  43. /*
  44. * Find the first set bit in a memory region.
  45. */
  46. unsigned long find_first_bit(const unsigned long *addr, unsigned long size)
  47. {
  48. unsigned long idx;
  49. for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
  50. if (addr[idx])
  51. return min(idx * BITS_PER_LONG + __ffs(addr[idx]), size);
  52. }
  53. return size;
  54. }
  55. /*
  56. * Find the first cleared bit in a memory region.
  57. */
  58. unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size)
  59. {
  60. unsigned long idx;
  61. for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
  62. if (addr[idx] != ~0UL)
  63. return min(idx * BITS_PER_LONG + ffz(addr[idx]), size);
  64. }
  65. return size;
  66. }
  67. unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
  68. unsigned long offset)
  69. {
  70. return _find_next_bit(addr, NULL, size, offset, ~0UL);
  71. }
  72. unsigned long find_next_and_bit(const unsigned long *addr1,
  73. const unsigned long *addr2, unsigned long size,
  74. unsigned long offset)
  75. {
  76. return _find_next_bit(addr1, addr2, size, offset, 0UL);
  77. }
  78. /**
  79. * hweightN - returns the hamming weight of a N-bit word
  80. * @x: the word to weigh
  81. *
  82. * The Hamming Weight of a number is the total number of bits set in it.
  83. */
  84. unsigned int __sw_hweight32(unsigned int w)
  85. {
  86. #ifdef CONFIG_ARCH_HAS_FAST_MULTIPLIER
  87. w -= (w >> 1) & 0x55555555;
  88. w = (w & 0x33333333) + ((w >> 2) & 0x33333333);
  89. w = (w + (w >> 4)) & 0x0f0f0f0f;
  90. return (w * 0x01010101) >> 24;
  91. #else
  92. unsigned int res = w - ((w >> 1) & 0x55555555);
  93. res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
  94. res = (res + (res >> 4)) & 0x0F0F0F0F;
  95. res = res + (res >> 8);
  96. return (res + (res >> 16)) & 0x000000FF;
  97. #endif
  98. }
  99. unsigned int __sw_hweight16(unsigned int w)
  100. {
  101. unsigned int res = w - ((w >> 1) & 0x5555);
  102. res = (res & 0x3333) + ((res >> 2) & 0x3333);
  103. res = (res + (res >> 4)) & 0x0F0F;
  104. return (res + (res >> 8)) & 0x00FF;
  105. }
  106. unsigned int __sw_hweight8(unsigned int w)
  107. {
  108. unsigned int res = w - ((w >> 1) & 0x55);
  109. res = (res & 0x33) + ((res >> 2) & 0x33);
  110. return (res + (res >> 4)) & 0x0F;
  111. }
  112. unsigned long __sw_hweight64(uint64_t w)
  113. {
  114. #if BITS_PER_LONG == 32
  115. return __sw_hweight32((unsigned int)(w >> 32)) +
  116. __sw_hweight32((unsigned int)w);
  117. #elif BITS_PER_LONG == 64
  118. #ifdef CONFIG_ARCH_HAS_FAST_MULTIPLIER
  119. w -= (w >> 1) & 0x5555555555555555ul;
  120. w = (w & 0x3333333333333333ul) + ((w >> 2) & 0x3333333333333333ul);
  121. w = (w + (w >> 4)) & 0x0f0f0f0f0f0f0f0ful;
  122. return (w * 0x0101010101010101ul) >> 56;
  123. #else
  124. uint64_t res = w - ((w >> 1) & 0x5555555555555555ul);
  125. res = (res & 0x3333333333333333ul) + ((res >> 2) & 0x3333333333333333ul);
  126. res = (res + (res >> 4)) & 0x0F0F0F0F0F0F0F0Ful;
  127. res = res + (res >> 8);
  128. res = res + (res >> 16);
  129. return (res + (res >> 32)) & 0x00000000000000FFul;
  130. #endif
  131. #endif
  132. }