]>
git.proxmox.com Git - rustc.git/blob - vendor/hashbrown-0.11.2/src/raw/sse2.rs
1 use super::bitmask
::BitMask
;
5 #[cfg(target_arch = "x86")]
7 #[cfg(target_arch = "x86_64")]
8 use core
::arch
::x86_64
as x86
;
10 pub type BitMaskWord
= u16;
11 pub const BITMASK_STRIDE
: usize = 1;
12 pub const BITMASK_MASK
: BitMaskWord
= 0xffff;
14 /// Abstraction over a group of control bytes which can be scanned in
17 /// This implementation uses a 128-bit SSE value.
18 #[derive(Copy, Clone)]
19 pub struct Group(x86
::__m128i
);
21 // FIXME: https://github.com/rust-lang/rust-clippy/issues/3859
22 #[allow(clippy::use_self)]
24 /// Number of bytes in the group.
25 pub const WIDTH
: usize = mem
::size_of
::<Self>();
27 /// Returns a full group of empty bytes, suitable for use as the initial
28 /// value for an empty hash table.
30 /// This is guaranteed to be aligned to the group size.
31 #[allow(clippy::items_after_statements)]
32 pub const fn static_empty() -> &'
static [u8; Group
::WIDTH
] {
36 bytes
: [u8; Group
::WIDTH
],
38 const ALIGNED_BYTES
: AlignedBytes
= AlignedBytes
{
40 bytes
: [EMPTY
; Group
::WIDTH
],
45 /// Loads a group of bytes starting at the given address.
47 #[allow(clippy::cast_ptr_alignment)] // unaligned load
48 pub unsafe fn load(ptr
: *const u8) -> Self {
49 Group(x86
::_mm_loadu_si128(ptr
.cast()))
52 /// Loads a group of bytes starting at the given address, which must be
53 /// aligned to `mem::align_of::<Group>()`.
55 #[allow(clippy::cast_ptr_alignment)]
56 pub unsafe fn load_aligned(ptr
: *const u8) -> Self {
57 // FIXME: use align_offset once it stabilizes
58 debug_assert_eq
!(ptr
as usize & (mem
::align_of
::<Self>() - 1), 0);
59 Group(x86
::_mm_load_si128(ptr
.cast()))
62 /// Stores the group of bytes to the given address, which must be
63 /// aligned to `mem::align_of::<Group>()`.
65 #[allow(clippy::cast_ptr_alignment)]
66 pub unsafe fn store_aligned(self, ptr
: *mut u8) {
67 // FIXME: use align_offset once it stabilizes
68 debug_assert_eq
!(ptr
as usize & (mem
::align_of
::<Self>() - 1), 0);
69 x86
::_mm_store_si128(ptr
.cast(), self.0);
72 /// Returns a `BitMask` indicating all bytes in the group which have
75 pub fn match_byte(self, byte
: u8) -> BitMask
{
77 clippy
::cast_possible_wrap
, // byte: u8 as i8
79 // note: _mm_movemask_epi8 returns a 16-bit mask in a i32, the
80 // upper 16-bits of the i32 are zeroed:
81 clippy
::cast_sign_loss
,
82 clippy
::cast_possible_truncation
85 let cmp
= x86
::_mm_cmpeq_epi8(self.0, x86
::_mm_set1_epi8(byte
as i8));
86 BitMask(x86
::_mm_movemask_epi8(cmp
) as u16)
90 /// Returns a `BitMask` indicating all bytes in the group which are
93 pub fn match_empty(self) -> BitMask
{
94 self.match_byte(EMPTY
)
97 /// Returns a `BitMask` indicating all bytes in the group which are
98 /// `EMPTY` or `DELETED`.
100 pub fn match_empty_or_deleted(self) -> BitMask
{
103 // note: _mm_movemask_epi8 returns a 16-bit mask in a i32, the
104 // upper 16-bits of the i32 are zeroed:
105 clippy
::cast_sign_loss
,
106 clippy
::cast_possible_truncation
109 // A byte is EMPTY or DELETED iff the high bit is set
110 BitMask(x86
::_mm_movemask_epi8(self.0) as u16)
114 /// Returns a `BitMask` indicating all bytes in the group which are full.
116 pub fn match_full(&self) -> BitMask
{
117 self.match_empty_or_deleted().invert()
120 /// Performs the following transformation on all bytes in the group:
121 /// - `EMPTY => EMPTY`
122 /// - `DELETED => EMPTY`
123 /// - `FULL => DELETED`
125 pub fn convert_special_to_empty_and_full_to_deleted(self) -> Self {
126 // Map high_bit = 1 (EMPTY or DELETED) to 1111_1111
127 // and high_bit = 0 (FULL) to 1000_0000
129 // Here's this logic expanded to concrete values:
130 // let special = 0 > byte = 1111_1111 (true) or 0000_0000 (false)
131 // 1111_1111 | 1000_0000 = 1111_1111
132 // 0000_0000 | 1000_0000 = 1000_0000
134 clippy
::cast_possible_wrap
, // byte: 0x80_u8 as i8
137 let zero
= x86
::_mm_setzero_si128();
138 let special
= x86
::_mm_cmpgt_epi8(zero
, self.0);
139 Group(x86
::_mm_or_si128(
141 x86
::_mm_set1_epi8(0x80_u8 as i8),