| 
									
										
										
										
											2014-10-16 21:49:48 -04:00
										 |  |  | // This file is under the public domain.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-29 13:00:48 -04:00
										 |  |  | #include <cstddef>
 | 
					
						
							| 
									
										
										
										
											2014-10-16 21:49:48 -04:00
										 |  |  | #include <initializer_list>
 | 
					
						
							|  |  |  | #include <type_traits>
 | 
					
						
							| 
									
										
										
										
											2018-04-22 23:56:28 -04:00
										 |  |  | #include "Common/CommonTypes.h"
 | 
					
						
							| 
									
										
										
										
											2014-10-16 21:49:48 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef _WIN32
 | 
					
						
							| 
									
										
										
										
											2016-01-07 19:17:35 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <intrin.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-22 23:45:02 -04:00
										 |  |  | namespace Common | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-10-16 21:49:48 -04:00
										 |  |  | template <typename T> | 
					
						
							| 
									
										
										
										
											2016-06-25 10:58:53 -03:00
										 |  |  | constexpr int CountSetBits(T v) | 
					
						
							| 
									
										
										
										
											2014-10-16 21:49:48 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   // from https://graphics.stanford.edu/~seander/bithacks.html
 | 
					
						
							|  |  |  |   // GCC has this built in, but MSVC's intrinsic will only emit the actual
 | 
					
						
							|  |  |  |   // POPCNT instruction, which we're not depending on
 | 
					
						
							|  |  |  |   v = v - ((v >> 1) & (T) ~(T)0 / 3); | 
					
						
							|  |  |  |   v = (v & (T) ~(T)0 / 15 * 3) + ((v >> 2) & (T) ~(T)0 / 15 * 3); | 
					
						
							|  |  |  |   v = (v + (v >> 4)) & (T) ~(T)0 / 255 * 15; | 
					
						
							|  |  |  |   return (T)(v * ((T) ~(T)0 / 255)) >> (sizeof(T) - 1) * 8; | 
					
						
							| 
									
										
										
										
											2014-10-16 21:49:48 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-06-25 10:58:53 -03:00
										 |  |  | inline int LeastSignificantSetBit(u8 val) | 
					
						
							| 
									
										
										
										
											2015-01-04 04:20:59 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   unsigned long index; | 
					
						
							|  |  |  |   _BitScanForward(&index, val); | 
					
						
							|  |  |  |   return (int)index; | 
					
						
							| 
									
										
										
										
											2015-01-04 04:20:59 -08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-06-25 10:58:53 -03:00
										 |  |  | inline int LeastSignificantSetBit(u16 val) | 
					
						
							| 
									
										
										
										
											2015-01-04 04:20:59 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   unsigned long index; | 
					
						
							|  |  |  |   _BitScanForward(&index, val); | 
					
						
							|  |  |  |   return (int)index; | 
					
						
							| 
									
										
										
										
											2015-01-04 04:20:59 -08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-06-25 10:58:53 -03:00
										 |  |  | inline int LeastSignificantSetBit(u32 val) | 
					
						
							| 
									
										
										
										
											2014-10-16 21:49:48 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   unsigned long index; | 
					
						
							|  |  |  |   _BitScanForward(&index, val); | 
					
						
							|  |  |  |   return (int)index; | 
					
						
							| 
									
										
										
										
											2014-10-16 21:49:48 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-06-25 10:58:53 -03:00
										 |  |  | inline int LeastSignificantSetBit(u64 val) | 
					
						
							| 
									
										
										
										
											2014-10-16 21:49:48 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   unsigned long index; | 
					
						
							|  |  |  |   _BitScanForward64(&index, val); | 
					
						
							|  |  |  |   return (int)index; | 
					
						
							| 
									
										
										
										
											2014-10-16 21:49:48 -04:00
										 |  |  | } | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2018-04-22 23:45:02 -04:00
										 |  |  | namespace Common | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-25 10:58:53 -03:00
										 |  |  | constexpr int CountSetBits(u8 val) | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | { | 
					
						
							|  |  |  |   return __builtin_popcount(val); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-06-25 10:58:53 -03:00
										 |  |  | constexpr int CountSetBits(u16 val) | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | { | 
					
						
							|  |  |  |   return __builtin_popcount(val); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-06-25 10:58:53 -03:00
										 |  |  | constexpr int CountSetBits(u32 val) | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | { | 
					
						
							|  |  |  |   return __builtin_popcount(val); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-06-25 10:58:53 -03:00
										 |  |  | constexpr int CountSetBits(u64 val) | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | { | 
					
						
							|  |  |  |   return __builtin_popcountll(val); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-06-25 10:58:53 -03:00
										 |  |  | inline int LeastSignificantSetBit(u8 val) | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | { | 
					
						
							|  |  |  |   return __builtin_ctz(val); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-06-25 10:58:53 -03:00
										 |  |  | inline int LeastSignificantSetBit(u16 val) | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | { | 
					
						
							|  |  |  |   return __builtin_ctz(val); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-06-25 10:58:53 -03:00
										 |  |  | inline int LeastSignificantSetBit(u32 val) | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | { | 
					
						
							|  |  |  |   return __builtin_ctz(val); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-06-25 10:58:53 -03:00
										 |  |  | inline int LeastSignificantSetBit(u64 val) | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | { | 
					
						
							|  |  |  |   return __builtin_ctzll(val); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-10-16 21:49:48 -04:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Similar to std::bitset, this is a class which encapsulates a bitset, i.e.
 | 
					
						
							|  |  |  | // using the set bits of an integer to represent a set of integers.  Like that
 | 
					
						
							|  |  |  | // class, it acts like an array of bools:
 | 
					
						
							| 
									
										
										
										
											2014-10-17 01:24:24 -04:00
										 |  |  | //     BitSet32 bs;
 | 
					
						
							| 
									
										
										
										
											2014-10-16 21:49:48 -04:00
										 |  |  | //     bs[1] = true;
 | 
					
						
							|  |  |  | // but also like the underlying integer ([0] = least significant bit):
 | 
					
						
							|  |  |  | //     BitSet32 bs2 = ...;
 | 
					
						
							|  |  |  | //     bs = (bs ^ bs2) & BitSet32(0xffff);
 | 
					
						
							|  |  |  | // The following additional functionality is provided:
 | 
					
						
							|  |  |  | // - Construction using an initializer list.
 | 
					
						
							|  |  |  | //     BitSet bs { 1, 2, 4, 8 };
 | 
					
						
							|  |  |  | // - Efficiently iterating through the set bits:
 | 
					
						
							|  |  |  | //     for (int i : bs)
 | 
					
						
							|  |  |  | //         [i is the *index* of a set bit]
 | 
					
						
							|  |  |  | //   (This uses the appropriate CPU instruction to find the next set bit in one
 | 
					
						
							|  |  |  | //   operation.)
 | 
					
						
							|  |  |  | // - Counting set bits using .Count() - see comment on that method.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // TODO: use constexpr when MSVC gets out of the Dark Ages
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <typename IntTy> | 
					
						
							|  |  |  | class BitSet | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   static_assert(!std::is_signed<IntTy>::value, "BitSet should not be used with signed types"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-16 21:49:48 -04:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   // A reference to a particular bit, returned from operator[].
 | 
					
						
							|  |  |  |   class Ref | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |   public: | 
					
						
							| 
									
										
										
										
											2016-06-25 10:58:53 -03:00
										 |  |  |     constexpr Ref(Ref&& other) : m_bs(other.m_bs), m_mask(other.m_mask) {} | 
					
						
							|  |  |  |     constexpr Ref(BitSet* bs, IntTy mask) : m_bs(bs), m_mask(mask) {} | 
					
						
							|  |  |  |     constexpr operator bool() const { return (m_bs->m_val & m_mask) != 0; } | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |     bool operator=(bool set) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       m_bs->m_val = (m_bs->m_val & ~m_mask) | (set ? m_mask : 0); | 
					
						
							|  |  |  |       return set; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private: | 
					
						
							|  |  |  |     BitSet* m_bs; | 
					
						
							|  |  |  |     IntTy m_mask; | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // A STL-like iterator is required to be able to use range-based for loops.
 | 
					
						
							|  |  |  |   class Iterator | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |   public: | 
					
						
							| 
									
										
										
										
											2016-06-25 10:58:53 -03:00
										 |  |  |     constexpr Iterator(const Iterator& other) : m_val(other.m_val), m_bit(other.m_bit) {} | 
					
						
							|  |  |  |     constexpr Iterator(IntTy val, int bit) : m_val(val), m_bit(bit) {} | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |     Iterator& operator=(Iterator other) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       new (this) Iterator(other); | 
					
						
							|  |  |  |       return *this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     Iterator& operator++() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       if (m_val == 0) | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         m_bit = -1; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         int bit = LeastSignificantSetBit(m_val); | 
					
						
							|  |  |  |         m_val &= ~(1 << bit); | 
					
						
							|  |  |  |         m_bit = bit; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       return *this; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-06-25 10:58:53 -03:00
										 |  |  |     Iterator operator++(int) | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |       Iterator other(*this); | 
					
						
							|  |  |  |       ++*this; | 
					
						
							|  |  |  |       return other; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-06-25 10:58:53 -03:00
										 |  |  |     constexpr int operator*() const { return m_bit; } | 
					
						
							|  |  |  |     constexpr bool operator==(Iterator other) const { return m_bit == other.m_bit; } | 
					
						
							|  |  |  |     constexpr bool operator!=(Iterator other) const { return m_bit != other.m_bit; } | 
					
						
							| 
									
										
										
										
											2018-04-12 14:18:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   private: | 
					
						
							|  |  |  |     IntTy m_val; | 
					
						
							|  |  |  |     int m_bit; | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-25 10:58:53 -03:00
										 |  |  |   constexpr BitSet() : m_val(0) {} | 
					
						
							|  |  |  |   constexpr explicit BitSet(IntTy val) : m_val(val) {} | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   BitSet(std::initializer_list<int> init) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     m_val = 0; | 
					
						
							|  |  |  |     for (int bit : init) | 
					
						
							|  |  |  |       m_val |= (IntTy)1 << bit; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-25 10:58:53 -03:00
										 |  |  |   constexpr static BitSet AllTrue(size_t count) | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   { | 
					
						
							|  |  |  |     return BitSet(count == sizeof(IntTy) * 8 ? ~(IntTy)0 : (((IntTy)1 << count) - 1)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Ref operator[](size_t bit) { return Ref(this, (IntTy)1 << bit); } | 
					
						
							| 
									
										
										
										
											2016-06-25 10:58:53 -03:00
										 |  |  |   constexpr const Ref operator[](size_t bit) const { return (*const_cast<BitSet*>(this))[bit]; } | 
					
						
							|  |  |  |   constexpr bool operator==(BitSet other) const { return m_val == other.m_val; } | 
					
						
							|  |  |  |   constexpr bool operator!=(BitSet other) const { return m_val != other.m_val; } | 
					
						
							|  |  |  |   constexpr bool operator<(BitSet other) const { return m_val < other.m_val; } | 
					
						
							|  |  |  |   constexpr bool operator>(BitSet other) const { return m_val > other.m_val; } | 
					
						
							|  |  |  |   constexpr BitSet operator|(BitSet other) const { return BitSet(m_val | other.m_val); } | 
					
						
							|  |  |  |   constexpr BitSet operator&(BitSet other) const { return BitSet(m_val & other.m_val); } | 
					
						
							|  |  |  |   constexpr BitSet operator^(BitSet other) const { return BitSet(m_val ^ other.m_val); } | 
					
						
							|  |  |  |   constexpr BitSet operator~() const { return BitSet(~m_val); } | 
					
						
							|  |  |  |   constexpr explicit operator bool() const { return m_val != 0; } | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   BitSet& operator|=(BitSet other) { return *this = *this | other; } | 
					
						
							|  |  |  |   BitSet& operator&=(BitSet other) { return *this = *this & other; } | 
					
						
							|  |  |  |   BitSet& operator^=(BitSet other) { return *this = *this ^ other; } | 
					
						
							|  |  |  |   // Warning: Even though on modern CPUs this is a single fast instruction,
 | 
					
						
							|  |  |  |   // Dolphin's official builds do not currently assume POPCNT support on x86,
 | 
					
						
							|  |  |  |   // so slower explicit bit twiddling is generated.  Still should generally
 | 
					
						
							|  |  |  |   // be faster than a loop.
 | 
					
						
							| 
									
										
										
										
											2016-06-25 10:58:53 -03:00
										 |  |  |   constexpr unsigned int Count() const { return CountSetBits(m_val); } | 
					
						
							|  |  |  |   constexpr Iterator begin() const { return ++Iterator(m_val, 0); } | 
					
						
							|  |  |  |   constexpr Iterator end() const { return Iterator(m_val, -1); } | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   IntTy m_val; | 
					
						
							| 
									
										
										
										
											2014-10-16 21:49:48 -04:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2018-04-22 23:43:05 -04:00
										 |  |  | }  // namespace Common
 | 
					
						
							| 
									
										
										
										
											2014-10-17 01:24:24 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-22 23:43:05 -04:00
										 |  |  | using BitSet8 = Common::BitSet<u8>; | 
					
						
							|  |  |  | using BitSet16 = Common::BitSet<u16>; | 
					
						
							|  |  |  | using BitSet32 = Common::BitSet<u32>; | 
					
						
							|  |  |  | using BitSet64 = Common::BitSet<u64>; |