Common: Force inlining of BitField/SignExtend/ZeroExtend functions

This commit is contained in:
Connor McLaughlin
2019-10-26 15:41:43 +10:00
parent a9e0fe6db2
commit c7a7d682bc
2 changed files with 56 additions and 60 deletions

View File

@ -1,4 +1,5 @@
#pragma once
#include "types.h"
#include <type_traits>
// Disable MSVC warnings that we actually handle
@ -10,105 +11,107 @@
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");
// 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;
constexpr BackingDataType GetMask() const
ALWAYS_INLINE constexpr BackingDataType GetMask() const
{
return ((static_cast<BackingDataType>(~0)) >> (8 * sizeof(BackingDataType) - BitCount)) << BitIndex;
}
operator DataType() const { return GetValue(); }
ALWAYS_INLINE operator DataType() const { return GetValue(); }
BitField& operator=(DataType value)
ALWAYS_INLINE BitField& operator=(DataType value)
{
SetValue(value);
return *this;
}
DataType operator++()
ALWAYS_INLINE DataType operator++()
{
DataType value = GetValue() + 1;
SetValue(value);
return GetValue();
}
DataType operator++(int)
ALWAYS_INLINE DataType operator++(int)
{
DataType value = GetValue();
SetValue(value + 1);
return value;
}
DataType operator--()
ALWAYS_INLINE DataType operator--()
{
DataType value = GetValue() - 1;
SetValue(value);
return GetValue();
}
DataType operator--(int)
ALWAYS_INLINE DataType operator--(int)
{
DataType value = GetValue();
SetValue(value - 1);
return value;
}
BitField& operator+=(DataType rhs)
ALWAYS_INLINE BitField& operator+=(DataType rhs)
{
SetValue(GetValue() + rhs);
return *this;
}
BitField& operator-=(DataType rhs)
ALWAYS_INLINE BitField& operator-=(DataType rhs)
{
SetValue(GetValue() - rhs);
return *this;
}
BitField& operator*=(DataType rhs)
ALWAYS_INLINE BitField& operator*=(DataType rhs)
{
SetValue(GetValue() * rhs);
return *this;
}
BitField& operator/=(DataType rhs)
ALWAYS_INLINE BitField& operator/=(DataType rhs)
{
SetValue(GetValue() / rhs);
return *this;
}
BitField& operator&=(DataType rhs)
ALWAYS_INLINE BitField& operator&=(DataType rhs)
{
SetValue(GetValue() & rhs);
return *this;
}
BitField& operator|=(DataType rhs)
ALWAYS_INLINE BitField& operator|=(DataType rhs)
{
SetValue(GetValue() | rhs);
return *this;
}
BitField& operator^=(DataType rhs)
ALWAYS_INLINE BitField& operator^=(DataType rhs)
{
SetValue(GetValue() ^ rhs);
return *this;
}
BitField& operator<<=(DataType rhs)
ALWAYS_INLINE BitField& operator<<=(DataType rhs)
{
SetValue(GetValue() << rhs);
return *this;
}
BitField& operator>>=(DataType rhs)
ALWAYS_INLINE BitField& operator>>=(DataType rhs)
{
SetValue(GetValue() >> rhs);
return *this;
}
DataType GetValue() const
ALWAYS_INLINE DataType GetValue() const
{
if constexpr (std::is_same_v<DataType, bool>)
{
@ -125,10 +128,9 @@ struct BitField
}
}
void SetValue(DataType value)
ALWAYS_INLINE void SetValue(DataType value)
{
data &= ~GetMask();
data |= (static_cast<BackingDataType>(value) << BitIndex) & GetMask();
data = (data & ~GetMask()) | (static_cast<BackingDataType>(value) << BitIndex) & GetMask();
}
BackingDataType data;