Skip to main content

hermes_simd/dispatch/
binary.rs

1//! Generic runtime-dispatch elementwise binary kernel.
2//!
3//! A single SIMD loop (`SimdView::zip_into`) parameterized by an `ElementOp` ZST
4//! marker (`Add`/`Sub`/`Mul`/`Div`) — the SIMD-effect SSOT for elementwise binary
5//! operations. Each `SimdOps::elementwise_*` method selects the marker at the call
6//! site; monomorphization erases the ZST and emits one specialized kernel per
7//! `(T, Op, Arch)`.
8
9use hermes_simd_core::{
10    align::Unaligned,
11    arch::SimdArch,
12    execution::Unmasked,
13    kernel::SimdKernel,
14    scalar::Scalar,
15    view::{SimdError, SimdView},
16    ElementOp,
17};
18use hermes_simd_macros::runtime_dispatch;
19
20#[runtime_dispatch(avx512f, avx2, neon, scalar)]
21pub(super) fn dispatch_elementwise_binary_kernel<T, Op, A>(
22    a: &[T],
23    b: &[T],
24    out: &mut [T],
25    op: Op,
26) -> Result<(), SimdError>
27where
28    T: Scalar,
29    Op: ElementOp<T>,
30    A: SimdArch + SimdKernel<T>,
31{
32    match (
33        SimdView::<T, A, Unaligned, Unmasked, &[T]>::new(a),
34        SimdView::<T, A, Unaligned, Unmasked, &[T]>::new(b),
35    ) {
36        (Some(v1), Some(v2)) => v1.zip_into(&v2, out, op),
37        _ => unsafe { core::hint::unreachable_unchecked() },
38    }
39}