Trait SimdBase
pub trait SimdBase<S: Simd>: ExtractToken<S = S> + Copy + Sync + Send + Debug + 'static + Seal + Bytes<Bytes = Self::ByteVector> + SimdFrom<Self::Element, S> + SimdFrom<Self::Array, S> + Index<usize, Output = Self::Element> + IndexMut<usize, Output = Self::Element> + Deref<Target = Self::Array> + DerefMut<Target = Self::Array> + Add<Output = Self> + AddAssign + Add<Self::Element, Output = Self> + AddAssign<Self::Element> + Sub<Output = Self> + SubAssign + Sub<Self::Element, Output = Self> + SubAssign<Self::Element> + Mul<Output = Self> + MulAssign + Mul<Self::Element, Output = Self> + MulAssign<Self::Element>
Base functionality implemented by all SIMD vectors.
Associated Types
type Element: SimdElement;The type of this vector's elements.
type ByteVector: SimdBase<S, Element = u8, ByteVector = Self::ByteVector>;The same-width SIMD vector of
u8lanes used as the byte representation.This is the same type as
Bytes::Bytes.This associated type exists because expressing the
SimdBasebound directly onBytes::Bytescreates a trait-solver cycle. TheBytes<Bytes = Self::ByteVector>supertrait bound ensures that the two types are identical. Generic callers should normally useBytes::Bytes, not this associated type.type Mask: SimdMask<S, Element = <Self::Element as SimdElement>::Mask> + Select<Self>;A SIMD vector mask with the same number of logical lanes.
Masks intentionally do not implement
SimdBase. SSE, NEON, WASM, and the fallback backend currently store masks as all-zero/all-one integer vectors, but AVX-512/RVV/SVE-style targets use compact predicate registers instead.type Block: SimdBase<S, Element = Self::Element, Block = Self::Block>;A 128-bit SIMD vector of the same scalar type.
type Array: Copy + Debug + IntoIterator<Item = Self::Element> + AsRef<[Self::Element]> + AsMut<[Self::Element]> + From<Self>;The array type that this vector type corresponds to, which will always be
[Self::Element; Self::LEN]. It has the same layout as this vector type, but likely has a lower alignment.
Associated Constants
const LEN: usize;This vector type's lane count. This is useful when you're working with a native-width vector (e.g.
Simd::f32s) and want to process data in native-width chunks.
Required Methods
fn as_slice(&self) -> &[Self::Element]fn as_mut_slice(&mut self) -> &mut [Self::Element]fn from_slice(simd: S, slice: &[Self::Element]) -> SelfCreate a SIMD vector from a slice.
The slice must be exactly the size of the SIMD vector.
fn store_slice(&self, slice: &mut [Self::Element])Store a SIMD vector into a slice.
The slice must be exactly the size of the SIMD vector.
fn block_splat(block: Self::Block) -> SelfCreate a SIMD vector from a 128-bit vector of the same scalar type, repeated.
fn from_fn(simd: S, f: impl FnMut(usize) -> Self::Element) -> SelfCreate a SIMD vector where each element is produced by calling
fwith that element's lane index (from 0 toSimdBase::LEN- 1).fn abs(self) -> SelfCompute the absolute value of each element.
Unsigned integers are unchanged. Signed integers use wrapping absolute value: the minimum representable value remains unchanged. This matches
i32::abs().For floating-point elements, clear the sign bit, preserving all other bits. For example, negative zero becomes positive zero.
fn splat(simd: S, val: Self::Element) -> SelfCreate a SIMD vector with all elements set to the given value.
fn reverse(self) -> SelfReverse the order of the vector's elements.
fn slide<const SHIFT: usize>(self, rhs: impl SimdInto<Self, S>) -> SelfConcatenate
[self, rhs]and extractSelf::LENelements starting at indexSHIFT.SHIFTmust be within [0,Self::LEN].This can be used to implement a "shift items" operation by providing all zeroes as one operand. For a left shift, the right-hand side should be all zeroes. For a right shift by
Mitems, the left-hand side should be all zeroes, and the shift amount will beSelf::LEN - M.This can also be used to rotate items within a vector by providing the same vector as both operands.
slide::<1>([a b c d], [e f g h]) == [b c d e]fn slide_within_blocks<const SHIFT: usize>(self, rhs: impl SimdInto<Self, S>) -> SelfLike
slide, but operates independently on each 128-bit block.fn swizzle_dyn_within_blocks(self, indices: impl SimdInto<Self::Bytes, S>) -> SelfDynamically swizzle this vector's bytes independently within each 128-bit block.
The
indicesoperand is a same-width byte vector. For each output byte, index values0..=15select the corresponding byte from the same 128-bit input block.Out-of-range index behavior varies by platform.
fn swizzle_dyn(self, indices: impl SimdInto<Self::Bytes, S>) -> SelfDynamically swizzle this vector's bytes across the whole vector.
The
indicesoperand is a same-width byte vector. For each output byte, index values within the vector's byte length select the corresponding byte from the input vector. Out-of-range indices safely produce implementation-defined byte values.Use
SimdBase::swizzle_dyn_preciseif out-of-range indices must produce zero.fn swizzle_dyn_precise(self, indices: impl SimdInto<Self::Bytes, S>) -> SelfDynamically swizzle this vector's bytes across the whole vector.
The
indicesoperand is a same-width byte vector. For each output byte, index values within the vector's byte length select the corresponding byte from the input vector. Out-of-range indices produce zero.fn concat_swizzle_dyn(self, rhs: impl SimdInto<Self, S>, indices: impl SimdInto<Self::Bytes, S>) -> SelfDynamically select bytes from the concatenation of this vector and
rhs.The
indicesoperand is a same-width byte vector. For each output byte, index values within the concatenated vectors' byte length select the corresponding byte: the first vector comes first, followed byrhs. Out-of-range indices safely produce implementation-defined byte values.Use
SimdBase::concat_swizzle_dyn_preciseif out-of-range indices must produce zero.fn concat_swizzle_dyn_precise(self, rhs: impl SimdInto<Self, S>, indices: impl SimdInto<Self::Bytes, S>) -> SelfDynamically select bytes from the concatenation of this vector and
rhs.The
indicesoperand is a same-width byte vector. For each output byte, index values within the concatenated vectors' byte length select the corresponding byte: the first vector comes first, followed byrhs. Out-of-range indices produce zero.fn compress(self, mask: Self::Mask) -> SelfCompact the elements selected by
maskinto consecutive low lanes, preserving their order.Lanes above the number of selected elements are zero (positive zero for floats).
Elements are moved without changing their bits, including floating-point NaN payloads and signed zeros.
This is equivalent to
SimdBase::compress_mergewith a zeroed merge vector.fn compress_merge(self, mask: Self::Mask, merge: impl SimdInto<Self, S>) -> SelfCompact the elements selected by
maskinto consecutive low lanes, preserving their order.Lanes above the number of selected elements retain the corresponding values from
merge.Elements are moved without changing their bits, including floating-point NaN payloads and signed zeros.
Pseudo code
let mut result = merge; let mut k = 0; for i in 0..LEN { if mask[i] { result[k] = self[i]; k += 1; } } return result;fn expand(self, mask: Self::Mask) -> SelfExpand consecutive low elements from
selfinto the lanes selected bymask, preserving their order.Unselected lanes are zero (positive zero for floats).
Elements are moved without changing their bits, including floating-point NaN payloads and signed zeros..
This is equivalent to
SimdBase::expand_mergewith a zeroed merge vector.fn expand_merge(self, mask: Self::Mask, merge: impl SimdInto<Self, S>) -> SelfExpand consecutive low elements from
selfinto the lanes selected bymask, preserving their order.Unselected lanes retain the corresponding values from
merge.Elements are moved without changing their bits, including floating-point NaN payloads and signed zeros.
Pseudo code
let mut result = merge; let mut k = 0; for i in 0..LEN { if mask[i] { result[i] = self[k]; k += 1; } } return result;fn reduce_max(self) -> Self::ElementReturn the maximum element in the vector. Integer vectors always return the exact maximum.
For floating-point vectors with no NaNs, this returns the true maximum. If any lane is NaN, the entire result is implementation-defined: it may be NaN or a numeric lane that is not the true maximum. See
reduce_max_precisefor a version that ignores quiet NaNs.If the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned.
fn reduce_min(self) -> Self::ElementReturn the minimum element in the vector. Integer vectors always return the exact minimum.
For floating-point vectors with no NaNs, this returns the true minimum. If any lane is NaN, the entire result is implementation-defined: it may be NaN or a numeric lane that is not the true minimum. See
reduce_min_precisefor a version that ignores quiet NaNs.If the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned.
fn reduce_max_precise(self) -> Self::ElementReturn the maximum element in the vector, ignoring quiet NaNs.
For integer vectors, this operation is the same as
reduce_max.For floating-point vectors, quiet NaNs are ignored. If there is at least one numeric lane, this returns the true maximum of the numeric lanes. If all lanes are quiet NaNs, this returns NaN, with an unspecified payload and sign.
If the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned.
If any lane is a signaling NaN, the result is fully non-deterministic: it may be NaN or a numeric lane and is not guaranteed to be the true maximum. Signaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them.
fn reduce_min_precise(self) -> Self::ElementReturn the minimum element in the vector, ignoring quiet NaNs.
For integer vectors, this operation is the same as
reduce_min.For floating-point vectors, quiet NaNs are ignored. If there is at least one numeric lane, this returns the true minimum of the numeric lanes. If all lanes are quiet NaNs, this returns NaN, with an unspecified payload and sign.
If the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned.
If any lane is a signaling NaN, the result is fully non-deterministic: it may be NaN or a numeric lane and is not guaranteed to be the true minimum. Signaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them.
fn reduce_sum(self) -> Self::ElementReturn the sum of all elements in the vector. Integer addition wraps.
Floating-point accuracy
For an input vector with N lanes, any lane's contribution may be rounded at most
log2(N)times.For a fixed vector type and lane count, this operation produces the same result on all platforms and backends down to the bit pattern, except that when the result is NaN, its exact bit pattern is unspecified. This fixed-width guarantee does not make code using native-width associated types such as
S::f32sindependent of the selected SIMD level, because their lane counts can differ.Because floating-point addition is not associative, separately reducing smaller vectors and then adding their results can differ from reducing their combined wider vector. See Taming Floating-Point Sums for more information and for other summation algorithms, including exact summation without accumulated rounding error. In that article's terms, our method has the precision properties of pairwise summation, although the exact pairing of values is different.
fn reduce_product(self) -> Self::ElementReturn the product of all elements in the vector. Integer multiplication wraps.
Floating-point behavior
For a vector with N elements, this operation performs N-1 roundings.
For a given vector type and lane count, this operation produces the same result on all platforms and backends down to the bit pattern, except that when the result is NaN, its exact bit pattern is unspecified. This fixed-width guarantee does not make code using native-width associated types such as
S::f32sindependent of the selected SIMD level, because their lane counts can differ.The result of this operation is not bit-exact to scalar product of the elements because it multiplies elements in a different (but fixed) order.
Intermediate operations can overflow, underflow, or multiply infinity by zero to produce NaN even when the exact real-number product is representable.
Because floating-point multiplication is not associative, separately reducing smaller vectors and then multiplying their results can differ from reducing their combined wider vector.
fn max(self, rhs: impl SimdInto<Self, S>) -> SelfReturn the element-wise maximum of two vectors.
For floating-point vectors, if either operand is NaN, the result for that lane is implementation-defined-- it could be either the first or second operand. See
max_precisefor a version that returns the non-NaN operand if only one is NaN.If one floating-point operand is positive zero and the other is negative zero, the result is also implementation-defined, and it could be either one.
fn min(self, rhs: impl SimdInto<Self, S>) -> SelfReturn the element-wise minimum of two vectors.
For floating-point vectors, if either operand is NaN, the result for that lane is implementation-defined-- it could be either the first or second operand. See
min_precisefor a version that returns the non-NaN operand if only one is NaN.If one floating-point operand is positive zero and the other is negative zero, the result is also implementation-defined, and it could be either one.
fn max_precise(self, rhs: impl SimdInto<Self, S>) -> SelfReturn the element-wise maximum of two vectors.
For integer vectors, this operation is the same as
max.For floating-point vectors, if one operand is a quiet NaN and the other is not, this operation will choose the non-NaN operand.
If one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one.
If a floating-point operand is a signaling NaN, the result is not just implementation-defined, but fully non-deterministic: it may be either NaN or the non-NaN operand. Signaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them.
fn min_precise(self, rhs: impl SimdInto<Self, S>) -> SelfReturn the element-wise minimum of two vectors.
For integer vectors, this operation is the same as
min.For floating-point vectors, if one operand is a quiet NaN and the other is not, this operation will choose the non-NaN operand.
If one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one.
If a floating-point operand is a signaling NaN, the result is not just implementation-defined, but fully non-deterministic: it may be either NaN or the non-NaN operand. Signaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them.
fn clamp(self, min: impl SimdInto<Self, S>, max: impl SimdInto<Self, S>) -> SelfClamp each element to the interval
[min, max].If
min > max, orminormaxare NaN, the result is implementation-defined for that lane.If
selfis NaN, the result is an implementation-defined non-deterministic NaN value. I.e. the result of the lane will be a NaN value but its exact bit pattern is not specified.If one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one.
Panics
Panics with debug assertions if
min > maxfor any element or any element ofminormaxis NaN.fn simd_eq(self, rhs: impl SimdInto<Self, S>) -> Self::MaskCompare two vectors element-wise for equality.
Returns a mask where each logical lane is true if the corresponding elements are equal, and false if not.
fn simd_ne(self, rhs: impl SimdInto<Self, S>) -> Self::MaskCompare two vectors element-wise for inequality.
Returns a mask where each logical lane is true if the corresponding elements are unequal, and false if equal.
For floating-point vectors, this follows Rust's
!=semantics: a lane is true if either operand is NaN, including when both are NaN. Positive and negative zero compare equal.This is equivalent to negating the mask returned by
simd_eq.fn simd_lt(self, rhs: impl SimdInto<Self, S>) -> Self::MaskCompare two vectors element-wise for less than.
Returns a mask where each logical lane is true if
selfis less thanrhs, and false if not.fn simd_le(self, rhs: impl SimdInto<Self, S>) -> Self::MaskCompare two vectors element-wise for less than or equal.
Returns a mask where each logical lane is true if
selfis less than or equal torhs, and false if not.fn simd_ge(self, rhs: impl SimdInto<Self, S>) -> Self::MaskCompare two vectors element-wise for greater than or equal.
Returns a mask where each logical lane is true if
selfis greater than or equal torhs, and false if not.fn simd_gt(self, rhs: impl SimdInto<Self, S>) -> Self::MaskCompare two vectors element-wise for greater than.
Returns a mask where each logical lane is true if
selfis greater thanrhs, and false if not.fn zip_low(self, rhs: impl SimdInto<Self, S>) -> SelfInterleave the lower half elements of two vectors.
For vectors
[a0, a1, a2, a3]and[b0, b1, b2, b3], returns[a0, b0, a1, b1].Note: This operation is only useful if you need to discard elements
a2, a3, b2, b3. For fully interleaving two vectors preferinterleave, which is faster thanzip_lowfollowed byzip_highon some platforms.fn zip_high(self, rhs: impl SimdInto<Self, S>) -> SelfInterleave the upper half elements of two vectors.
For vectors
[a0, a1, a2, a3]and[b0, b1, b2, b3], returns[a2, b2, a3, b3].Note: This operation is only useful if you need to discard elements
a0, a1, b0, b1.For fully interleaving two vectors preferinterleave, which is faster thanzip_lowfollowed byzip_highon some platforms.fn unzip_low(self, rhs: impl SimdInto<Self, S>) -> SelfExtract even-indexed elements from two vectors.
For vectors
[a0, a1, a2, a3]and[b0, b1, b2, b3], returns[a0, a2, b0, b2].Note: This operation is only useful if you need to discard elements
a1, a3, b1, b3.For fully deinterleaving two vectors preferdeinterleave, which is faster thanunzip_lowfollowed byunzip_highon some platforms.fn unzip_high(self, rhs: impl SimdInto<Self, S>) -> SelfExtract odd-indexed elements from two vectors.
For vectors
[a0, a1, a2, a3]and[b0, b1, b2, b3], returns[a1, a3, b1, b3].Note: This operation is only useful if you need to discard elements
a0, a2, b0, b2.For fully deinterleaving two vectors preferdeinterleave, which is faster thanunzip_lowfollowed byunzip_highon some platforms.fn interleave(self, rhs: impl SimdInto<Self, S>) -> (Self, Self)Interleave two vectors.
The resulting vectors contain elements taken alternately from
selfandrhs, first filling the first result, and then the second.The reverse of this operation is
deinterleave.For vectors
[a0, a1, a2, a3]and[b0, b1, b2, b3], returns([a0, b0, a1, b1], [a2, b2, a3, b3]).fn deinterleave(self, rhs: impl SimdInto<Self, S>) -> (Self, Self)Deinterleave two vectors.
The first result contains all even-indexed elements from
selffollowed by all even-indexed elements fromrhs. The second result contains all odd-indexed elements fromselffollowed by all odd-indexed elements fromrhs.The reverse of this operation is
interleave.For vectors
[a0, b0, a1, b1]and[a2, b2, a3, b3], returns([a0, a1, a2, a3], [b0, b1, b2, b3]).
Provided Methods
fn load_array(simd: S, val: Self::Array) -> SelfCreate a SIMD vector from its corresponding lane array.
fn load_array_ref(simd: S, val: &Self::Array) -> SelfCreate a SIMD vector from a reference to its corresponding lane array.
fn to_array(self) -> Self::ArrayConvert this SIMD vector to its corresponding lane array.
fn as_array(&self) -> &Self::ArrayProject this SIMD vector reference to its corresponding lane array reference.
fn as_mut_array(&mut self) -> &mut Self::ArrayProject this mutable SIMD vector reference to its corresponding mutable lane array reference.
fn store_array(self, dest: &mut Self::Array)Store this SIMD vector into its corresponding lane array.
fn rotate_elements_left<const OFFSET: usize>(self) -> SelfRotate the vector elements to the left by
OFFSET.If
OFFSETis greater than or equal toSelf::LEN, it wraps moduloSelf::LEN.fn rotate_elements_right<const OFFSET: usize>(self) -> SelfRotate the vector elements to the right by
OFFSET.If
OFFSETis greater than or equal toSelf::LEN, it wraps moduloSelf::LEN.fn shift_elements_left<const OFFSET: usize>(self, padding: Self::Element) -> SelfShift the vector elements to the left by
OFFSET, filling in withpaddingfrom the right.If
OFFSETis greater than or equal toSelf::LEN, all lanes are filled withpadding.fn shift_elements_right<const OFFSET: usize>(self, padding: Self::Element) -> SelfShift the vector elements to the right by
OFFSET, filling in withpaddingfrom the left.If
OFFSETis greater than or equal toSelf::LEN, all lanes are filled withpadding.
Implementors
impl<S: Simd> SimdBase<S> for f32x16<S>impl<S: Simd> SimdBase<S> for f32x4<S>impl<S: Simd> SimdBase<S> for f32x8<S>impl<S: Simd> SimdBase<S> for f64x2<S>impl<S: Simd> SimdBase<S> for f64x4<S>impl<S: Simd> SimdBase<S> for f64x8<S>impl<S: Simd> SimdBase<S> for i16x16<S>impl<S: Simd> SimdBase<S> for i16x32<S>impl<S: Simd> SimdBase<S> for i16x8<S>impl<S: Simd> SimdBase<S> for i32x16<S>impl<S: Simd> SimdBase<S> for i32x4<S>impl<S: Simd> SimdBase<S> for i32x8<S>impl<S: Simd> SimdBase<S> for i64x2<S>impl<S: Simd> SimdBase<S> for i64x4<S>impl<S: Simd> SimdBase<S> for i64x8<S>impl<S: Simd> SimdBase<S> for i8x16<S>impl<S: Simd> SimdBase<S> for i8x32<S>impl<S: Simd> SimdBase<S> for i8x64<S>impl<S: Simd> SimdBase<S> for u16x16<S>impl<S: Simd> SimdBase<S> for u16x32<S>impl<S: Simd> SimdBase<S> for u16x8<S>impl<S: Simd> SimdBase<S> for u32x16<S>impl<S: Simd> SimdBase<S> for u32x4<S>impl<S: Simd> SimdBase<S> for u32x8<S>impl<S: Simd> SimdBase<S> for u64x2<S>impl<S: Simd> SimdBase<S> for u64x4<S>impl<S: Simd> SimdBase<S> for u64x8<S>impl<S: Simd> SimdBase<S> for u8x16<S>impl<S: Simd> SimdBase<S> for u8x32<S>impl<S: Simd> SimdBase<S> for u8x64<S>