hashbrown/control/group/sse2.rs
1use super::super::{BitMask, Tag};
2use core::num::NonZeroU16;
3
4#[cfg(target_arch = "x86")]
5use core::arch::x86;
6#[cfg(target_arch = "x86_64")]
7use core::arch::x86_64 as x86;
8
9pub(crate) type BitMaskWord = u16;
10pub(crate) type NonZeroBitMaskWord = NonZeroU16;
11pub(crate) const BITMASK_STRIDE: usize = 1;
12pub(crate) const BITMASK_ITER_MASK: BitMaskWord = !0;
13
14/// Abstraction over a group of control tags which can be scanned in
15/// parallel.
16///
17/// This implementation uses a 128-bit SSE value.
18#[derive(Copy, Clone)]
19pub(crate) struct Group(x86::__m128i);
20
21// FIXME: https://github.com/rust-lang/rust-clippy/issues/3859
22#[expect(clippy::use_self)]
23impl Group {
24 /// Number of bytes in the group.
25 pub(crate) const WIDTH: usize = size_of::<Self>();
26
27 /// Returns a full group of empty tags, suitable for use as the initial
28 /// value for an empty hash table.
29 ///
30 /// This is guaranteed to be aligned to the group size.
31 #[inline]
32 pub(crate) const fn static_empty() -> &'static [Tag; Group::WIDTH] {
33 #[repr(C)]
34 struct AlignedTags {
35 _align: [Group; 0],
36 tags: [Tag; Group::WIDTH],
37 }
38 const ALIGNED_TAGS: AlignedTags = AlignedTags {
39 _align: [],
40 tags: [Tag::EMPTY; Group::WIDTH],
41 };
42 &ALIGNED_TAGS.tags
43 }
44
45 /// Loads a group of tags starting at the given address.
46 #[inline]
47 pub(crate) unsafe fn load(ptr: *const Tag) -> Self {
48 unsafe { Group(x86::_mm_loadu_si128(ptr.cast())) }
49 }
50
51 /// Loads a group of tags starting at the given address, which must be
52 /// aligned to `align_of::<Group>()`.
53 #[inline]
54 pub(crate) unsafe fn load_aligned(ptr: *const Tag) -> Self {
55 debug_assert_eq!(ptr.align_offset(align_of::<Self>()), 0);
56 unsafe { Group(x86::_mm_load_si128(ptr.cast())) }
57 }
58
59 /// Stores the group of tags to the given address, which must be
60 /// aligned to `align_of::<Group>()`.
61 #[inline]
62 pub(crate) unsafe fn store_aligned(self, ptr: *mut Tag) {
63 debug_assert_eq!(ptr.align_offset(align_of::<Self>()), 0);
64 unsafe {
65 x86::_mm_store_si128(ptr.cast(), self.0);
66 }
67 }
68
69 /// Returns a `BitMask` indicating all tags in the group which have
70 /// the given value.
71 #[inline]
72 pub(crate) fn match_tag(self, tag: Tag) -> BitMask {
73 #[expect(
74 clippy::cast_possible_wrap, // tag.0: Tag as i8
75 // tag: i32 as u16
76 // note: _mm_movemask_epi8 returns a 16-bit mask in a i32, the
77 // upper 16-bits of the i32 are zeroed:
78 clippy::cast_sign_loss,
79 clippy::cast_possible_truncation
80 )]
81 unsafe {
82 let cmp = x86::_mm_cmpeq_epi8(self.0, x86::_mm_set1_epi8(tag.0 as i8));
83 BitMask(x86::_mm_movemask_epi8(cmp) as u16)
84 }
85 }
86
87 /// Returns a `BitMask` indicating all tags in the group which are
88 /// `EMPTY`.
89 #[inline]
90 pub(crate) fn match_empty(self) -> BitMask {
91 self.match_tag(Tag::EMPTY)
92 }
93
94 /// Returns a `BitMask` indicating all tags in the group which are
95 /// `EMPTY` or `DELETED`.
96 #[inline]
97 pub(crate) fn match_empty_or_deleted(self) -> BitMask {
98 #[expect(
99 // tag: i32 as u16
100 // note: _mm_movemask_epi8 returns a 16-bit mask in a i32, the
101 // upper 16-bits of the i32 are zeroed:
102 clippy::cast_sign_loss,
103 clippy::cast_possible_truncation
104 )]
105 unsafe {
106 // A tag is EMPTY or DELETED iff the high bit is set
107 BitMask(x86::_mm_movemask_epi8(self.0) as u16)
108 }
109 }
110
111 /// Returns a `BitMask` indicating all tags in the group which are full.
112 #[inline]
113 pub(crate) fn match_full(&self) -> BitMask {
114 BitMask(!self.match_empty_or_deleted().0)
115 }
116
117 /// Performs the following transformation on all tags in the group:
118 /// - `EMPTY => EMPTY`
119 /// - `DELETED => EMPTY`
120 /// - `FULL => DELETED`
121 #[inline]
122 pub(crate) fn convert_special_to_empty_and_full_to_deleted(self) -> Self {
123 // Map high_bit = 1 (EMPTY or DELETED) to 1111_1111
124 // and high_bit = 0 (FULL) to 1000_0000
125 //
126 // Here's this logic expanded to concrete values:
127 // let special = 0 > tag = 1111_1111 (true) or 0000_0000 (false)
128 // 1111_1111 | 1000_0000 = 1111_1111
129 // 0000_0000 | 1000_0000 = 1000_0000
130 #[expect(
131 clippy::cast_possible_wrap, // tag: Tag::DELETED.0 as i8
132 )]
133 unsafe {
134 let zero = x86::_mm_setzero_si128();
135 let special = x86::_mm_cmpgt_epi8(zero, self.0);
136 Group(x86::_mm_or_si128(
137 special,
138 x86::_mm_set1_epi8(Tag::DELETED.0 as i8),
139 ))
140 }
141 }
142}