-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
341 lines (313 loc) · 8.49 KB
/
lib.rs
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#![feature(portable_simd, split_array)]
use std::{
cmp::min,
num::{NonZeroU16, NonZeroU32, NonZeroU8},
simd::{LaneCount, Simd, SimdPartialEq, SupportedLaneCount, ToBitMask},
};
use pa_types::{Seq, I};
pub type ExtendFn = fn(Seq, Seq) -> I;
pub fn zip(a: Seq, b: Seq) -> I {
a.iter().zip(b).take_while(|(ca, cb)| ca == cb).count() as _
}
fn eq(a: Seq, b: Seq, cnt: usize) -> bool {
unsafe { a.get_unchecked(cnt) == b.get_unchecked(cnt) }
}
fn index_u64(a: Seq, cnt: usize) -> u64 {
unsafe { *(a.as_ptr().offset(cnt as _) as *const u64) }
}
fn index_s<const L: usize>(a: Seq, cnt: usize) -> Simd<u8, L>
where
LaneCount<L>: SupportedLaneCount,
{
unsafe { Simd::from_array(*(a.as_ptr().offset(cnt as _) as *const [u8; L])) }
}
pub fn naive(mut a: Seq, mut b: Seq) -> I {
let mut cnt = 0;
while !a.is_empty() && !b.is_empty() && eq(a, b, 0) {
cnt += 1;
a = &a[1..];
b = &b[1..];
}
cnt
}
pub fn naive_fast(mut a: Seq, mut b: Seq) -> I {
let mut cnt = 0;
let len = min(a.len(), b.len());
while cnt < len && eq(a, b, 0) {
cnt += 1;
a = &a[1..];
b = &b[1..];
}
cnt as I
}
pub fn naive_unsafe(mut a: Seq, mut b: Seq) -> I {
let mut cnt = 0;
while eq(a, b, 0) {
cnt += 1;
a = &a[1..];
b = &b[1..];
}
cnt
}
pub fn scalar(a: Seq, b: Seq) -> I {
let mut cnt = 0;
while cnt < a.len() && cnt < b.len() && eq(a, b, cnt) {
cnt += 1;
}
cnt as I
}
pub fn scalar_fast(a: Seq, b: Seq) -> I {
let mut cnt = 0;
let len = min(a.len(), b.len());
while cnt < len && eq(a, b, cnt) {
cnt += 1;
}
cnt as I
}
pub fn scalar_unsafe(a: Seq, b: Seq) -> I {
let mut cnt = 0;
while eq(a, b, cnt) {
cnt += 1;
}
cnt as I
}
pub fn u64(a: Seq, b: Seq) -> I {
let mut cnt = 0;
while cnt + 8 <= a.len() && cnt + 8 <= b.len() {
let a = index_u64(a, cnt);
let b = index_u64(b, cnt);
let cmp = a ^ b;
if cmp == 0 {
cnt += 8;
} else {
return cnt as I + (cmp.leading_zeros() / u8::BITS) as I;
};
}
cnt as I + zip(&a[cnt..], &b[cnt..])
}
pub fn u64_unsafe(a: Seq, b: Seq) -> I {
let mut cnt = 0;
loop {
let a = index_u64(a, cnt);
let b = index_u64(b, cnt);
let cmp = a ^ b;
if cmp == 0 {
cnt += 8;
} else {
return cnt as I + (cmp.leading_zeros() / u8::BITS) as I;
};
}
}
pub fn u64_unsafe_eq(a: Seq, b: Seq) -> I {
let mut cnt = 0;
loop {
let a = index_u64(a, cnt);
let b = index_u64(b, cnt);
if a == b {
cnt += 8;
} else {
return cnt as I + ((a ^ b).leading_zeros() / u8::BITS) as I;
};
}
}
pub fn u64_unsafe_eq_if0(a: Seq, b: Seq) -> I {
if a[0] != b[0] {
return 0;
}
let mut cnt = 0;
loop {
let a = index_u64(a, cnt);
let b = index_u64(b, cnt);
if a == b {
cnt += 8;
} else {
return cnt as I + ((a ^ b).leading_zeros() / u8::BITS) as I;
};
}
}
pub fn u64_unsafe_eq_if1(a: Seq, b: Seq) -> I {
if a[0] != b[0] {
return 0;
}
let mut cnt = 1;
loop {
let a = index_u64(a, cnt);
let b = index_u64(b, cnt);
if a == b {
cnt += 8;
} else {
return cnt as I + ((a ^ b).leading_zeros() / u8::BITS) as I;
};
}
}
pub fn s256_unsafe_eq(a: Seq, b: Seq) -> I {
const L: usize = 32;
let mut cnt = 0;
loop {
let simd_a = index_s::<L>(a, cnt);
let simd_b = index_s::<L>(b, cnt);
if a == b {
cnt += L;
} else {
let mask = simd_a.simd_eq(simd_b);
let eq = !mask.to_bitmask();
return cnt as I
+ (if cfg!(target_endian = "little") {
eq.leading_zeros() as I
} else {
eq.trailing_zeros() as I
}) / u8::BITS as I;
};
}
}
pub fn s64_unsafe(a: Seq, b: Seq) -> I {
const L: usize = 8;
let mut cnt = 0;
loop {
let simd_a = index_s::<L>(a, cnt);
let simd_b = index_s::<L>(b, cnt);
let mask = simd_a.simd_eq(simd_b);
let eq = !mask.to_bitmask();
if eq == 0 {
cnt += L;
} else {
return cnt as I
+ (if cfg!(target_endian = "little") {
eq.leading_zeros() as I
} else {
eq.trailing_zeros() as I
});
};
}
}
pub fn s64_unsafe_nz(a: Seq, b: Seq) -> I {
const L: usize = 8;
let mut cnt = 0;
loop {
let simd_a = index_s::<L>(a, cnt);
let simd_b = index_s::<L>(b, cnt);
let mask = simd_a.simd_eq(simd_b);
let eq = !mask.to_bitmask();
if let Some(eq) = NonZeroU8::new(eq) {
return cnt as I
+ (if cfg!(target_endian = "little") {
eq.leading_zeros() as I
} else {
eq.trailing_zeros() as I
});
} else {
cnt += L;
};
}
}
pub fn s128_unsafe(a: Seq, b: Seq) -> I {
const L: usize = 16;
let mut cnt = 0;
loop {
let simd_a = index_s::<L>(a, cnt);
let simd_b = index_s::<L>(b, cnt);
let mask = simd_a.simd_eq(simd_b);
let eq = !mask.to_bitmask();
if eq == 0 {
cnt += L;
} else {
return cnt as I
+ (if cfg!(target_endian = "little") {
eq.leading_zeros() as I
} else {
eq.trailing_zeros() as I
});
};
}
}
pub fn s128_unsafe_nz(a: Seq, b: Seq) -> I {
const L: usize = 16;
let mut cnt = 0;
loop {
let simd_a = index_s::<L>(a, cnt);
let simd_b = index_s::<L>(b, cnt);
let mask = simd_a.simd_eq(simd_b);
let eq = !mask.to_bitmask();
if let Some(eq) = NonZeroU16::new(eq) {
return cnt as I
+ (if cfg!(target_endian = "little") {
eq.leading_zeros() as I
} else {
eq.trailing_zeros() as I
});
} else {
cnt += L;
};
}
}
pub fn s256_unsafe(a: Seq, b: Seq) -> I {
const L: usize = 32;
let mut cnt = 0;
loop {
let simd_a = index_s::<L>(a, cnt);
let simd_b = index_s::<L>(b, cnt);
let mask = simd_a.simd_eq(simd_b);
let eq = !mask.to_bitmask();
if eq == 0 {
cnt += L;
} else {
return cnt as I
+ (if cfg!(target_endian = "little") {
eq.leading_zeros() as I
} else {
eq.trailing_zeros() as I
});
};
}
}
pub fn s256_unsafe_nz(a: Seq, b: Seq) -> I {
const L: usize = 32;
let mut cnt = 0;
loop {
let simd_a = index_s::<L>(a, cnt);
let simd_b = index_s::<L>(b, cnt);
let mask = simd_a.simd_eq(simd_b);
let eq = !mask.to_bitmask();
if let Some(eq) = NonZeroU32::new(eq) {
return cnt as I
+ (if cfg!(target_endian = "little") {
eq.leading_zeros() as I
} else {
eq.trailing_zeros() as I
});
} else {
cnt += L;
};
}
}
pub mod parallel {
pub type ExtendFnParallel<const K: usize> = fn(Seq, Seq, Simd<I, K>, Simd<I, K>) -> Simd<I, K>;
fn index<T: std::simd::SimdElement, const K: usize>(a: Seq, is: Simd<I, K>) -> Simd<T, K>
where
LaneCount<K>: SupportedLaneCount,
{
unsafe {
Simd::from_array(array::from_fn(|k| {
*(a.as_ptr().offset(is[k] as isize) as *const T)
}))
}
}
use super::*;
use std::array;
pub fn u32_unsafe_once_ss(a: Seq, b: Seq, is: Simd<I, 8>, js: Simd<I, 8>) -> Simd<I, 8> {
let a = index::<u32, 8>(a, is);
let b = index::<u32, 8>(b, js);
let cmp = a ^ b;
Simd::from(array::from_fn(|k| {
cmp[k].leading_zeros() as I / u8::BITS as I
}))
}
pub fn u16_unsafe_once_ss(a: Seq, b: Seq, is: Simd<I, 16>, js: Simd<I, 16>) -> Simd<I, 16> {
let a = index::<u16, 16>(a, is);
let b = index::<u16, 16>(b, js);
let cmp = a ^ b;
Simd::from(array::from_fn(|k| {
cmp[k].leading_zeros() as I / u8::BITS as I
}))
}
}