diff options
| author | 2016-09-21 11:29:48 -0700 | |
|---|---|---|
| committer | 2016-09-21 11:29:48 -0700 | |
| commit | d5d2ca8058a0f1c00ab7ca9fe2c058ba47546c0a (patch) | |
| tree | 8a22ca73ff838f3f0090b29a548ae81087fc90ed /src/common/bit_set.h | |
| parent | README: Specify master branch for Travis CI badge (diff) | |
| parent | Fix Travis clang-format check (diff) | |
| download | yuzu-d5d2ca8058a0f1c00ab7ca9fe2c058ba47546c0a.tar.gz yuzu-d5d2ca8058a0f1c00ab7ca9fe2c058ba47546c0a.tar.xz yuzu-d5d2ca8058a0f1c00ab7ca9fe2c058ba47546c0a.zip | |
Merge pull request #2086 from linkmauve/clang-format
Add clang-format as part of our {commit,travis}-time checks
Diffstat (limited to 'src/common/bit_set.h')
| -rw-r--r-- | src/common/bit_set.h | 185 |
1 files changed, 117 insertions, 68 deletions
diff --git a/src/common/bit_set.h b/src/common/bit_set.h index 7f5de8df2..c48b3b769 100644 --- a/src/common/bit_set.h +++ b/src/common/bit_set.h | |||
| @@ -18,49 +18,60 @@ namespace Common { | |||
| 18 | 18 | ||
| 19 | #ifdef _WIN32 | 19 | #ifdef _WIN32 |
| 20 | template <typename T> | 20 | template <typename T> |
| 21 | static inline int CountSetBits(T v) | 21 | static inline int CountSetBits(T v) { |
| 22 | { | ||
| 23 | // from https://graphics.stanford.edu/~seander/bithacks.html | 22 | // from https://graphics.stanford.edu/~seander/bithacks.html |
| 24 | // GCC has this built in, but MSVC's intrinsic will only emit the actual | 23 | // GCC has this built in, but MSVC's intrinsic will only emit the actual |
| 25 | // POPCNT instruction, which we're not depending on | 24 | // POPCNT instruction, which we're not depending on |
| 26 | v = v - ((v >> 1) & (T)~(T)0/3); | 25 | v = v - ((v >> 1) & (T) ~(T)0 / 3); |
| 27 | v = (v & (T)~(T)0/15*3) + ((v >> 2) & (T)~(T)0/15*3); | 26 | v = (v & (T) ~(T)0 / 15 * 3) + ((v >> 2) & (T) ~(T)0 / 15 * 3); |
| 28 | v = (v + (v >> 4)) & (T)~(T)0/255*15; | 27 | v = (v + (v >> 4)) & (T) ~(T)0 / 255 * 15; |
| 29 | return (T)(v * ((T)~(T)0/255)) >> (sizeof(T) - 1) * 8; | 28 | return (T)(v * ((T) ~(T)0 / 255)) >> (sizeof(T) - 1) * 8; |
| 30 | } | 29 | } |
| 31 | static inline int LeastSignificantSetBit(u8 val) | 30 | static inline int LeastSignificantSetBit(u8 val) { |
| 32 | { | ||
| 33 | unsigned long index; | 31 | unsigned long index; |
| 34 | _BitScanForward(&index, val); | 32 | _BitScanForward(&index, val); |
| 35 | return (int)index; | 33 | return (int)index; |
| 36 | } | 34 | } |
| 37 | static inline int LeastSignificantSetBit(u16 val) | 35 | static inline int LeastSignificantSetBit(u16 val) { |
| 38 | { | ||
| 39 | unsigned long index; | 36 | unsigned long index; |
| 40 | _BitScanForward(&index, val); | 37 | _BitScanForward(&index, val); |
| 41 | return (int)index; | 38 | return (int)index; |
| 42 | } | 39 | } |
| 43 | static inline int LeastSignificantSetBit(u32 val) | 40 | static inline int LeastSignificantSetBit(u32 val) { |
| 44 | { | ||
| 45 | unsigned long index; | 41 | unsigned long index; |
| 46 | _BitScanForward(&index, val); | 42 | _BitScanForward(&index, val); |
| 47 | return (int)index; | 43 | return (int)index; |
| 48 | } | 44 | } |
| 49 | static inline int LeastSignificantSetBit(u64 val) | 45 | static inline int LeastSignificantSetBit(u64 val) { |
| 50 | { | ||
| 51 | unsigned long index; | 46 | unsigned long index; |
| 52 | _BitScanForward64(&index, val); | 47 | _BitScanForward64(&index, val); |
| 53 | return (int)index; | 48 | return (int)index; |
| 54 | } | 49 | } |
| 55 | #else | 50 | #else |
| 56 | static inline int CountSetBits(u8 val) { return __builtin_popcount(val); } | 51 | static inline int CountSetBits(u8 val) { |
| 57 | static inline int CountSetBits(u16 val) { return __builtin_popcount(val); } | 52 | return __builtin_popcount(val); |
| 58 | static inline int CountSetBits(u32 val) { return __builtin_popcount(val); } | 53 | } |
| 59 | static inline int CountSetBits(u64 val) { return __builtin_popcountll(val); } | 54 | static inline int CountSetBits(u16 val) { |
| 60 | static inline int LeastSignificantSetBit(u8 val) { return __builtin_ctz(val); } | 55 | return __builtin_popcount(val); |
| 61 | static inline int LeastSignificantSetBit(u16 val) { return __builtin_ctz(val); } | 56 | } |
| 62 | static inline int LeastSignificantSetBit(u32 val) { return __builtin_ctz(val); } | 57 | static inline int CountSetBits(u32 val) { |
| 63 | static inline int LeastSignificantSetBit(u64 val) { return __builtin_ctzll(val); } | 58 | return __builtin_popcount(val); |
| 59 | } | ||
| 60 | static inline int CountSetBits(u64 val) { | ||
| 61 | return __builtin_popcountll(val); | ||
| 62 | } | ||
| 63 | static inline int LeastSignificantSetBit(u8 val) { | ||
| 64 | return __builtin_ctz(val); | ||
| 65 | } | ||
| 66 | static inline int LeastSignificantSetBit(u16 val) { | ||
| 67 | return __builtin_ctz(val); | ||
| 68 | } | ||
| 69 | static inline int LeastSignificantSetBit(u32 val) { | ||
| 70 | return __builtin_ctz(val); | ||
| 71 | } | ||
| 72 | static inline int LeastSignificantSetBit(u64 val) { | ||
| 73 | return __builtin_ctzll(val); | ||
| 74 | } | ||
| 64 | #endif | 75 | #endif |
| 65 | 76 | ||
| 66 | // Similar to std::bitset, this is a class which encapsulates a bitset, i.e. | 77 | // Similar to std::bitset, this is a class which encapsulates a bitset, i.e. |
| @@ -84,57 +95,62 @@ static inline int LeastSignificantSetBit(u64 val) { return __builtin_ctzll(val); | |||
| 84 | // TODO: use constexpr when MSVC gets out of the Dark Ages | 95 | // TODO: use constexpr when MSVC gets out of the Dark Ages |
| 85 | 96 | ||
| 86 | template <typename IntTy> | 97 | template <typename IntTy> |
| 87 | class BitSet | 98 | class BitSet { |
| 88 | { | ||
| 89 | static_assert(!std::is_signed<IntTy>::value, "BitSet should not be used with signed types"); | 99 | static_assert(!std::is_signed<IntTy>::value, "BitSet should not be used with signed types"); |
| 100 | |||
| 90 | public: | 101 | public: |
| 91 | // A reference to a particular bit, returned from operator[]. | 102 | // A reference to a particular bit, returned from operator[]. |
| 92 | class Ref | 103 | class Ref { |
| 93 | { | ||
| 94 | public: | 104 | public: |
| 95 | Ref(Ref&& other) : m_bs(other.m_bs), m_mask(other.m_mask) {} | 105 | Ref(Ref&& other) : m_bs(other.m_bs), m_mask(other.m_mask) {} |
| 96 | Ref(BitSet* bs, IntTy mask) : m_bs(bs), m_mask(mask) {} | 106 | Ref(BitSet* bs, IntTy mask) : m_bs(bs), m_mask(mask) {} |
| 97 | operator bool() const { return (m_bs->m_val & m_mask) != 0; } | 107 | operator bool() const { |
| 98 | bool operator=(bool set) | 108 | return (m_bs->m_val & m_mask) != 0; |
| 99 | { | 109 | } |
| 110 | bool operator=(bool set) { | ||
| 100 | m_bs->m_val = (m_bs->m_val & ~m_mask) | (set ? m_mask : 0); | 111 | m_bs->m_val = (m_bs->m_val & ~m_mask) | (set ? m_mask : 0); |
| 101 | return set; | 112 | return set; |
| 102 | } | 113 | } |
| 114 | |||
| 103 | private: | 115 | private: |
| 104 | BitSet* m_bs; | 116 | BitSet* m_bs; |
| 105 | IntTy m_mask; | 117 | IntTy m_mask; |
| 106 | }; | 118 | }; |
| 107 | 119 | ||
| 108 | // A STL-like iterator is required to be able to use range-based for loops. | 120 | // A STL-like iterator is required to be able to use range-based for loops. |
| 109 | class Iterator | 121 | class Iterator { |
| 110 | { | ||
| 111 | public: | 122 | public: |
| 112 | Iterator(const Iterator& other) : m_val(other.m_val), m_bit(other.m_bit) {} | 123 | Iterator(const Iterator& other) : m_val(other.m_val), m_bit(other.m_bit) {} |
| 113 | Iterator(IntTy val, int bit) : m_val(val), m_bit(bit) {} | 124 | Iterator(IntTy val, int bit) : m_val(val), m_bit(bit) {} |
| 114 | Iterator& operator=(Iterator other) { new (this) Iterator(other); return *this; } | 125 | Iterator& operator=(Iterator other) { |
| 115 | int operator*() { return m_bit; } | 126 | new (this) Iterator(other); |
| 116 | Iterator& operator++() | 127 | return *this; |
| 117 | { | 128 | } |
| 118 | if (m_val == 0) | 129 | int operator*() { |
| 119 | { | 130 | return m_bit; |
| 131 | } | ||
| 132 | Iterator& operator++() { | ||
| 133 | if (m_val == 0) { | ||
| 120 | m_bit = -1; | 134 | m_bit = -1; |
| 121 | } | 135 | } else { |
| 122 | else | ||
| 123 | { | ||
| 124 | int bit = LeastSignificantSetBit(m_val); | 136 | int bit = LeastSignificantSetBit(m_val); |
| 125 | m_val &= ~(1 << bit); | 137 | m_val &= ~(1 << bit); |
| 126 | m_bit = bit; | 138 | m_bit = bit; |
| 127 | } | 139 | } |
| 128 | return *this; | 140 | return *this; |
| 129 | } | 141 | } |
| 130 | Iterator operator++(int _) | 142 | Iterator operator++(int _) { |
| 131 | { | ||
| 132 | Iterator other(*this); | 143 | Iterator other(*this); |
| 133 | ++*this; | 144 | ++*this; |
| 134 | return other; | 145 | return other; |
| 135 | } | 146 | } |
| 136 | bool operator==(Iterator other) const { return m_bit == other.m_bit; } | 147 | bool operator==(Iterator other) const { |
| 137 | bool operator!=(Iterator other) const { return m_bit != other.m_bit; } | 148 | return m_bit == other.m_bit; |
| 149 | } | ||
| 150 | bool operator!=(Iterator other) const { | ||
| 151 | return m_bit != other.m_bit; | ||
| 152 | } | ||
| 153 | |||
| 138 | private: | 154 | private: |
| 139 | IntTy m_val; | 155 | IntTy m_val; |
| 140 | int m_bit; | 156 | int m_bit; |
| @@ -142,42 +158,75 @@ public: | |||
| 142 | 158 | ||
| 143 | BitSet() : m_val(0) {} | 159 | BitSet() : m_val(0) {} |
| 144 | explicit BitSet(IntTy val) : m_val(val) {} | 160 | explicit BitSet(IntTy val) : m_val(val) {} |
| 145 | BitSet(std::initializer_list<int> init) | 161 | BitSet(std::initializer_list<int> init) { |
| 146 | { | ||
| 147 | m_val = 0; | 162 | m_val = 0; |
| 148 | for (int bit : init) | 163 | for (int bit : init) |
| 149 | m_val |= (IntTy)1 << bit; | 164 | m_val |= (IntTy)1 << bit; |
| 150 | } | 165 | } |
| 151 | 166 | ||
| 152 | static BitSet AllTrue(size_t count) | 167 | static BitSet AllTrue(size_t count) { |
| 153 | { | 168 | return BitSet(count == sizeof(IntTy) * 8 ? ~(IntTy)0 : (((IntTy)1 << count) - 1)); |
| 154 | return BitSet(count == sizeof(IntTy)*8 ? ~(IntTy)0 : (((IntTy)1 << count) - 1)); | 169 | } |
| 155 | } | 170 | |
| 156 | 171 | Ref operator[](size_t bit) { | |
| 157 | Ref operator[](size_t bit) { return Ref(this, (IntTy)1 << bit); } | 172 | return Ref(this, (IntTy)1 << bit); |
| 158 | const Ref operator[](size_t bit) const { return (*const_cast<BitSet*>(this))[bit]; } | 173 | } |
| 159 | bool operator==(BitSet other) const { return m_val == other.m_val; } | 174 | const Ref operator[](size_t bit) const { |
| 160 | bool operator!=(BitSet other) const { return m_val != other.m_val; } | 175 | return (*const_cast<BitSet*>(this))[bit]; |
| 161 | bool operator<(BitSet other) const { return m_val < other.m_val; } | 176 | } |
| 162 | bool operator>(BitSet other) const { return m_val > other.m_val; } | 177 | bool operator==(BitSet other) const { |
| 163 | BitSet operator|(BitSet other) const { return BitSet(m_val | other.m_val); } | 178 | return m_val == other.m_val; |
| 164 | BitSet operator&(BitSet other) const { return BitSet(m_val & other.m_val); } | 179 | } |
| 165 | BitSet operator^(BitSet other) const { return BitSet(m_val ^ other.m_val); } | 180 | bool operator!=(BitSet other) const { |
| 166 | BitSet operator~() const { return BitSet(~m_val); } | 181 | return m_val != other.m_val; |
| 167 | BitSet& operator|=(BitSet other) { return *this = *this | other; } | 182 | } |
| 168 | BitSet& operator&=(BitSet other) { return *this = *this & other; } | 183 | bool operator<(BitSet other) const { |
| 169 | BitSet& operator^=(BitSet other) { return *this = *this ^ other; } | 184 | return m_val < other.m_val; |
| 185 | } | ||
| 186 | bool operator>(BitSet other) const { | ||
| 187 | return m_val > other.m_val; | ||
| 188 | } | ||
| 189 | BitSet operator|(BitSet other) const { | ||
| 190 | return BitSet(m_val | other.m_val); | ||
| 191 | } | ||
| 192 | BitSet operator&(BitSet other) const { | ||
| 193 | return BitSet(m_val & other.m_val); | ||
| 194 | } | ||
| 195 | BitSet operator^(BitSet other) const { | ||
| 196 | return BitSet(m_val ^ other.m_val); | ||
| 197 | } | ||
| 198 | BitSet operator~() const { | ||
| 199 | return BitSet(~m_val); | ||
| 200 | } | ||
| 201 | BitSet& operator|=(BitSet other) { | ||
| 202 | return *this = *this | other; | ||
| 203 | } | ||
| 204 | BitSet& operator&=(BitSet other) { | ||
| 205 | return *this = *this & other; | ||
| 206 | } | ||
| 207 | BitSet& operator^=(BitSet other) { | ||
| 208 | return *this = *this ^ other; | ||
| 209 | } | ||
| 170 | operator u32() = delete; | 210 | operator u32() = delete; |
| 171 | operator bool() { return m_val != 0; } | 211 | operator bool() { |
| 212 | return m_val != 0; | ||
| 213 | } | ||
| 172 | 214 | ||
| 173 | // Warning: Even though on modern CPUs this is a single fast instruction, | 215 | // Warning: Even though on modern CPUs this is a single fast instruction, |
| 174 | // Dolphin's official builds do not currently assume POPCNT support on x86, | 216 | // Dolphin's official builds do not currently assume POPCNT support on x86, |
| 175 | // so slower explicit bit twiddling is generated. Still should generally | 217 | // so slower explicit bit twiddling is generated. Still should generally |
| 176 | // be faster than a loop. | 218 | // be faster than a loop. |
| 177 | unsigned int Count() const { return CountSetBits(m_val); } | 219 | unsigned int Count() const { |
| 220 | return CountSetBits(m_val); | ||
| 221 | } | ||
| 178 | 222 | ||
| 179 | Iterator begin() const { Iterator it(m_val, 0); return ++it; } | 223 | Iterator begin() const { |
| 180 | Iterator end() const { return Iterator(m_val, -1); } | 224 | Iterator it(m_val, 0); |
| 225 | return ++it; | ||
| 226 | } | ||
| 227 | Iterator end() const { | ||
| 228 | return Iterator(m_val, -1); | ||
| 229 | } | ||
| 181 | 230 | ||
| 182 | IntTy m_val; | 231 | IntTy m_val; |
| 183 | }; | 232 | }; |