diff options
| author | 2015-08-26 09:11:07 +0200 | |
|---|---|---|
| committer | 2015-09-01 23:39:52 +0200 | |
| commit | 0b6c0afeb7d5eb4a44897303a4e93d6c863c486d (patch) | |
| tree | 6d38cfa5345b873442cc7e445f4e76dc39f0f641 /src | |
| parent | Merge pull request #1072 from yuriks/GetSystemTick-advance-time (diff) | |
| download | yuzu-0b6c0afeb7d5eb4a44897303a4e93d6c863c486d.tar.gz yuzu-0b6c0afeb7d5eb4a44897303a4e93d6c863c486d.tar.xz yuzu-0b6c0afeb7d5eb4a44897303a4e93d6c863c486d.zip | |
Common: Import BitSet from Dolphin
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/common/bit_set.h | 189 |
2 files changed, 190 insertions, 0 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 7f3712efa..2be6fe996 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt | |||
| @@ -24,6 +24,7 @@ set(SRCS | |||
| 24 | set(HEADERS | 24 | set(HEADERS |
| 25 | assert.h | 25 | assert.h |
| 26 | bit_field.h | 26 | bit_field.h |
| 27 | bit_set.h | ||
| 27 | break_points.h | 28 | break_points.h |
| 28 | chunk_file.h | 29 | chunk_file.h |
| 29 | code_block.h | 30 | code_block.h |
diff --git a/src/common/bit_set.h b/src/common/bit_set.h new file mode 100644 index 000000000..85f91e786 --- /dev/null +++ b/src/common/bit_set.h | |||
| @@ -0,0 +1,189 @@ | |||
| 1 | // This file is under the public domain. | ||
| 2 | |||
| 3 | #pragma once | ||
| 4 | |||
| 5 | #include <cstddef> | ||
| 6 | #ifdef _WIN32 | ||
| 7 | #include <intrin.h> | ||
| 8 | #endif | ||
| 9 | #include <initializer_list> | ||
| 10 | #include <type_traits> | ||
| 11 | #include "common/common_types.h" | ||
| 12 | |||
| 13 | // namespace avoids conflict with OS X Carbon; don't use BitSet<T> directly | ||
| 14 | namespace Common { | ||
| 15 | |||
| 16 | // Helper functions: | ||
| 17 | |||
| 18 | #ifdef _WIN32 | ||
| 19 | template <typename T> | ||
| 20 | static inline int CountSetBits(T v) | ||
| 21 | { | ||
| 22 | // from https://graphics.stanford.edu/~seander/bithacks.html | ||
| 23 | // GCC has this built in, but MSVC's intrinsic will only emit the actual | ||
| 24 | // POPCNT instruction, which we're not depending on | ||
| 25 | v = v - ((v >> 1) & (T)~(T)0/3); | ||
| 26 | v = (v & (T)~(T)0/15*3) + ((v >> 2) & (T)~(T)0/15*3); | ||
| 27 | v = (v + (v >> 4)) & (T)~(T)0/255*15; | ||
| 28 | return (T)(v * ((T)~(T)0/255)) >> (sizeof(T) - 1) * 8; | ||
| 29 | } | ||
| 30 | static inline int LeastSignificantSetBit(u8 val) | ||
| 31 | { | ||
| 32 | unsigned long index; | ||
| 33 | _BitScanForward(&index, val); | ||
| 34 | return (int)index; | ||
| 35 | } | ||
| 36 | static inline int LeastSignificantSetBit(u16 val) | ||
| 37 | { | ||
| 38 | unsigned long index; | ||
| 39 | _BitScanForward(&index, val); | ||
| 40 | return (int)index; | ||
| 41 | } | ||
| 42 | static inline int LeastSignificantSetBit(u32 val) | ||
| 43 | { | ||
| 44 | unsigned long index; | ||
| 45 | _BitScanForward(&index, val); | ||
| 46 | return (int)index; | ||
| 47 | } | ||
| 48 | static inline int LeastSignificantSetBit(u64 val) | ||
| 49 | { | ||
| 50 | unsigned long index; | ||
| 51 | _BitScanForward64(&index, val); | ||
| 52 | return (int)index; | ||
| 53 | } | ||
| 54 | #else | ||
| 55 | static inline int CountSetBits(u8 val) { return __builtin_popcount(val); } | ||
| 56 | static inline int CountSetBits(u16 val) { return __builtin_popcount(val); } | ||
| 57 | static inline int CountSetBits(u32 val) { return __builtin_popcount(val); } | ||
| 58 | static inline int CountSetBits(u64 val) { return __builtin_popcountll(val); } | ||
| 59 | static inline int LeastSignificantSetBit(u8 val) { return __builtin_ctz(val); } | ||
| 60 | static inline int LeastSignificantSetBit(u16 val) { return __builtin_ctz(val); } | ||
| 61 | static inline int LeastSignificantSetBit(u32 val) { return __builtin_ctz(val); } | ||
| 62 | static inline int LeastSignificantSetBit(u64 val) { return __builtin_ctzll(val); } | ||
| 63 | #endif | ||
| 64 | |||
| 65 | // Similar to std::bitset, this is a class which encapsulates a bitset, i.e. | ||
| 66 | // using the set bits of an integer to represent a set of integers. Like that | ||
| 67 | // class, it acts like an array of bools: | ||
| 68 | // BitSet32 bs; | ||
| 69 | // bs[1] = true; | ||
| 70 | // but also like the underlying integer ([0] = least significant bit): | ||
| 71 | // BitSet32 bs2 = ...; | ||
| 72 | // bs = (bs ^ bs2) & BitSet32(0xffff); | ||
| 73 | // The following additional functionality is provided: | ||
| 74 | // - Construction using an initializer list. | ||
| 75 | // BitSet bs { 1, 2, 4, 8 }; | ||
| 76 | // - Efficiently iterating through the set bits: | ||
| 77 | // for (int i : bs) | ||
| 78 | // [i is the *index* of a set bit] | ||
| 79 | // (This uses the appropriate CPU instruction to find the next set bit in one | ||
| 80 | // operation.) | ||
| 81 | // - Counting set bits using .Count() - see comment on that method. | ||
| 82 | |||
| 83 | // TODO: use constexpr when MSVC gets out of the Dark Ages | ||
| 84 | |||
| 85 | template <typename IntTy> | ||
| 86 | class BitSet | ||
| 87 | { | ||
| 88 | static_assert(!std::is_signed<IntTy>::value, "BitSet should not be used with signed types"); | ||
| 89 | public: | ||
| 90 | // A reference to a particular bit, returned from operator[]. | ||
| 91 | class Ref | ||
| 92 | { | ||
| 93 | public: | ||
| 94 | Ref(Ref&& other) : m_bs(other.m_bs), m_mask(other.m_mask) {} | ||
| 95 | Ref(BitSet* bs, IntTy mask) : m_bs(bs), m_mask(mask) {} | ||
| 96 | operator bool() const { return (m_bs->m_val & m_mask) != 0; } | ||
| 97 | bool operator=(bool set) | ||
| 98 | { | ||
| 99 | m_bs->m_val = (m_bs->m_val & ~m_mask) | (set ? m_mask : 0); | ||
| 100 | return set; | ||
| 101 | } | ||
| 102 | private: | ||
| 103 | BitSet* m_bs; | ||
| 104 | IntTy m_mask; | ||
| 105 | }; | ||
| 106 | |||
| 107 | // A STL-like iterator is required to be able to use range-based for loops. | ||
| 108 | class Iterator | ||
| 109 | { | ||
| 110 | public: | ||
| 111 | Iterator(const Iterator& other) : m_val(other.m_val), m_bit(other.m_bit) {} | ||
| 112 | Iterator(IntTy val, int bit) : m_val(val), m_bit(bit) {} | ||
| 113 | Iterator& operator=(Iterator other) { new (this) Iterator(other); return *this; } | ||
| 114 | int operator*() { return m_bit; } | ||
| 115 | Iterator& operator++() | ||
| 116 | { | ||
| 117 | if (m_val == 0) | ||
| 118 | { | ||
| 119 | m_bit = -1; | ||
| 120 | } | ||
| 121 | else | ||
| 122 | { | ||
| 123 | int bit = LeastSignificantSetBit(m_val); | ||
| 124 | m_val &= ~(1 << bit); | ||
| 125 | m_bit = bit; | ||
| 126 | } | ||
| 127 | return *this; | ||
| 128 | } | ||
| 129 | Iterator operator++(int _) | ||
| 130 | { | ||
| 131 | Iterator other(*this); | ||
| 132 | ++*this; | ||
| 133 | return other; | ||
| 134 | } | ||
| 135 | bool operator==(Iterator other) const { return m_bit == other.m_bit; } | ||
| 136 | bool operator!=(Iterator other) const { return m_bit != other.m_bit; } | ||
| 137 | private: | ||
| 138 | IntTy m_val; | ||
| 139 | int m_bit; | ||
| 140 | }; | ||
| 141 | |||
| 142 | BitSet() : m_val(0) {} | ||
| 143 | explicit BitSet(IntTy val) : m_val(val) {} | ||
| 144 | BitSet(std::initializer_list<int> init) | ||
| 145 | { | ||
| 146 | m_val = 0; | ||
| 147 | for (int bit : init) | ||
| 148 | m_val |= (IntTy)1 << bit; | ||
| 149 | } | ||
| 150 | |||
| 151 | static BitSet AllTrue(size_t count) | ||
| 152 | { | ||
| 153 | return BitSet(count == sizeof(IntTy)*8 ? ~(IntTy)0 : (((IntTy)1 << count) - 1)); | ||
| 154 | } | ||
| 155 | |||
| 156 | Ref operator[](size_t bit) { return Ref(this, (IntTy)1 << bit); } | ||
| 157 | const Ref operator[](size_t bit) const { return (*const_cast<BitSet*>(this))[bit]; } | ||
| 158 | bool operator==(BitSet other) const { return m_val == other.m_val; } | ||
| 159 | bool operator!=(BitSet other) const { return m_val != other.m_val; } | ||
| 160 | bool operator<(BitSet other) const { return m_val < other.m_val; } | ||
| 161 | bool operator>(BitSet other) const { return m_val > other.m_val; } | ||
| 162 | BitSet operator|(BitSet other) const { return BitSet(m_val | other.m_val); } | ||
| 163 | BitSet operator&(BitSet other) const { return BitSet(m_val & other.m_val); } | ||
| 164 | BitSet operator^(BitSet other) const { return BitSet(m_val ^ other.m_val); } | ||
| 165 | BitSet operator~() const { return BitSet(~m_val); } | ||
| 166 | BitSet& operator|=(BitSet other) { return *this = *this | other; } | ||
| 167 | BitSet& operator&=(BitSet other) { return *this = *this & other; } | ||
| 168 | BitSet& operator^=(BitSet other) { return *this = *this ^ other; } | ||
| 169 | operator u32() = delete; | ||
| 170 | operator bool() { return m_val != 0; } | ||
| 171 | |||
| 172 | // Warning: Even though on modern CPUs this is a single fast instruction, | ||
| 173 | // Dolphin's official builds do not currently assume POPCNT support on x86, | ||
| 174 | // so slower explicit bit twiddling is generated. Still should generally | ||
| 175 | // be faster than a loop. | ||
| 176 | unsigned int Count() const { return CountSetBits(m_val); } | ||
| 177 | |||
| 178 | Iterator begin() const { Iterator it(m_val, 0); return ++it; } | ||
| 179 | Iterator end() const { return Iterator(m_val, -1); } | ||
| 180 | |||
| 181 | IntTy m_val; | ||
| 182 | }; | ||
| 183 | |||
| 184 | } // Common | ||
| 185 | |||
| 186 | typedef Common::BitSet<u8> BitSet8; | ||
| 187 | typedef Common::BitSet<u16> BitSet16; | ||
| 188 | typedef Common::BitSet<u32> BitSet32; | ||
| 189 | typedef Common::BitSet<u64> BitSet64; \ No newline at end of file | ||