mirror of
https://codeberg.org/icewind/bitbuffer.git
synced 2026-06-03 16:44:06 +02:00
experimental simd dynamic string reading
This commit is contained in:
parent
da62bb9e9d
commit
3c1081be9a
2 changed files with 39 additions and 0 deletions
|
|
@ -13,11 +13,13 @@ travis-ci = { repository = "icewind1991/bitstream_reader" }
|
|||
[dependencies]
|
||||
num-traits = "0.2"
|
||||
bitstream_reader_derive = { version = "0.4", path = "bitstream_reader_derive" }
|
||||
packed_simd = { version = "0.3", features = ["into_bits"], optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
maplit = "1.0.1"
|
||||
|
||||
[features]
|
||||
unchecked_utf8 = []
|
||||
simd = ["packed_simd"]
|
||||
|
||||
[workspace]
|
||||
|
|
@ -421,6 +421,7 @@ impl<E> BitBuffer<E>
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "simd"))]
|
||||
fn read_string_bytes(&self, position: usize) -> Vec<u8> {
|
||||
let mut acc = Vec::with_capacity(32);
|
||||
let mut pos = position;
|
||||
|
|
@ -451,6 +452,42 @@ impl<E> BitBuffer<E>
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "simd")]
|
||||
fn read_string_bytes(&self, position: usize) -> Vec<u8> {
|
||||
use packed_simd::u8x16;
|
||||
use packed_simd::u128x1;
|
||||
use packed_simd::IntoBits;
|
||||
|
||||
let bit_index = position & 7;
|
||||
const ZEROS: u8x16 = u8x16::splat(0);
|
||||
let mut byte_index = position / 8;
|
||||
let mut acc = Vec::with_capacity(32);
|
||||
let bit_index_simd = u128x1::new(bit_index as u128);
|
||||
|
||||
loop {
|
||||
let raw_value: u128x1 = u8x16::from_slice_unaligned(&self.bytes[byte_index..byte_index + 16]).into_bits();
|
||||
let shifted = raw_value.rotate_right(bit_index_simd);
|
||||
let input_bytes: u8x16 = shifted.into_bits();
|
||||
let has_zero = ZEROS.eq(input_bytes).any();
|
||||
|
||||
let mut bytes = [0u8; 16];
|
||||
input_bytes.write_to_slice_unaligned(&mut bytes);
|
||||
|
||||
if has_zero {
|
||||
for byte in &bytes {
|
||||
if *byte == 0 {
|
||||
return acc;
|
||||
}
|
||||
acc.push(*byte);
|
||||
}
|
||||
} else {
|
||||
acc.extend_from_slice(&bytes[0..15]);
|
||||
}
|
||||
|
||||
byte_index += 15;
|
||||
}
|
||||
}
|
||||
|
||||
/// Read a sequence of bits from the buffer as float
|
||||
///
|
||||
/// # Errors
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue