use super::bitmask::BitMask;
use super::EMPTY;
use core::intrinsics::atomic_load_acq;
use core::mem;
#[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;
#[allow(clippy::cast_possible_truncation, clippy::unnecessary_cast)]
pub const BITMASK_MASK: BitMaskWord = 0x8080_8080_8080_8080_u64 as GroupWord;
#[inline]
fn repeat(byte: u8) -> GroupWord {
GroupWord::from_ne_bytes([byte; Group::WIDTH])
}
#[derive(Copy, Clone)]
pub struct Group(GroupWord);
#[allow(clippy::use_self)]
impl Group {
pub const WIDTH: usize = mem::size_of::<Self>();
pub const EMPTY: Group = {
#[repr(C)]
struct AlignedBytes {
_align: [Group; 0],
bytes: [u8; Group::WIDTH],
}
const ALIGNED_BYTES: AlignedBytes = AlignedBytes {
_align: [],
bytes: [EMPTY; Group::WIDTH],
};
unsafe { mem::transmute(ALIGNED_BYTES) }
};
#[inline]
#[allow(clippy::cast_ptr_alignment)] pub unsafe fn load(ptr: *const u8) -> Self {
let mut bytes = [0u8; Group::WIDTH];
for (i, byte) in bytes.iter_mut().enumerate() {
*byte = atomic_load_acq(ptr.add(i));
}
mem::transmute(bytes)
}
#[inline]
#[allow(clippy::cast_ptr_alignment)]
pub unsafe fn load_aligned(ptr: *const u8) -> Self {
debug_assert_eq!(ptr as usize & (mem::align_of::<Self>() - 1), 0);
Group::load(ptr)
}
#[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 {
self.match_byte(EMPTY)
}
#[inline]
pub fn match_empty_or_deleted(self) -> BitMask {
BitMask((self.0 & repeat(0x80)).to_le())
}
#[inline]
pub fn match_full(self) -> BitMask {
self.match_empty_or_deleted().invert()
}
}