Debian Codegraph @

debian/rust-vendor/hashbrown-0.14.5/src/raw/neon.rs

raw
1 use super::bitmask::BitMask;
2 use super::EMPTY;
3 use core::arch::aarch64 as neon;
4 use core::mem;
5 use core::num::NonZeroU64;
6
7 pub(crate) type BitMaskWord = u64;
8 pub(crate) type NonZeroBitMaskWord = NonZeroU64;
9 pub(crate) const BITMASK_STRIDE: usize = 8;
10 pub(crate) const BITMASK_MASK: BitMaskWord = !0;
11 pub(crate) const BITMASK_ITER_MASK: BitMaskWord = 0x8080_8080_8080_8080;
12
13 /// Abstraction over a group of control bytes which can be scanned in
14 /// parallel.
15 ///
16 /// This implementation uses a 64-bit NEON value.
17 #[derive(Copy, Clone)]
18 pub(crate) struct Group(neon::uint8x8_t);
19
20 #[allow(clippy::use_self)]
21 impl Group {
22 /// Number of bytes in the group.
23 pub(crate) const WIDTH: usize = mem::size_of::<Self>();
24
25 /// Returns a full group of empty bytes, suitable for use as the initial
26 /// value for an empty hash table.
27 ///
28 /// This is guaranteed to be aligned to the group size.
29 #[inline]
30 pub(crate) const fn static_empty() -> &'static [u8; Group::WIDTH] {
31 #[repr(C)]
32 struct AlignedBytes {
33 _align: [Group; 0],
34 bytes: [u8; Group::WIDTH],
35 }
36 const ALIGNED_BYTES: AlignedBytes = AlignedBytes {
37 _align: [],
38 bytes: [EMPTY; Group::WIDTH],
39 };
40 &ALIGNED_BYTES.bytes
41 }
42
43 /// Loads a group of bytes starting at the given address.
44 #[inline]
45 #[allow(clippy::cast_ptr_alignment)] // unaligned load
46 pub(crate) unsafe fn load(ptr: *const u8) -> Self {
47 Group(neon::vld1_u8(ptr))
48 }
49
50 /// Loads a group of bytes starting at the given address, which must be
51 /// aligned to `mem::align_of::<Group>()`.
52 #[inline]
53 #[allow(clippy::cast_ptr_alignment)]
54 pub(crate) unsafe fn load_aligned(ptr: *const u8) -> Self {
55 // FIXME: use align_offset once it stabilizes
56 debug_assert_eq!(ptr as usize & (mem::align_of::<Self>() - 1), 0);
57 Group(neon::vld1_u8(ptr))
58 }
59
60 /// Stores the group of bytes to the given address, which must be
61 /// aligned to `mem::align_of::<Group>()`.
62 #[inline]
63 #[allow(clippy::cast_ptr_alignment)]
64 pub(crate) unsafe fn store_aligned(self, ptr: *mut u8) {
65 // FIXME: use align_offset once it stabilizes
66 debug_assert_eq!(ptr as usize & (mem::align_of::<Self>() - 1), 0);
67 neon::vst1_u8(ptr, self.0);
68 }
69
70 /// Returns a `BitMask` indicating all bytes in the group which *may*
71 /// have the given value.
72 #[inline]
73 pub(crate) fn match_byte(self, byte: u8) -> BitMask {
74 unsafe {
75 let cmp = neon::vceq_u8(self.0, neon::vdup_n_u8(byte));
76 BitMask(neon::vget_lane_u64(neon::vreinterpret_u64_u8(cmp), 0))
77 }
78 }
79
80 /// Returns a `BitMask` indicating all bytes in the group which are
81 /// `EMPTY`.
82 #[inline]
83 pub(crate) fn match_empty(self) -> BitMask {
84 self.match_byte(EMPTY)
85 }
86
87 /// Returns a `BitMask` indicating all bytes in the group which are
88 /// `EMPTY` or `DELETED`.
89 #[inline]
90 pub(crate) fn match_empty_or_deleted(self) -> BitMask {
91 unsafe {
92 let cmp = neon::vcltz_s8(neon::vreinterpret_s8_u8(self.0));
93 BitMask(neon::vget_lane_u64(neon::vreinterpret_u64_u8(cmp), 0))
94 }
95 }
96
97 /// Returns a `BitMask` indicating all bytes in the group which are full.
98 #[inline]
99 pub(crate) fn match_full(self) -> BitMask {
100 unsafe {
101 let cmp = neon::vcgez_s8(neon::vreinterpret_s8_u8(self.0));
102 BitMask(neon::vget_lane_u64(neon::vreinterpret_u64_u8(cmp), 0))
103 }
104 }
105
106 /// Performs the following transformation on all bytes in the group:
107 /// - `EMPTY => EMPTY`
108 /// - `DELETED => EMPTY`
109 /// - `FULL => DELETED`
110 #[inline]
111 pub(crate) fn convert_special_to_empty_and_full_to_deleted(self) -> Self {
112 // Map high_bit = 1 (EMPTY or DELETED) to 1111_1111
113 // and high_bit = 0 (FULL) to 1000_0000
114 //
115 // Here's this logic expanded to concrete values:
116 // let special = 0 > byte = 1111_1111 (true) or 0000_0000 (false)
117 // 1111_1111 | 1000_0000 = 1111_1111
118 // 0000_0000 | 1000_0000 = 1000_0000
119 unsafe {
120 let special = neon::vcltz_s8(neon::vreinterpret_s8_u8(self.0));
121 Group(neon::vorr_u8(special, neon::vdup_n_u8(0x80)))
122 }
123 }
124 }