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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#![allow(dead_code)]
use std::arch::x86_64::*;
use std::fmt;
#[derive(Clone, Copy, Debug)]
pub struct SSSE3VectorBuilder(());
impl SSSE3VectorBuilder {
pub fn new() -> Option<SSSE3VectorBuilder> {
if is_x86_feature_detected!("ssse3") {
Some(SSSE3VectorBuilder(()))
} else {
None
}
}
#[inline]
pub fn u8x16_splat(self, n: u8) -> u8x16 {
unsafe { u8x16::splat(n) }
}
#[inline]
pub fn u8x16_load_unaligned(self, slice: &[u8]) -> u8x16 {
unsafe { u8x16::load_unaligned(slice) }
}
#[inline]
pub unsafe fn u8x16_load_unchecked_unaligned(self, slice: &[u8]) -> u8x16 {
u8x16::load_unchecked_unaligned(slice)
}
#[inline]
pub fn u8x16_load(self, slice: &[u8]) -> u8x16 {
unsafe { u8x16::load(slice) }
}
#[inline]
pub unsafe fn u8x16_load_unchecked(self, slice: &[u8]) -> u8x16 {
u8x16::load_unchecked(slice)
}
}
#[derive(Clone, Copy)]
#[allow(non_camel_case_types)]
pub union u8x16 {
vector: __m128i,
bytes: [u8; 16],
}
impl u8x16 {
#[inline]
unsafe fn splat(n: u8) -> u8x16 {
u8x16 { vector: _mm_set1_epi8(n as i8) }
}
#[inline]
unsafe fn load_unaligned(slice: &[u8]) -> u8x16 {
assert!(slice.len() >= 16);
u8x16::load_unchecked(slice)
}
#[inline]
unsafe fn load_unchecked_unaligned(slice: &[u8]) -> u8x16 {
let v = _mm_loadu_si128(slice.as_ptr() as *const u8 as *const __m128i);
u8x16 { vector: v }
}
#[inline]
unsafe fn load(slice: &[u8]) -> u8x16 {
assert!(slice.len() >= 16);
assert!(slice.as_ptr() as usize % 16 == 0);
u8x16::load_unchecked(slice)
}
#[inline]
unsafe fn load_unchecked(slice: &[u8]) -> u8x16 {
let v = _mm_load_si128(slice.as_ptr() as *const u8 as *const __m128i);
u8x16 { vector: v }
}
#[inline]
pub fn extract(self, i: usize) -> u8 {
unsafe { self.bytes[i] }
}
#[inline]
pub fn replace(&mut self, i: usize, byte: u8) {
unsafe { self.bytes[i] = byte; }
}
#[inline]
pub fn shuffle(self, indices: u8x16) -> u8x16 {
unsafe {
u8x16 { vector: _mm_shuffle_epi8(self.vector, indices.vector) }
}
}
#[inline]
pub fn ne(self, other: u8x16) -> u8x16 {
unsafe {
let boolv = _mm_cmpeq_epi8(self.vector, other.vector);
let ones = _mm_set1_epi8(0xFF as u8 as i8);
u8x16 { vector: _mm_andnot_si128(boolv, ones) }
}
}
#[inline]
pub fn and(self, other: u8x16) -> u8x16 {
unsafe {
u8x16 { vector: _mm_and_si128(self.vector, other.vector) }
}
}
#[inline]
pub fn movemask(self) -> u32 {
unsafe {
_mm_movemask_epi8(self.vector) as u32
}
}
#[inline]
pub fn alignr_14(self, other: u8x16) -> u8x16 {
unsafe {
u8x16 { vector: _mm_alignr_epi8(self.vector, other.vector, 14) }
}
}
#[inline]
pub fn alignr_15(self, other: u8x16) -> u8x16 {
unsafe {
u8x16 { vector: _mm_alignr_epi8(self.vector, other.vector, 15) }
}
}
#[inline]
pub fn bit_shift_right_4(self) -> u8x16 {
unsafe {
u8x16 { vector: _mm_srli_epi16(self.vector, 4) }
}
}
}
impl fmt::Debug for u8x16 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
unsafe { self.bytes.fmt(f) }
}
}