Debian Codegraph @

third_party/rust/aho-corasick/src/packed/vector.rs

raw
1 // NOTE: The descriptions for each of the vector methods on the traits below
2 // are pretty inscrutable. For this reason, there are tests for every method
3 // on for every trait impl below. If you're confused about what an op does,
4 // consult its test. (They probably should be doc tests, but I couldn't figure
5 // out how to write them in a non-annoying way.)
6
7 use core::{
8 fmt::Debug,
9 panic::{RefUnwindSafe, UnwindSafe},
10 };
11
12 /// A trait for describing vector operations used by vectorized searchers.
13 ///
14 /// The trait is highly constrained to low level vector operations needed for
15 /// the specific algorithms used in this crate. In general, it was invented
16 /// mostly to be generic over x86's __m128i and __m256i types. At time of
17 /// writing, it also supports wasm and aarch64 128-bit vector types as well.
18 ///
19 /// # Safety
20 ///
21 /// All methods are not safe since they are intended to be implemented using
22 /// vendor intrinsics, which are also not safe. Callers must ensure that
23 /// the appropriate target features are enabled in the calling function,
24 /// and that the current CPU supports them. All implementations should
25 /// avoid marking the routines with `#[target_feature]` and instead mark
26 /// them as `#[inline(always)]` to ensure they get appropriately inlined.
27 /// (`inline(always)` cannot be used with target_feature.)
28 pub(crate) trait Vector:
29 Copy + Debug + Send + Sync + UnwindSafe + RefUnwindSafe
30 {
31 /// The number of bits in the vector.
32 const BITS: usize;
33 /// The number of bytes in the vector. That is, this is the size of the
34 /// vector in memory.
35 const BYTES: usize;
36
37 /// Create a vector with 8-bit lanes with the given byte repeated into each
38 /// lane.
39 ///
40 /// # Safety
41 ///
42 /// Callers must ensure that this is okay to call in the current target for
43 /// the current CPU.
44 unsafe fn splat(byte: u8) -> Self;
45
46 /// Read a vector-size number of bytes from the given pointer. The pointer
47 /// does not need to be aligned.
48 ///
49 /// # Safety
50 ///
51 /// Callers must ensure that this is okay to call in the current target for
52 /// the current CPU.
53 ///
54 /// Callers must guarantee that at least `BYTES` bytes are readable from
55 /// `data`.
56 unsafe fn load_unaligned(data: *const u8) -> Self;
57
58 /// Returns true if and only if this vector has zero in all of its lanes.
59 ///
60 /// # Safety
61 ///
62 /// Callers must ensure that this is okay to call in the current target for
63 /// the current CPU.
64 unsafe fn is_zero(self) -> bool;
65
66 /// Do an 8-bit pairwise equality check. If lane `i` is equal in this
67 /// vector and the one given, then lane `i` in the resulting vector is set
68 /// to `0xFF`. Otherwise, it is set to `0x00`.
69 ///
70 /// # Safety
71 ///
72 /// Callers must ensure that this is okay to call in the current target for
73 /// the current CPU.
74 unsafe fn cmpeq(self, vector2: Self) -> Self;
75
76 /// Perform a bitwise 'and' of this vector and the one given and return
77 /// the result.
78 ///
79 /// # Safety
80 ///
81 /// Callers must ensure that this is okay to call in the current target for
82 /// the current CPU.
83 unsafe fn and(self, vector2: Self) -> Self;
84
85 /// Perform a bitwise 'or' of this vector and the one given and return
86 /// the result.
87 ///
88 /// # Safety
89 ///
90 /// Callers must ensure that this is okay to call in the current target for
91 /// the current CPU.
92 unsafe fn or(self, vector2: Self) -> Self;
93
94 /// Shift each 8-bit lane in this vector to the right by the number of
95 /// bits indictated by the `BITS` type parameter.
96 ///
97 /// # Safety
98 ///
99 /// Callers must ensure that this is okay to call in the current target for
100 /// the current CPU.
101 unsafe fn shift_8bit_lane_right<const BITS: i32>(self) -> Self;
102
103 /// Shift this vector to the left by one byte and shift the most
104 /// significant byte of `vector2` into the least significant position of
105 /// this vector.
106 ///
107 /// Stated differently, this behaves as if `self` and `vector2` were
108 /// concatenated into a `2 * Self::BITS` temporary buffer and then shifted
109 /// right by `Self::BYTES - 1` bytes.
110 ///
111 /// With respect to the Teddy algorithm, `vector2` is usually a previous
112 /// `Self::BYTES` chunk from the haystack and `self` is the chunk
113 /// immediately following it. This permits combining the last two bytes
114 /// from the previous chunk (`vector2`) with the first `Self::BYTES - 1`
115 /// bytes from the current chunk. This permits aligning the result of
116 /// various shuffles so that they can be and-ed together and a possible
117 /// candidate discovered.
118 ///
119 /// # Safety
120 ///
121 /// Callers must ensure that this is okay to call in the current target for
122 /// the current CPU.
123 unsafe fn shift_in_one_byte(self, vector2: Self) -> Self;
124
125 /// Shift this vector to the left by two bytes and shift the two most
126 /// significant bytes of `vector2` into the least significant position of
127 /// this vector.
128 ///
129 /// Stated differently, this behaves as if `self` and `vector2` were
130 /// concatenated into a `2 * Self::BITS` temporary buffer and then shifted
131 /// right by `Self::BYTES - 2` bytes.
132 ///
133 /// With respect to the Teddy algorithm, `vector2` is usually a previous
134 /// `Self::BYTES` chunk from the haystack and `self` is the chunk
135 /// immediately following it. This permits combining the last two bytes
136 /// from the previous chunk (`vector2`) with the first `Self::BYTES - 2`
137 /// bytes from the current chunk. This permits aligning the result of
138 /// various shuffles so that they can be and-ed together and a possible
139 /// candidate discovered.
140 ///
141 /// # Safety
142 ///
143 /// Callers must ensure that this is okay to call in the current target for
144 /// the current CPU.
145 unsafe fn shift_in_two_bytes(self, vector2: Self) -> Self;
146
147 /// Shift this vector to the left by three bytes and shift the three most
148 /// significant bytes of `vector2` into the least significant position of
149 /// this vector.
150 ///
151 /// Stated differently, this behaves as if `self` and `vector2` were
152 /// concatenated into a `2 * Self::BITS` temporary buffer and then shifted
153 /// right by `Self::BYTES - 3` bytes.
154 ///
155 /// With respect to the Teddy algorithm, `vector2` is usually a previous
156 /// `Self::BYTES` chunk from the haystack and `self` is the chunk
157 /// immediately following it. This permits combining the last three bytes
158 /// from the previous chunk (`vector2`) with the first `Self::BYTES - 3`
159 /// bytes from the current chunk. This permits aligning the result of
160 /// various shuffles so that they can be and-ed together and a possible
161 /// candidate discovered.
162 ///
163 /// # Safety
164 ///
165 /// Callers must ensure that this is okay to call in the current target for
166 /// the current CPU.
167 unsafe fn shift_in_three_bytes(self, vector2: Self) -> Self;
168
169 /// Shuffles the bytes in this vector according to the indices in each of
170 /// the corresponding lanes in `indices`.
171 ///
172 /// If `i` is the index of corresponding lanes, `A` is this vector, `B` is
173 /// indices and `C` is the resulting vector, then `C = A[B[i]]`.
174 ///
175 /// # Safety
176 ///
177 /// Callers must ensure that this is okay to call in the current target for
178 /// the current CPU.
179 unsafe fn shuffle_bytes(self, indices: Self) -> Self;
180
181 /// Call the provided function for each 64-bit lane in this vector. The
182 /// given function is provided the lane index and lane value as a `u64`.
183 ///
184 /// If `f` returns `Some`, then iteration over the lanes is stopped and the
185 /// value is returned. Otherwise, this returns `None`.
186 ///
187 /// # Notes
188 ///
189 /// Conceptually it would be nice if we could have a
190 /// `unpack64(self) -> [u64; BITS / 64]` method, but defining that is
191 /// tricky given Rust's [current support for const generics][support].
192 /// And even if we could, it would be tricky to write generic code over
193 /// it. (Not impossible. We could introduce another layer that requires
194 /// `AsRef<[u64]>` or something.)
195 ///
196 /// [support]: https://github.com/rust-lang/rust/issues/60551
197 ///
198 /// # Safety
199 ///
200 /// Callers must ensure that this is okay to call in the current target for
201 /// the current CPU.
202 unsafe fn for_each_64bit_lane<T>(
203 self,
204 f: impl FnMut(usize, u64) -> Option<T>,
205 ) -> Option<T>;
206 }
207
208 /// This trait extends the `Vector` trait with additional operations to support
209 /// Fat Teddy.
210 ///
211 /// Fat Teddy uses 16 buckets instead of 8, but reads half as many bytes (as
212 /// the vector size) instead of the full size of a vector per iteration. For
213 /// example, when using a 256-bit vector, Slim Teddy reads 32 bytes at a timr
214 /// but Fat Teddy reads 16 bytes at a time.
215 ///
216 /// Fat Teddy is useful when searching for a large number of literals.
217 /// The extra number of buckets spreads the literals out more and reduces
218 /// verification time.
219 ///
220 /// Currently we only implement this for AVX on x86_64. It would be nice to
221 /// implement this for SSE on x86_64 and NEON on aarch64, with the latter two
222 /// only reading 8 bytes at a time. It's not clear how well it would work, but
223 /// there are some tricky things to figure out in terms of implementation. The
224 /// `half_shift_in_{one,two,three}_bytes` methods in particular are probably
225 /// the trickiest of the bunch. For AVX2, these are implemented by taking
226 /// advantage of the fact that `_mm256_alignr_epi8` operates on each 128-bit
227 /// half instead of the full 256-bit vector. (Where as `_mm_alignr_epi8`
228 /// operates on the full 128-bit vector and not on each 64-bit half.) I didn't
229 /// do a careful survey of NEON to see if it could easily support these
230 /// operations.
231 pub(crate) trait FatVector: Vector {
232 type Half: Vector;
233
234 /// Read a half-vector-size number of bytes from the given pointer, and
235 /// broadcast it across both halfs of a full vector. The pointer does not
236 /// need to be aligned.
237 ///
238 /// # Safety
239 ///
240 /// Callers must ensure that this is okay to call in the current target for
241 /// the current CPU.
242 ///
243 /// Callers must guarantee that at least `Self::HALF::BYTES` bytes are
244 /// readable from `data`.
245 unsafe fn load_half_unaligned(data: *const u8) -> Self;
246
247 /// Like `Vector::shift_in_one_byte`, except this is done for each half
248 /// of the vector instead.
249 ///
250 /// # Safety
251 ///
252 /// Callers must ensure that this is okay to call in the current target for
253 /// the current CPU.
254 unsafe fn half_shift_in_one_byte(self, vector2: Self) -> Self;
255
256 /// Like `Vector::shift_in_two_bytes`, except this is done for each half
257 /// of the vector instead.
258 ///
259 /// # Safety
260 ///
261 /// Callers must ensure that this is okay to call in the current target for
262 /// the current CPU.
263 unsafe fn half_shift_in_two_bytes(self, vector2: Self) -> Self;
264
265 /// Like `Vector::shift_in_two_bytes`, except this is done for each half
266 /// of the vector instead.
267 ///
268 /// # Safety
269 ///
270 /// Callers must ensure that this is okay to call in the current target for
271 /// the current CPU.
272 unsafe fn half_shift_in_three_bytes(self, vector2: Self) -> Self;
273
274 /// Swap the 128-bit lanes in this vector.
275 ///
276 /// # Safety
277 ///
278 /// Callers must ensure that this is okay to call in the current target for
279 /// the current CPU.
280 unsafe fn swap_halves(self) -> Self;
281
282 /// Unpack and interleave the 8-bit lanes from the low 128 bits of each
283 /// vector and return the result.
284 ///
285 /// # Safety
286 ///
287 /// Callers must ensure that this is okay to call in the current target for
288 /// the current CPU.
289 unsafe fn interleave_low_8bit_lanes(self, vector2: Self) -> Self;
290
291 /// Unpack and interleave the 8-bit lanes from the high 128 bits of each
292 /// vector and return the result.
293 ///
294 /// # Safety
295 ///
296 /// Callers must ensure that this is okay to call in the current target for
297 /// the current CPU.
298 unsafe fn interleave_high_8bit_lanes(self, vector2: Self) -> Self;
299
300 /// Call the provided function for each 64-bit lane in the lower half
301 /// of this vector and then in the other vector. The given function is
302 /// provided the lane index and lane value as a `u64`. (The high 128-bits
303 /// of each vector are ignored.)
304 ///
305 /// If `f` returns `Some`, then iteration over the lanes is stopped and the
306 /// value is returned. Otherwise, this returns `None`.
307 ///
308 /// # Safety
309 ///
310 /// Callers must ensure that this is okay to call in the current target for
311 /// the current CPU.
312 unsafe fn for_each_low_64bit_lane<T>(
313 self,
314 vector2: Self,
315 f: impl FnMut(usize, u64) -> Option<T>,
316 ) -> Option<T>;
317 }
318
319 #[cfg(all(target_arch = "x86_64", target_feature = "sse2"))]
320 mod x86_64_ssse3 {
321 use core::arch::x86_64::*;
322
323 use crate::util::int::{I32, I64, I8};
324
325 use super::Vector;
326
327 impl Vector for __m128i {
328 const BITS: usize = 128;
329 const BYTES: usize = 16;
330
331 #[inline(always)]
332 unsafe fn splat(byte: u8) -> __m128i {
333 _mm_set1_epi8(i8::from_bits(byte))
334 }
335
336 #[inline(always)]
337 unsafe fn load_unaligned(data: *const u8) -> __m128i {
338 _mm_loadu_si128(data.cast::<__m128i>())
339 }
340
341 #[inline(always)]
342 unsafe fn is_zero(self) -> bool {
343 let cmp = self.cmpeq(Self::splat(0));
344 _mm_movemask_epi8(cmp).to_bits() == 0xFFFF
345 }
346
347 #[inline(always)]
348 unsafe fn cmpeq(self, vector2: Self) -> __m128i {
349 _mm_cmpeq_epi8(self, vector2)
350 }
351
352 #[inline(always)]
353 unsafe fn and(self, vector2: Self) -> __m128i {
354 _mm_and_si128(self, vector2)
355 }
356
357 #[inline(always)]
358 unsafe fn or(self, vector2: Self) -> __m128i {
359 _mm_or_si128(self, vector2)
360 }
361
362 #[inline(always)]
363 unsafe fn shift_8bit_lane_right<const BITS: i32>(self) -> Self {
364 // Apparently there is no _mm_srli_epi8, so we emulate it by
365 // shifting 16-bit integers and masking out the high nybble of each
366 // 8-bit lane (since that nybble will contain bits from the low
367 // nybble of the previous lane).
368 let lomask = Self::splat(0xF);
369 _mm_srli_epi16(self, BITS).and(lomask)
370 }
371
372 #[inline(always)]
373 unsafe fn shift_in_one_byte(self, vector2: Self) -> Self {
374 _mm_alignr_epi8(self, vector2, 15)
375 }
376
377 #[inline(always)]
378 unsafe fn shift_in_two_bytes(self, vector2: Self) -> Self {
379 _mm_alignr_epi8(self, vector2, 14)
380 }
381
382 #[inline(always)]
383 unsafe fn shift_in_three_bytes(self, vector2: Self) -> Self {
384 _mm_alignr_epi8(self, vector2, 13)
385 }
386
387 #[inline(always)]
388 unsafe fn shuffle_bytes(self, indices: Self) -> Self {
389 _mm_shuffle_epi8(self, indices)
390 }
391
392 #[inline(always)]
393 unsafe fn for_each_64bit_lane<T>(
394 self,
395 mut f: impl FnMut(usize, u64) -> Option<T>,
396 ) -> Option<T> {
397 let lane = _mm_extract_epi64(self, 0).to_bits();
398 if let Some(t) = f(0, lane) {
399 return Some(t);
400 }
401 let lane = _mm_extract_epi64(self, 1).to_bits();
402 if let Some(t) = f(1, lane) {
403 return Some(t);
404 }
405 None
406 }
407 }
408 }
409
410 #[cfg(all(target_arch = "x86_64", target_feature = "sse2"))]
411 mod x86_64_avx2 {
412 use core::arch::x86_64::*;
413
414 use crate::util::int::{I32, I64, I8};
415
416 use super::{FatVector, Vector};
417
418 impl Vector for __m256i {
419 const BITS: usize = 256;
420 const BYTES: usize = 32;
421
422 #[inline(always)]
423 unsafe fn splat(byte: u8) -> __m256i {
424 _mm256_set1_epi8(i8::from_bits(byte))
425 }
426
427 #[inline(always)]
428 unsafe fn load_unaligned(data: *const u8) -> __m256i {
429 _mm256_loadu_si256(data.cast::<__m256i>())
430 }
431
432 #[inline(always)]
433 unsafe fn is_zero(self) -> bool {
434 let cmp = self.cmpeq(Self::splat(0));
435 _mm256_movemask_epi8(cmp).to_bits() == 0xFFFFFFFF
436 }
437
438 #[inline(always)]
439 unsafe fn cmpeq(self, vector2: Self) -> __m256i {
440 _mm256_cmpeq_epi8(self, vector2)
441 }
442
443 #[inline(always)]
444 unsafe fn and(self, vector2: Self) -> __m256i {
445 _mm256_and_si256(self, vector2)
446 }
447
448 #[inline(always)]
449 unsafe fn or(self, vector2: Self) -> __m256i {
450 _mm256_or_si256(self, vector2)
451 }
452
453 #[inline(always)]
454 unsafe fn shift_8bit_lane_right<const BITS: i32>(self) -> Self {
455 let lomask = Self::splat(0xF);
456 _mm256_srli_epi16(self, BITS).and(lomask)
457 }
458
459 #[inline(always)]
460 unsafe fn shift_in_one_byte(self, vector2: Self) -> Self {
461 // Credit goes to jneem for figuring this out:
462 // https://github.com/jneem/teddy/blob/9ab5e899ad6ef6911aecd3cf1033f1abe6e1f66c/src/x86/teddy_simd.rs#L145-L184
463 //
464 // TL;DR avx2's PALIGNR instruction is actually just two 128-bit
465 // PALIGNR instructions, which is not what we want, so we need to
466 // do some extra shuffling.
467 let v = _mm256_permute2x128_si256(vector2, self, 0x21);
468 _mm256_alignr_epi8(self, v, 15)
469 }
470
471 #[inline(always)]
472 unsafe fn shift_in_two_bytes(self, vector2: Self) -> Self {
473 // Credit goes to jneem for figuring this out:
474 // https://github.com/jneem/teddy/blob/9ab5e899ad6ef6911aecd3cf1033f1abe6e1f66c/src/x86/teddy_simd.rs#L145-L184
475 //
476 // TL;DR avx2's PALIGNR instruction is actually just two 128-bit
477 // PALIGNR instructions, which is not what we want, so we need to
478 // do some extra shuffling.
479 let v = _mm256_permute2x128_si256(vector2, self, 0x21);
480 _mm256_alignr_epi8(self, v, 14)
481 }
482
483 #[inline(always)]
484 unsafe fn shift_in_three_bytes(self, vector2: Self) -> Self {
485 // Credit goes to jneem for figuring this out:
486 // https://github.com/jneem/teddy/blob/9ab5e899ad6ef6911aecd3cf1033f1abe6e1f66c/src/x86/teddy_simd.rs#L145-L184
487 //
488 // TL;DR avx2's PALIGNR instruction is actually just two 128-bit
489 // PALIGNR instructions, which is not what we want, so we need to
490 // do some extra shuffling.
491 let v = _mm256_permute2x128_si256(vector2, self, 0x21);
492 _mm256_alignr_epi8(self, v, 13)
493 }
494
495 #[inline(always)]
496 unsafe fn shuffle_bytes(self, indices: Self) -> Self {
497 _mm256_shuffle_epi8(self, indices)
498 }
499
500 #[inline(always)]
501 unsafe fn for_each_64bit_lane<T>(
502 self,
503 mut f: impl FnMut(usize, u64) -> Option<T>,
504 ) -> Option<T> {
505 // NOTE: At one point in the past, I used transmute to this to
506 // get a [u64; 4], but it turned out to lead to worse codegen IIRC.
507 // I've tried it more recently, and it looks like that's no longer
508 // the case. But since there's no difference, we stick with the
509 // slightly more complicated but transmute-free version.
510 let lane = _mm256_extract_epi64(self, 0).to_bits();
511 if let Some(t) = f(0, lane) {
512 return Some(t);
513 }
514 let lane = _mm256_extract_epi64(self, 1).to_bits();
515 if let Some(t) = f(1, lane) {
516 return Some(t);
517 }
518 let lane = _mm256_extract_epi64(self, 2).to_bits();
519 if let Some(t) = f(2, lane) {
520 return Some(t);
521 }
522 let lane = _mm256_extract_epi64(self, 3).to_bits();
523 if let Some(t) = f(3, lane) {
524 return Some(t);
525 }
526 None
527 }
528 }
529
530 impl FatVector for __m256i {
531 type Half = __m128i;
532
533 #[inline(always)]
534 unsafe fn load_half_unaligned(data: *const u8) -> Self {
535 let half = Self::Half::load_unaligned(data);
536 _mm256_broadcastsi128_si256(half)
537 }
538
539 #[inline(always)]
540 unsafe fn half_shift_in_one_byte(self, vector2: Self) -> Self {
541 _mm256_alignr_epi8(self, vector2, 15)
542 }
543
544 #[inline(always)]
545 unsafe fn half_shift_in_two_bytes(self, vector2: Self) -> Self {
546 _mm256_alignr_epi8(self, vector2, 14)
547 }
548
549 #[inline(always)]
550 unsafe fn half_shift_in_three_bytes(self, vector2: Self) -> Self {
551 _mm256_alignr_epi8(self, vector2, 13)
552 }
553
554 #[inline(always)]
555 unsafe fn swap_halves(self) -> Self {
556 _mm256_permute4x64_epi64(self, 0x4E)
557 }
558
559 #[inline(always)]
560 unsafe fn interleave_low_8bit_lanes(self, vector2: Self) -> Self {
561 _mm256_unpacklo_epi8(self, vector2)
562 }
563
564 #[inline(always)]
565 unsafe fn interleave_high_8bit_lanes(self, vector2: Self) -> Self {
566 _mm256_unpackhi_epi8(self, vector2)
567 }
568
569 #[inline(always)]
570 unsafe fn for_each_low_64bit_lane<T>(
571 self,
572 vector2: Self,
573 mut f: impl FnMut(usize, u64) -> Option<T>,
574 ) -> Option<T> {
575 let lane = _mm256_extract_epi64(self, 0).to_bits();
576 if let Some(t) = f(0, lane) {
577 return Some(t);
578 }
579 let lane = _mm256_extract_epi64(self, 1).to_bits();
580 if let Some(t) = f(1, lane) {
581 return Some(t);
582 }
583 let lane = _mm256_extract_epi64(vector2, 0).to_bits();
584 if let Some(t) = f(2, lane) {
585 return Some(t);
586 }
587 let lane = _mm256_extract_epi64(vector2, 1).to_bits();
588 if let Some(t) = f(3, lane) {
589 return Some(t);
590 }
591 None
592 }
593 }
594 }
595
596 #[cfg(target_arch = "aarch64")]
597 mod aarch64_neon {
598 use core::arch::aarch64::*;
599
600 use super::Vector;
601
602 impl Vector for uint8x16_t {
603 const BITS: usize = 128;
604 const BYTES: usize = 16;
605
606 #[inline(always)]
607 unsafe fn splat(byte: u8) -> uint8x16_t {
608 vdupq_n_u8(byte)
609 }
610
611 #[inline(always)]
612 unsafe fn load_unaligned(data: *const u8) -> uint8x16_t {
613 vld1q_u8(data)
614 }
615
616 #[inline(always)]
617 unsafe fn is_zero(self) -> bool {
618 // Could also use vmaxvq_u8.
619 // ... I tried that and couldn't observe any meaningful difference
620 // in benchmarks.
621 let maxes = vreinterpretq_u64_u8(vpmaxq_u8(self, self));
622 vgetq_lane_u64(maxes, 0) == 0
623 }
624
625 #[inline(always)]
626 unsafe fn cmpeq(self, vector2: Self) -> uint8x16_t {
627 vceqq_u8(self, vector2)
628 }
629
630 #[inline(always)]
631 unsafe fn and(self, vector2: Self) -> uint8x16_t {
632 vandq_u8(self, vector2)
633 }
634
635 #[inline(always)]
636 unsafe fn or(self, vector2: Self) -> uint8x16_t {
637 vorrq_u8(self, vector2)
638 }
639
640 #[inline(always)]
641 unsafe fn shift_8bit_lane_right<const BITS: i32>(self) -> Self {
642 debug_assert!(BITS <= 7);
643 vshrq_n_u8(self, BITS)
644 }
645
646 #[inline(always)]
647 unsafe fn shift_in_one_byte(self, vector2: Self) -> Self {
648 vextq_u8(vector2, self, 15)
649 }
650
651 #[inline(always)]
652 unsafe fn shift_in_two_bytes(self, vector2: Self) -> Self {
653 vextq_u8(vector2, self, 14)
654 }
655
656 #[inline(always)]
657 unsafe fn shift_in_three_bytes(self, vector2: Self) -> Self {
658 vextq_u8(vector2, self, 13)
659 }
660
661 #[inline(always)]
662 unsafe fn shuffle_bytes(self, indices: Self) -> Self {
663 vqtbl1q_u8(self, indices)
664 }
665
666 #[inline(always)]
667 unsafe fn for_each_64bit_lane<T>(
668 self,
669 mut f: impl FnMut(usize, u64) -> Option<T>,
670 ) -> Option<T> {
671 let this = vreinterpretq_u64_u8(self);
672 let lane = vgetq_lane_u64(this, 0);
673 if let Some(t) = f(0, lane) {
674 return Some(t);
675 }
676 let lane = vgetq_lane_u64(this, 1);
677 if let Some(t) = f(1, lane) {
678 return Some(t);
679 }
680 None
681 }
682 }
683 }
684
685 #[cfg(all(test, target_arch = "x86_64", target_feature = "sse2"))]
686 mod tests_x86_64_ssse3 {
687 use core::arch::x86_64::*;
688
689 use crate::util::int::{I32, U32};
690
691 use super::*;
692
693 fn is_runnable() -> bool {
694 std::is_x86_feature_detected!("ssse3")
695 }
696
697 #[target_feature(enable = "ssse3")]
698 unsafe fn load(lanes: [u8; 16]) -> __m128i {
699 __m128i::load_unaligned(&lanes as *const u8)
700 }
701
702 #[target_feature(enable = "ssse3")]
703 unsafe fn unload(v: __m128i) -> [u8; 16] {
704 [
705 _mm_extract_epi8(v, 0).to_bits().low_u8(),
706 _mm_extract_epi8(v, 1).to_bits().low_u8(),
707 _mm_extract_epi8(v, 2).to_bits().low_u8(),
708 _mm_extract_epi8(v, 3).to_bits().low_u8(),
709 _mm_extract_epi8(v, 4).to_bits().low_u8(),
710 _mm_extract_epi8(v, 5).to_bits().low_u8(),
711 _mm_extract_epi8(v, 6).to_bits().low_u8(),
712 _mm_extract_epi8(v, 7).to_bits().low_u8(),
713 _mm_extract_epi8(v, 8).to_bits().low_u8(),
714 _mm_extract_epi8(v, 9).to_bits().low_u8(),
715 _mm_extract_epi8(v, 10).to_bits().low_u8(),
716 _mm_extract_epi8(v, 11).to_bits().low_u8(),
717 _mm_extract_epi8(v, 12).to_bits().low_u8(),
718 _mm_extract_epi8(v, 13).to_bits().low_u8(),
719 _mm_extract_epi8(v, 14).to_bits().low_u8(),
720 _mm_extract_epi8(v, 15).to_bits().low_u8(),
721 ]
722 }
723
724 #[test]
725 fn vector_splat() {
726 #[target_feature(enable = "ssse3")]
727 unsafe fn test() {
728 let v = __m128i::splat(0xAF);
729 assert_eq!(
730 unload(v),
731 [
732 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF,
733 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF
734 ]
735 );
736 }
737 if !is_runnable() {
738 return;
739 }
740 unsafe { test() }
741 }
742
743 #[test]
744 fn vector_is_zero() {
745 #[target_feature(enable = "ssse3")]
746 unsafe fn test() {
747 let v = load([0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
748 assert!(!v.is_zero());
749 let v = load([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
750 assert!(v.is_zero());
751 }
752 if !is_runnable() {
753 return;
754 }
755 unsafe { test() }
756 }
757
758 #[test]
759 fn vector_cmpeq() {
760 #[target_feature(enable = "ssse3")]
761 unsafe fn test() {
762 let v1 =
763 load([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1]);
764 let v2 =
765 load([16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);
766 assert_eq!(
767 unload(v1.cmpeq(v2)),
768 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF]
769 );
770 }
771 if !is_runnable() {
772 return;
773 }
774 unsafe { test() }
775 }
776
777 #[test]
778 fn vector_and() {
779 #[target_feature(enable = "ssse3")]
780 unsafe fn test() {
781 let v1 =
782 load([0, 0, 0, 0, 0, 0b1001, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
783 let v2 =
784 load([0, 0, 0, 0, 0, 0b1010, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
785 assert_eq!(
786 unload(v1.and(v2)),
787 [0, 0, 0, 0, 0, 0b1000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
788 );
789 }
790 if !is_runnable() {
791 return;
792 }
793 unsafe { test() }
794 }
795
796 #[test]
797 fn vector_or() {
798 #[target_feature(enable = "ssse3")]
799 unsafe fn test() {
800 let v1 =
801 load([0, 0, 0, 0, 0, 0b1001, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
802 let v2 =
803 load([0, 0, 0, 0, 0, 0b1010, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
804 assert_eq!(
805 unload(v1.or(v2)),
806 [0, 0, 0, 0, 0, 0b1011, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
807 );
808 }
809 if !is_runnable() {
810 return;
811 }
812 unsafe { test() }
813 }
814
815 #[test]
816 fn vector_shift_8bit_lane_right() {
817 #[target_feature(enable = "ssse3")]
818 unsafe fn test() {
819 let v = load([
820 0, 0, 0, 0, 0b1011, 0b0101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
821 ]);
822 assert_eq!(
823 unload(v.shift_8bit_lane_right::<2>()),
824 [0, 0, 0, 0, 0b0010, 0b0001, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
825 );
826 }
827 if !is_runnable() {
828 return;
829 }
830 unsafe { test() }
831 }
832
833 #[test]
834 fn vector_shift_in_one_byte() {
835 #[target_feature(enable = "ssse3")]
836 unsafe fn test() {
837 let v1 =
838 load([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
839 let v2 = load([
840 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
841 ]);
842 assert_eq!(
843 unload(v1.shift_in_one_byte(v2)),
844 [32, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
845 );
846 }
847 if !is_runnable() {
848 return;
849 }
850 unsafe { test() }
851 }
852
853 #[test]
854 fn vector_shift_in_two_bytes() {
855 #[target_feature(enable = "ssse3")]
856 unsafe fn test() {
857 let v1 =
858 load([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
859 let v2 = load([
860 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
861 ]);
862 assert_eq!(
863 unload(v1.shift_in_two_bytes(v2)),
864 [31, 32, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14],
865 );
866 }
867 if !is_runnable() {
868 return;
869 }
870 unsafe { test() }
871 }
872
873 #[test]
874 fn vector_shift_in_three_bytes() {
875 #[target_feature(enable = "ssse3")]
876 unsafe fn test() {
877 let v1 =
878 load([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
879 let v2 = load([
880 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
881 ]);
882 assert_eq!(
883 unload(v1.shift_in_three_bytes(v2)),
884 [30, 31, 32, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13],
885 );
886 }
887 if !is_runnable() {
888 return;
889 }
890 unsafe { test() }
891 }
892
893 #[test]
894 fn vector_shuffle_bytes() {
895 #[target_feature(enable = "ssse3")]
896 unsafe fn test() {
897 let v1 =
898 load([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
899 let v2 =
900 load([0, 0, 0, 0, 4, 4, 4, 4, 8, 8, 8, 8, 12, 12, 12, 12]);
901 assert_eq!(
902 unload(v1.shuffle_bytes(v2)),
903 [1, 1, 1, 1, 5, 5, 5, 5, 9, 9, 9, 9, 13, 13, 13, 13],
904 );
905 }
906 if !is_runnable() {
907 return;
908 }
909 unsafe { test() }
910 }
911
912 #[test]
913 fn vector_for_each_64bit_lane() {
914 #[target_feature(enable = "ssse3")]
915 unsafe fn test() {
916 let v = load([
917 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
918 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
919 ]);
920 let mut lanes = [0u64; 2];
921 v.for_each_64bit_lane(|i, lane| {
922 lanes[i] = lane;
923 None::<()>
924 });
925 assert_eq!(lanes, [0x0807060504030201, 0x100F0E0D0C0B0A09],);
926 }
927 if !is_runnable() {
928 return;
929 }
930 unsafe { test() }
931 }
932 }
933
934 #[cfg(all(test, target_arch = "x86_64", target_feature = "sse2"))]
935 mod tests_x86_64_avx2 {
936 use core::arch::x86_64::*;
937
938 use crate::util::int::{I32, U32};
939
940 use super::*;
941
942 fn is_runnable() -> bool {
943 std::is_x86_feature_detected!("avx2")
944 }
945
946 #[target_feature(enable = "avx2")]
947 unsafe fn load(lanes: [u8; 32]) -> __m256i {
948 __m256i::load_unaligned(&lanes as *const u8)
949 }
950
951 #[target_feature(enable = "avx2")]
952 unsafe fn load_half(lanes: [u8; 16]) -> __m256i {
953 __m256i::load_half_unaligned(&lanes as *const u8)
954 }
955
956 #[target_feature(enable = "avx2")]
957 unsafe fn unload(v: __m256i) -> [u8; 32] {
958 [
959 _mm256_extract_epi8(v, 0).to_bits().low_u8(),
960 _mm256_extract_epi8(v, 1).to_bits().low_u8(),
961 _mm256_extract_epi8(v, 2).to_bits().low_u8(),
962 _mm256_extract_epi8(v, 3).to_bits().low_u8(),
963 _mm256_extract_epi8(v, 4).to_bits().low_u8(),
964 _mm256_extract_epi8(v, 5).to_bits().low_u8(),
965 _mm256_extract_epi8(v, 6).to_bits().low_u8(),
966 _mm256_extract_epi8(v, 7).to_bits().low_u8(),
967 _mm256_extract_epi8(v, 8).to_bits().low_u8(),
968 _mm256_extract_epi8(v, 9).to_bits().low_u8(),
969 _mm256_extract_epi8(v, 10).to_bits().low_u8(),
970 _mm256_extract_epi8(v, 11).to_bits().low_u8(),
971 _mm256_extract_epi8(v, 12).to_bits().low_u8(),
972 _mm256_extract_epi8(v, 13).to_bits().low_u8(),
973 _mm256_extract_epi8(v, 14).to_bits().low_u8(),
974 _mm256_extract_epi8(v, 15).to_bits().low_u8(),
975 _mm256_extract_epi8(v, 16).to_bits().low_u8(),
976 _mm256_extract_epi8(v, 17).to_bits().low_u8(),
977 _mm256_extract_epi8(v, 18).to_bits().low_u8(),
978 _mm256_extract_epi8(v, 19).to_bits().low_u8(),
979 _mm256_extract_epi8(v, 20).to_bits().low_u8(),
980 _mm256_extract_epi8(v, 21).to_bits().low_u8(),
981 _mm256_extract_epi8(v, 22).to_bits().low_u8(),
982 _mm256_extract_epi8(v, 23).to_bits().low_u8(),
983 _mm256_extract_epi8(v, 24).to_bits().low_u8(),
984 _mm256_extract_epi8(v, 25).to_bits().low_u8(),
985 _mm256_extract_epi8(v, 26).to_bits().low_u8(),
986 _mm256_extract_epi8(v, 27).to_bits().low_u8(),
987 _mm256_extract_epi8(v, 28).to_bits().low_u8(),
988 _mm256_extract_epi8(v, 29).to_bits().low_u8(),
989 _mm256_extract_epi8(v, 30).to_bits().low_u8(),
990 _mm256_extract_epi8(v, 31).to_bits().low_u8(),
991 ]
992 }
993
994 #[test]
995 fn vector_splat() {
996 #[target_feature(enable = "avx2")]
997 unsafe fn test() {
998 let v = __m256i::splat(0xAF);
999 assert_eq!(
1000 unload(v),
1001 [
1002 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF,
1003 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF,
1004 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF,
1005 0xAF, 0xAF, 0xAF, 0xAF, 0xAF,
1006 ]
1007 );
1008 }
1009 if !is_runnable() {
1010 return;
1011 }
1012 unsafe { test() }
1013 }
1014
1015 #[test]
1016 fn vector_is_zero() {
1017 #[target_feature(enable = "avx2")]
1018 unsafe fn test() {
1019 let v = load([
1020 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1021 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1022 ]);
1023 assert!(!v.is_zero());
1024 let v = load([
1025 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1026 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1027 ]);
1028 assert!(v.is_zero());
1029 }
1030 if !is_runnable() {
1031 return;
1032 }
1033 unsafe { test() }
1034 }
1035
1036 #[test]
1037 fn vector_cmpeq() {
1038 #[target_feature(enable = "avx2")]
1039 unsafe fn test() {
1040 let v1 = load([
1041 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
1042 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 1,
1043 ]);
1044 let v2 = load([
1045 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18,
1046 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1,
1047 ]);
1048 assert_eq!(
1049 unload(v1.cmpeq(v2)),
1050 [
1051 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1052 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF
1053 ]
1054 );
1055 }
1056 if !is_runnable() {
1057 return;
1058 }
1059 unsafe { test() }
1060 }
1061
1062 #[test]
1063 fn vector_and() {
1064 #[target_feature(enable = "avx2")]
1065 unsafe fn test() {
1066 let v1 = load([
1067 0, 0, 0, 0, 0, 0b1001, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1068 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1069 ]);
1070 let v2 = load([
1071 0, 0, 0, 0, 0, 0b1010, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1072 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1073 ]);
1074 assert_eq!(
1075 unload(v1.and(v2)),
1076 [
1077 0, 0, 0, 0, 0, 0b1000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1078 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1079 ]
1080 );
1081 }
1082 if !is_runnable() {
1083 return;
1084 }
1085 unsafe { test() }
1086 }
1087
1088 #[test]
1089 fn vector_or() {
1090 #[target_feature(enable = "avx2")]
1091 unsafe fn test() {
1092 let v1 = load([
1093 0, 0, 0, 0, 0, 0b1001, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1094 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1095 ]);
1096 let v2 = load([
1097 0, 0, 0, 0, 0, 0b1010, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1098 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1099 ]);
1100 assert_eq!(
1101 unload(v1.or(v2)),
1102 [
1103 0, 0, 0, 0, 0, 0b1011, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1104 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1105 ]
1106 );
1107 }
1108 if !is_runnable() {
1109 return;
1110 }
1111 unsafe { test() }
1112 }
1113
1114 #[test]
1115 fn vector_shift_8bit_lane_right() {
1116 #[target_feature(enable = "avx2")]
1117 unsafe fn test() {
1118 let v = load([
1119 0, 0, 0, 0, 0b1011, 0b0101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1120 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1121 ]);
1122 assert_eq!(
1123 unload(v.shift_8bit_lane_right::<2>()),
1124 [
1125 0, 0, 0, 0, 0b0010, 0b0001, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1126 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1127 ]
1128 );
1129 }
1130 if !is_runnable() {
1131 return;
1132 }
1133 unsafe { test() }
1134 }
1135
1136 #[test]
1137 fn vector_shift_in_one_byte() {
1138 #[target_feature(enable = "avx2")]
1139 unsafe fn test() {
1140 let v1 = load([
1141 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
1142 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
1143 ]);
1144 let v2 = load([
1145 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
1146 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
1147 63, 64,
1148 ]);
1149 assert_eq!(
1150 unload(v1.shift_in_one_byte(v2)),
1151 [
1152 64, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
1153 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
1154 31,
1155 ],
1156 );
1157 }
1158 if !is_runnable() {
1159 return;
1160 }
1161 unsafe { test() }
1162 }
1163
1164 #[test]
1165 fn vector_shift_in_two_bytes() {
1166 #[target_feature(enable = "avx2")]
1167 unsafe fn test() {
1168 let v1 = load([
1169 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
1170 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
1171 ]);
1172 let v2 = load([
1173 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
1174 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
1175 63, 64,
1176 ]);
1177 assert_eq!(
1178 unload(v1.shift_in_two_bytes(v2)),
1179 [
1180 63, 64, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
1181 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
1182 30,
1183 ],
1184 );
1185 }
1186 if !is_runnable() {
1187 return;
1188 }
1189 unsafe { test() }
1190 }
1191
1192 #[test]
1193 fn vector_shift_in_three_bytes() {
1194 #[target_feature(enable = "avx2")]
1195 unsafe fn test() {
1196 let v1 = load([
1197 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
1198 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
1199 ]);
1200 let v2 = load([
1201 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
1202 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
1203 63, 64,
1204 ]);
1205 assert_eq!(
1206 unload(v1.shift_in_three_bytes(v2)),
1207 [
1208 62, 63, 64, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
1209 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
1210 29,
1211 ],
1212 );
1213 }
1214 if !is_runnable() {
1215 return;
1216 }
1217 unsafe { test() }
1218 }
1219
1220 #[test]
1221 fn vector_shuffle_bytes() {
1222 #[target_feature(enable = "avx2")]
1223 unsafe fn test() {
1224 let v1 = load([
1225 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
1226 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
1227 ]);
1228 let v2 = load([
1229 0, 0, 0, 0, 4, 4, 4, 4, 8, 8, 8, 8, 12, 12, 12, 12, 16, 16,
1230 16, 16, 20, 20, 20, 20, 24, 24, 24, 24, 28, 28, 28, 28,
1231 ]);
1232 assert_eq!(
1233 unload(v1.shuffle_bytes(v2)),
1234 [
1235 1, 1, 1, 1, 5, 5, 5, 5, 9, 9, 9, 9, 13, 13, 13, 13, 17,
1236 17, 17, 17, 21, 21, 21, 21, 25, 25, 25, 25, 29, 29, 29,
1237 29
1238 ],
1239 );
1240 }
1241 if !is_runnable() {
1242 return;
1243 }
1244 unsafe { test() }
1245 }
1246
1247 #[test]
1248 fn vector_for_each_64bit_lane() {
1249 #[target_feature(enable = "avx2")]
1250 unsafe fn test() {
1251 let v = load([
1252 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
1253 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14,
1254 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E,
1255 0x1F, 0x20,
1256 ]);
1257 let mut lanes = [0u64; 4];
1258 v.for_each_64bit_lane(|i, lane| {
1259 lanes[i] = lane;
1260 None::<()>
1261 });
1262 assert_eq!(
1263 lanes,
1264 [
1265 0x0807060504030201,
1266 0x100F0E0D0C0B0A09,
1267 0x1817161514131211,
1268 0x201F1E1D1C1B1A19
1269 ]
1270 );
1271 }
1272 if !is_runnable() {
1273 return;
1274 }
1275 unsafe { test() }
1276 }
1277
1278 #[test]
1279 fn fat_vector_half_shift_in_one_byte() {
1280 #[target_feature(enable = "avx2")]
1281 unsafe fn test() {
1282 let v1 = load_half([
1283 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
1284 ]);
1285 let v2 = load_half([
1286 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
1287 ]);
1288 assert_eq!(
1289 unload(v1.half_shift_in_one_byte(v2)),
1290 [
1291 32, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 32,
1292 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
1293 ],
1294 );
1295 }
1296 if !is_runnable() {
1297 return;
1298 }
1299 unsafe { test() }
1300 }
1301
1302 #[test]
1303 fn fat_vector_half_shift_in_two_bytes() {
1304 #[target_feature(enable = "avx2")]
1305 unsafe fn test() {
1306 let v1 = load_half([
1307 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
1308 ]);
1309 let v2 = load_half([
1310 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
1311 ]);
1312 assert_eq!(
1313 unload(v1.half_shift_in_two_bytes(v2)),
1314 [
1315 31, 32, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 31,
1316 32, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
1317 ],
1318 );
1319 }
1320 if !is_runnable() {
1321 return;
1322 }
1323 unsafe { test() }
1324 }
1325
1326 #[test]
1327 fn fat_vector_half_shift_in_three_bytes() {
1328 #[target_feature(enable = "avx2")]
1329 unsafe fn test() {
1330 let v1 = load_half([
1331 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
1332 ]);
1333 let v2 = load_half([
1334 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
1335 ]);
1336 assert_eq!(
1337 unload(v1.half_shift_in_three_bytes(v2)),
1338 [
1339 30, 31, 32, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 30,
1340 31, 32, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
1341 ],
1342 );
1343 }
1344 if !is_runnable() {
1345 return;
1346 }
1347 unsafe { test() }
1348 }
1349
1350 #[test]
1351 fn fat_vector_swap_halves() {
1352 #[target_feature(enable = "avx2")]
1353 unsafe fn test() {
1354 let v = load([
1355 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
1356 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
1357 ]);
1358 assert_eq!(
1359 unload(v.swap_halves()),
1360 [
1361 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
1362 31, 32, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
1363 16,
1364 ],
1365 );
1366 }
1367 if !is_runnable() {
1368 return;
1369 }
1370 unsafe { test() }
1371 }
1372
1373 #[test]
1374 fn fat_vector_interleave_low_8bit_lanes() {
1375 #[target_feature(enable = "avx2")]
1376 unsafe fn test() {
1377 let v1 = load([
1378 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
1379 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
1380 ]);
1381 let v2 = load([
1382 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
1383 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
1384 63, 64,
1385 ]);
1386 assert_eq!(
1387 unload(v1.interleave_low_8bit_lanes(v2)),
1388 [
1389 1, 33, 2, 34, 3, 35, 4, 36, 5, 37, 6, 38, 7, 39, 8, 40,
1390 17, 49, 18, 50, 19, 51, 20, 52, 21, 53, 22, 54, 23, 55,
1391 24, 56,
1392 ],
1393 );
1394 }
1395 if !is_runnable() {
1396 return;
1397 }
1398 unsafe { test() }
1399 }
1400
1401 #[test]
1402 fn fat_vector_interleave_high_8bit_lanes() {
1403 #[target_feature(enable = "avx2")]
1404 unsafe fn test() {
1405 let v1 = load([
1406 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
1407 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
1408 ]);
1409 let v2 = load([
1410 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
1411 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
1412 63, 64,
1413 ]);
1414 assert_eq!(
1415 unload(v1.interleave_high_8bit_lanes(v2)),
1416 [
1417 9, 41, 10, 42, 11, 43, 12, 44, 13, 45, 14, 46, 15, 47, 16,
1418 48, 25, 57, 26, 58, 27, 59, 28, 60, 29, 61, 30, 62, 31,
1419 63, 32, 64,
1420 ],
1421 );
1422 }
1423 if !is_runnable() {
1424 return;
1425 }
1426 unsafe { test() }
1427 }
1428
1429 #[test]
1430 fn fat_vector_for_each_low_64bit_lane() {
1431 #[target_feature(enable = "avx2")]
1432 unsafe fn test() {
1433 let v1 = load([
1434 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
1435 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14,
1436 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E,
1437 0x1F, 0x20,
1438 ]);
1439 let v2 = load([
1440 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A,
1441 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34,
1442 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E,
1443 0x3F, 0x40,
1444 ]);
1445 let mut lanes = [0u64; 4];
1446 v1.for_each_low_64bit_lane(v2, |i, lane| {
1447 lanes[i] = lane;
1448 None::<()>
1449 });
1450 assert_eq!(
1451 lanes,
1452 [
1453 0x0807060504030201,
1454 0x100F0E0D0C0B0A09,
1455 0x2827262524232221,
1456 0x302F2E2D2C2B2A29
1457 ]
1458 );
1459 }
1460 if !is_runnable() {
1461 return;
1462 }
1463 unsafe { test() }
1464 }
1465 }
1466
1467 #[cfg(all(test, target_arch = "aarch64", target_feature = "neon"))]
1468 mod tests_aarch64_neon {
1469 use core::arch::aarch64::*;
1470
1471 use super::*;
1472
1473 #[target_feature(enable = "neon")]
1474 unsafe fn load(lanes: [u8; 16]) -> uint8x16_t {
1475 uint8x16_t::load_unaligned(&lanes as *const u8)
1476 }
1477
1478 #[target_feature(enable = "neon")]
1479 unsafe fn unload(v: uint8x16_t) -> [u8; 16] {
1480 [
1481 vgetq_lane_u8(v, 0),
1482 vgetq_lane_u8(v, 1),
1483 vgetq_lane_u8(v, 2),
1484 vgetq_lane_u8(v, 3),
1485 vgetq_lane_u8(v, 4),
1486 vgetq_lane_u8(v, 5),
1487 vgetq_lane_u8(v, 6),
1488 vgetq_lane_u8(v, 7),
1489 vgetq_lane_u8(v, 8),
1490 vgetq_lane_u8(v, 9),
1491 vgetq_lane_u8(v, 10),
1492 vgetq_lane_u8(v, 11),
1493 vgetq_lane_u8(v, 12),
1494 vgetq_lane_u8(v, 13),
1495 vgetq_lane_u8(v, 14),
1496 vgetq_lane_u8(v, 15),
1497 ]
1498 }
1499
1500 // Example functions. These don't test the Vector traits, but rather,
1501 // specific NEON instructions. They are basically little experiments I
1502 // wrote to figure out what an instruction does since their descriptions
1503 // are so dense. I decided to keep the experiments around as example tests
1504 // in case there' useful.
1505
1506 #[test]
1507 fn example_vmaxvq_u8_non_zero() {
1508 #[target_feature(enable = "neon")]
1509 unsafe fn example() {
1510 let v = load([0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
1511 assert_eq!(vmaxvq_u8(v), 1);
1512 }
1513 unsafe { example() }
1514 }
1515
1516 #[test]
1517 fn example_vmaxvq_u8_zero() {
1518 #[target_feature(enable = "neon")]
1519 unsafe fn example() {
1520 let v = load([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
1521 assert_eq!(vmaxvq_u8(v), 0);
1522 }
1523 unsafe { example() }
1524 }
1525
1526 #[test]
1527 fn example_vpmaxq_u8_non_zero() {
1528 #[target_feature(enable = "neon")]
1529 unsafe fn example() {
1530 let v = load([0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
1531 let r = vpmaxq_u8(v, v);
1532 assert_eq!(
1533 unload(r),
1534 [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
1535 );
1536 }
1537 unsafe { example() }
1538 }
1539
1540 #[test]
1541 fn example_vpmaxq_u8_self() {
1542 #[target_feature(enable = "neon")]
1543 unsafe fn example() {
1544 let v =
1545 load([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
1546 let r = vpmaxq_u8(v, v);
1547 assert_eq!(
1548 unload(r),
1549 [2, 4, 6, 8, 10, 12, 14, 16, 2, 4, 6, 8, 10, 12, 14, 16]
1550 );
1551 }
1552 unsafe { example() }
1553 }
1554
1555 #[test]
1556 fn example_vpmaxq_u8_other() {
1557 #[target_feature(enable = "neon")]
1558 unsafe fn example() {
1559 let v1 =
1560 load([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
1561 let v2 = load([
1562 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
1563 ]);
1564 let r = vpmaxq_u8(v1, v2);
1565 assert_eq!(
1566 unload(r),
1567 [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32]
1568 );
1569 }
1570 unsafe { example() }
1571 }
1572
1573 // Now we test the actual methods on the Vector trait.
1574
1575 #[test]
1576 fn vector_splat() {
1577 #[target_feature(enable = "neon")]
1578 unsafe fn test() {
1579 let v = uint8x16_t::splat(0xAF);
1580 assert_eq!(
1581 unload(v),
1582 [
1583 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF,
1584 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF
1585 ]
1586 );
1587 }
1588 unsafe { test() }
1589 }
1590
1591 #[test]
1592 fn vector_is_zero() {
1593 #[target_feature(enable = "neon")]
1594 unsafe fn test() {
1595 let v = load([0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
1596 assert!(!v.is_zero());
1597 let v = load([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
1598 assert!(v.is_zero());
1599 }
1600 unsafe { test() }
1601 }
1602
1603 #[test]
1604 fn vector_cmpeq() {
1605 #[target_feature(enable = "neon")]
1606 unsafe fn test() {
1607 let v1 =
1608 load([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1]);
1609 let v2 =
1610 load([16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);
1611 assert_eq!(
1612 unload(v1.cmpeq(v2)),
1613 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF]
1614 );
1615 }
1616 unsafe { test() }
1617 }
1618
1619 #[test]
1620 fn vector_and() {
1621 #[target_feature(enable = "neon")]
1622 unsafe fn test() {
1623 let v1 =
1624 load([0, 0, 0, 0, 0, 0b1001, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
1625 let v2 =
1626 load([0, 0, 0, 0, 0, 0b1010, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
1627 assert_eq!(
1628 unload(v1.and(v2)),
1629 [0, 0, 0, 0, 0, 0b1000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
1630 );
1631 }
1632 unsafe { test() }
1633 }
1634
1635 #[test]
1636 fn vector_or() {
1637 #[target_feature(enable = "neon")]
1638 unsafe fn test() {
1639 let v1 =
1640 load([0, 0, 0, 0, 0, 0b1001, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
1641 let v2 =
1642 load([0, 0, 0, 0, 0, 0b1010, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
1643 assert_eq!(
1644 unload(v1.or(v2)),
1645 [0, 0, 0, 0, 0, 0b1011, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
1646 );
1647 }
1648 unsafe { test() }
1649 }
1650
1651 #[test]
1652 fn vector_shift_8bit_lane_right() {
1653 #[target_feature(enable = "neon")]
1654 unsafe fn test() {
1655 let v = load([
1656 0, 0, 0, 0, 0b1011, 0b0101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1657 ]);
1658 assert_eq!(
1659 unload(v.shift_8bit_lane_right::<2>()),
1660 [0, 0, 0, 0, 0b0010, 0b0001, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
1661 );
1662 }
1663 unsafe { test() }
1664 }
1665
1666 #[test]
1667 fn vector_shift_in_one_byte() {
1668 #[target_feature(enable = "neon")]
1669 unsafe fn test() {
1670 let v1 =
1671 load([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
1672 let v2 = load([
1673 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
1674 ]);
1675 assert_eq!(
1676 unload(v1.shift_in_one_byte(v2)),
1677 [32, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
1678 );
1679 }
1680 unsafe { test() }
1681 }
1682
1683 #[test]
1684 fn vector_shift_in_two_bytes() {
1685 #[target_feature(enable = "neon")]
1686 unsafe fn test() {
1687 let v1 =
1688 load([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
1689 let v2 = load([
1690 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
1691 ]);
1692 assert_eq!(
1693 unload(v1.shift_in_two_bytes(v2)),
1694 [31, 32, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14],
1695 );
1696 }
1697 unsafe { test() }
1698 }
1699
1700 #[test]
1701 fn vector_shift_in_three_bytes() {
1702 #[target_feature(enable = "neon")]
1703 unsafe fn test() {
1704 let v1 =
1705 load([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
1706 let v2 = load([
1707 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
1708 ]);
1709 assert_eq!(
1710 unload(v1.shift_in_three_bytes(v2)),
1711 [30, 31, 32, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13],
1712 );
1713 }
1714 unsafe { test() }
1715 }
1716
1717 #[test]
1718 fn vector_shuffle_bytes() {
1719 #[target_feature(enable = "neon")]
1720 unsafe fn test() {
1721 let v1 =
1722 load([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
1723 let v2 =
1724 load([0, 0, 0, 0, 4, 4, 4, 4, 8, 8, 8, 8, 12, 12, 12, 12]);
1725 assert_eq!(
1726 unload(v1.shuffle_bytes(v2)),
1727 [1, 1, 1, 1, 5, 5, 5, 5, 9, 9, 9, 9, 13, 13, 13, 13],
1728 );
1729 }
1730 unsafe { test() }
1731 }
1732
1733 #[test]
1734 fn vector_for_each_64bit_lane() {
1735 #[target_feature(enable = "neon")]
1736 unsafe fn test() {
1737 let v = load([
1738 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
1739 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
1740 ]);
1741 let mut lanes = [0u64; 2];
1742 v.for_each_64bit_lane(|i, lane| {
1743 lanes[i] = lane;
1744 None::<()>
1745 });
1746 assert_eq!(lanes, [0x0807060504030201, 0x100F0E0D0C0B0A09],);
1747 }
1748 unsafe { test() }
1749 }
1750 }