Skip to content

Commit

Permalink
Add WebAssembly SIMD Support
Browse files Browse the repository at this point in the history
Chrome 91 just released yesterday with stable support for WASM SIMD.
Firefox will shortly follow as well. Rust also intends to stabilize the
intrinsics soon. So I decided to go ahead and already port SIMD support
for a couple of crates including now hashbrown.
  • Loading branch information
CryZe committed May 26, 2021
1 parent 805b5e2 commit 3b2bc3a
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
maybe_uninit_array_assume_init
)
)]
#![cfg_attr(target_arch = "wasm32", 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;

#[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(core::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.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.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)))
}
}
}

0 comments on commit 3b2bc3a

Please sign in to comment.