use super::{
bitmask::BitMask,
table::{MODIFIED_TOUCHED, SAFE_TOUCHED},
};
use core::mem;
#[cfg(target_arch = "x86")]
use core::arch::x86;
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64 as x86;
pub type BitMaskWord = u16;
pub const BITMASK_STRIDE: usize = 1;
pub const BITMASK_MASK: BitMaskWord = 0xffff;
#[derive(Copy, Clone)]
pub struct Group(x86::__m128i);
#[allow(clippy::use_self)]
impl Group {
pub const WIDTH: usize = mem::size_of::<Self>();
#[inline]
#[allow(clippy::cast_ptr_alignment)] pub unsafe fn load(ptr: *const u8) -> Self {
Group(x86::_mm_loadu_si128(ptr as *const _))
}
#[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(x86::_mm_load_si128(ptr as *const _))
}
#[inline]
pub fn match_byte(self, byte: u8) -> BitMask {
#[allow(
clippy::cast_possible_wrap, // byte: u8 as i8
// byte: i32 as u16
// note: _mm_movemask_epi8 returns a 16-bit mask in a i32, the
// upper 16-bits of the i32 are zeroed:
clippy::cast_sign_loss,
clippy::cast_possible_truncation
)]
unsafe {
let cmp = x86::_mm_cmpeq_epi8(self.0, x86::_mm_set1_epi8(byte as i8));
BitMask(x86::_mm_movemask_epi8(cmp) as u16)
}
}
#[inline]
pub fn match_empty(self) -> BitMask {
#[allow(
// byte: i32 as u16
// note: _mm_movemask_epi8 returns a 16-bit mask in a i32, the
// upper 16-bits of the i32 are zeroed:
clippy::cast_sign_loss,
clippy::cast_possible_truncation
)]
unsafe {
BitMask(x86::_mm_movemask_epi8(self.0) as u16)
}
}
#[inline]
pub fn match_full(&self) -> BitMask {
self.match_empty().invert()
}
#[inline]
pub fn match_modified(&self) -> BitMask {
self.match_empty()
}
#[inline]
#[allow(clippy::cast_ptr_alignment)]
pub fn convert_mod_to_safe(self, ptr: *mut u8) -> Group {
unsafe {
let mod_touched_eq =
x86::_mm_cmpeq_epi8(x86::_mm_set1_epi8(MODIFIED_TOUCHED as i8), self.0);
let transformed_group =
x86::_mm_and_si128(mod_touched_eq, x86::_mm_set1_epi8(SAFE_TOUCHED as i8));
x86::_mm_storeu_si128(ptr as *mut _, transformed_group);
Group(transformed_group)
}
}
}