Skip to main content

simd_vector_engine/
engine.rs

1// Весь unsafe живёт здесь: ядро вызывается только после проверки фичи в cpu.rs.
2
3use crate::cpu::{best_isa, Isa};
4use crate::kernels::scalar;
5
6#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
7use crate::kernels::{avx2, avx512};
8
9#[cfg(target_arch = "aarch64")]
10use crate::kernels::neon;
11
12pub fn count_gt_i32(data: &[i32], threshold: i32) -> usize {
13    match best_isa() {
14        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
15        Isa::Avx512 => unsafe { avx512::count_gt_i32(data, threshold) },
16        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
17        Isa::Avx2 => unsafe { avx2::count_gt_i32(data, threshold) },
18        #[cfg(target_arch = "aarch64")]
19        Isa::Neon => unsafe { neon::count_gt_i32(data, threshold) },
20        _ => scalar::count_gt_i32(data, threshold),
21    }
22}
23
24pub fn count_gt_f32(data: &[f32], threshold: f32) -> usize {
25    match best_isa() {
26        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
27        Isa::Avx512 => unsafe { avx512::count_gt_f32(data, threshold) },
28        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
29        Isa::Avx2 => unsafe { avx2::count_gt_f32(data, threshold) },
30        #[cfg(target_arch = "aarch64")]
31        Isa::Neon => unsafe { neon::count_gt_f32(data, threshold) },
32        _ => scalar::count_gt_f32(data, threshold),
33    }
34}
35
36pub fn sum_i32(data: &[i32]) -> i64 {
37    match best_isa() {
38        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
39        Isa::Avx512 => unsafe { avx512::sum_i32(data) },
40        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
41        Isa::Avx2 => unsafe { avx2::sum_i32(data) },
42        #[cfg(target_arch = "aarch64")]
43        Isa::Neon => unsafe { neon::sum_i32(data) },
44        _ => scalar::sum_i32(data),
45    }
46}
47
48pub fn find_i32(data: &[i32], needle: i32) -> Option<usize> {
49    match best_isa() {
50        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
51        Isa::Avx512 => unsafe { avx512::find_i32(data, needle) },
52        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
53        Isa::Avx2 => unsafe { avx2::find_i32(data, needle) },
54        #[cfg(target_arch = "aarch64")]
55        Isa::Neon => unsafe { neon::find_i32(data, needle) },
56        _ => scalar::find_i32(data, needle),
57    }
58}
59
60/// Копирует в `out` все элементы `data` больше `threshold`, сохраняя порядок.
61///
62/// # Panics
63///
64/// Если `out.len() < data.len()`.
65pub fn filter_into_i32(data: &[i32], threshold: i32, out: &mut [i32]) -> usize {
66    assert!(out.len() >= data.len(), "out должен вмещать весь вход");
67
68    // Ядра пишут целыми регистрами и сдвигаются на popcount маски,
69    // поэтому последняя запись захватывает до TAIL_GUARD-1 элементов за результатом.
70    // Если вызывающий не дал запаса, пишем во временный буфер.
71    const TAIL_GUARD: usize = 16;
72    if out.len() >= data.len() + TAIL_GUARD {
73        return filter_dispatch(data, threshold, out);
74    }
75
76    let mut scratch = vec![0i32; data.len() + TAIL_GUARD];
77    let n = filter_dispatch(data, threshold, &mut scratch);
78    out[..n].copy_from_slice(&scratch[..n]);
79    n
80}
81
82/// Диспетчер без проверки размера `out`.
83///
84/// Вызывающий обязан гарантировать запас не менее 16 элементов между числом
85/// совпадений и `out.len()`: векторные ядра пишут целым регистром.
86/// Единственный внутренний пользователь — `parallel::fill_exact`.
87pub(crate) fn filter_dispatch(data: &[i32], threshold: i32, out: &mut [i32]) -> usize {
88    match best_isa() {
89        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
90        Isa::Avx512 => unsafe { avx512::filter_into_i32(data, threshold, out) },
91        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
92        Isa::Avx2 => unsafe { avx2::filter_into_i32(data, threshold, out) },
93        #[cfg(target_arch = "aarch64")]
94        Isa::Neon => unsafe { neon::filter_into_i32(data, threshold, out) },
95        _ => scalar::filter_into_i32(data, threshold, out),
96    }
97}
98
99#[cfg(test)]
100mod tests {
101    use super::*;
102    use crate::aligned::AlignedVec;
103
104    fn sample() -> AlignedVec<i32> {
105        // Длина не кратна 64 — проверяем хвосты.
106        AlignedVec::from_fn(100_003, |i| (i as i32 % 1000) - 500)
107    }
108
109    #[test]
110    fn all_backends_agree_on_count() {
111        let v = sample();
112        let expect = scalar::count_gt_i32(&v, 250);
113        assert_eq!(count_gt_i32(&v, 250), expect);
114    }
115
116    #[test]
117    fn all_backends_agree_on_sum() {
118        let v = sample();
119        assert_eq!(sum_i32(&v), scalar::sum_i32(&v));
120    }
121
122    #[test]
123    fn find_matches_scalar() {
124        let v = sample();
125        for needle in [-500i32, 0, 499, 12345] {
126            assert_eq!(find_i32(&v, needle), scalar::find_i32(&v, needle));
127        }
128    }
129
130    #[test]
131    fn filter_matches_scalar() {
132        let v = sample();
133        let mut a = vec![0i32; v.len()];
134        let mut b = vec![0i32; v.len()];
135        let n1 = filter_into_i32(&v, 250, &mut a);
136        let n2 = scalar::filter_into_i32(&v, 250, &mut b);
137        assert_eq!(n1, n2);
138        assert_eq!(a[..n1], b[..n2]);
139    }
140
141    #[test]
142    fn float_count_matches_scalar() {
143        let v: Vec<f32> = (0..100_003).map(|i| (i as f32 * 0.5) - 1000.0).collect();
144        assert_eq!(count_gt_f32(&v, 0.0), scalar::count_gt_f32(&v, 0.0));
145    }
146}