| 1 |
use super::super::{BitMask, Tag}; |
| 2 |
use core::mem; |
| 3 |
use core::num::NonZeroU16; |
| 4 |
|
| 5 |
use core::arch::loongarch64::*; |
| 6 |
|
| 7 |
pub(crate) type BitMaskWord = u16; |
| 8 |
pub(crate) type NonZeroBitMaskWord = NonZeroU16; |
| 9 |
pub(crate) const BITMASK_STRIDE: usize = 1; |
| 10 |
pub(crate) const BITMASK_ITER_MASK: BitMaskWord = !0; |
| 11 |
|
| 12 |
/// Abstraction over a group of control tags which can be scanned in |
| 13 |
/// parallel. |
| 14 |
/// |
| 15 |
/// This implementation uses a 128-bit LSX value. |
| 16 |
#[derive(Copy, Clone)] |
| 17 |
pub(crate) struct Group(m128i); |
| 18 |
|
| 19 |
// FIXME: https://github.com/rust-lang/rust-clippy/issues/3859 |
| 20 |
#[expect(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 tags, 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 [Tag; Group::WIDTH] { |
| 31 |
#[repr(C)] |
| 32 |
struct AlignedTags { |
| 33 |
_align: [Group; 0], |
| 34 |
tags: [Tag; Group::WIDTH], |
| 35 |
} |
| 36 |
const ALIGNED_TAGS: AlignedTags = AlignedTags { |
| 37 |
_align: [], |
| 38 |
tags: [Tag::EMPTY; Group::WIDTH], |
| 39 |
}; |
| 40 |
&ALIGNED_TAGS.tags |
| 41 |
} |
| 42 |
|
| 43 |
/// Loads a group of tags starting at the given address. |
| 44 |
#[inline] |
| 45 |
pub(crate) unsafe fn load(ptr: *const Tag) -> Self { |
| 46 |
unsafe { Group(lsx_vld::<0>(ptr.cast())) } |
| 47 |
} |
| 48 |
|
| 49 |
/// Loads a group of tags starting at the given address, which must be |
| 50 |
/// aligned to `mem::align_of::<Group>()`. |
| 51 |
#[inline] |
| 52 |
pub(crate) unsafe fn load_aligned(ptr: *const Tag) -> Self { |
| 53 |
debug_assert_eq!(ptr.align_offset(mem::align_of::<Self>()), 0); |
| 54 |
unsafe { Group(lsx_vld::<0>(ptr.cast())) } |
| 55 |
} |
| 56 |
|
| 57 |
/// Stores the group of tags to the given address, which must be |
| 58 |
/// aligned to `mem::align_of::<Group>()`. |
| 59 |
#[inline] |
| 60 |
pub(crate) unsafe fn store_aligned(self, ptr: *mut Tag) { |
| 61 |
debug_assert_eq!(ptr.align_offset(mem::align_of::<Self>()), 0); |
| 62 |
unsafe { |
| 63 |
lsx_vst::<0>(self.0, ptr.cast()); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/// Returns a `BitMask` indicating all tags in the group which have |
| 68 |
/// the given value. |
| 69 |
#[inline] |
| 70 |
pub(crate) fn match_tag(self, tag: Tag) -> BitMask { |
| 71 |
unsafe { |
| 72 |
let cmp = lsx_vseq_b(self.0, lsx_vreplgr2vr_b(tag.0 as i32)); |
| 73 |
BitMask(lsx_vpickve2gr_hu::<0>(lsx_vmskltz_b(cmp)) as u16) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/// Returns a `BitMask` indicating all tags in the group which are |
| 78 |
/// `EMPTY`. |
| 79 |
#[inline] |
| 80 |
pub(crate) fn match_empty(self) -> BitMask { |
| 81 |
unsafe { |
| 82 |
let cmp = lsx_vseqi_b::<{ Tag::EMPTY.0 as i8 as i32 }>(self.0); |
| 83 |
BitMask(lsx_vpickve2gr_hu::<0>(lsx_vmskltz_b(cmp)) as u16) |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/// Returns a `BitMask` indicating all tags in the group which are |
| 88 |
/// `EMPTY` or `DELETED`. |
| 89 |
#[inline] |
| 90 |
pub(crate) fn match_empty_or_deleted(self) -> BitMask { |
| 91 |
unsafe { |
| 92 |
// A tag is EMPTY or DELETED iff the high bit is set |
| 93 |
BitMask(lsx_vpickve2gr_hu::<0>(lsx_vmskltz_b(self.0)) as u16) |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/// Returns a `BitMask` indicating all tags in the group which are full. |
| 98 |
#[inline] |
| 99 |
pub(crate) fn match_full(&self) -> BitMask { |
| 100 |
unsafe { |
| 101 |
// A tag is EMPTY or DELETED iff the high bit is set |
| 102 |
BitMask(lsx_vpickve2gr_hu::<0>(lsx_vmskgez_b(self.0)) as u16) |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
/// Performs the following transformation on all tags 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 > tag = 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 = lsx_vslti_b::<0>(self.0); |
| 121 |
Group(lsx_vori_b::<{ Tag::DELETED.0 as u32 }>(special)) |
| 122 |
} |
| 123 |
} |
| 124 |
} |