#[allow(unused_macros)]
macro_rules! integer_contract {
($backend:ident, $feature:literal, $lanes:literal) => {
mod $backend {
use crate::simd::$backend::i32 as v;
#[allow(unused_unsafe)]
#[target_feature(enable = $feature)]
pub(super) fn check() {
let values = [i32::MIN, i32::MAX, -1, 0, 1, -65535, 65535, 0x2aaa_aaab];
for offset in 0..values.len() {
let a: [i32; $lanes] = std::array::from_fn(|i| values[i % values.len()]);
let b: [i32; $lanes] =
std::array::from_fn(|i| values[(offset + i) % values.len()]);
let [av, bv] = [a, b].map(|v| unsafe { v::load(v.as_ptr()) });
let mut actual = [0; $lanes];
unsafe { v::store(actual.as_mut_ptr(), v::mulhi(av, bv)) };
assert_eq!(
actual,
std::array::from_fn(|i| {
((i64::from(a[i]) * i64::from(b[i])) >> 32) as i32
})
);
unsafe {
v::store(
actual.as_mut_ptr(),
v::select(v::lt_unsigned(av, bv), av, bv),
)
};
assert_eq!(
actual,
std::array::from_fn(|i| {
if (a[i] as u32) < (b[i] as u32) {
a[i]
} else {
b[i]
}
})
);
assert_eq!(
v::bits_clear(av, bv),
a.into_iter().zip(b).all(|(a, b)| a & b == 0)
);
}
let pairs: [i32; 2 * $lanes] = std::array::from_fn(|i| values[i % values.len()]);
let (a, b) = unsafe { v::load_pairs(pairs.as_ptr()) };
let mut even = [0; $lanes];
let mut odd = [0; $lanes];
unsafe {
v::store(even.as_mut_ptr(), a);
v::store(odd.as_mut_ptr(), b);
}
assert_eq!(even, std::array::from_fn(|i| pairs[2 * i]));
assert_eq!(odd, std::array::from_fn(|i| pairs[2 * i + 1]));
}
}
};
}
#[cfg(all(target_arch = "aarch64", feature = "neon"))]
integer_contract!(neon, "neon", 4);
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "sse"))]
integer_contract!(sse, "sse4.1", 4);
#[cfg(all(target_arch = "x86_64", feature = "avx"))]
integer_contract!(avx, "avx2", 8);
#[cfg(all(target_arch = "wasm32", feature = "wasm", target_feature = "simd128"))]
integer_contract!(wasm, "simd128", 4);
#[test]
#[allow(unused_unsafe)]
fn integer_lanes_match_widened_scalar_math() {
#[cfg(all(target_arch = "aarch64", feature = "neon"))]
unsafe {
neon::check()
};
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), feature = "sse"))]
if std::is_x86_feature_detected!("sse4.1") {
unsafe { sse::check() };
}
#[cfg(all(target_arch = "x86_64", feature = "avx"))]
if std::is_x86_feature_detected!("avx2") {
unsafe { avx::check() };
}
#[cfg(all(target_arch = "wasm32", feature = "wasm", target_feature = "simd128"))]
unsafe {
wasm::check()
};
}