From 8dbfa4e1a4af2cda4500304ba8fe1a1b09bbb814 Mon Sep 17 00:00:00 2001
From: bunnei
Date: Fri, 27 Nov 2020 14:13:18 -0800
Subject: common: Port BitSet from Mesosphere.
---
src/common/bit_set.h | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 100 insertions(+)
create mode 100644 src/common/bit_set.h
(limited to 'src/common/bit_set.h')
diff --git a/src/common/bit_set.h b/src/common/bit_set.h
new file mode 100644
index 000000000..4f966de40
--- /dev/null
+++ b/src/common/bit_set.h
@@ -0,0 +1,100 @@
+/*
+ * Copyright (c) 2018-2020 Atmosphère-NX
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
+#pragma once
+
+#include "common/alignment.h"
+#include "common/bit_util.h"
+#include "common/common_types.h"
+
+namespace Common {
+
+namespace impl {
+
+#define BITSIZEOF(x) (sizeof(x) * CHAR_BIT)
+
+template
+class BitSet {
+private:
+ static_assert(std::is_integral::value);
+ static_assert(std::is_unsigned::value);
+ static_assert(sizeof(Storage) <= sizeof(u64));
+
+ static constexpr size_t FlagsPerWord = BITSIZEOF(Storage);
+ static constexpr size_t NumWords = AlignUp(N, FlagsPerWord) / FlagsPerWord;
+
+ static constexpr auto CountLeadingZeroImpl(Storage word) {
+ return CountLeadingZeroes64(static_cast(word)) -
+ (BITSIZEOF(unsigned long long) - FlagsPerWord);
+ }
+
+ static constexpr Storage GetBitMask(size_t bit) {
+ return Storage(1) << (FlagsPerWord - 1 - bit);
+ }
+
+private:
+ Storage words[NumWords];
+
+public:
+ constexpr BitSet() : words() { /* ... */
+ }
+
+ constexpr void SetBit(size_t i) {
+ this->words[i / FlagsPerWord] |= GetBitMask(i % FlagsPerWord);
+ }
+
+ constexpr void ClearBit(size_t i) {
+ this->words[i / FlagsPerWord] &= ~GetBitMask(i % FlagsPerWord);
+ }
+
+ constexpr size_t CountLeadingZero() const {
+ for (size_t i = 0; i < NumWords; i++) {
+ if (this->words[i]) {
+ return FlagsPerWord * i + CountLeadingZeroImpl(this->words[i]);
+ }
+ }
+ return FlagsPerWord * NumWords;
+ }
+
+ constexpr size_t GetNextSet(size_t n) const {
+ for (size_t i = (n + 1) / FlagsPerWord; i < NumWords; i++) {
+ Storage word = this->words[i];
+ if (!IsAligned(n + 1, FlagsPerWord)) {
+ word &= GetBitMask(n % FlagsPerWord) - 1;
+ }
+ if (word) {
+ return FlagsPerWord * i + CountLeadingZeroImpl(word);
+ }
+ }
+ return FlagsPerWord * NumWords;
+ }
+};
+
+} // namespace impl
+
+template
+using BitSet8 = impl::BitSet;
+
+template
+using BitSet16 = impl::BitSet;
+
+template
+using BitSet32 = impl::BitSet;
+
+template
+using BitSet64 = impl::BitSet;
+
+} // namespace Common
--
cgit v1.2.3
From d2c0c94f0b76fbae9dd5a7b7cb81b9b53db8be0e Mon Sep 17 00:00:00 2001
From: bunnei
Date: Fri, 4 Dec 2020 23:46:37 -0800
Subject: common: BitSet: Various style fixes based on code review feedback.
---
src/common/bit_set.h | 45 ++++++++++++++++++++++-----------------------
1 file changed, 22 insertions(+), 23 deletions(-)
(limited to 'src/common/bit_set.h')
diff --git a/src/common/bit_set.h b/src/common/bit_set.h
index 4f966de40..9235ad412 100644
--- a/src/common/bit_set.h
+++ b/src/common/bit_set.h
@@ -16,6 +16,9 @@
#pragma once
+#include
+#include
+
#include "common/alignment.h"
#include "common/bit_util.h"
#include "common/common_types.h"
@@ -24,33 +27,11 @@ namespace Common {
namespace impl {
-#define BITSIZEOF(x) (sizeof(x) * CHAR_BIT)
-
template
class BitSet {
-private:
- static_assert(std::is_integral::value);
- static_assert(std::is_unsigned::value);
- static_assert(sizeof(Storage) <= sizeof(u64));
-
- static constexpr size_t FlagsPerWord = BITSIZEOF(Storage);
- static constexpr size_t NumWords = AlignUp(N, FlagsPerWord) / FlagsPerWord;
-
- static constexpr auto CountLeadingZeroImpl(Storage word) {
- return CountLeadingZeroes64(static_cast(word)) -
- (BITSIZEOF(unsigned long long) - FlagsPerWord);
- }
-
- static constexpr Storage GetBitMask(size_t bit) {
- return Storage(1) << (FlagsPerWord - 1 - bit);
- }
-
-private:
- Storage words[NumWords];
public:
- constexpr BitSet() : words() { /* ... */
- }
+ constexpr BitSet() = default;
constexpr void SetBit(size_t i) {
this->words[i / FlagsPerWord] |= GetBitMask(i % FlagsPerWord);
@@ -81,6 +62,24 @@ public:
}
return FlagsPerWord * NumWords;
}
+
+private:
+ static_assert(std::is_unsigned_v);
+ static_assert(sizeof(Storage) <= sizeof(u64));
+
+ static constexpr size_t FlagsPerWord = BitSize();
+ static constexpr size_t NumWords = AlignUp(N, FlagsPerWord) / FlagsPerWord;
+
+ static constexpr auto CountLeadingZeroImpl(Storage word) {
+ return std::countl_zero(static_cast(word)) -
+ (BitSize() - FlagsPerWord);
+ }
+
+ static constexpr Storage GetBitMask(size_t bit) {
+ return Storage(1) << (FlagsPerWord - 1 - bit);
+ }
+
+ std::array words{};
};
} // namespace impl
--
cgit v1.2.3