Add Helpers:{getBits,getBit}

`constexpr` functions for extractint bitfields that lends itself a bit
better to emitting instructions like `bextr` on x86 or `ubfx` on arm64.
And may subjectively make things a bit more readable.
"Extract `5` bits" rather than `& 0x1F`.
This commit is contained in:
Wunkolo 2023-06-09 21:28:46 -07:00
parent c7e3343974
commit 2c94d0d683
9 changed files with 180 additions and 147 deletions

View file

@ -1,5 +1,6 @@
#pragma once
#include <cstdarg>
#include <climits>
#include <fstream>
#include <iostream>
#include <iterator>
@ -90,6 +91,30 @@ namespace Helpers {
return (u16) (temp << bitsToShift >> bitsToShift);
}
/// Create a mask with `count` number of one bits.
template<typename T, usize count>
static constexpr T ones () {
constexpr usize bitsize = CHAR_BIT * sizeof(T);
static_assert(count <= bitsize, "count larger than bitsize of T");
if (count == T(0)) {
return T(0);
}
return static_cast<T>(~static_cast<T>(0)) >> (bitsize - count);
}
/// Extract bits from an integer-type
template<usize offset, typename T>
static constexpr T getBit (T value) {
return (value >> offset) & T(1);
}
/// 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>();
}
/// Check if a bit "bit" of value is set
static constexpr bool isBitSet (u32 value, int bit) {
return (value >> bit) & 1;