GetNumOps

Trait GetNumOps 

Source
pub trait GetNumOps
where Self: GetSimd + 'static,
{ // Required method fn num_ops<I: Isa>(isa: I) -> impl NumOps<Self, Simd = Self::Simd<I>>; }
Expand description

Get the NumOps implementation from an Isa for a given element type.

This trait is useful for writing SIMD operations which are generic over the element type. It is implemented for all of the element types supported in SIMD vectors.

§Example

This example shows how to use GetNumOps to write a vectorized Sum operation.

use rten_simd::{Isa, SimdIterable, SimdOp};
use rten_simd::ops::{GetNumOps, NumOps};

struct Sum<'a, T>(&'a [T]);

impl<T: std::ops::Add<Output=T> + GetNumOps> SimdOp for Sum<'_, T> {
  type Output = T;
   
  #[inline(always)]
  fn eval<I: Isa>(self, isa: I) -> Self::Output {
    let ops = T::num_ops(isa);

    // Build `ops.len()` partial sums in parallel. If the slice length is
    // not a multiple of `ops.len()` it will be padded with zeros.
    let mut sum = ops.zero();
    for chunk in self.0.simd_iter_pad(ops) {
        sum = ops.add(sum, chunk);
    }

    // Horizontally reduce the SIMD vector containing partial sums to a
    // single value.
    ops.sum(sum)
  }
}

let vals: Vec<_> = (1..20i32).collect();
let sum = Sum(&vals).dispatch();
assert_eq!(sum, vals.iter().sum());

Required Methods§

Source

fn num_ops<I: Isa>(isa: I) -> impl NumOps<Self, Simd = Self::Simd<I>>

Return the NumOps implementation from a SIMD Isa that provides operations on vectors containing elements of type Self.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl GetNumOps for f32

Source§

fn num_ops<I: Isa>( isa: I, ) -> impl NumOps<Self, Simd = <Self as GetSimd>::Simd<I>>

Source§

impl GetNumOps for i8

Source§

fn num_ops<I: Isa>( isa: I, ) -> impl NumOps<Self, Simd = <Self as GetSimd>::Simd<I>>

Source§

impl GetNumOps for i16

Source§

fn num_ops<I: Isa>( isa: I, ) -> impl NumOps<Self, Simd = <Self as GetSimd>::Simd<I>>

Source§

impl GetNumOps for i32

Source§

fn num_ops<I: Isa>( isa: I, ) -> impl NumOps<Self, Simd = <Self as GetSimd>::Simd<I>>

Source§

impl GetNumOps for u8

Source§

fn num_ops<I: Isa>( isa: I, ) -> impl NumOps<Self, Simd = <Self as GetSimd>::Simd<I>>

Source§

impl GetNumOps for u16

Source§

fn num_ops<I: Isa>( isa: I, ) -> impl NumOps<Self, Simd = <Self as GetSimd>::Simd<I>>

Implementors§