| 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_MASK: BitMaskWord = 0xffff; |
| 11 |
pub(crate) const BITMASK_ITER_MASK: BitMaskWord = !0; |
| 12 |
|
| 13 |
/// Abstraction over a group of control tags which can be scanned in |
| 14 |
/// parallel. |
| 15 |
/// |
| 16 |
/// This implementation uses a 128-bit LSX value. |
| 17 |
#[derive(Copy, Clone)] |
| 18 |
pub(crate) struct Group(m128i); |
| 19 |
|
| 20 |
// FIXME: https://github.com/rust-lang/rust-clippy/issues/3859 |
| 21 |
#[allow(clippy::use_self)] |
| 22 |
impl Group { |
| 23 |
/// Number of bytes in the group. |
| 24 |
pub(crate) const WIDTH: usize = mem::size_of::<Self>(); |
| 25 |
|
| 26 |
/// Returns a full group of empty tags, suitable for use as the initial |
| 27 |
/// value for an empty hash table. |
| 28 |
/// |
| 29 |
/// This is guaranteed to be aligned to the group size. |
| 30 |
#[inline] |
| 31 |
#[allow(clippy::items_after_statements)] |
| 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 |
#[allow(clippy::cast_ptr_alignment)] // unaligned load |
| 48 |
pub(crate) unsafe fn load(ptr: *const Tag) -> Self { |
| 49 |
Group(lsx_vld::<0>(ptr.cast())) |
| 50 |
} |
| 51 |
|
| 52 |
/// Loads a group of tags starting at the given address, which must be |
| 53 |
/// aligned to `mem::align_of::<Group>()`. |
| 54 |
#[inline] |
| 55 |
#[allow(clippy::cast_ptr_alignment)] |
| 56 |
pub(crate) unsafe fn load_aligned(ptr: *const Tag) -> Self { |
| 57 |
debug_assert_eq!(ptr.align_offset(mem::align_of::<Self>()), 0); |
| 58 |
Group(lsx_vld::<0>(ptr.cast())) |
| 59 |
} |
| 60 |
|
| 61 |
/// Stores the group of tags to the given address, which must be |
| 62 |
/// aligned to `mem::align_of::<Group>()`. |
| 63 |
#[inline] |
| 64 |
#[allow(clippy::cast_ptr_alignment)] |
| 65 |
pub(crate) unsafe fn store_aligned(self, ptr: *mut Tag) { |
| 66 |
debug_assert_eq!(ptr.align_offset(mem::align_of::<Self>()), 0); |
| 67 |
lsx_vst::<0>(self.0, ptr.cast()); |
| 68 |
} |
| 69 |
|
| 70 |
/// Returns a `BitMask` indicating all tags in the group which have |
| 71 |
/// the given value. |
| 72 |
#[inline] |
| 73 |
pub(crate) fn match_tag(self, tag: Tag) -> BitMask { |
| 74 |
unsafe { |
| 75 |
let cmp = lsx_vseq_b(self.0, lsx_vreplgr2vr_b(tag.0 as i32)); |
| 76 |
BitMask(lsx_vpickve2gr_hu::<0>(lsx_vmskltz_b(cmp)) as u16) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/// Returns a `BitMask` indicating all tags in the group which are |
| 81 |
/// `EMPTY`. |
| 82 |
#[inline] |
| 83 |
pub(crate) fn match_empty(self) -> BitMask { |
| 84 |
unsafe { |
| 85 |
let cmp = lsx_vseqi_b::<{ Tag::EMPTY.0 as i8 as i32 }>(self.0); |
| 86 |
BitMask(lsx_vpickve2gr_hu::<0>(lsx_vmskltz_b(cmp)) as u16) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
/// Returns a `BitMask` indicating all tags in the group which are |
| 91 |
/// `EMPTY` or `DELETED`. |
| 92 |
#[inline] |
| 93 |
pub(crate) fn match_empty_or_deleted(self) -> BitMask { |
| 94 |
unsafe { |
| 95 |
// A tag is EMPTY or DELETED iff the high bit is set |
| 96 |
BitMask(lsx_vpickve2gr_hu::<0>(lsx_vmskltz_b(self.0)) as u16) |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
/// Returns a `BitMask` indicating all tags in the group which are full. |
| 101 |
#[inline] |
| 102 |
pub(crate) fn match_full(&self) -> BitMask { |
| 103 |
unsafe { |
| 104 |
// A tag is EMPTY or DELETED iff the high bit is set |
| 105 |
BitMask(lsx_vpickve2gr_hu::<0>(lsx_vmskgez_b(self.0)) as u16) |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
/// Performs the following transformation on all tags in the group: |
| 110 |
/// - `EMPTY => EMPTY` |
| 111 |
/// - `DELETED => EMPTY` |
| 112 |
/// - `FULL => DELETED` |
| 113 |
#[inline] |
| 114 |
pub(crate) fn convert_special_to_empty_and_full_to_deleted(self) -> Self { |
| 115 |
// Map high_bit = 1 (EMPTY or DELETED) to 1111_1111 |
| 116 |
// and high_bit = 0 (FULL) to 1000_0000 |
| 117 |
// |
| 118 |
// Here's this logic expanded to concrete values: |
| 119 |
// let special = 0 > tag = 1111_1111 (true) or 0000_0000 (false) |
| 120 |
// 1111_1111 | 1000_0000 = 1111_1111 |
| 121 |
// 0000_0000 | 1000_0000 = 1000_0000 |
| 122 |
unsafe { |
| 123 |
let zero = lsx_vreplgr2vr_b(0); |
| 124 |
let special = lsx_vslt_b(self.0, zero); |
| 125 |
Group(lsx_vor_v(special, lsx_vreplgr2vr_b(Tag::DELETED.0 as i32))) |
| 126 |
} |
| 127 |
} |
| 128 |
} |