// NOTE: The descriptions for each of the vector methods on the traits below // are pretty inscrutable. For this reason, there are tests for every method // on for every trait impl below. If you're confused about what an op does, // consult its test. (They probably should be doc tests, but I couldn't figure // out how to write them in a non-annoying way.)
use core::{
fmt::Debug,
panic::{RefUnwindSafe, UnwindSafe},
};
/// A trait for describing vector operations used by vectorized searchers. /// /// The trait is highly constrained to low level vector operations needed for /// the specific algorithms used in this crate. In general, it was invented /// mostly to be generic over x86's __m128i and __m256i types. At time of /// writing, it also supports wasm and aarch64 128-bit vector types as well. /// /// # Safety /// /// All methods are not safe since they are intended to be implemented using /// vendor intrinsics, which are also not safe. Callers must ensure that /// the appropriate target features are enabled in the calling function, /// and that the current CPU supports them. All implementations should /// avoid marking the routines with `#[target_feature]` and instead mark /// them as `#[inline(always)]` to ensure they get appropriately inlined. /// (`inline(always)` cannot be used with target_feature.) pub(crate) trait Vector:
Copy + Debug + Send + Sync + UnwindSafe + RefUnwindSafe
{ /// The number of bits in the vector. const BITS: usize; /// The number of bytes in the vector. That is, this is the size of the /// vector in memory. const BYTES: usize;
/// Create a vector with 8-bit lanes with the given byte repeated into each /// lane. /// /// # Safety /// /// Callers must ensure that this is okay to call in the current target for /// the current CPU. unsafefn splat(byte: u8) -> Self;
/// Read a vector-size number of bytes from the given pointer. The pointer /// does not need to be aligned. /// /// # Safety /// /// Callers must ensure that this is okay to call in the current target for /// the current CPU. /// /// Callers must guarantee that at least `BYTES` bytes are readable from /// `data`. unsafefn load_unaligned(data: *const u8) -> Self;
/// Returns true if and only if this vector has zero in all of its lanes. /// /// # Safety /// /// Callers must ensure that this is okay to call in the current target for /// the current CPU. unsafefn is_zero(self) -> bool;
/// Do an 8-bit pairwise equality check. If lane `i` is equal in this /// vector and the one given, then lane `i` in the resulting vector is set /// to `0xFF`. Otherwise, it is set to `0x00`. /// /// # Safety /// /// Callers must ensure that this is okay to call in the current target for /// the current CPU. unsafefn cmpeq(self, vector2: Self) -> Self;
/// Perform a bitwise 'and' of this vector and the one given and return /// the result. /// /// # Safety /// /// Callers must ensure that this is okay to call in the current target for /// the current CPU. unsafefn and(self, vector2: Self) -> Self;
/// Perform a bitwise 'or' of this vector and the one given and return /// the result. /// /// # Safety /// /// Callers must ensure that this is okay to call in the current target for /// the current CPU. unsafefn or(self, vector2: Self) -> Self;
/// Shift each 8-bit lane in this vector to the right by the number of /// bits indictated by the `BITS` type parameter. /// /// # Safety /// /// Callers must ensure that this is okay to call in the current target for /// the current CPU. unsafefn shift_8bit_lane_right<const BITS: i32>(self) -> Self;
/// Shift this vector to the left by one byte and shift the most /// significant byte of `vector2` into the least significant position of /// this vector. /// /// Stated differently, this behaves as if `self` and `vector2` were /// concatenated into a `2 * Self::BITS` temporary buffer and then shifted /// right by `Self::BYTES - 1` bytes. /// /// With respect to the Teddy algorithm, `vector2` is usually a previous /// `Self::BYTES` chunk from the haystack and `self` is the chunk /// immediately following it. This permits combining the last two bytes /// from the previous chunk (`vector2`) with the first `Self::BYTES - 1` /// bytes from the current chunk. This permits aligning the result of /// various shuffles so that they can be and-ed together and a possible /// candidate discovered. /// /// # Safety /// /// Callers must ensure that this is okay to call in the current target for /// the current CPU. unsafefn shift_in_one_byte(self, vector2: Self) -> Self;
/// Shift this vector to the left by two bytes and shift the two most /// significant bytes of `vector2` into the least significant position of /// this vector. /// /// Stated differently, this behaves as if `self` and `vector2` were /// concatenated into a `2 * Self::BITS` temporary buffer and then shifted /// right by `Self::BYTES - 2` bytes. /// /// With respect to the Teddy algorithm, `vector2` is usually a previous /// `Self::BYTES` chunk from the haystack and `self` is the chunk /// immediately following it. This permits combining the last two bytes /// from the previous chunk (`vector2`) with the first `Self::BYTES - 2` /// bytes from the current chunk. This permits aligning the result of /// various shuffles so that they can be and-ed together and a possible /// candidate discovered. /// /// # Safety /// /// Callers must ensure that this is okay to call in the current target for /// the current CPU. unsafefn shift_in_two_bytes(self, vector2: Self) -> Self;
/// Shift this vector to the left by three bytes and shift the three most /// significant bytes of `vector2` into the least significant position of /// this vector. /// /// Stated differently, this behaves as if `self` and `vector2` were /// concatenated into a `2 * Self::BITS` temporary buffer and then shifted /// right by `Self::BYTES - 3` bytes. /// /// With respect to the Teddy algorithm, `vector2` is usually a previous /// `Self::BYTES` chunk from the haystack and `self` is the chunk /// immediately following it. This permits combining the last three bytes /// from the previous chunk (`vector2`) with the first `Self::BYTES - 3` /// bytes from the current chunk. This permits aligning the result of /// various shuffles so that they can be and-ed together and a possible /// candidate discovered. /// /// # Safety /// /// Callers must ensure that this is okay to call in the current target for /// the current CPU. unsafefn shift_in_three_bytes(self, vector2: Self) -> Self;
/// Shuffles the bytes in this vector according to the indices in each of /// the corresponding lanes in `indices`. /// /// If `i` is the index of corresponding lanes, `A` is this vector, `B` is /// indices and `C` is the resulting vector, then `C = A[B[i]]`. /// /// # Safety /// /// Callers must ensure that this is okay to call in the current target for /// the current CPU. unsafefn shuffle_bytes(self, indices: Self) -> Self;
/// Call the provided function for each 64-bit lane in this vector. The /// given function is provided the lane index and lane value as a `u64`. /// /// If `f` returns `Some`, then iteration over the lanes is stopped and the /// value is returned. Otherwise, this returns `None`. /// /// # Notes /// /// Conceptually it would be nice if we could have a /// `unpack64(self) -> [u64; BITS / 64]` method, but defining that is /// tricky given Rust's [current support for const generics][support]. /// And even if we could, it would be tricky to write generic code over /// it. (Not impossible. We could introduce another layer that requires /// `AsRef<[u64]>` or something.) /// /// [support]: https://github.com/rust-lang/rust/issues/60551 /// /// # Safety /// /// Callers must ensure that this is okay to call in the current target for /// the current CPU. unsafefn for_each_64bit_lane<T>( self,
f: impl FnMut(usize, u64) -> Option<T>,
) -> Option<T>;
}
/// This trait extends the `Vector` trait with additional operations to support /// Fat Teddy. /// /// Fat Teddy uses 16 buckets instead of 8, but reads half as many bytes (as /// the vector size) instead of the full size of a vector per iteration. For /// example, when using a 256-bit vector, Slim Teddy reads 32 bytes at a timr /// but Fat Teddy reads 16 bytes at a time. /// /// Fat Teddy is useful when searching for a large number of literals. /// The extra number of buckets spreads the literals out more and reduces /// verification time. /// /// Currently we only implement this for AVX on x86_64. It would be nice to /// implement this for SSE on x86_64 and NEON on aarch64, with the latter two /// only reading 8 bytes at a time. It's not clear how well it would work, but /// there are some tricky things to figure out in terms of implementation. The /// `half_shift_in_{one,two,three}_bytes` methods in particular are probably /// the trickiest of the bunch. For AVX2, these are implemented by taking /// advantage of the fact that `_mm256_alignr_epi8` operates on each 128-bit /// half instead of the full 256-bit vector. (Where as `_mm_alignr_epi8` /// operates on the full 128-bit vector and not on each 64-bit half.) I didn't /// do a careful survey of NEON to see if it could easily support these /// operations. pub(crate) trait FatVector: Vector { type Half: Vector;
/// Read a half-vector-size number of bytes from the given pointer, and /// broadcast it across both halfs of a full vector. The pointer does not /// need to be aligned. /// /// # Safety /// /// Callers must ensure that this is okay to call in the current target for /// the current CPU. /// /// Callers must guarantee that at least `Self::HALF::BYTES` bytes are /// readable from `data`. unsafefn load_half_unaligned(data: *const u8) -> Self;
/// Like `Vector::shift_in_one_byte`, except this is done for each half /// of the vector instead. /// /// # Safety /// /// Callers must ensure that this is okay to call in the current target for /// the current CPU. unsafefn half_shift_in_one_byte(self, vector2: Self) -> Self;
/// Like `Vector::shift_in_two_bytes`, except this is done for each half /// of the vector instead. /// /// # Safety /// /// Callers must ensure that this is okay to call in the current target for /// the current CPU. unsafefn half_shift_in_two_bytes(self, vector2: Self) -> Self;
/// Like `Vector::shift_in_two_bytes`, except this is done for each half /// of the vector instead. /// /// # Safety /// /// Callers must ensure that this is okay to call in the current target for /// the current CPU. unsafefn half_shift_in_three_bytes(self, vector2: Self) -> Self;
/// Swap the 128-bit lanes in this vector. /// /// # Safety /// /// Callers must ensure that this is okay to call in the current target for /// the current CPU. unsafefn swap_halves(self) -> Self;
/// Unpack and interleave the 8-bit lanes from the low 128 bits of each /// vector and return the result. /// /// # Safety /// /// Callers must ensure that this is okay to call in the current target for /// the current CPU. unsafefn interleave_low_8bit_lanes(self, vector2: Self) -> Self;
/// Unpack and interleave the 8-bit lanes from the high 128 bits of each /// vector and return the result. /// /// # Safety /// /// Callers must ensure that this is okay to call in the current target for /// the current CPU. unsafefn interleave_high_8bit_lanes(self, vector2: Self) -> Self;
/// Call the provided function for each 64-bit lane in the lower half /// of this vector and then in the other vector. The given function is /// provided the lane index and lane value as a `u64`. (The high 128-bits /// of each vector are ignored.) /// /// If `f` returns `Some`, then iteration over the lanes is stopped and the /// value is returned. Otherwise, this returns `None`. /// /// # Safety /// /// Callers must ensure that this is okay to call in the current target for /// the current CPU. unsafefn for_each_low_64bit_lane<T>( self,
vector2: Self,
f: impl FnMut(usize, u64) -> Option<T>,
) -> Option<T>;
}
#[cfg(all(target_arch = "x86_64", target_feature = "sse2"))] mod x86_64_ssse3 { use core::arch::x86_64::*;
#[inline(always)] unsafefn shift_8bit_lane_right<const BITS: i32>(self) -> Self { // Apparently there is no _mm_srli_epi8, so we emulate it by // shifting 16-bit integers and masking out the high nybble of each // 8-bit lane (since that nybble will contain bits from the low // nybble of the previous lane). let lomask = Self::splat(0xF);
_mm_srli_epi16(self, BITS).and(lomask)
}
#[inline(always)] unsafefn shift_in_one_byte(self, vector2: Self) -> Self { // Credit goes to jneem for figuring this out: // https://github.com/jneem/teddy/blob/9ab5e899ad6ef6911aecd3cf1033f1abe6e1f66c/src/x86/teddy_simd.rs#L145-L184 // // TL;DR avx2's PALIGNR instruction is actually just two 128-bit // PALIGNR instructions, which is not what we want, so we need to // do some extra shuffling. let v = _mm256_permute2x128_si256(vector2, self, 0x21);
_mm256_alignr_epi8(self, v, 15)
}
#[inline(always)] unsafefn shift_in_two_bytes(self, vector2: Self) -> Self { // Credit goes to jneem for figuring this out: // https://github.com/jneem/teddy/blob/9ab5e899ad6ef6911aecd3cf1033f1abe6e1f66c/src/x86/teddy_simd.rs#L145-L184 // // TL;DR avx2's PALIGNR instruction is actually just two 128-bit // PALIGNR instructions, which is not what we want, so we need to // do some extra shuffling. let v = _mm256_permute2x128_si256(vector2, self, 0x21);
_mm256_alignr_epi8(self, v, 14)
}
#[inline(always)] unsafefn shift_in_three_bytes(self, vector2: Self) -> Self { // Credit goes to jneem for figuring this out: // https://github.com/jneem/teddy/blob/9ab5e899ad6ef6911aecd3cf1033f1abe6e1f66c/src/x86/teddy_simd.rs#L145-L184 // // TL;DR avx2's PALIGNR instruction is actually just two 128-bit // PALIGNR instructions, which is not what we want, so we need to // do some extra shuffling. let v = _mm256_permute2x128_si256(vector2, self, 0x21);
_mm256_alignr_epi8(self, v, 13)
}
#[inline(always)] unsafefn for_each_64bit_lane<T>( self, mut f: impl FnMut(usize, u64) -> Option<T>,
) -> Option<T> { // NOTE: At one point in the past, I used transmute to this to // get a [u64; 4], but it turned out to lead to worse codegen IIRC. // I've tried it more recently, and it looks like that's no longer // the case. But since there's no difference, we stick with the // slightly more complicated but transmute-free version. let lane = _mm256_extract_epi64(self, 0).to_bits(); iflet Some(t) = f(0, lane) { return Some(t);
} let lane = _mm256_extract_epi64(self, 1).to_bits(); iflet Some(t) = f(1, lane) { return Some(t);
} let lane = _mm256_extract_epi64(self, 2).to_bits(); iflet Some(t) = f(2, lane) { return Some(t);
} let lane = _mm256_extract_epi64(self, 3).to_bits(); iflet Some(t) = f(3, lane) { return Some(t);
}
None
}
}
#[inline(always)] unsafefn is_zero(self) -> bool { // Could also use vmaxvq_u8. // ... I tried that and couldn't observe any meaningful difference // in benchmarks. let maxes = vreinterpretq_u64_u8(vpmaxq_u8(self, self));
vgetq_lane_u64(maxes, 0) == 0
}
// Example functions. These don't test the Vector traits, but rather, // specific NEON instructions. They are basically little experiments I // wrote to figure out what an instruction does since their descriptions // are so dense. I decided to keep the experiments around as example tests // in case there' useful.
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.