Trait SimdFloat

pub trait SimdFloat: Copy

Operations on SIMD vectors of floats.

Associated Types

type Mask;

Mask type used for manipulating this SIMD vector type.

type Scalar;

Scalar type contained by this SIMD vector type.

type Bits;

Bit representation of this SIMD vector type.

type Cast<T: SimdElement>;

A SIMD vector with a different element type.

Required Methods

fn cast<T: SimdCast>(self) -> Self::Cast<T>

Performs elementwise conversion of this vector's elements to another SIMD-valid type.

This follows the semantics of Rust's as conversion for floats (truncating or saturating at the limits) for each element.

Example

# #![feature(portable_simd)]
# #[cfg(feature = "as_crate")] use core_simd::simd;
# #[cfg(not(feature = "as_crate"))] use core::simd;
# use simd::prelude::*;
let floats: Simd<f32, 4> = Simd::from_array([1.9, -4.5, f32::INFINITY, f32::NAN]);
let ints = floats.cast::<i32>();
assert_eq!(ints, Simd::from_array([1, -4, i32::MAX, 0]));

// Formally equivalent, but `Simd::cast` can optimize better.
assert_eq!(ints, Simd::from_array(floats.to_array().map(|x| x as i32)));

// The float conversion does not round-trip.
let floats_again = ints.cast();
assert_ne!(floats, floats_again);
assert_eq!(floats_again, Simd::from_array([1.0, -4.0, 2147483647.0, 0.0]));
unsafe fn to_int_unchecked<I: SimdCast>(self) -> Self::Cast<I>
where
    Self::Scalar: FloatToInt<I>,

Rounds toward zero and converts to the same-width integer type, assuming that the value is finite and fits in that type.

Safety

The value must:

  • Not be NaN
  • Not be infinite
  • Be representable in the return type, after truncating off its fractional part

If these requirements are infeasible or costly, consider using the safe function cast, which saturates on conversion.

fn to_bits(self) -> Self::Bits

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

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

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

fn abs(self) -> Self

Produces a vector where every element has the absolute value of the equivalently-indexed element in self.

fn recip(self) -> Self

Takes the reciprocal (inverse) of each element, 1/x.

fn to_degrees(self) -> Self

Converts each element from radians to degrees.

fn to_radians(self) -> Self

Converts each element from degrees to radians.

fn is_sign_positive(self) -> Self::Mask

Returns true for each element if it has a positive sign, including +0.0, NaNs with positive sign bit and positive infinity.

fn is_sign_negative(self) -> Self::Mask

Returns true for each element if it has a negative sign, including -0.0, NaNs with negative sign bit and negative infinity.

fn is_nan(self) -> Self::Mask

Returns true for each element if its value is NaN.

fn is_infinite(self) -> Self::Mask

Returns true for each element if its value is positive infinity or negative infinity.

fn is_finite(self) -> Self::Mask

Returns true for each element if its value is neither infinite nor NaN.

fn is_subnormal(self) -> Self::Mask

Returns true for each element if its value is subnormal.

fn is_normal(self) -> Self::Mask

Returns true for each element if its value is neither zero, infinite, subnormal, nor NaN.

fn signum(self) -> Self

Replaces each element with a number that represents its sign.

  • 1.0 if the number is positive, +0.0, or INFINITY
  • -1.0 if the number is negative, -0.0, or NEG_INFINITY
  • NAN if the number is NAN
fn copysign(self, sign: Self) -> Self

Returns each element with the magnitude of self and the sign of sign.

For any element containing a NAN, a NAN with the sign of sign is returned.

fn simd_min(self, other: Self) -> Self

Returns the minimum of each element.

If one of the values is NAN, then the other value is returned.

fn simd_max(self, other: Self) -> Self

Returns the maximum of each element.

If one of the values is NAN, then the other value is returned.

fn simd_clamp(self, min: Self, max: Self) -> Self

Restrict each element to a certain interval unless it is NaN.

For each element in self, returns the corresponding element in max if the element is greater than max, and the corresponding element in min if the element is less than min. Otherwise returns the element in self.

fn reduce_sum(self) -> Self::Scalar

Returns the sum of the elements of the vector.

Examples

# #![feature(portable_simd)]
# #[cfg(feature = "as_crate")] use core_simd::simd;
# #[cfg(not(feature = "as_crate"))] use core::simd;
# use simd::prelude::*;
let v = f32x2::from_array([1., 2.]);
assert_eq!(v.reduce_sum(), 3.);
fn reduce_product(self) -> Self::Scalar

Reducing multiply. Returns the product of the elements of the vector.

Examples

# #![feature(portable_simd)]
# #[cfg(feature = "as_crate")] use core_simd::simd;
# #[cfg(not(feature = "as_crate"))] use core::simd;
# use simd::prelude::*;
let v = f32x2::from_array([3., 4.]);
assert_eq!(v.reduce_product(), 12.);
fn reduce_max(self) -> Self::Scalar

Returns the maximum element in the vector.

Returns values based on equality, so a vector containing both 0. and -0. may return either.

This function will not return NaN unless all elements are NaN.

Examples

# #![feature(portable_simd)]
# #[cfg(feature = "as_crate")] use core_simd::simd;
# #[cfg(not(feature = "as_crate"))] use core::simd;
# use simd::prelude::*;
let v = f32x2::from_array([1., 2.]);
assert_eq!(v.reduce_max(), 2.);

// NaN values are skipped...
let v = f32x2::from_array([1., f32::NAN]);
assert_eq!(v.reduce_max(), 1.);

// ...unless all values are NaN
let v = f32x2::from_array([f32::NAN, f32::NAN]);
assert!(v.reduce_max().is_nan());
fn reduce_min(self) -> Self::Scalar

Returns the minimum element in the vector.

Returns values based on equality, so a vector containing both 0. and -0. may return either.

This function will not return NaN unless all elements are NaN.

Examples

# #![feature(portable_simd)]
# #[cfg(feature = "as_crate")] use core_simd::simd;
# #[cfg(not(feature = "as_crate"))] use core::simd;
# use simd::prelude::*;
let v = f32x2::from_array([3., 7.]);
assert_eq!(v.reduce_min(), 3.);

// NaN values are skipped...
let v = f32x2::from_array([1., f32::NAN]);
assert_eq!(v.reduce_min(), 1.);

// ...unless all values are NaN
let v = f32x2::from_array([f32::NAN, f32::NAN]);
assert!(v.reduce_min().is_nan());

Implementors