hashbrown/control/group/
sse2.rs1use super::super::{BitMask, Tag};
2use core::mem;
3use core::num::NonZeroU16;
4
5#[cfg(target_arch = "x86")]
6use core::arch::x86;
7#[cfg(target_arch = "x86_64")]
8use core::arch::x86_64 as x86;
9
10pub(crate) type BitMaskWord = u16;
11pub(crate) type NonZeroBitMaskWord = NonZeroU16;
12pub(crate) const BITMASK_STRIDE: usize = 1;
13pub(crate) const BITMASK_MASK: BitMaskWord = 0xffff;
14pub(crate) const BITMASK_ITER_MASK: BitMaskWord = !0;
15
16#[derive(Copy, Clone)]
21pub(crate) struct Group(x86::__m128i);
22
23#[allow(clippy::use_self)]
25impl Group {
26    pub(crate) const WIDTH: usize = mem::size_of::<Self>();
28
29    #[inline]
34    #[allow(clippy::items_after_statements)]
35    pub(crate) const fn static_empty() -> &'static [Tag; Group::WIDTH] {
36        #[repr(C)]
37        struct AlignedTags {
38            _align: [Group; 0],
39            tags: [Tag; Group::WIDTH],
40        }
41        const ALIGNED_TAGS: AlignedTags = AlignedTags {
42            _align: [],
43            tags: [Tag::EMPTY; Group::WIDTH],
44        };
45        &ALIGNED_TAGS.tags
46    }
47
48    #[inline]
50    #[allow(clippy::cast_ptr_alignment)] pub(crate) unsafe fn load(ptr: *const Tag) -> Self {
52        Group(x86::_mm_loadu_si128(ptr.cast()))
53    }
54
55    #[inline]
58    #[allow(clippy::cast_ptr_alignment)]
59    pub(crate) unsafe fn load_aligned(ptr: *const Tag) -> Self {
60        debug_assert_eq!(ptr.align_offset(mem::align_of::<Self>()), 0);
61        Group(x86::_mm_load_si128(ptr.cast()))
62    }
63
64    #[inline]
67    #[allow(clippy::cast_ptr_alignment)]
68    pub(crate) unsafe fn store_aligned(self, ptr: *mut Tag) {
69        debug_assert_eq!(ptr.align_offset(mem::align_of::<Self>()), 0);
70        x86::_mm_store_si128(ptr.cast(), self.0);
71    }
72
73    #[inline]
76    pub(crate) fn match_tag(self, tag: Tag) -> BitMask {
77        #[allow(
78            clippy::cast_possible_wrap, clippy::cast_sign_loss,
83            clippy::cast_possible_truncation
84        )]
85        unsafe {
86            let cmp = x86::_mm_cmpeq_epi8(self.0, x86::_mm_set1_epi8(tag.0 as i8));
87            BitMask(x86::_mm_movemask_epi8(cmp) as u16)
88        }
89    }
90
91    #[inline]
94    pub(crate) fn match_empty(self) -> BitMask {
95        self.match_tag(Tag::EMPTY)
96    }
97
98    #[inline]
101    pub(crate) fn match_empty_or_deleted(self) -> BitMask {
102        #[allow(
103            clippy::cast_sign_loss,
107            clippy::cast_possible_truncation
108        )]
109        unsafe {
110            BitMask(x86::_mm_movemask_epi8(self.0) as u16)
112        }
113    }
114
115    #[inline]
117    pub(crate) fn match_full(&self) -> BitMask {
118        self.match_empty_or_deleted().invert()
119    }
120
121    #[inline]
126    pub(crate) fn convert_special_to_empty_and_full_to_deleted(self) -> Self {
127        #[allow(
135            clippy::cast_possible_wrap, )]
137        unsafe {
138            let zero = x86::_mm_setzero_si128();
139            let special = x86::_mm_cmpgt_epi8(zero, self.0);
140            Group(x86::_mm_or_si128(
141                special,
142                x86::_mm_set1_epi8(Tag::DELETED.0 as i8),
143            ))
144        }
145    }
146}