Debian Codegraph @

debian/rust-vendor/hashbrown/src/control/group/generic.rs

raw
1 use super::super::{BitMask, Tag};
2 use core::{mem, ptr};
3
4 // Use the native word size as the group size. Using a 64-bit group size on
5 // a 32-bit architecture will just end up being more expensive because
6 // shifts and multiplies will need to be emulated.
7
8 cfg_if! {
9 if #[cfg(any(
10 target_pointer_width = "64",
11 target_arch = "aarch64",
12 target_arch = "x86_64",
13 target_arch = "wasm32",
14 ))] {
15 type GroupWord = u64;
16 type NonZeroGroupWord = core::num::NonZeroU64;
17 } else {
18 type GroupWord = u32;
19 type NonZeroGroupWord = core::num::NonZeroU32;
20 }
21 }
22
23 pub(crate) type BitMaskWord = GroupWord;
24 pub(crate) type NonZeroBitMaskWord = NonZeroGroupWord;
25 pub(crate) const BITMASK_STRIDE: usize = 8;
26 // We only care about the highest bit of each tag for the mask.
27 const BITMASK_MASK: BitMaskWord = u64::from_ne_bytes([Tag::DELETED.0; 8]) as GroupWord;
28 pub(crate) const BITMASK_ITER_MASK: BitMaskWord = !0;
29
30 /// Helper function to replicate a tag across a `GroupWord`.
31 #[inline]
32 fn repeat(tag: Tag) -> GroupWord {
33 GroupWord::from_ne_bytes([tag.0; Group::WIDTH])
34 }
35
36 /// Abstraction over a group of control tags which can be scanned in
37 /// parallel.
38 ///
39 /// This implementation uses a word-sized integer.
40 #[derive(Copy, Clone)]
41 pub(crate) struct Group(GroupWord);
42
43 // We perform all operations in the native endianness, and convert to
44 // little-endian just before creating a BitMask. The can potentially
45 // enable the compiler to eliminate unnecessary byte swaps if we are
46 // only checking whether a BitMask is empty.
47 #[expect(clippy::use_self)]
48 impl Group {
49 /// Number of bytes in the group.
50 pub(crate) const WIDTH: usize = mem::size_of::<Self>();
51
52 /// Returns a full group of empty tags, suitable for use as the initial
53 /// value for an empty hash table.
54 ///
55 /// This is guaranteed to be aligned to the group size.
56 #[inline]
57 pub(crate) const fn static_empty() -> &'static [Tag; Group::WIDTH] {
58 #[repr(C)]
59 struct AlignedTags {
60 _align: [Group; 0],
61 tags: [Tag; Group::WIDTH],
62 }
63 const ALIGNED_TAGS: AlignedTags = AlignedTags {
64 _align: [],
65 tags: [Tag::EMPTY; Group::WIDTH],
66 };
67 &ALIGNED_TAGS.tags
68 }
69
70 /// Loads a group of tags starting at the given address.
71 #[inline]
72 pub(crate) unsafe fn load(ptr: *const Tag) -> Self {
73 unsafe { Group(ptr::read_unaligned(ptr.cast())) }
74 }
75
76 /// Loads a group of tags starting at the given address, which must be
77 /// aligned to `mem::align_of::<Group>()`.
78 #[inline]
79 pub(crate) unsafe fn load_aligned(ptr: *const Tag) -> Self {
80 debug_assert_eq!(ptr.align_offset(mem::align_of::<Self>()), 0);
81 unsafe { Group(ptr::read(ptr.cast())) }
82 }
83
84 /// Stores the group of tags to the given address, which must be
85 /// aligned to `mem::align_of::<Group>()`.
86 #[inline]
87 pub(crate) unsafe fn store_aligned(self, ptr: *mut Tag) {
88 debug_assert_eq!(ptr.align_offset(mem::align_of::<Self>()), 0);
89 unsafe {
90 ptr::write(ptr.cast(), self.0);
91 }
92 }
93
94 /// Returns a `BitMask` indicating all tags in the group which *may*
95 /// have the given value.
96 ///
97 /// This function may return a false positive in certain cases where
98 /// the tag in the group differs from the searched value only in its
99 /// lowest bit. This is fine because:
100 /// - This never happens for `EMPTY` and `DELETED`, only full entries.
101 /// - The check for key equality will catch these.
102 /// - This only happens if there is at least 1 true match.
103 /// - The chance of this happening is very low (< 1% chance per tag).
104 #[inline]
105 pub(crate) fn match_tag(self, tag: Tag) -> BitMask {
106 // This algorithm is derived from
107 // https://graphics.stanford.edu/~seander/bithacks.html##ValueInWord
108 let cmp = self.0 ^ repeat(tag);
109 BitMask((cmp.wrapping_sub(repeat(Tag(0x01))) & !cmp & repeat(Tag::DELETED)).to_le())
110 }
111
112 /// Returns a `BitMask` indicating all tags in the group which are
113 /// `EMPTY`.
114 #[inline]
115 pub(crate) fn match_empty(self) -> BitMask {
116 // If the high bit is set, then the tag must be either:
117 // 1111_1111 (EMPTY) or 1000_0000 (DELETED).
118 // So we can just check if the top two bits are 1 by ANDing them.
119 BitMask((self.0 & (self.0 << 1) & repeat(Tag::DELETED)).to_le())
120 }
121
122 /// Returns a `BitMask` indicating all tags in the group which are
123 /// `EMPTY` or `DELETED`.
124 #[inline]
125 pub(crate) fn match_empty_or_deleted(self) -> BitMask {
126 // A tag is EMPTY or DELETED iff the high bit is set
127 BitMask((self.0 & repeat(Tag::DELETED)).to_le())
128 }
129
130 /// Returns a `BitMask` indicating all tags in the group which are full.
131 #[inline]
132 pub(crate) fn match_full(self) -> BitMask {
133 BitMask(self.match_empty_or_deleted().0 ^ BITMASK_MASK)
134 }
135
136 /// Performs the following transformation on all tags in the group:
137 /// - `EMPTY => EMPTY`
138 /// - `DELETED => EMPTY`
139 /// - `FULL => DELETED`
140 #[inline]
141 pub(crate) fn convert_special_to_empty_and_full_to_deleted(self) -> Self {
142 // Map high_bit = 1 (EMPTY or DELETED) to 1111_1111
143 // and high_bit = 0 (FULL) to 1000_0000
144 //
145 // Here's this logic expanded to concrete values:
146 // let full = 1000_0000 (true) or 0000_0000 (false)
147 // !1000_0000 + 1 = 0111_1111 + 1 = 1000_0000 (no carry)
148 // !0000_0000 + 0 = 1111_1111 + 0 = 1111_1111 (no carry)
149 let full = !self.0 & repeat(Tag::DELETED);
150 Group(!full + (full >> 7))
151 }
152 }