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) -> Self

Compute the square root of each element.

Negative elements other than -0.0 will become NaN.

fn approximate_recip(self) -> Self

Compute 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 than 1.5 × 2^-12. On AArch64 (f32 and f64), this has a relative error less than 2^-8. The precision of this operation may change as new platform support is added.

fn copysign(self, rhs: impl SimdInto<Self, S>) -> Self

Return a vector with the magnitude of self and the sign of rhs for 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>) -> Self

Compute (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>) -> Self

Compute (self * op1) + op2 for 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_add on 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 NaN payloads as well as signaling NaNs are not preserved.

fn mul_sub(self, op1: impl SimdInto<Self, S>, op2: impl SimdInto<Self, S>) -> Self

Compute (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>) -> Self

Compute (self * op1) - op2 for 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_sub on 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 NaN payloads as well as signaling NaNs are not preserved.

fn floor(self) -> Self

Return the largest integer less than or equal to each element, that is, round towards negative infinity.

fn ceil(self) -> Self

Return the smallest integer greater than or equal to each element, that is, round towards positive infinity.

fn round_ties_even(self) -> Self

Round each element to the nearest integer, with ties rounding to the nearest even integer.

fn round(self) -> Self

Round each element to the nearest integer, with ties rounding away from zero.

Performance considerations

Only AArch64 has native instructions for this operation. round has to be emulated on all other platforms which is around 2-4x slower than round_ties_even. Prefer using round_ties_even if possible.

fn fract(self) -> Self

Return the fractional part of each element.

This is equivalent to self - self.trunc().

fn trunc(self) -> Self

Return the integer part of each element, rounding towards zero.

fn to_degrees(self) -> Self

Converts 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) -> Self

Converts 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::Mask

Return a mask indicating which elements are NaN.

fn is_infinite(self) -> Self::Mask

Return a mask indicating which elements are positive or negative infinite.

fn is_finite(self) -> Self::Mask

Return a mask indicating which elements are neither infinite nor NaN.

fn is_subnormal(self) -> Self::Mask

Return 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::Mask

Return a mask indicating which elements are neither zero, infinite, subnormal, or NaN.

fn is_sign_positive(self) -> Self::Mask

Return 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_positive for details.

fn is_sign_negative(self) -> Self::Mask

Return 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_negative for details.

Provided Methods

fn to_bits(self) -> Self::Bits

Raw transmutation to an unsigned integer vector type with the same size and number of elements.

This is analogous to f32::to_bits and f64::to_bits.

fn from_bits(bits: Self::Bits) -> Self

Raw transmutation from an unsigned integer vector type with the same size and number of elements.

This is analogous to f32::from_bits and f64::from_bits.

fn to_int<T: SimdCvtTruncate<Self>>(self) -> T

Convert 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/i32 for f32, or u64/i64 for f64).

For more information about the semantics of this specific conversion, see the concrete SimdCvtTruncate implementations for integer types.

fn to_int_precise<T: SimdCvtTruncate<Self>>(self) -> T

Convert 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/i32 for f32, or u64/i64 for f64).

For more information about the semantics of this specific conversion, see the concrete SimdCvtTruncate implementations for integer types.

Implementors