#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;
use crate::runtime::cpu::kernels::simd::dot::{DOT_SPILL_ITERS, saturate_i64_to_i32};
const I8_LANES: usize = 32;
#[target_feature(enable = "avx2")]
unsafe fn hsum_epi32_wide(v: __m256i) -> i64 {
let mut lanes = [0i32; 8];
_mm256_storeu_si256(lanes.as_mut_ptr() as *mut __m256i, v);
lanes.iter().map(|&x| x as i64).sum()
}
#[target_feature(enable = "avx2")]
pub unsafe fn i8xi8_dot_i32(a: *const i8, b: *const i8, len: usize) -> i32 {
let chunks = len / I8_LANES;
let remainder = len % I8_LANES;
let mut total = 0i64;
let mut acc = _mm256_setzero_si256();
for i in 0..chunks {
let offset = i * I8_LANES;
let va = _mm256_loadu_si256(a.add(offset) as *const __m256i);
let vb = _mm256_loadu_si256(b.add(offset) as *const __m256i);
let va_lo = _mm256_cvtepi8_epi16(_mm256_castsi256_si128(va));
let vb_lo = _mm256_cvtepi8_epi16(_mm256_castsi256_si128(vb));
let prod_lo = _mm256_madd_epi16(va_lo, vb_lo);
acc = _mm256_add_epi32(acc, prod_lo);
let va_hi = _mm256_cvtepi8_epi16(_mm256_extracti128_si256(va, 1));
let vb_hi = _mm256_cvtepi8_epi16(_mm256_extracti128_si256(vb, 1));
let prod_hi = _mm256_madd_epi16(va_hi, vb_hi);
acc = _mm256_add_epi32(acc, prod_hi);
if (i + 1) % DOT_SPILL_ITERS == 0 {
total += hsum_epi32_wide(acc);
acc = _mm256_setzero_si256();
}
}
total += hsum_epi32_wide(acc);
for i in 0..remainder {
let offset = chunks * I8_LANES + i;
total += (*a.add(offset) as i64) * (*b.add(offset) as i64);
}
saturate_i64_to_i32(total)
}