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,11 +112,19 @@ struct BitField
DataType GetValue() const DataType GetValue() const
{ {
// TODO: Handle signed types if constexpr (std::is_same_v<DataType, bool>)
if (std::is_same<DataType, bool>::value) {
return static_cast<DataType>(!!((data & GetMask()) >> BitIndex)); 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 else
{
return static_cast<DataType>((data & GetMask()) >> BitIndex); return static_cast<DataType>((data & GetMask()) >> BitIndex);
}
} }
void SetValue(DataType value) void SetValue(DataType value)