summaryrefslogtreecommitdiffstats
path: root/src/common/bit_set.h
diff options
context:
space:
mode:
authorbunnei <bunneidev@gmail.com>2020-11-27 23:13:18 +0100
committerbunnei <bunneidev@gmail.com>2020-12-06 09:03:24 +0100
commit8dbfa4e1a4af2cda4500304ba8fe1a1b09bbb814 (patch)
tree98331155b0fbe7c5fcdabde69d4b0419b84cda65 /src/common/bit_set.h
parenthle: kernel: Port KAffinityMask from Mesosphere. (diff)
downloadyuzu-8dbfa4e1a4af2cda4500304ba8fe1a1b09bbb814.tar
yuzu-8dbfa4e1a4af2cda4500304ba8fe1a1b09bbb814.tar.gz
yuzu-8dbfa4e1a4af2cda4500304ba8fe1a1b09bbb814.tar.bz2
yuzu-8dbfa4e1a4af2cda4500304ba8fe1a1b09bbb814.tar.lz
yuzu-8dbfa4e1a4af2cda4500304ba8fe1a1b09bbb814.tar.xz
yuzu-8dbfa4e1a4af2cda4500304ba8fe1a1b09bbb814.tar.zst
yuzu-8dbfa4e1a4af2cda4500304ba8fe1a1b09bbb814.zip
Diffstat (limited to 'src/common/bit_set.h')
-rw-r--r--src/common/bit_set.h100
1 files changed, 100 insertions, 0 deletions
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 <http://www.gnu.org/licenses/>.
+ */
+
+#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 <typename Storage, size_t N>
+class BitSet {
+private:
+ static_assert(std::is_integral<Storage>::value);
+ static_assert(std::is_unsigned<Storage>::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<unsigned long long>(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 <size_t N>
+using BitSet8 = impl::BitSet<u8, N>;
+
+template <size_t N>
+using BitSet16 = impl::BitSet<u16, N>;
+
+template <size_t N>
+using BitSet32 = impl::BitSet<u32, N>;
+
+template <size_t N>
+using BitSet64 = impl::BitSet<u64, N>;
+
+} // namespace Common