bitops.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*************************************************************************
  2. *
  3. * Copyright (C) 2018-2023 Ruilin Peng (Nick) <[email protected]>.
  4. *
  5. * smartdns is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * smartdns is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "bitmap.h"
  19. #include "bitops.h"
  20. /*
  21. * This is a common helper function for find_next_bit, find_next_zero_bit, and
  22. * find_next_and_bit. The differences are:
  23. * - The "invert" argument, which is XORed with each fetched word before
  24. * searching it for one bits.
  25. * - The optional "addr2", which is addr2 with "addr1" if present.
  26. */
  27. static inline unsigned long _find_next_bit(const unsigned long *addr1,
  28. const unsigned long *addr2, unsigned long nbits,
  29. unsigned long start, unsigned long invert)
  30. {
  31. unsigned long tmp;
  32. if (unlikely(start >= nbits))
  33. return nbits;
  34. tmp = addr1[start / BITS_PER_LONG];
  35. if (addr2)
  36. tmp &= addr2[start / BITS_PER_LONG];
  37. tmp ^= invert;
  38. /* Handle 1st word. */
  39. tmp &= BITMAP_FIRST_WORD_MASK(start);
  40. start = round_down(start, BITS_PER_LONG);
  41. while (!tmp) {
  42. start += BITS_PER_LONG;
  43. if (start >= nbits)
  44. return nbits;
  45. tmp = addr1[start / BITS_PER_LONG];
  46. if (addr2)
  47. tmp &= addr2[start / BITS_PER_LONG];
  48. tmp ^= invert;
  49. }
  50. return min(start + __ffs(tmp), nbits);
  51. }
  52. /*
  53. * Find the next set bit in a memory region.
  54. */
  55. unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
  56. unsigned long offset)
  57. {
  58. return _find_next_bit(addr, NULL, size, offset, 0UL);
  59. }
  60. /*
  61. * Find the first set bit in a memory region.
  62. */
  63. unsigned long find_first_bit(const unsigned long *addr, unsigned long size)
  64. {
  65. unsigned long idx;
  66. for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
  67. if (addr[idx])
  68. return min(idx * BITS_PER_LONG + __ffs(addr[idx]), size);
  69. }
  70. return size;
  71. }
  72. /*
  73. * Find the first cleared bit in a memory region.
  74. */
  75. unsigned long find_first_zero_bit(const unsigned long *addr, unsigned long size)
  76. {
  77. unsigned long idx;
  78. for (idx = 0; idx * BITS_PER_LONG < size; idx++) {
  79. if (addr[idx] != ~0UL)
  80. return min(idx * BITS_PER_LONG + ffz(addr[idx]), size);
  81. }
  82. return size;
  83. }
  84. unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size,
  85. unsigned long offset)
  86. {
  87. return _find_next_bit(addr, NULL, size, offset, ~0UL);
  88. }
  89. unsigned long find_next_and_bit(const unsigned long *addr1,
  90. const unsigned long *addr2, unsigned long size,
  91. unsigned long offset)
  92. {
  93. return _find_next_bit(addr1, addr2, size, offset, 0UL);
  94. }
  95. /**
  96. * hweightN - returns the hamming weight of a N-bit word
  97. * @x: the word to weigh
  98. *
  99. * The Hamming Weight of a number is the total number of bits set in it.
  100. */
  101. unsigned int __sw_hweight32(unsigned int w)
  102. {
  103. #ifdef CONFIG_ARCH_HAS_FAST_MULTIPLIER
  104. w -= (w >> 1) & 0x55555555;
  105. w = (w & 0x33333333) + ((w >> 2) & 0x33333333);
  106. w = (w + (w >> 4)) & 0x0f0f0f0f;
  107. return (w * 0x01010101) >> 24;
  108. #else
  109. unsigned int res = w - ((w >> 1) & 0x55555555);
  110. res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
  111. res = (res + (res >> 4)) & 0x0F0F0F0F;
  112. res = res + (res >> 8);
  113. return (res + (res >> 16)) & 0x000000FF;
  114. #endif
  115. }
  116. unsigned int __sw_hweight16(unsigned int w)
  117. {
  118. unsigned int res = w - ((w >> 1) & 0x5555);
  119. res = (res & 0x3333) + ((res >> 2) & 0x3333);
  120. res = (res + (res >> 4)) & 0x0F0F;
  121. return (res + (res >> 8)) & 0x00FF;
  122. }
  123. unsigned int __sw_hweight8(unsigned int w)
  124. {
  125. unsigned int res = w - ((w >> 1) & 0x55);
  126. res = (res & 0x33) + ((res >> 2) & 0x33);
  127. return (res + (res >> 4)) & 0x0F;
  128. }
  129. unsigned long __sw_hweight64(uint64_t w)
  130. {
  131. #if BITS_PER_LONG == 32
  132. return __sw_hweight32((unsigned int)(w >> 32)) +
  133. __sw_hweight32((unsigned int)w);
  134. #elif BITS_PER_LONG == 64
  135. #ifdef CONFIG_ARCH_HAS_FAST_MULTIPLIER
  136. w -= (w >> 1) & 0x5555555555555555ul;
  137. w = (w & 0x3333333333333333ul) + ((w >> 2) & 0x3333333333333333ul);
  138. w = (w + (w >> 4)) & 0x0f0f0f0f0f0f0f0ful;
  139. return (w * 0x0101010101010101ul) >> 56;
  140. #else
  141. uint64_t res = w - ((w >> 1) & 0x5555555555555555ul);
  142. res = (res & 0x3333333333333333ul) + ((res >> 2) & 0x3333333333333333ul);
  143. res = (res + (res >> 4)) & 0x0F0F0F0F0F0F0F0Ful;
  144. res = res + (res >> 8);
  145. res = res + (res >> 16);
  146. return (res + (res >> 32)) & 0x00000000000000FFul;
  147. #endif
  148. #endif
  149. }