Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WebAssembly SIMD Support #269

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions src/raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
119 changes: 119 additions & 0 deletions src/raw/wasm.rs
Original file line number Diff line number Diff line change
@@ -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::<Self>();

/// 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::<Group>()`.
#[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::<Self>() - 1), 0);
Group(ptr::read(ptr.cast()))
}

/// Stores the group of bytes to the given address, which must be
/// aligned to `mem::align_of::<Group>()`.
#[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::<Self>() - 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)))
}
}
}