| 1 |
use vsimd::isa::detect; |
| 2 |
use vsimd::isa::{NEON, SSE2, WASM128}; |
| 3 |
use vsimd::vector::V128; |
| 4 |
use vsimd::SIMD128; |
| 5 |
|
| 6 |
use const_str::hex; |
| 7 |
|
| 8 |
#[cfg(not(miri))] |
| 9 |
#[cfg_attr(not(target_arch = "wasm32"), test)] |
| 10 |
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] |
| 11 |
fn native_sum() { |
| 12 |
use vsimd::native::Native; |
| 13 |
|
| 14 |
let x: u32 = rand::random::<u32>() / 2; |
| 15 |
let y: u32 = rand::random::<u32>() / 2; |
| 16 |
|
| 17 |
const N: usize = 100; |
| 18 |
let a = [x; N]; |
| 19 |
let b = [y; N]; |
| 20 |
let mut c = [0; N]; |
| 21 |
|
| 22 |
Native::detect().exec(|| { |
| 23 |
assert!(a.len() == N && b.len() == N && c.len() == N); |
| 24 |
for i in 0..N { |
| 25 |
c[i] = a[i] + b[i]; |
| 26 |
} |
| 27 |
}); |
| 28 |
|
| 29 |
assert!(c.iter().copied().all(|z| z == x + y)); |
| 30 |
} |
| 31 |
|
| 32 |
#[cfg_attr(not(target_arch = "wasm32"), test)] |
| 33 |
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] |
| 34 |
fn u8x16_any_zero() { |
| 35 |
fn f(a: [u8; 16]) -> bool { |
| 36 |
let a = V128::from_bytes(a); |
| 37 |
if let Some(s) = detect::<SSE2>() { |
| 38 |
return s.u8x16_any_zero(a); |
| 39 |
} |
| 40 |
if let Some(s) = detect::<NEON>() { |
| 41 |
return s.u8x16_any_zero(a); |
| 42 |
} |
| 43 |
if let Some(s) = detect::<WASM128>() { |
| 44 |
return s.u8x16_any_zero(a); |
| 45 |
} |
| 46 |
a.as_bytes().iter().any(|&x| x == 0) |
| 47 |
} |
| 48 |
|
| 49 |
fn test(a: [u8; 16], expected: bool) { |
| 50 |
assert_eq!(f(a), expected); |
| 51 |
} |
| 52 |
|
| 53 |
test([0x00; 16], true); |
| 54 |
test([0xff; 16], false); |
| 55 |
test(hex!("00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F"), true); |
| 56 |
test(hex!("10 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F"), false); |
| 57 |
} |