The bytes are unsigned and are all less than 16 so they can be fit into a nibble.
I'm currently shifting the bytes in a loop and &
them with 0xf
:
pub fn compress(offsets: [u8; 8]) -> u32 {
let mut co: u32 = 0;
for (i, o) in offsets.iter().enumerate() {
co |= ((*o as u32) & 0xf ) << (i * 4);
}
co
}
The compiler does already some good optimization on that:
But maybe it is possible to do some bit twiddling or use SIMD commands with a u64
to reduce the amount of operations?