diff --git a/src/lib.rs b/src/lib.rs index b2d6584ff3..3bdeaf8954 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,6 +24,14 @@ maybe_uninit_array_assume_init ) )] +#![cfg_attr( + all( + feature = "nightly", + target_feature = "simd128", + any(target_arch = "wasm32", target_arch = "wasm64"), + ), + feature(wasm_simd) +)] #![allow( clippy::doc_markdown, clippy::module_name_repetitions, diff --git a/src/raw/mod.rs b/src/raw/mod.rs index d0418e387d..1263c151a5 100644 --- a/src/raw/mod.rs +++ b/src/raw/mod.rs @@ -28,6 +28,13 @@ cfg_if! { ))] { mod sse2; use sse2 as imp; + } else if #[cfg(all( + target_feature = "simd128", + any(target_arch = "wasm32", target_arch = "wasm64"), + not(miri) + ))] { + mod wasm; + use wasm as imp; } else { #[path = "generic.rs"] mod generic; diff --git a/src/raw/wasm.rs b/src/raw/wasm.rs new file mode 100644 index 0000000000..15d4ffb30b --- /dev/null +++ b/src/raw/wasm.rs @@ -0,0 +1,119 @@ +use super::{bitmask::BitMask, EMPTY}; +use core::{mem, ptr}; + +#[cfg(target_arch = "wasm32")] +use core::arch::wasm32 as wasm; +#[cfg(target_arch = "wasm64")] +use core::arch::wasm64 as wasm; + +pub type BitMaskWord = u16; +pub const BITMASK_STRIDE: usize = 1; +pub const BITMASK_MASK: BitMaskWord = 0xffff; + +/// Abstraction over a group of control bytes which can be scanned in +/// parallel. +/// +/// This implementation uses a 128-bit SIMD value. +/// It uses repr(transparent) so the v128 can be passed around directly. +#[derive(Copy, Clone)] +#[repr(transparent)] +pub struct Group(wasm::v128); + +impl Group { + /// Number of bytes in the group. + pub const WIDTH: usize = mem::size_of::(); + + /// Returns a full group of empty bytes, suitable for use as the initial + /// value for an empty hash table. + /// + /// This is guaranteed to be aligned to the group size. + pub const fn static_empty() -> &'static [u8; Group::WIDTH] { + #[repr(C)] + struct AlignedBytes { + _align: [Group; 0], + bytes: [u8; Group::WIDTH], + } + const ALIGNED_BYTES: AlignedBytes = AlignedBytes { + _align: [], + bytes: [EMPTY; Group::WIDTH], + }; + &ALIGNED_BYTES.bytes + } + + /// Loads a group of bytes starting at the given address. + #[inline] + pub unsafe fn load(ptr: *const u8) -> Self { + Group(ptr::read_unaligned(ptr.cast())) + } + + /// Loads a group of bytes starting at the given address, which must be + /// aligned to `mem::align_of::()`. + #[inline] + pub unsafe fn load_aligned(ptr: *const u8) -> Self { + // FIXME: use align_offset once it stabilizes + debug_assert_eq!(ptr as usize & (mem::align_of::() - 1), 0); + Group(ptr::read(ptr.cast())) + } + + /// Stores the group of bytes to the given address, which must be + /// aligned to `mem::align_of::()`. + #[inline] + pub unsafe fn store_aligned(self, ptr: *mut u8) { + // FIXME: use align_offset once it stabilizes + debug_assert_eq!(ptr as usize & (mem::align_of::() - 1), 0); + ptr::write(ptr.cast(), self.0); + } + + /// Returns a `BitMask` indicating all bytes in the group which have + /// the given value. + #[inline] + pub fn match_byte(self, byte: u8) -> BitMask { + unsafe { + let cmp = wasm::i8x16_eq(self.0, wasm::u8x16_splat(byte)); + BitMask(wasm::i8x16_bitmask(cmp) as u16) + } + } + + /// Returns a `BitMask` indicating all bytes in the group which are + /// `EMPTY`. + #[inline] + pub fn match_empty(self) -> BitMask { + self.match_byte(EMPTY) + } + + /// Returns a `BitMask` indicating all bytes in the group which are + /// `EMPTY` or `DELETED`. + #[inline] + pub fn match_empty_or_deleted(self) -> BitMask { + unsafe { + // A byte is EMPTY or DELETED iff the high bit is set + BitMask(wasm::i8x16_bitmask(self.0) as u16) + } + } + + /// Returns a `BitMask` indicating all bytes in the group which are full. + #[inline] + pub fn match_full(&self) -> BitMask { + self.match_empty_or_deleted().invert() + } + + /// Performs the following transformation on all bytes in the group: + /// - `EMPTY => EMPTY` + /// - `DELETED => EMPTY` + /// - `FULL => DELETED` + #[inline] + pub fn convert_special_to_empty_and_full_to_deleted(self) -> Self { + // Map high_bit = 1 (EMPTY or DELETED) to 1111_1111 + // and high_bit = 0 (FULL) to 1000_0000 + // + // Here's this logic expanded to concrete values: + // let special = 0 > byte = 1111_1111 (true) or 0000_0000 (false) + // 1111_1111 | 1000_0000 = 1111_1111 + // 0000_0000 | 1000_0000 = 1000_0000 + unsafe { + let zero = wasm::u8x16_splat(0); + let special = wasm::i8x16_gt(zero, self.0); + Group(wasm::v128_or(special, wasm::u8x16_splat(0x80))) + } + } +}