use crate::cpu::{best_isa, Isa};
use crate::kernels::scalar;
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
use crate::kernels::{avx2, avx512};
#[cfg(target_arch = "aarch64")]
use crate::kernels::neon;
pub fn count_gt_i32(data: &[i32], threshold: i32) -> usize {
match best_isa() {
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Isa::Avx512 => unsafe { avx512::count_gt_i32(data, threshold) },
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Isa::Avx2 => unsafe { avx2::count_gt_i32(data, threshold) },
#[cfg(target_arch = "aarch64")]
Isa::Neon => unsafe { neon::count_gt_i32(data, threshold) },
_ => scalar::count_gt_i32(data, threshold),
}
}
pub fn count_gt_f32(data: &[f32], threshold: f32) -> usize {
match best_isa() {
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Isa::Avx512 => unsafe { avx512::count_gt_f32(data, threshold) },
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Isa::Avx2 => unsafe { avx2::count_gt_f32(data, threshold) },
#[cfg(target_arch = "aarch64")]
Isa::Neon => unsafe { neon::count_gt_f32(data, threshold) },
_ => scalar::count_gt_f32(data, threshold),
}
}
pub fn sum_i32(data: &[i32]) -> i64 {
match best_isa() {
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Isa::Avx512 => unsafe { avx512::sum_i32(data) },
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Isa::Avx2 => unsafe { avx2::sum_i32(data) },
#[cfg(target_arch = "aarch64")]
Isa::Neon => unsafe { neon::sum_i32(data) },
_ => scalar::sum_i32(data),
}
}
pub fn find_i32(data: &[i32], needle: i32) -> Option<usize> {
match best_isa() {
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Isa::Avx512 => unsafe { avx512::find_i32(data, needle) },
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Isa::Avx2 => unsafe { avx2::find_i32(data, needle) },
#[cfg(target_arch = "aarch64")]
Isa::Neon => unsafe { neon::find_i32(data, needle) },
_ => scalar::find_i32(data, needle),
}
}
pub fn filter_into_i32(data: &[i32], threshold: i32, out: &mut [i32]) -> usize {
assert!(out.len() >= data.len(), "out должен вмещать весь вход");
const TAIL_GUARD: usize = 16;
if out.len() >= data.len() + TAIL_GUARD {
return filter_dispatch(data, threshold, out);
}
let mut scratch = vec![0i32; data.len() + TAIL_GUARD];
let n = filter_dispatch(data, threshold, &mut scratch);
out[..n].copy_from_slice(&scratch[..n]);
n
}
pub(crate) fn filter_dispatch(data: &[i32], threshold: i32, out: &mut [i32]) -> usize {
match best_isa() {
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Isa::Avx512 => unsafe { avx512::filter_into_i32(data, threshold, out) },
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
Isa::Avx2 => unsafe { avx2::filter_into_i32(data, threshold, out) },
#[cfg(target_arch = "aarch64")]
Isa::Neon => unsafe { neon::filter_into_i32(data, threshold, out) },
_ => scalar::filter_into_i32(data, threshold, out),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::aligned::AlignedVec;
fn sample() -> AlignedVec<i32> {
AlignedVec::from_fn(100_003, |i| (i as i32 % 1000) - 500)
}
#[test]
fn all_backends_agree_on_count() {
let v = sample();
let expect = scalar::count_gt_i32(&v, 250);
assert_eq!(count_gt_i32(&v, 250), expect);
}
#[test]
fn all_backends_agree_on_sum() {
let v = sample();
assert_eq!(sum_i32(&v), scalar::sum_i32(&v));
}
#[test]
fn find_matches_scalar() {
let v = sample();
for needle in [-500i32, 0, 499, 12345] {
assert_eq!(find_i32(&v, needle), scalar::find_i32(&v, needle));
}
}
#[test]
fn filter_matches_scalar() {
let v = sample();
let mut a = vec![0i32; v.len()];
let mut b = vec![0i32; v.len()];
let n1 = filter_into_i32(&v, 250, &mut a);
let n2 = scalar::filter_into_i32(&v, 250, &mut b);
assert_eq!(n1, n2);
assert_eq!(a[..n1], b[..n2]);
}
#[test]
fn float_count_matches_scalar() {
let v: Vec<f32> = (0..100_003).map(|i| (i as f32 * 0.5) - 1000.0).collect();
assert_eq!(count_gt_f32(&v, 0.0), scalar::count_gt_f32(&v, 0.0));
}
}