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

@ -128,14 +128,14 @@ class PICAShader {
bool negate;
if constexpr (sourceIndex == 1) { // SRC1
negate = ((opDescriptor >> 4) & 1) != 0;
compSwizzle = (opDescriptor >> 5) & 0xff;
negate = (Helpers::getBit<4>(opDescriptor)) != 0;
compSwizzle = Helpers::getBits<5, 8>(opDescriptor);
} else if constexpr (sourceIndex == 2) { // SRC2
negate = ((opDescriptor >> 13) & 1) != 0;
compSwizzle = (opDescriptor >> 14) & 0xff;
negate = (Helpers::getBit<13>(opDescriptor)) != 0;
compSwizzle = Helpers::getBits<14, 8>(opDescriptor);
} else if constexpr (sourceIndex == 3) { // SRC3
negate = ((opDescriptor >> 22) & 1) != 0;
compSwizzle = (opDescriptor >> 23) & 0xff;
negate = (Helpers::getBit<22>(opDescriptor)) != 0;
compSwizzle = Helpers::getBits<23, 8>(opDescriptor);
}
// Iterate through every component of the swizzled vector in reverse order
@ -239,9 +239,9 @@ public:
void uploadIntUniform(int index, u32 word) {
auto& u = intUniforms[index];
u.x() = word & 0xff;
u.y() = (word >> 8) & 0xff;
u.z() = (word >> 16) & 0xff;
u.w() = (word >> 24) & 0xff;
u.y() = Helpers::getBits<8, 8>(word);
u.z() = Helpers::getBits<16, 8>(word);
u.w() = Helpers::getBits<24, 8>(word);
}
void run();

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;