💿🐜 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.

201 lines
4.8 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 dna
  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 bit_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 bit_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 <typename T>
  48. constexpr int popcount(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 <typename T>
  57. constexpr int hamming_distance(T x, T y) noexcept;
  58. template <typename T>
  59. constexpr T bit_merge(T a, T b, T mask) noexcept
  60. {
  61. return a ^ ((a ^ b) & mask);
  62. }
  63. template <class T>
  64. constexpr T bit_pad(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 <typename T>
  78. constexpr T bit_interleave(T a, T b) noexcept
  79. {
  80. return (bit_pad(b) << 1) | bit_pad(a);
  81. }
  82. template <typename T>
  83. constexpr T bit_swap_adjacent(T x) noexcept
  84. {
  85. return ((x & T(0xaaaaaaaaaaaaaaaa)) >> 1) | ((x & T(0x5555555555555555)) << 1);
  86. }
  87. template <typename T>
  88. constexpr T bit_shuffle_adjacent(T x, T mask) noexcept
  89. {
  90. T y = bit_swap_adjacent(x);
  91. return bit_merge(x, y, bit_interleave<T>(mask, mask));
  92. }
  93. /**
  94. * Shuffles the adjacent bits of a value.
  95. *
  96. * @param x Value to shuffle.
  97. * @param g Uniform random bit generator.
  98. * @return Value with adjacent bits shuffled.
  99. */
  100. /*
  101. template <class T, class URBG>
  102. constexpr T bit_shuffle_adjacent(T x, URBG&& g) noexcept
  103. {
  104. return bit_swap_adjacent(x, static_cast<T>(g()));
  105. }*/
  106. /**
  107. *
  108. */
  109. template <class T, class U>
  110. U bit_splice(T a, T b, U mask)
  111. {
  112. return bit_deposit(static_cast<U>(a), ~mask) | bit_deposit(static_cast<U>(b), mask);
  113. }
  114. /**
  115. * For an n-bit number with r set bits, there are `n! / ((n - r)! * r!)` permutations.
  116. */
  117. template <class T>
  118. T next_bit_permutation(T x)
  119. {
  120. T y = (x | (x - 1)) + 1;
  121. return y | ((((y & -y) / (x & -x)) >> 1) - 1);
  122. }
  123. template <class T>
  124. constexpr T bit_deposit(T x, T mask) noexcept
  125. {
  126. T result = 0;
  127. for (T i = 1; mask; i <<= 1)
  128. {
  129. if (x & i)
  130. result |= mask & -mask;
  131. mask &= mask - 1;
  132. }
  133. return result;
  134. }
  135. template <class T>
  136. constexpr T bit_extract(T x, T mask) noexcept
  137. {
  138. T result = 0;
  139. for (T i = 1; mask; i <<= 1)
  140. {
  141. if (x & mask & -mask)
  142. result |= i;
  143. mask &= mask - 1;
  144. }
  145. return result;
  146. }
  147. template <typename T>
  148. constexpr int popcount(T x) noexcept
  149. {
  150. int n = 0;
  151. for (; x; ++n)
  152. x &= x - 1;
  153. return n;
  154. }
  155. template <typename T>
  156. inline constexpr int hamming_distance(T x, T y) noexcept
  157. {
  158. return popcount(x ^ y);
  159. }
  160. /**
  161. *
  162. * @param mask Least significant bits indicate whether to inherit the first (0) or second (1) allele from @p a. Most significant bits indicate whether to inherit the first (0) or second (1) allele from @p b.
  163. */
  164. template <class T>
  165. constexpr T inherit(T a, T b, T mask) noexcept
  166. {
  167. a = bit_shuffle_adjacent<T>(a, mask);
  168. b = bit_shuffle_adjacent<T>(b, mask >> (sizeof(T) << 2));
  169. return bit_merge<T>(a, b, T(0x5555555555555555));
  170. }
  171. } // namespace dna
  172. #endif // ANTKEEPER_BIT_MATH_HPP