1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use super::bitmask::BitMask;
use super::EMPTY;
use core::mem;
#[cfg(target_arch = "x86")]
use core::arch::x86;
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64 as x86;
pub type BitMaskWord = u16;
pub const BITMASK_STRIDE: usize = 1;
pub const BITMASK_MASK: BitMaskWord = 0xffff;
#[derive(Copy, Clone)]
pub struct Group(x86::__m128i);
#[allow(clippy::use_self)]
impl Group {
pub const WIDTH: usize = mem::size_of::<Self>();
pub const fn static_empty() -> &'static [u8; Group::WIDTH] {
#[repr(C)]
struct AlignedBytes {
_align: [Group; 0],
bytes: [u8; Group::WIDTH],
};
const ALIGNED_BYTES: AlignedBytes = AlignedBytes {
_align: [],
bytes: [EMPTY; Group::WIDTH],
};
&ALIGNED_BYTES.bytes
}
#[inline]
#[allow(clippy::cast_ptr_alignment)]
pub unsafe fn load(ptr: *const u8) -> Self {
Group(x86::_mm_loadu_si128(ptr as *const _))
}
#[inline]
#[allow(clippy::cast_ptr_alignment)]
pub unsafe fn load_aligned(ptr: *const u8) -> Self {
debug_assert_eq!(ptr as usize & (mem::align_of::<Self>() - 1), 0);
Group(x86::_mm_load_si128(ptr as *const _))
}
#[inline]
#[allow(clippy::cast_ptr_alignment)]
pub unsafe fn store_aligned(self, ptr: *mut u8) {
debug_assert_eq!(ptr as usize & (mem::align_of::<Self>() - 1), 0);
x86::_mm_store_si128(ptr as *mut _, self.0);
}
#[inline]
pub fn match_byte(self, byte: u8) -> BitMask {
#[allow(
clippy::cast_possible_wrap,
clippy::cast_sign_loss,
clippy::cast_possible_truncation
)]
unsafe {
let cmp = x86::_mm_cmpeq_epi8(self.0, x86::_mm_set1_epi8(byte as i8));
BitMask(x86::_mm_movemask_epi8(cmp) as u16)
}
}
#[inline]
pub fn match_empty(self) -> BitMask {
self.match_byte(EMPTY)
}
#[inline]
pub fn match_empty_or_deleted(self) -> BitMask {
#[allow(
clippy::cast_sign_loss,
clippy::cast_possible_truncation
)]
unsafe {
BitMask(x86::_mm_movemask_epi8(self.0) as u16)
}
}
#[inline]
pub fn match_full(&self) -> BitMask {
self.match_empty_or_deleted().invert()
}
#[inline]
pub fn convert_special_to_empty_and_full_to_deleted(self) -> Self {
#[allow(
clippy::cast_possible_wrap,
)]
unsafe {
let zero = x86::_mm_setzero_si128();
let special = x86::_mm_cmpgt_epi8(zero, self.0);
Group(x86::_mm_or_si128(
special,
x86::_mm_set1_epi8(0x80_u8 as i8),
))
}
}
}