pub fn read_n_bytes(&mut self, n: usize, input: &[u8], out: &mut [u8]) {
let left_mask = 0b00001111;
let right_mask = 0b11110000;
for i in 0..n {
let left_part = input[i] & left_mask;
let right_part = input[i + 1] & right_mask;
out[i] = left_part | right_part;
}
}
The function reads bytes from a bitstream. The bytes are "unaligned" in the bitstream, ie our wanted byte will have some number of bits from the left byte and the rest from the right byte. For simplicity lets just say the bytes are always "4 bits" off. so each byte is 4 bits from first byte and 4 bits from second byte. My question is can we do better than this? To me this sounds like a great fit for simd but all my attempts are much slower.
other info:
- Typical read is 100-1000 bytes.
- I cannot change the format.
- removing temp variables seems to have no effect. Same with iterators and unsafe.