Duckstation/src/common/bitfield.h

142 lines
3.3 KiB
C
Raw Normal View History

2019-09-09 07:01:26 +00:00
#pragma once
#include "types.h"
2019-09-09 07:01:26 +00:00
#include <type_traits>
// Disable MSVC warnings that we actually handle
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4800) // warning C4800: 'int': forcing value to bool 'true' or 'false' (performance warning)
#endif
template<typename BackingDataType, typename DataType, unsigned BitIndex, unsigned BitCount>
struct BitField
{
static_assert(!std::is_same_v<DataType, bool> || BitCount == 1, "Boolean bitfields should only be 1 bit");
2019-10-22 13:07:51 +00:00
// We have to delete the copy assignment operator otherwise we can't use this class in anonymous structs/unions.
BitField& operator=(const BitField& rhs) = delete;
2019-09-09 07:01:26 +00:00
ALWAYS_INLINE constexpr BackingDataType GetMask() const
2019-09-09 07:01:26 +00:00
{
return ((static_cast<BackingDataType>(~0)) >> (8 * sizeof(BackingDataType) - BitCount)) << BitIndex;
}
ALWAYS_INLINE constexpr operator DataType() const { return GetValue(); }
2019-09-09 07:01:26 +00:00
ALWAYS_INLINE constexpr BitField& operator=(DataType value)
2019-09-09 07:01:26 +00:00
{
SetValue(value);
return *this;
}
ALWAYS_INLINE constexpr DataType operator++()
2019-09-09 07:01:26 +00:00
{
DataType value = GetValue() + 1;
SetValue(value);
return GetValue();
}
ALWAYS_INLINE constexpr DataType operator++(int)
2019-09-09 07:01:26 +00:00
{
DataType value = GetValue();
SetValue(value + 1);
return value;
}
ALWAYS_INLINE constexpr DataType operator--()
2019-09-09 07:01:26 +00:00
{
DataType value = GetValue() - 1;
SetValue(value);
return GetValue();
}
ALWAYS_INLINE constexpr DataType operator--(int)
2019-09-09 07:01:26 +00:00
{
DataType value = GetValue();
SetValue(value - 1);
return value;
}
ALWAYS_INLINE constexpr BitField& operator+=(DataType rhs)
2019-09-09 07:01:26 +00:00
{
SetValue(GetValue() + rhs);
return *this;
}
ALWAYS_INLINE constexpr BitField& operator-=(DataType rhs)
2019-09-09 07:01:26 +00:00
{
SetValue(GetValue() - rhs);
return *this;
}
ALWAYS_INLINE constexpr BitField& operator*=(DataType rhs)
2019-09-09 07:01:26 +00:00
{
SetValue(GetValue() * rhs);
return *this;
}
ALWAYS_INLINE constexpr BitField& operator/=(DataType rhs)
2019-09-09 07:01:26 +00:00
{
SetValue(GetValue() / rhs);
return *this;
}
ALWAYS_INLINE constexpr BitField& operator&=(DataType rhs)
2019-09-17 06:26:00 +00:00
{
SetValue(GetValue() & rhs);
return *this;
}
ALWAYS_INLINE constexpr BitField& operator|=(DataType rhs)
2019-09-17 06:26:00 +00:00
{
SetValue(GetValue() | rhs);
2019-09-17 06:26:00 +00:00
return *this;
}
ALWAYS_INLINE constexpr BitField& operator^=(DataType rhs)
2019-09-09 07:01:26 +00:00
{
SetValue(GetValue() ^ rhs);
return *this;
}
ALWAYS_INLINE constexpr BitField& operator<<=(DataType rhs)
2019-09-09 07:01:26 +00:00
{
SetValue(GetValue() << rhs);
2019-09-09 07:01:26 +00:00
return *this;
}
ALWAYS_INLINE constexpr BitField& operator>>=(DataType rhs)
2019-09-09 07:01:26 +00:00
{
SetValue(GetValue() >> rhs);
return *this;
}
ALWAYS_INLINE constexpr DataType GetValue() const
2019-09-09 07:01:26 +00:00
{
if constexpr (std::is_same_v<DataType, bool>)
{
2019-09-09 07:01:26 +00:00
return static_cast<DataType>(!!((data & GetMask()) >> BitIndex));
}
else if constexpr (std::is_signed_v<DataType>)
{
constexpr int shift = 8 * sizeof(DataType) - BitCount + 1;
return (static_cast<DataType>(data >> BitIndex) << shift) >> shift;
}
2019-09-09 07:01:26 +00:00
else
{
2019-09-09 07:01:26 +00:00
return static_cast<DataType>((data & GetMask()) >> BitIndex);
}
2019-09-09 07:01:26 +00:00
}
ALWAYS_INLINE constexpr void SetValue(DataType value)
2019-09-09 07:01:26 +00:00
{
2019-11-02 14:15:42 +00:00
data = (data & ~GetMask()) | ((static_cast<BackingDataType>(value) << BitIndex) & GetMask());
2019-09-09 07:01:26 +00:00
}
BackingDataType data;
};
#ifdef _MSC_VER
#pragma warning(pop)
2019-10-22 13:07:51 +00:00
#endif