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

96 lines
2.4 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. template <class T>
  50. constexpr T bit_deposit(T x, T mask) noexcept
  51. {
  52. T result = 0;
  53. for (T i = 1; mask; i <<= 1)
  54. {
  55. if (x & i)
  56. result |= mask & -mask;
  57. mask &= mask - 1;
  58. }
  59. return result;
  60. }
  61. template <class T>
  62. constexpr T bit_extract(T x, T mask) noexcept
  63. {
  64. T result = 0;
  65. for (T i = 1; mask; i <<= 1)
  66. {
  67. if (x & mask & -mask)
  68. result |= i;
  69. mask &= mask - 1;
  70. }
  71. return result;
  72. }
  73. template <typename T>
  74. constexpr int popcount(T x) noexcept
  75. {
  76. int n = 0;
  77. for (; x; ++n)
  78. x &= x - 1;
  79. return n;
  80. }
  81. } // namespace dna
  82. #endif // ANTKEEPER_BIT_MATH_HPP