common/BitField: Support sign extension

This commit is contained in:
Connor McLaughlin 2019-10-04 23:20:42 +10:00
parent 004c22f031
commit e7d68ba304

View file

@ -112,12 +112,20 @@ struct BitField
DataType GetValue() const
{
// TODO: Handle signed types
if (std::is_same<DataType, bool>::value)
if constexpr (std::is_same_v<DataType, bool>)
{
return static_cast<DataType>(!!((data & GetMask()) >> BitIndex));
}
else if constexpr (std::is_signed_v<DataType>)
{
constexpr int shift = 8 * sizeof(DataType) - BitCount;
return (static_cast<DataType>((data & GetMask()) >> BitIndex) << shift) >> shift;
}
else
{
return static_cast<DataType>((data & GetMask()) >> BitIndex);
}
}
void SetValue(DataType value)
{