Skip to main content

hermes_simd/dispatch/
popcount.rs

1//! Generic runtime-dispatch population count and bitwise reduction kernels.
2#![allow(missing_docs)]
3
4use hermes_simd_core::{
5    align::Unaligned,
6    arch::SimdArch,
7    execution::Unmasked,
8    kernel::SimdKernel,
9    scalar::Scalar,
10    view::{SimdError, SimdView},
11};
12use hermes_simd_macros::runtime_dispatch;
13
14/// Dispatch population count reduction over a slice.
15#[allow(missing_docs)]
16#[runtime_dispatch(avx512f, avx2, neon, scalar)]
17pub fn dispatch_reduce_popcount_kernel<T, A>(data: &[T]) -> usize
18where
19    T: Scalar,
20    A: SimdArch + SimdKernel<T>,
21{
22    match SimdView::<T, A, Unaligned, Unmasked, &[T]>::new(data) {
23        Some(v) => v.reduce_popcount(),
24        None => unsafe { core::hint::unreachable_unchecked() },
25    }
26}
27
28/// Dispatch bitwise AND population count reduction over two slices.
29#[allow(missing_docs)]
30#[runtime_dispatch(avx512f, avx2, neon, scalar)]
31pub fn dispatch_reduce_popcount_and_kernel<T, A>(a: &[T], b: &[T]) -> Result<usize, SimdError>
32where
33    T: Scalar,
34    A: SimdArch + SimdKernel<T>,
35{
36    match (
37        SimdView::<T, A, Unaligned, Unmasked, &[T]>::new(a),
38        SimdView::<T, A, Unaligned, Unmasked, &[T]>::new(b),
39    ) {
40        (Some(v1), Some(v2)) => v1.reduce_popcount_and(&v2),
41        _ => unsafe { core::hint::unreachable_unchecked() },
42    }
43}
44
45/// Dispatch bitwise OR population count reduction over two slices.
46#[allow(missing_docs)]
47#[runtime_dispatch(avx512f, avx2, neon, scalar)]
48pub fn dispatch_reduce_popcount_or_kernel<T, A>(a: &[T], b: &[T]) -> Result<usize, SimdError>
49where
50    T: Scalar,
51    A: SimdArch + SimdKernel<T>,
52{
53    match (
54        SimdView::<T, A, Unaligned, Unmasked, &[T]>::new(a),
55        SimdView::<T, A, Unaligned, Unmasked, &[T]>::new(b),
56    ) {
57        (Some(v1), Some(v2)) => v1.reduce_popcount_or(&v2),
58        _ => unsafe { core::hint::unreachable_unchecked() },
59    }
60}
61
62/// Dispatch bitwise XOR population count reduction over two slices.
63#[allow(missing_docs)]
64#[runtime_dispatch(avx512f, avx2, neon, scalar)]
65pub fn dispatch_reduce_popcount_xor_kernel<T, A>(a: &[T], b: &[T]) -> Result<usize, SimdError>
66where
67    T: Scalar,
68    A: SimdArch + SimdKernel<T>,
69{
70    match (
71        SimdView::<T, A, Unaligned, Unmasked, &[T]>::new(a),
72        SimdView::<T, A, Unaligned, Unmasked, &[T]>::new(b),
73    ) {
74        (Some(v1), Some(v2)) => v1.reduce_popcount_xor(&v2),
75        _ => unsafe { core::hint::unreachable_unchecked() },
76    }
77}