Trait SimdFloat
pub trait SimdFloat<S: Simd>: SimdBase<S, Element: SimdFloatElement> + Seal + Neg<Output = Self> + Div<Output = Self> + DivAssign + Div<Self::Element, Output = Self> + DivAssign<Self::Element>
Functionality implemented by floating-point SIMD vectors.
Associated Types
type Bits: SimdInt<S, Element = <Self::Element as SimdFloatElement>::Bits, ByteVector = Self::ByteVector>;Bit representation of this SIMD float vector type.
Required Methods
fn sqrt(self) -> SelfCompute the square root of each element.
Negative elements other than
-0.0will become NaN.fn approximate_recip(self) -> SelfCompute an approximate reciprocal (
1. / x) for each element.This uses a fast hardware estimate where available, and falls back to exact division otherwise.
On x86 for
f32, this has a relative error less than1.5 × 2^-12. OnAArch64(f32andf64), this has a relative error less than2^-8. The precision of this operation may change as new platform support is added.fn copysign(self, rhs: impl SimdInto<Self, S>) -> SelfReturn a vector with the magnitude of
selfand the sign ofrhsfor each element.This operation copies the sign bit, so if an input element is NaN, the output element will be a NaN with the same payload and a copied sign bit.
fn mul_add(self, op1: impl SimdInto<Self, S>, op2: impl SimdInto<Self, S>) -> SelfCompute
(self * op1) + op2(fused multiply-add) for each element.Depending on platform support, the result may be computed with only one rounding error, or may be implemented as a regular multiply followed by an add, which will result in two rounding errors.
fn mul_add_precise(self, op1: impl SimdInto<Self, S>, op2: impl SimdInto<Self, S>) -> SelfCompute
(self * op1) + op2for each element, with a single rounding at the end.The result is the infinite-precision product-plus-add rounded once to the element type. This may be substantially slower than
mul_addon platforms without fused multiply-add instructions, including WebAssembly.Precision guarantees
This function is not susceptible to the bug in Rust standard library and in musl libc that causes incorrect rounding for near-zero values on CPUs without hardware support for this operation.
On the tier-2 i586-* targets, this function is correct if and only if SSE2 is available on the CPU.
The exact
NaNpayloads as well as signalingNaNs are not preserved.fn mul_sub(self, op1: impl SimdInto<Self, S>, op2: impl SimdInto<Self, S>) -> SelfCompute
(self * op1) - op2(fused multiply-subtract) for each element.Depending on platform support, the result may be computed with only one rounding error, or may be implemented as a regular multiply followed by a subtract, which will result in two rounding errors.
fn mul_sub_precise(self, op1: impl SimdInto<Self, S>, op2: impl SimdInto<Self, S>) -> SelfCompute
(self * op1) - op2for each element, with a single rounding at the end.The result is the infinite-precision product-minus-subtrahend rounded once to the element type. This may be substantially slower than
mul_subon platforms without fused multiply-add instructions, including WebAssembly. # Precision guaranteesThis function is not susceptible to the bug in Rust standard library and in musl libc that causes incorrect rounding for near-zero values on CPUs without hardware support for this operation.
On the tier-2 i586-* targets, this function is correct if and only if SSE2 is available on the CPU.
The exact
NaNpayloads as well as signalingNaNs are not preserved.fn floor(self) -> SelfReturn the largest integer less than or equal to each element, that is, round towards negative infinity.
fn ceil(self) -> SelfReturn the smallest integer greater than or equal to each element, that is, round towards positive infinity.
fn round_ties_even(self) -> SelfRound each element to the nearest integer, with ties rounding to the nearest even integer.
fn round(self) -> SelfRound each element to the nearest integer, with ties rounding away from zero.
Performance considerations
Only
AArch64has native instructions for this operation.roundhas to be emulated on all other platforms which is around 2-4x slower thanround_ties_even. Prefer usinground_ties_evenif possible.fn fract(self) -> SelfReturn the fractional part of each element.
This is equivalent to
self - self.trunc().fn trunc(self) -> SelfReturn the integer part of each element, rounding towards zero.
fn to_degrees(self) -> SelfConverts radians to degrees.
The precision of this function is non-deterministic. This means it varies by platform, Rust version, and can even differ within the same execution from one invocation to the next.
fn to_radians(self) -> SelfConverts degrees to radians.
The precision of this function is non-deterministic. This means it varies by platform, Rust version, and can even differ within the same execution from one invocation to the next.
fn is_nan(self) -> Self::MaskReturn a mask indicating which elements are NaN.
fn is_infinite(self) -> Self::MaskReturn a mask indicating which elements are positive or negative infinite.
fn is_finite(self) -> Self::MaskReturn a mask indicating which elements are neither infinite nor NaN.
fn is_subnormal(self) -> Self::MaskReturn a mask indicating which elements are subnormal.
This classifies the stored bit pattern, even when floating-point operations treat subnormals as zero.
fn is_normal(self) -> Self::MaskReturn a mask indicating which elements are neither zero, infinite, subnormal, or NaN.
fn is_sign_positive(self) -> Self::MaskReturn a mask indicating which elements have a positive sign, including
+0.0, NaNs with positive sign bit and positive infinity.NaN inputs may produce an unexpected or non-portable result, see
f32::is_sign_positivefor details.fn is_sign_negative(self) -> Self::MaskReturn a mask indicating which elements have a negative sign, including
-0.0, NaNs with negative sign bit and negative infinity.NaN inputs may produce an unexpected or non-portable result, see
f32::is_sign_negativefor details.
Provided Methods
fn to_bits(self) -> Self::BitsRaw transmutation to an unsigned integer vector type with the same size and number of elements.
This is analogous to
f32::to_bitsandf64::to_bits.fn from_bits(bits: Self::Bits) -> SelfRaw transmutation from an unsigned integer vector type with the same size and number of elements.
This is analogous to
f32::from_bitsandf64::from_bits.fn to_int<T: SimdCvtTruncate<Self>>(self) -> TConvert this floating-point type to an integer. This is a convenience method that delegates to
SimdCvtTruncate::truncate_from, and can only be called if there actually exists a target type of the same bit width (u32/i32forf32, oru64/i64forf64).For more information about the semantics of this specific conversion, see the concrete
SimdCvtTruncateimplementations for integer types.fn to_int_precise<T: SimdCvtTruncate<Self>>(self) -> TConvert this floating-point type to an integer, saturating on overflow and returning 0 for NaN. This is a convenience method that delegates to
SimdCvtTruncate::truncate_from_precise, and can only be called if there actually exists a target type of the same bit width (u32/i32forf32, oru64/i64forf64).For more information about the semantics of this specific conversion, see the concrete
SimdCvtTruncateimplementations for integer types.