Trait SimdInt

pub trait SimdInt: Copy

Operations on SIMD vectors of signed integers.

Associated Types

type Mask;

Mask type used for manipulating this SIMD vector type.

type Scalar;

Scalar type contained by this SIMD vector type.

type Unsigned;

A SIMD vector of unsigned integers with the same element size.

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 casting integers (wrapping to other integer types, and saturating to float types).

fn saturating_add(self, second: Self) -> Self

Lanewise saturating add.

Examples

# #![feature(portable_simd)]
# #[cfg(feature = "as_crate")] use core_simd::simd;
# #[cfg(not(feature = "as_crate"))] use core::simd;
# use simd::prelude::*;
use core::i32::{MIN, MAX};
let x = Simd::from_array([MIN, 0, 1, MAX]);
let max = Simd::splat(MAX);
let unsat = x + max;
let sat = x.saturating_add(max);
assert_eq!(unsat, Simd::from_array([-1, MAX, MIN, -2]));
assert_eq!(sat, Simd::from_array([-1, MAX, MAX, MAX]));
fn saturating_sub(self, second: Self) -> Self

Lanewise saturating subtract.

Examples

# #![feature(portable_simd)]
# #[cfg(feature = "as_crate")] use core_simd::simd;
# #[cfg(not(feature = "as_crate"))] use core::simd;
# use simd::prelude::*;
use core::i32::{MIN, MAX};
let x = Simd::from_array([MIN, -2, -1, MAX]);
let max = Simd::splat(MAX);
let unsat = x - max;
let sat = x.saturating_sub(max);
assert_eq!(unsat, Simd::from_array([1, MAX, MIN, 0]));
assert_eq!(sat, Simd::from_array([MIN, MIN, MIN, 0]));
fn abs(self) -> Self

Lanewise absolute value, implemented in Rust. Every element becomes its absolute value.

Examples

# #![feature(portable_simd)]
# #[cfg(feature = "as_crate")] use core_simd::simd;
# #[cfg(not(feature = "as_crate"))] use core::simd;
# use simd::prelude::*;
use core::i32::{MIN, MAX};
let xs = Simd::from_array([MIN, MIN + 1, -5, 0]);
assert_eq!(xs.abs(), Simd::from_array([MIN, MAX, 5, 0]));
fn abs_diff(self, second: Self) -> Self::Unsigned

Lanewise absolute difference. Every element becomes the absolute difference of self and second.

Examples

# #![feature(portable_simd)]
# #[cfg(feature = "as_crate")] use core_simd::simd;
# #[cfg(not(feature = "as_crate"))] use core::simd;
# use simd::prelude::*;
use core::i32::{MIN, MAX};
let a = Simd::from_array([MIN, MAX, 100, -100]);
let b = Simd::from_array([MAX, MIN, -80, -120]);
assert_eq!(a.abs_diff(b), Simd::from_array([u32::MAX, u32::MAX, 180, 20]));
fn saturating_abs(self) -> Self

Lanewise saturating absolute value, implemented in Rust. As abs(), except the MIN value becomes MAX instead of itself.

Examples

# #![feature(portable_simd)]
# #[cfg(feature = "as_crate")] use core_simd::simd;
# #[cfg(not(feature = "as_crate"))] use core::simd;
# use simd::prelude::*;
use core::i32::{MIN, MAX};
let xs = Simd::from_array([MIN, -2, 0, 3]);
let unsat = xs.abs();
let sat = xs.saturating_abs();
assert_eq!(unsat, Simd::from_array([MIN, 2, 0, 3]));
assert_eq!(sat, Simd::from_array([MAX, 2, 0, 3]));
fn saturating_neg(self) -> Self

Lanewise saturating negation, implemented in Rust. As neg(), except the MIN value becomes MAX instead of itself.

Examples

# #![feature(portable_simd)]
# #[cfg(feature = "as_crate")] use core_simd::simd;
# #[cfg(not(feature = "as_crate"))] use core::simd;
# use simd::prelude::*;
use core::i32::{MIN, MAX};
let x = Simd::from_array([MIN, -2, 3, MAX]);
let unsat = -x;
let sat = x.saturating_neg();
assert_eq!(unsat, Simd::from_array([MIN, 2, -3, MIN + 1]));
assert_eq!(sat, Simd::from_array([MAX, 2, -3, MIN + 1]));
fn is_positive(self) -> Self::Mask

Returns true for each positive element and false if it is zero or negative.

fn is_negative(self) -> Self::Mask

Returns true for each negative element and false if it is zero or positive.

fn signum(self) -> Self

Returns numbers representing the sign of each element.

  • 0 if the number is zero
  • 1 if the number is positive
  • -1 if the number is negative
fn reduce_sum(self) -> Self::Scalar

Returns the sum of the elements of the vector, with wrapping addition.

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 = i32x4::from_array([1, 2, 3, 4]);
assert_eq!(v.reduce_sum(), 10);

// SIMD integer addition is always wrapping
let v = i32x4::from_array([i32::MAX, 1, 0, 0]);
assert_eq!(v.reduce_sum(), i32::MIN);
fn reduce_product(self) -> Self::Scalar

Returns the product of the elements of the vector, with wrapping multiplication.

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 = i32x4::from_array([1, 2, 3, 4]);
assert_eq!(v.reduce_product(), 24);

// SIMD integer multiplication is always wrapping
let v = i32x4::from_array([i32::MAX, 2, 1, 1]);
assert!(v.reduce_product() < i32::MAX);
fn reduce_max(self) -> Self::Scalar

Returns the maximum element in 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 = i32x4::from_array([1, 2, 3, 4]);
assert_eq!(v.reduce_max(), 4);
fn reduce_min(self) -> Self::Scalar

Returns the minimum element in 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 = i32x4::from_array([1, 2, 3, 4]);
assert_eq!(v.reduce_min(), 1);
fn reduce_and(self) -> Self::Scalar

Returns the cumulative bitwise "and" across the elements of the vector.

fn reduce_or(self) -> Self::Scalar

Returns the cumulative bitwise "or" across the elements of the vector.

fn reduce_xor(self) -> Self::Scalar

Returns the cumulative bitwise "xor" across the elements of the vector.

fn swap_bytes(self) -> Self

Reverses the byte order of each element.

fn reverse_bits(self) -> Self

Reverses the order of bits in each elemnent. The least significant bit becomes the most significant bit, second least-significant bit becomes second most-significant bit, etc.

fn count_ones(self) -> Self::Unsigned

Returns the number of ones in the binary representation of each element.

fn count_zeros(self) -> Self::Unsigned

Returns the number of zeros in the binary representation of each element.

fn leading_zeros(self) -> Self::Unsigned

Returns the number of leading zeros in the binary representation of each element.

fn trailing_zeros(self) -> Self::Unsigned

Returns the number of trailing zeros in the binary representation of each element.

fn leading_ones(self) -> Self::Unsigned

Returns the number of leading ones in the binary representation of each element.

fn trailing_ones(self) -> Self::Unsigned

Returns the number of trailing ones in the binary representation of each element.

Implementors