use super::*;
const I32_ACC_LIMIT: usize = (i32::MAX as usize) / (128 * 128);
#[test]
fn saturation_clamps_instead_of_wrapping() {
assert_eq!(saturate_i64_to_i32(0), 0);
assert_eq!(saturate_i64_to_i32(i32::MAX as i64), i32::MAX);
assert_eq!(saturate_i64_to_i32(i32::MAX as i64 + 1), i32::MAX);
assert_eq!(saturate_i64_to_i32(i32::MIN as i64 - 1), i32::MIN);
assert_eq!(saturate_i64_to_i32(i64::MAX), i32::MAX);
assert_eq!(saturate_i64_to_i32(i64::MIN), i32::MIN);
}
#[test]
fn a_long_dot_product_saturates_rather_than_changing_sign() {
let len = (8 * I32_ACC_LIMIT).max(3 * DOT_SPILL_ITERS * 32);
let a = vec![127i8; len];
let b = vec![127i8; len];
let got = unsafe { i8xi8_dot_i32(a.as_ptr(), b.as_ptr(), len) };
let exact: i64 = 127 * 127 * len as i64;
assert!(exact > i32::MAX as i64, "test input must actually overflow");
assert_eq!(got, i32::MAX, "expected clamp, got {got}");
assert!(
got > 0,
"an all-positive dot product must not come back negative"
);
}
#[test]
fn the_scalar_path_saturates_the_same_way() {
let len = 4 * I32_ACC_LIMIT;
let a = vec![127i8; len];
let b = vec![127i8; len];
let got = unsafe { i8xi8_dot_scalar(a.as_ptr(), b.as_ptr(), len) };
assert_eq!(got, i32::MAX, "expected clamp, got {got}");
}
#[test]
fn negative_overflow_clamps_to_min() {
let len = (8 * I32_ACC_LIMIT).max(3 * DOT_SPILL_ITERS * 32);
let a = vec![127i8; len];
let b = vec![-127i8; len];
let got = unsafe { i8xi8_dot_i32(a.as_ptr(), b.as_ptr(), len) };
assert_eq!(got, i32::MIN, "expected clamp, got {got}");
assert!(got < 0);
}
#[test]
fn a_long_dot_product_that_fits_is_still_exact() {
let len = 3 * DOT_SPILL_ITERS * 32 + 7;
let a: Vec<i8> = (0..len).map(|i| ((i % 5) as i8) - 2).collect();
let b: Vec<i8> = (0..len).map(|i| ((i % 7) as i8) - 3).collect();
let expected: i64 = a
.iter()
.zip(b.iter())
.map(|(&x, &y)| x as i64 * y as i64)
.sum();
assert!(
expected.abs() < i32::MAX as i64,
"this case must NOT saturate"
);
let got = unsafe { i8xi8_dot_i32(a.as_ptr(), b.as_ptr(), len) };
assert_eq!(got as i64, expected);
}
#[test]
fn simd_and_scalar_agree_across_lengths_that_straddle_the_spill() {
for len in [
1,
31,
32,
33,
DOT_SPILL_ITERS * 32 - 1,
DOT_SPILL_ITERS * 32,
DOT_SPILL_ITERS * 32 + 1,
DOT_SPILL_ITERS * 64 + 17,
] {
let a: Vec<i8> = (0..len).map(|i| ((i % 11) as i8) - 5).collect();
let b: Vec<i8> = (0..len).map(|i| ((i % 13) as i8) - 6).collect();
let simd = unsafe { i8xi8_dot_i32(a.as_ptr(), b.as_ptr(), len) };
let scalar = unsafe { i8xi8_dot_scalar(a.as_ptr(), b.as_ptr(), len) };
assert_eq!(simd, scalar, "len {len}");
}
}
#[test]
fn test_i8xi8_dot_basic() {
let a: Vec<i8> = (0..100).map(|x| (x % 127) as i8).collect();
let b: Vec<i8> = (0..100).map(|x| ((x * 3) % 127) as i8).collect();
let result = unsafe { i8xi8_dot_i32(a.as_ptr(), b.as_ptr(), a.len()) };
let expected: i32 = a
.iter()
.zip(b.iter())
.map(|(&x, &y)| x as i32 * y as i32)
.sum();
assert_eq!(result, expected);
}
#[test]
fn test_i8xi8_dot_negative() {
let a: Vec<i8> = (0..64).map(|x| (x as i8) - 32).collect();
let b: Vec<i8> = (0..64).map(|x| (x as i8) - 16).collect();
let result = unsafe { i8xi8_dot_i32(a.as_ptr(), b.as_ptr(), a.len()) };
let expected: i32 = a
.iter()
.zip(b.iter())
.map(|(&x, &y)| x as i32 * y as i32)
.sum();
assert_eq!(result, expected);
}
#[test]
fn test_i8xi8_dot_tail() {
let a: Vec<i8> = (0..67).map(|x| (x % 50) as i8).collect();
let b: Vec<i8> = (0..67).map(|x| ((x * 2) % 50) as i8).collect();
let result = unsafe { i8xi8_dot_i32(a.as_ptr(), b.as_ptr(), a.len()) };
let expected: i32 = a
.iter()
.zip(b.iter())
.map(|(&x, &y)| x as i32 * y as i32)
.sum();
assert_eq!(result, expected);
}
#[test]
fn test_i8xi8_dot_small() {
let a: Vec<i8> = vec![1, 2, 3, 4];
let b: Vec<i8> = vec![5, 6, 7, 8];
let result = unsafe { i8xi8_dot_i32(a.as_ptr(), b.as_ptr(), a.len()) };
assert_eq!(result, 1 * 5 + 2 * 6 + 3 * 7 + 4 * 8);
}
#[test]
fn test_i8xi8_dot_f32_scaled() {
let a: Vec<i8> = vec![10, 20, 30, 40];
let b: Vec<i8> = vec![1, 2, 3, 4];
let scale = 0.5f32;
let result = unsafe { i8xi8_dot_f32(a.as_ptr(), b.as_ptr(), scale, a.len()) };
let expected = (10 + 40 + 90 + 160) as f32 * scale;
assert!((result - expected).abs() < 1e-6);
}
#[test]
fn test_i8xi8_dot_extremes() {
let a: Vec<i8> = vec![
-128, 127, -128, 127, -128, 127, -128, 127, -128, 127, -128, 127, -128, 127, -128, 127,
-128, 127, -128, 127, -128, 127, -128, 127, -128, 127, -128, 127, -128, 127, -128, 127,
];
let b: Vec<i8> = vec![
127, -128, 127, -128, 127, -128, 127, -128, 127, -128, 127, -128, 127, -128, 127, -128,
127, -128, 127, -128, 127, -128, 127, -128, 127, -128, 127, -128, 127, -128, 127, -128,
];
let result = unsafe { i8xi8_dot_i32(a.as_ptr(), b.as_ptr(), a.len()) };
let expected: i32 = a
.iter()
.zip(b.iter())
.map(|(&x, &y)| x as i32 * y as i32)
.sum();
assert_eq!(result, expected);
}
#[test]
fn test_i8xi8_dot_large() {
let a: Vec<i8> = (0..1024)
.map(|x| ((x * 7 + 13) % 256 - 128) as i8)
.collect();
let b: Vec<i8> = (0..1024)
.map(|x| ((x * 11 + 5) % 256 - 128) as i8)
.collect();
let result = unsafe { i8xi8_dot_i32(a.as_ptr(), b.as_ptr(), a.len()) };
let expected: i32 = a
.iter()
.zip(b.iter())
.map(|(&x, &y)| x as i32 * y as i32)
.sum();
assert_eq!(result, expected);
}