#[inline(always)]
pub(crate) fn highbit32(value: u32) -> u32 {
debug_assert!(value != 0);
31u32 ^ value.leading_zeros()
}
#[inline(always)]
pub(crate) fn read_u32(input: &[u8], offset: usize) -> u32 {
let bytes = input[offset..].first_chunk::<4>().unwrap();
u32::from_le_bytes(*bytes)
}
#[allow(unsafe_code)]
#[inline(always)]
pub(crate) unsafe fn read_u32_unchecked(input: &[u8], offset: usize) -> u32 {
debug_assert!(offset + 4 <= input.len());
let raw = unsafe { core::ptr::read_unaligned(input.as_ptr().add(offset) as *const u32) };
raw.to_le()
}
#[inline(always)]
pub(crate) fn read_u64(input: &[u8], offset: usize) -> u64 {
let bytes = input[offset..].first_chunk::<8>().unwrap();
u64::from_le_bytes(*bytes)
}
#[allow(unsafe_code)]
#[inline(always)]
pub(crate) unsafe fn read_u64_unchecked(input: &[u8], offset: usize) -> u64 {
debug_assert!(offset + 8 <= input.len());
let raw = unsafe { core::ptr::read_unaligned(input.as_ptr().add(offset) as *const u64) };
raw.to_le()
}
#[inline(always)]
pub(crate) fn write_u64(output: &mut [u8], offset: usize, value: u64) {
output[offset..offset + 8].copy_from_slice(&value.to_le_bytes());
}
#[allow(unsafe_code)]
#[inline(always)]
pub(crate) unsafe fn prefetch_l1(ptr: *const u8) {
#[cfg(miri)]
let _ = ptr;
#[cfg(not(miri))]
{
#[cfg(target_arch = "x86_64")]
{
#[cfg(target_feature = "sse")]
unsafe {
core::arch::x86_64::_mm_prefetch::<{ core::arch::x86_64::_MM_HINT_T0 }>(
ptr as *const i8,
)
};
}
#[cfg(target_arch = "aarch64")]
unsafe {
core::arch::asm!("prfm pldl1keep, [{ptr}]", ptr = in(reg) ptr,
options(nostack, preserves_flags))
};
#[cfg(not(any(
all(target_arch = "x86_64", target_feature = "sse"),
target_arch = "aarch64"
)))]
{
let _ = ptr;
}
}
}
#[allow(unsafe_code)]
#[inline(always)]
pub(crate) unsafe fn prefetch_l2(ptr: *const u8) {
#[cfg(miri)]
let _ = ptr;
#[cfg(not(miri))]
{
#[cfg(target_arch = "x86_64")]
{
#[cfg(target_feature = "sse")]
unsafe {
core::arch::x86_64::_mm_prefetch::<{ core::arch::x86_64::_MM_HINT_T1 }>(
ptr as *const i8,
)
};
}
#[cfg(target_arch = "aarch64")]
unsafe {
core::arch::asm!("prfm pldl2keep, [{ptr}]", ptr = in(reg) ptr,
options(nostack, preserves_flags))
};
#[cfg(not(any(
all(target_arch = "x86_64", target_feature = "sse"),
target_arch = "aarch64"
)))]
{
let _ = ptr;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reads_and_writes_little_endian_words() {
let mut bytes = [0u8; 16];
let value = 0x1122_3344_5566_7788u64;
write_u64(&mut bytes, 2, value);
assert_eq!(read_u64(&bytes, 2), value);
bytes[..4].copy_from_slice(&0xA1B2_C3D4u32.to_le_bytes());
assert_eq!(read_u32(&bytes, 0), 0xA1B2_C3D4);
}
#[test]
fn computes_highbit32() {
assert_eq!(highbit32(1), 0);
assert_eq!(highbit32(2), 1);
assert_eq!(highbit32(3), 1);
assert_eq!(highbit32(0x8000_0000), 31);
}
}