Add explicit return-type overload for getBits

Allows the return-type to be specified, allowing a concise final cast
after extracting the bit-type.  Addresses the remaining `C4244` warnings
regarding `getBits`.
This commit is contained in:
Wunkolo 2023-06-19 21:18:51 -07:00
parent 37b75f0928
commit 119c908aa7
3 changed files with 36 additions and 29 deletions

View file

@ -113,9 +113,16 @@ namespace Helpers {
}
/// Extract bits from an integer-type
template <usize offset, usize bits, typename T>
static constexpr T getBits(T value) {
return (value >> offset) & ones<T, bits>();
template <usize offset, usize bits, typename ReturnT, typename ValueT>
static constexpr ReturnT getBits(ValueT value) {
static_assert((offset + bits) <= (CHAR_BIT * sizeof(ValueT)), "Invalid bit range");
static_assert(bits > 0, "Invalid bit size");
return ReturnT(ValueT(value >> offset) & ones<ValueT, bits>());
}
template <usize offset, usize bits, typename ValueT>
static constexpr ValueT getBits(ValueT value) {
return getBits<offset, bits, ValueT, ValueT>(value);
}
#ifdef HELPERS_APPLE_CLANG