bit_set.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2018-2020 Atmosphère-NX
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #pragma once
  17. #include <array>
  18. #include <bit>
  19. #include "common/alignment.h"
  20. #include "common/bit_util.h"
  21. #include "common/common_types.h"
  22. namespace Common {
  23. namespace impl {
  24. template <typename Storage, size_t N>
  25. class BitSet {
  26. public:
  27. constexpr BitSet() = default;
  28. constexpr void SetBit(size_t i) {
  29. this->words[i / FlagsPerWord] |= GetBitMask(i % FlagsPerWord);
  30. }
  31. constexpr void ClearBit(size_t i) {
  32. this->words[i / FlagsPerWord] &= ~GetBitMask(i % FlagsPerWord);
  33. }
  34. constexpr size_t CountLeadingZero() const {
  35. for (size_t i = 0; i < NumWords; i++) {
  36. if (this->words[i]) {
  37. return FlagsPerWord * i + CountLeadingZeroImpl(this->words[i]);
  38. }
  39. }
  40. return FlagsPerWord * NumWords;
  41. }
  42. constexpr size_t GetNextSet(size_t n) const {
  43. for (size_t i = (n + 1) / FlagsPerWord; i < NumWords; i++) {
  44. Storage word = this->words[i];
  45. if (!IsAligned(n + 1, FlagsPerWord)) {
  46. word &= GetBitMask(n % FlagsPerWord) - 1;
  47. }
  48. if (word) {
  49. return FlagsPerWord * i + CountLeadingZeroImpl(word);
  50. }
  51. }
  52. return FlagsPerWord * NumWords;
  53. }
  54. private:
  55. static_assert(std::is_unsigned_v<Storage>);
  56. static_assert(sizeof(Storage) <= sizeof(u64));
  57. static constexpr size_t FlagsPerWord = BitSize<Storage>();
  58. static constexpr size_t NumWords = AlignUp(N, FlagsPerWord) / FlagsPerWord;
  59. static constexpr auto CountLeadingZeroImpl(Storage word) {
  60. return std::countl_zero(static_cast<unsigned long long>(word)) -
  61. (BitSize<unsigned long long>() - FlagsPerWord);
  62. }
  63. static constexpr Storage GetBitMask(size_t bit) {
  64. return Storage(1) << (FlagsPerWord - 1 - bit);
  65. }
  66. std::array<Storage, NumWords> words{};
  67. };
  68. } // namespace impl
  69. template <size_t N>
  70. using BitSet8 = impl::BitSet<u8, N>;
  71. template <size_t N>
  72. using BitSet16 = impl::BitSet<u16, N>;
  73. template <size_t N>
  74. using BitSet32 = impl::BitSet<u32, N>;
  75. template <size_t N>
  76. using BitSet64 = impl::BitSet<u64, N>;
  77. } // namespace Common