use super::bitmask::BitMask;
use super::EMPTY;
use core::{mem, ptr};
#[cfg(any(
target_pointer_width = "64",
target_arch = "aarch64",
target_arch = "x86_64",
))]
type GroupWord = u64;
#[cfg(all(
target_pointer_width = "32",
not(target_arch = "aarch64"),
not(target_arch = "x86_64"),
))]
type GroupWord = u32;
pub type BitMaskWord = GroupWord;
pub const BITMASK_STRIDE: usize = 8;
pub const BITMASK_MASK: BitMaskWord = 0x8080_8080_8080_8080u64 as GroupWord;
#[inline]
fn repeat(byte: u8) -> GroupWord {
let repeat = byte as GroupWord;
let repeat = repeat | repeat.wrapping_shl(8);
let repeat = repeat | repeat.wrapping_shl(16);
repeat | repeat.wrapping_shl(32)
}
#[derive(Copy, Clone)]
pub struct Group(GroupWord);
impl Group {
pub const WIDTH: usize = mem::size_of::<Self>();
#[inline]
pub fn static_empty() -> &'static [u8] {
union AlignedBytes {
_align: Group,
bytes: [u8; Group::WIDTH],
};
const ALIGNED_BYTES: AlignedBytes = AlignedBytes {
bytes: [EMPTY; Group::WIDTH],
};
unsafe { &ALIGNED_BYTES.bytes }
}
#[inline]
pub unsafe fn load(ptr: *const u8) -> Group {
Group(ptr::read_unaligned(ptr as *const _))
}
#[inline]
pub unsafe fn load_aligned(ptr: *const u8) -> Group {
debug_assert_eq!(ptr as usize & (mem::align_of::<Group>() - 1), 0);
Group(ptr::read(ptr as *const _))
}
#[inline]
pub unsafe fn store_aligned(&self, ptr: *mut u8) {
debug_assert_eq!(ptr as usize & (mem::align_of::<Group>() - 1), 0);
ptr::write(ptr as *mut _, self.0);
}
#[inline]
pub fn match_byte(&self, byte: u8) -> BitMask {
let cmp = self.0 ^ repeat(byte);
BitMask((cmp.wrapping_sub(repeat(0x01)) & !cmp & repeat(0x80)).to_le())
}
#[inline]
pub fn match_empty(&self) -> BitMask {
BitMask((self.0 & (self.0 << 1) & repeat(0x80)).to_le())
}
#[inline]
pub fn match_empty_or_deleted(&self) -> BitMask {
BitMask((self.0 & repeat(0x80)).to_le())
}
#[inline]
pub fn convert_special_to_empty_and_full_to_deleted(&self) -> Group {
let full = !self.0 & repeat(0x80);
Group(!full + (full >> 7))
}
}