Inserting a 0 bit in the middle of a value
fgiesen.wordpress.com
fgiesen.wordpress.com
uint64 insert_zero_bit(uint64 value, int pos) {
uint64 top_mask = ~0u64 << pos;
return value + (value & top_mask);
}
> It only works for inserting exactly 1 bitBut is easily generalized to inserting k bits:
return value + ((1<<k)-1) * (value & top_mask);https://programming.sirrida.de/calcperm.php
It doesn't always find the optimal solution but it usually gets close.
If compilers/languages/standard libraries provided these bit permutations, and it was just something ~everyone had learned, it would be a lot easier to work with bits without needing to come up with the bitwise ops (or use that generator). In addition it would probably make better use of the hardware: sure, people like to pretend that we’re still programming C for PDP11, but modern hardware supports more operations than C has operators for (RISC-V B extension and co have the right idea <3)
Modern compilers are probably pretty good? but I doubt they are perfect at turning code like that in OP into the best instructions. It is probably a bit late for C/C++ though. maaaybe possible to get it into LLVM and Rust.
Most languages do have intrinsics for ROL/ROR at least, which you should generally use instead of relying on optimizer magic. I've certainly run into cases where those magic patterns don't get optimized (looking at you MSVC) but intrinsics always work.
The very tough problem is recognizing to emit the correct code.
which is pretty convenient for this use-case.
How often are these texturers transfered and how small are they?
Doing all this to avoid sending at most three bits for a texture file sounds like a colossal waste of human life and of money.