pub trait GetNumOpswhere
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§
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.