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

353 lines
7.7 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. *
  25. */
  26. template <class T>
  27. constexpr T compress(T x) noexcept;
  28. /**
  29. * Returns the number of set bits in a value, known as a *population count* or *Hamming weight*.
  30. *
  31. * @param x Value to count.
  32. * @return Number of set bits in @p x.
  33. */
  34. template <class T>
  35. constexpr int count(T x) noexcept;
  36. /**
  37. * Performs a single-point crossover between two values.
  38. *
  39. * @param a First value.
  40. * @param b Second value.
  41. * @param i Index of the crossover point.
  42. * @return Crossed over value.
  43. */
  44. template <class T>
  45. constexpr T crossover(T a, T b, int i) noexcept;
  46. /**
  47. * Performs an n-point crossover between two values.
  48. *
  49. * @param a First value.
  50. * @param b Second value.
  51. * @param mask Bit mask with set bits marking crossover points.
  52. * @return Crossed over value.
  53. */
  54. template <class T>
  55. constexpr T crossover_n(T a, T b, T mask) noexcept;
  56. /**
  57. * Reads bits from the least significant bits of a value and returns them in the positions marked by a mask.
  58. *
  59. * @param x Value from which bits should be read.
  60. * @param mask Bit mask indicating where bits should be deposited.
  61. * @return Bits from the least significant bits of @p x in the positions marked by @p mask.
  62. */
  63. template <class T>
  64. constexpr T deposit(T x, T mask) noexcept;
  65. /**
  66. * Returns the number of differing bits between two values, known as *Hamming distance*.
  67. *
  68. * @param x First value.
  69. * @param y Second value.
  70. * @return Hamming distance between @px and @p y.
  71. */
  72. template <class T>
  73. constexpr int difference(T x, T y) noexcept;
  74. /**
  75. * Reads bits from a value in the positions marked by a mask and returns them in the least significant bits.
  76. *
  77. * @param x Value from which bits should be read.
  78. * @param mask Bit mask indicating which bits to extract.
  79. * @return Bits of @p x from the positions marked by @p mask in the least significant bits.
  80. */
  81. template <class T>
  82. constexpr T extract(T x, T mask) noexcept;
  83. /**
  84. * Flips a single bit in a value.
  85. *
  86. * @param x Value to modify.
  87. * @param i Index of the bit to flip.
  88. * @return Copy of @p x with one bit flipped.
  89. */
  90. template <class T>
  91. constexpr T flip(T x, int i) noexcept;
  92. /**
  93. *
  94. */
  95. template <class T, class U = T>
  96. constexpr U interleave(T a, T b) noexcept;
  97. /**
  98. *
  99. */
  100. template <class T>
  101. constexpr T merge(T a, T b, T mask) noexcept;
  102. /**
  103. * Returns the parity of a value.
  104. *
  105. * @param x Value to test.
  106. * @return `1` if an odd number of bits are set, `0` otherwise.
  107. */
  108. template <class T>
  109. constexpr T parity(T x) noexcept;
  110. /**
  111. *
  112. */
  113. template <class T>
  114. constexpr T swap_adjacent(T x) noexcept;
  115. /**
  116. * Segregates the odd and even bits of a value.
  117. *
  118. * @param x Value to segregate.
  119. * @return Value with even bits of @p x in the lower half, and odd bits in the upper half.
  120. */
  121. template <class T>
  122. T segregate(T x)
  123. {
  124. T odd = bit::compress(x);
  125. T even = bit::compress(x >> 1);
  126. return odd | (even << (sizeof(T) << 2));
  127. }
  128. /**
  129. * Interleaves bits of the lower and upper halves of a value.
  130. *
  131. * @param x Value to desegregate.
  132. * @return Value with bits from the upper half of @p x interleaved with bits from the lower half.
  133. */
  134. template <class T>
  135. T desegregate(T x)
  136. {
  137. return bit::interleave<T>(x, x >> (sizeof(T) << 2));
  138. }
  139. /**
  140. * Replicates each bit in the lower half of a value.
  141. *
  142. * @param x Value to replicate.
  143. * @return Value of @p x interleaved with itself.
  144. */
  145. template <class T>
  146. T replicate(T x)
  147. {
  148. x = bit::expand(x);
  149. return x | (x << 1);
  150. }
  151. /**
  152. * Isolates a pair of bits and returns them in the two least significant bits.
  153. *
  154. * @param x Diploid chromosome with interleaved alleles.
  155. * @param i Index of the pair to isolate.
  156. * @return Isolated pair of bits in the two least significant bits.
  157. */
  158. template <class T>
  159. T isolate(T x, int i)
  160. {
  161. return (x >> (i << 1)) & 0b11;
  162. }
  163. /**
  164. * Isolates a sequence of genes and returns their alleles in the least significant bits.
  165. *
  166. * @param x Diploid chromosome with interleaed alleles.
  167. * @param i Index of the first gene in the sequence.
  168. * @param n Length of the sequence, in genes.
  169. * @return Alleles of the sequeuence in the least significant bits.
  170. */
  171. template <class T>
  172. T isolate_n(T x, int i, int n)
  173. {
  174. T mask = (T(1) << (n << 1)) - 1;
  175. return (x >> (i << 1)) & mask;
  176. }
  177. template <class T>
  178. constexpr T compress(T x) noexcept
  179. {
  180. x &= T(0x5555555555555555);
  181. x = (x ^ (x >> 1)) & T(0x3333333333333333);
  182. x = (x ^ (x >> 2)) & T(0x0f0f0f0f0f0f0f0f);
  183. if constexpr(sizeof(T) >= 2)
  184. x = (x ^ (x >> 4)) & T(0x00ff00ff00ff00ff);
  185. if constexpr(sizeof(T) >= 4)
  186. x = (x ^ (x >> 8)) & T(0x0000ffff0000ffff);
  187. if constexpr(sizeof(T) >= 8)
  188. x = (x ^ (x >> 16)) & T(0x00000000ffffffff);
  189. return x;
  190. }
  191. template <class T>
  192. constexpr int count(T x) noexcept
  193. {
  194. int n = 0;
  195. for (; x; ++n)
  196. x &= x - 1;
  197. return n;
  198. }
  199. template <class T>
  200. inline constexpr T crossover(T a, T b, int i) noexcept
  201. {
  202. T mask = (T(1) << i) - 1;
  203. return bit::merge(b, a, mask);
  204. }
  205. template <class T>
  206. constexpr T crossover_n(T a, T b, T mask) noexcept
  207. {
  208. T merge = ~T(0) * bit::parity(mask);
  209. while (mask)
  210. {
  211. merge ^= (mask ^ (mask - 1)) >> 1;
  212. mask &= mask - 1;
  213. }
  214. return bit::merge(a, b, merge);
  215. }
  216. template <class T>
  217. constexpr T expand(T x) noexcept
  218. {
  219. x &= (1 << (sizeof(T) << 2)) - 1;
  220. if constexpr(sizeof(T) >= 8)
  221. x = (x ^ (x << 16)) & T(0x0000ffff0000ffff);
  222. if constexpr(sizeof(T) >= 4)
  223. x = (x ^ (x << 8)) & T(0x00ff00ff00ff00ff);
  224. if constexpr(sizeof(T) >= 2)
  225. x = (x ^ (x << 4)) & T(0x0f0f0f0f0f0f0f0f);
  226. x = (x ^ (x << 2)) & T(0x3333333333333333);
  227. x = (x ^ (x << 1)) & T(0x5555555555555555);
  228. return x;
  229. }
  230. template <class T>
  231. inline constexpr T flip(T x, int i) noexcept
  232. {
  233. return x ^ (T(1) << i);
  234. }
  235. template <class T, class U>
  236. inline constexpr U interleave(T a, T b) noexcept
  237. {
  238. return expand<U>(a) | (expand<U>(b) << 1);
  239. }
  240. /**
  241. * For an n-bit number with r set bits, there are `n! / ((n - r)! * r!)` permutations.
  242. */
  243. template <class T>
  244. constexpr T next_permutation(T x) noexcept
  245. {
  246. T y = (x | (x - 1)) + 1;
  247. return y | ((((y & -y) / (x & -x)) >> 1) - 1);
  248. }
  249. template <class T>
  250. constexpr T deposit(T x, T mask) noexcept
  251. {
  252. T result = 0;
  253. for (T i = 1; mask; i <<= 1)
  254. {
  255. if (x & i)
  256. result |= mask & -mask;
  257. mask &= mask - 1;
  258. }
  259. return result;
  260. }
  261. template <class T>
  262. constexpr T extract(T x, T mask) noexcept
  263. {
  264. T result = 0;
  265. for (T i = 1; mask; i <<= 1)
  266. {
  267. if (x & mask & -mask)
  268. result |= i;
  269. mask &= mask - 1;
  270. }
  271. return result;
  272. }
  273. template <class T>
  274. inline constexpr int difference(T x, T y) noexcept
  275. {
  276. return count(x ^ y);
  277. }
  278. template <class T>
  279. constexpr T merge(T a, T b, T mask) noexcept
  280. {
  281. return a ^ ((a ^ b) & mask);
  282. }
  283. template <class T>
  284. constexpr T parity(T x) noexcept
  285. {
  286. if constexpr(sizeof(T) >= 8)
  287. x ^= x >> 32;
  288. if constexpr(sizeof(T) >= 4)
  289. x ^= x >> 16;
  290. if constexpr(sizeof(T) >= 2)
  291. x ^= x >> 8;
  292. x ^= x >> 4;
  293. return (0x6996 >> (x & 0xf)) & 1;
  294. }
  295. template <class T>
  296. constexpr T swap_adjacent(T x) noexcept
  297. {
  298. return ((x & T(0xaaaaaaaaaaaaaaaa)) >> 1) | ((x & T(0x5555555555555555)) << 1);
  299. }
  300. } // namespace bit
  301. #endif // ANTKEEPER_BIT_MATH_HPP