]>
git.proxmox.com Git - rustc.git/blob - vendor/hashbrown-0.9.1/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 pub const fn static_empty() -> &'
static [u8; Group
::WIDTH
] {
35 bytes
: [u8; Group
::WIDTH
],
37 const ALIGNED_BYTES
: AlignedBytes
= AlignedBytes
{
39 bytes
: [EMPTY
; Group
::WIDTH
],
44 /// Loads a group of bytes starting at the given address.
46 #[allow(clippy::cast_ptr_alignment)] // unaligned load
47 pub unsafe fn load(ptr
: *const u8) -> Self {
48 Group(x86
::_mm_loadu_si128(ptr
as *const _
))
51 /// Loads a group of bytes starting at the given address, which must be
52 /// aligned to `mem::align_of::<Group>()`.
54 #[allow(clippy::cast_ptr_alignment)]
55 pub unsafe fn load_aligned(ptr
: *const u8) -> Self {
56 // FIXME: use align_offset once it stabilizes
57 debug_assert_eq
!(ptr
as usize & (mem
::align_of
::<Self>() - 1), 0);
58 Group(x86
::_mm_load_si128(ptr
as *const _
))
61 /// Stores the group of bytes to the given address, which must be
62 /// aligned to `mem::align_of::<Group>()`.
64 #[allow(clippy::cast_ptr_alignment)]
65 pub unsafe fn store_aligned(self, ptr
: *mut u8) {
66 // FIXME: use align_offset once it stabilizes
67 debug_assert_eq
!(ptr
as usize & (mem
::align_of
::<Self>() - 1), 0);
68 x86
::_mm_store_si128(ptr
as *mut _
, self.0);
71 /// Returns a `BitMask` indicating all bytes in the group which have
74 pub fn match_byte(self, byte
: u8) -> BitMask
{
76 clippy
::cast_possible_wrap
, // byte: u8 as i8
78 // note: _mm_movemask_epi8 returns a 16-bit mask in a i32, the
79 // upper 16-bits of the i32 are zeroed:
80 clippy
::cast_sign_loss
,
81 clippy
::cast_possible_truncation
84 let cmp
= x86
::_mm_cmpeq_epi8(self.0, x86
::_mm_set1_epi8(byte
as i8));
85 BitMask(x86
::_mm_movemask_epi8(cmp
) as u16)
89 /// Returns a `BitMask` indicating all bytes in the group which are
92 pub fn match_empty(self) -> BitMask
{
93 self.match_byte(EMPTY
)
96 /// Returns a `BitMask` indicating all bytes in the group which are
97 /// `EMPTY` or `DELETED`.
99 pub fn match_empty_or_deleted(self) -> BitMask
{
102 // note: _mm_movemask_epi8 returns a 16-bit mask in a i32, the
103 // upper 16-bits of the i32 are zeroed:
104 clippy
::cast_sign_loss
,
105 clippy
::cast_possible_truncation
108 // A byte is EMPTY or DELETED iff the high bit is set
109 BitMask(x86
::_mm_movemask_epi8(self.0) as u16)
113 /// Returns a `BitMask` indicating all bytes in the group which are full.
115 pub fn match_full(&self) -> BitMask
{
116 self.match_empty_or_deleted().invert()
119 /// Performs the following transformation on all bytes in the group:
120 /// - `EMPTY => EMPTY`
121 /// - `DELETED => EMPTY`
122 /// - `FULL => DELETED`
124 pub fn convert_special_to_empty_and_full_to_deleted(self) -> Self {
125 // Map high_bit = 1 (EMPTY or DELETED) to 1111_1111
126 // and high_bit = 0 (FULL) to 1000_0000
128 // Here's this logic expanded to concrete values:
129 // let special = 0 > byte = 1111_1111 (true) or 0000_0000 (false)
130 // 1111_1111 | 1000_0000 = 1111_1111
131 // 0000_0000 | 1000_0000 = 1000_0000
133 clippy
::cast_possible_wrap
, // byte: 0x80_u8 as i8
136 let zero
= x86
::_mm_setzero_si128();
137 let special
= x86
::_mm_cmpgt_epi8(zero
, self.0);
138 Group(x86
::_mm_or_si128(
140 x86
::_mm_set1_epi8(0x80_u8 as i8),