💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
4.5 KiB

  1. /*
  2. * Copyright (C) 2020 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_BIT_MATH_HPP
  20. #define ANTKEEPER_BIT_MATH_HPP
  21. namespace bit
  22. {
  23. /**
  24. * Reads bits from the least significant bits of a value and returns them in the positions marked by a mask.
  25. *
  26. * @param x Value from which bits should be read.
  27. * @param mask Bit mask indicating where bits should be deposited.
  28. * @return Bits from the least significant bits of @p x in the positions marked by @p mask.
  29. */
  30. template <class T>
  31. constexpr T deposit(T x, T mask) noexcept;
  32. /**
  33. * Reads bits from a value in the positions marked by a mask and returns them in the least significant bits.
  34. *
  35. * @param x Value from which bits should be read.
  36. * @param mask Bit mask indicating which bits to extract.
  37. * @return Bits of @p x from the positions marked by @p mask in the least significant bits.
  38. */
  39. template <class T>
  40. constexpr T extract(T x, T mask) noexcept;
  41. /**
  42. * Returns the number of set bits in a value, known as a *population count* or *Hamming weight*.
  43. *
  44. * @param x Value to count.
  45. * @return Number of set bits in @p x.
  46. */
  47. template <class T>
  48. constexpr int count(T x) noexcept;
  49. /**
  50. * Returns the number of differing bits between two values, known as *Hamming distance*.
  51. *
  52. * @param x First value.
  53. * @param y Second value.
  54. * @return Hamming distance between @px and @p y.
  55. */
  56. template <class T>
  57. constexpr int difference(T x, T y) noexcept;
  58. template <class T>
  59. constexpr T merge(T a, T b, T mask) noexcept
  60. {
  61. return a ^ ((a ^ b) & mask);
  62. }
  63. template <class T>
  64. constexpr T expand(T x) noexcept
  65. {
  66. x &= (1 << (sizeof(T) << 2)) - 1;
  67. if constexpr(sizeof(T) >= 8)
  68. x = (x ^ (x << 16)) & T(0x0000ffff0000ffff);
  69. if constexpr(sizeof(T) >= 4)
  70. x = (x ^ (x << 8)) & T(0x00ff00ff00ff00ff);
  71. if constexpr(sizeof(T) >= 2)
  72. x = (x ^ (x << 4)) & T(0x0f0f0f0f0f0f0f0f);
  73. x = (x ^ (x << 2)) & T(0x3333333333333333);
  74. x = (x ^ (x << 1)) & T(0x5555555555555555);
  75. return x;
  76. }
  77. template <class T, class U = T>
  78. inline constexpr U interleave(T a, T b) noexcept
  79. {
  80. return bit_expand<U>(a) | (bit_expand<U>(b) << 1);
  81. }
  82. /**
  83. * Returns the parity of a value.
  84. *
  85. * @param x Value to test.
  86. * @return `1` if an odd number of bits are set, `0` otherwise.
  87. */
  88. template <class T>
  89. constexpr T parity(T x) noexcept
  90. {
  91. if constexpr(sizeof(T) >= 8)
  92. x ^= x >> 32;
  93. if constexpr(sizeof(T) >= 4)
  94. x ^= x >> 16;
  95. if constexpr(sizeof(T) >= 2)
  96. x ^= x >> 8;
  97. x ^= x >> 4;
  98. return (0x6996 >> (x & 0xf)) & 1;
  99. }
  100. template <class T>
  101. constexpr T compress(T x) noexcept
  102. {
  103. x &= T(0x5555555555555555);
  104. x = (x ^ (x >> 1)) & T(0x3333333333333333);
  105. x = (x ^ (x >> 2)) & T(0x0f0f0f0f0f0f0f0f);
  106. if constexpr(sizeof(T) >= 2)
  107. x = (x ^ (x >> 4)) & T(0x00ff00ff00ff00ff);
  108. if constexpr(sizeof(T) >= 4)
  109. x = (x ^ (x >> 8)) & T(0x0000ffff0000ffff);
  110. if constexpr(sizeof(T) >= 8)
  111. x = (x ^ (x >> 16)) & T(0x00000000ffffffff);
  112. return x;
  113. }
  114. template <class T>
  115. constexpr T swap_adjacent(T x) noexcept
  116. {
  117. return ((x & T(0xaaaaaaaaaaaaaaaa)) >> 1) | ((x & T(0x5555555555555555)) << 1);
  118. }
  119. /**
  120. * For an n-bit number with r set bits, there are `n! / ((n - r)! * r!)` permutations.
  121. */
  122. template <class T>
  123. constexpr T next_permutation(T x) noexcept
  124. {
  125. T y = (x | (x - 1)) + 1;
  126. return y | ((((y & -y) / (x & -x)) >> 1) - 1);
  127. }
  128. template <class T>
  129. constexpr T bit_deposit(T x, T mask) noexcept
  130. {
  131. T result = 0;
  132. for (T i = 1; mask; i <<= 1)
  133. {
  134. if (x & i)
  135. result |= mask & -mask;
  136. mask &= mask - 1;
  137. }
  138. return result;
  139. }
  140. template <class T>
  141. constexpr T extract(T x, T mask) noexcept
  142. {
  143. T result = 0;
  144. for (T i = 1; mask; i <<= 1)
  145. {
  146. if (x & mask & -mask)
  147. result |= i;
  148. mask &= mask - 1;
  149. }
  150. return result;
  151. }
  152. template <class T>
  153. constexpr int count(T x) noexcept
  154. {
  155. int n = 0;
  156. for (; x; ++n)
  157. x &= x - 1;
  158. return n;
  159. }
  160. template <class T>
  161. inline constexpr int difference(T x, T y) noexcept
  162. {
  163. return count(x ^ y);
  164. }
  165. } // namespace bit
  166. #endif // ANTKEEPER_BIT_MATH_HPP