-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
3 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
} | ||
} | ||
} |