2015-08-26 08:11:07 +01:00
|
|
|
// This file is under the public domain.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <intrin.h>
|
|
|
|
#endif
|
|
|
|
#include <initializer_list>
|
2016-04-30 16:34:51 +01:00
|
|
|
#include <new>
|
2015-08-26 08:11:07 +01:00
|
|
|
#include <type_traits>
|
|
|
|
#include "common/common_types.h"
|
|
|
|
|
|
|
|
// namespace avoids conflict with OS X Carbon; don't use BitSet<T> directly
|
|
|
|
namespace Common {
|
|
|
|
|
|
|
|
// Helper functions:
|
|
|
|
|
2016-12-03 20:08:02 +00:00
|
|
|
#ifdef _MSC_VER
|
2015-08-26 08:11:07 +01:00
|
|
|
template <typename T>
|
2016-09-18 01:38:01 +01:00
|
|
|
static inline int CountSetBits(T v) {
|
2015-08-26 08:11:07 +01: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
|
2016-09-18 01:38:01 +01:00
|
|
|
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;
|
2015-08-26 08:11:07 +01:00
|
|
|
}
|
2016-09-18 01:38:01 +01:00
|
|
|
static inline int LeastSignificantSetBit(u8 val) {
|
2015-08-26 08:11:07 +01:00
|
|
|
unsigned long index;
|
|
|
|
_BitScanForward(&index, val);
|
|
|
|
return (int)index;
|
|
|
|
}
|
2016-09-18 01:38:01 +01:00
|
|
|
static inline int LeastSignificantSetBit(u16 val) {
|
2015-08-26 08:11:07 +01:00
|
|
|
unsigned long index;
|
|
|
|
_BitScanForward(&index, val);
|
|
|
|
return (int)index;
|
|
|
|
}
|
2016-09-18 01:38:01 +01:00
|
|
|
static inline int LeastSignificantSetBit(u32 val) {
|
2015-08-26 08:11:07 +01:00
|
|
|
unsigned long index;
|
|
|
|
_BitScanForward(&index, val);
|
|
|
|
return (int)index;
|
|
|
|
}
|
2016-09-18 01:38:01 +01:00
|
|
|
static inline int LeastSignificantSetBit(u64 val) {
|
2015-08-26 08:11:07 +01:00
|
|
|
unsigned long index;
|
|
|
|
_BitScanForward64(&index, val);
|
|
|
|
return (int)index;
|
|
|
|
}
|
|
|
|
#else
|
2016-09-18 01:38:01 +01:00
|
|
|
static inline int CountSetBits(u8 val) {
|
|
|
|
return __builtin_popcount(val);
|
|
|
|
}
|
|
|
|
static inline int CountSetBits(u16 val) {
|
|
|
|
return __builtin_popcount(val);
|
|
|
|
}
|
|
|
|
static inline int CountSetBits(u32 val) {
|
|
|
|
return __builtin_popcount(val);
|
|
|
|
}
|
|
|
|
static inline int CountSetBits(u64 val) {
|
|
|
|
return __builtin_popcountll(val);
|
|
|
|
}
|
|
|
|
static inline int LeastSignificantSetBit(u8 val) {
|
|
|
|
return __builtin_ctz(val);
|
|
|
|
}
|
|
|
|
static inline int LeastSignificantSetBit(u16 val) {
|
|
|
|
return __builtin_ctz(val);
|
|
|
|
}
|
|
|
|
static inline int LeastSignificantSetBit(u32 val) {
|
|
|
|
return __builtin_ctz(val);
|
|
|
|
}
|
|
|
|
static inline int LeastSignificantSetBit(u64 val) {
|
|
|
|
return __builtin_ctzll(val);
|
|
|
|
}
|
2015-08-26 08:11:07 +01: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:
|
|
|
|
// BitSet32 bs;
|
|
|
|
// 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>
|
2016-09-18 01:38:01 +01:00
|
|
|
class BitSet {
|
2018-08-07 18:31:57 +01:00
|
|
|
static_assert(!std::is_signed_v<IntTy>, "BitSet should not be used with signed types");
|
2016-09-18 01:38:01 +01:00
|
|
|
|
2015-08-26 08:11:07 +01:00
|
|
|
public:
|
|
|
|
// A reference to a particular bit, returned from operator[].
|
2016-09-18 01:38:01 +01:00
|
|
|
class Ref {
|
2015-08-26 08:11:07 +01:00
|
|
|
public:
|
2016-09-19 02:01:46 +01:00
|
|
|
Ref(Ref&& other) : m_bs(other.m_bs), m_mask(other.m_mask) {}
|
|
|
|
Ref(BitSet* bs, IntTy mask) : m_bs(bs), m_mask(mask) {}
|
2016-09-18 01:38:01 +01:00
|
|
|
operator bool() const {
|
|
|
|
return (m_bs->m_val & m_mask) != 0;
|
|
|
|
}
|
|
|
|
bool operator=(bool set) {
|
2015-08-26 08:11:07 +01:00
|
|
|
m_bs->m_val = (m_bs->m_val & ~m_mask) | (set ? m_mask : 0);
|
|
|
|
return set;
|
|
|
|
}
|
2016-09-18 01:38:01 +01:00
|
|
|
|
2015-08-26 08:11:07 +01:00
|
|
|
private:
|
|
|
|
BitSet* m_bs;
|
|
|
|
IntTy m_mask;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A STL-like iterator is required to be able to use range-based for loops.
|
2016-09-18 01:38:01 +01:00
|
|
|
class Iterator {
|
2015-08-26 08:11:07 +01:00
|
|
|
public:
|
2017-12-10 04:16:07 +00:00
|
|
|
Iterator(const Iterator& other) : m_val(other.m_val) {}
|
|
|
|
Iterator(IntTy val) : m_val(val) {}
|
2016-09-18 01:38:01 +01:00
|
|
|
int operator*() {
|
2017-12-10 04:16:07 +00:00
|
|
|
// This will never be called when m_val == 0, because that would be the end() iterator
|
|
|
|
return LeastSignificantSetBit(m_val);
|
2016-09-18 01:38:01 +01:00
|
|
|
}
|
|
|
|
Iterator& operator++() {
|
2017-12-10 04:16:07 +00:00
|
|
|
// Unset least significant set bit
|
|
|
|
m_val &= m_val - IntTy(1);
|
2015-08-26 08:11:07 +01:00
|
|
|
return *this;
|
|
|
|
}
|
2016-09-18 01:38:01 +01:00
|
|
|
Iterator operator++(int _) {
|
2015-08-26 08:11:07 +01:00
|
|
|
Iterator other(*this);
|
|
|
|
++*this;
|
|
|
|
return other;
|
|
|
|
}
|
2016-09-18 01:38:01 +01:00
|
|
|
bool operator==(Iterator other) const {
|
2016-12-19 05:48:12 +00:00
|
|
|
return m_val == other.m_val;
|
2016-09-18 01:38:01 +01:00
|
|
|
}
|
|
|
|
bool operator!=(Iterator other) const {
|
2016-12-19 05:48:12 +00:00
|
|
|
return m_val != other.m_val;
|
2016-09-18 01:38:01 +01:00
|
|
|
}
|
|
|
|
|
2015-08-26 08:11:07 +01:00
|
|
|
private:
|
|
|
|
IntTy m_val;
|
|
|
|
};
|
|
|
|
|
2016-09-19 02:01:46 +01:00
|
|
|
BitSet() : m_val(0) {}
|
|
|
|
explicit BitSet(IntTy val) : m_val(val) {}
|
2016-09-18 01:38:01 +01:00
|
|
|
BitSet(std::initializer_list<int> init) {
|
2015-08-26 08:11:07 +01:00
|
|
|
m_val = 0;
|
|
|
|
for (int bit : init)
|
|
|
|
m_val |= (IntTy)1 << bit;
|
|
|
|
}
|
|
|
|
|
2018-09-06 21:03:28 +01:00
|
|
|
static BitSet AllTrue(std::size_t count) {
|
2016-09-18 01:38:01 +01:00
|
|
|
return BitSet(count == sizeof(IntTy) * 8 ? ~(IntTy)0 : (((IntTy)1 << count) - 1));
|
|
|
|
}
|
|
|
|
|
2018-09-06 21:03:28 +01:00
|
|
|
Ref operator[](std::size_t bit) {
|
2016-09-18 01:38:01 +01:00
|
|
|
return Ref(this, (IntTy)1 << bit);
|
|
|
|
}
|
2018-09-06 21:03:28 +01:00
|
|
|
const Ref operator[](std::size_t bit) const {
|
2016-09-18 01:38:01 +01:00
|
|
|
return (*const_cast<BitSet*>(this))[bit];
|
|
|
|
}
|
|
|
|
bool operator==(BitSet other) const {
|
|
|
|
return m_val == other.m_val;
|
|
|
|
}
|
|
|
|
bool operator!=(BitSet other) const {
|
|
|
|
return m_val != other.m_val;
|
|
|
|
}
|
|
|
|
bool operator<(BitSet other) const {
|
|
|
|
return m_val < other.m_val;
|
|
|
|
}
|
|
|
|
bool operator>(BitSet other) const {
|
|
|
|
return m_val > other.m_val;
|
|
|
|
}
|
|
|
|
BitSet operator|(BitSet other) const {
|
|
|
|
return BitSet(m_val | other.m_val);
|
|
|
|
}
|
|
|
|
BitSet operator&(BitSet other) const {
|
|
|
|
return BitSet(m_val & other.m_val);
|
|
|
|
}
|
|
|
|
BitSet operator^(BitSet other) const {
|
|
|
|
return BitSet(m_val ^ other.m_val);
|
|
|
|
}
|
|
|
|
BitSet operator~() const {
|
|
|
|
return BitSet(~m_val);
|
|
|
|
}
|
|
|
|
BitSet& operator|=(BitSet other) {
|
|
|
|
return *this = *this | other;
|
|
|
|
}
|
|
|
|
BitSet& operator&=(BitSet other) {
|
|
|
|
return *this = *this & other;
|
|
|
|
}
|
|
|
|
BitSet& operator^=(BitSet other) {
|
|
|
|
return *this = *this ^ other;
|
|
|
|
}
|
2015-08-26 08:11:07 +01:00
|
|
|
operator u32() = delete;
|
2016-09-18 01:38:01 +01:00
|
|
|
operator bool() {
|
|
|
|
return m_val != 0;
|
|
|
|
}
|
2015-08-26 08:11:07 +01:00
|
|
|
|
|
|
|
// 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-09-18 01:38:01 +01:00
|
|
|
unsigned int Count() const {
|
|
|
|
return CountSetBits(m_val);
|
|
|
|
}
|
2015-08-26 08:11:07 +01:00
|
|
|
|
2016-09-18 01:38:01 +01:00
|
|
|
Iterator begin() const {
|
2016-12-19 05:48:12 +00:00
|
|
|
return Iterator(m_val);
|
2016-09-18 01:38:01 +01:00
|
|
|
}
|
|
|
|
Iterator end() const {
|
2016-12-19 05:48:12 +00:00
|
|
|
return Iterator(0);
|
2016-09-18 01:38:01 +01:00
|
|
|
}
|
2015-08-26 08:11:07 +01:00
|
|
|
|
|
|
|
IntTy m_val;
|
|
|
|
};
|
|
|
|
|
2018-03-09 17:54:43 +00:00
|
|
|
} // namespace Common
|
2015-08-26 08:11:07 +01:00
|
|
|
|
|
|
|
typedef Common::BitSet<u8> BitSet8;
|
|
|
|
typedef Common::BitSet<u16> BitSet16;
|
|
|
|
typedef Common::BitSet<u32> BitSet32;
|
2016-04-30 16:34:51 +01:00
|
|
|
typedef Common::BitSet<u64> BitSet64;
|