2021-07-19 18:13:24 -05:00
|
|
|
#![feature(portable_simd)]
|
|
|
|
|
2021-02-04 19:47:13 -06:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
use wasm_bindgen_test::*;
|
|
|
|
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
wasm_bindgen_test_configure!(run_in_browser);
|
|
|
|
|
|
|
|
macro_rules! test_mask_api {
|
2021-08-07 16:22:10 -05:00
|
|
|
{ $type:ident } => {
|
2021-02-04 19:47:13 -06:00
|
|
|
#[allow(non_snake_case)]
|
2021-08-07 16:22:10 -05:00
|
|
|
mod $type {
|
2021-02-08 23:09:27 -06:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
use wasm_bindgen_test::*;
|
|
|
|
|
2022-11-27 22:44:20 -06:00
|
|
|
use core_simd::simd::Mask;
|
|
|
|
|
2021-02-04 19:47:13 -06:00
|
|
|
#[test]
|
|
|
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
|
|
|
|
fn set_and_test() {
|
|
|
|
let values = [true, false, false, true, false, false, true, false];
|
2022-11-27 22:44:20 -06:00
|
|
|
let mut mask = Mask::<$type, 8>::splat(false);
|
2021-02-04 19:47:13 -06:00
|
|
|
for (lane, value) in values.iter().copied().enumerate() {
|
|
|
|
mask.set(lane, value);
|
|
|
|
}
|
|
|
|
for (lane, value) in values.iter().copied().enumerate() {
|
|
|
|
assert_eq!(mask.test(lane), value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn set_invalid_lane() {
|
2022-11-27 22:44:20 -06:00
|
|
|
let mut mask = Mask::<$type, 8>::splat(false);
|
2021-02-04 19:47:13 -06:00
|
|
|
mask.set(8, true);
|
|
|
|
let _ = mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn test_invalid_lane() {
|
2022-11-27 22:44:20 -06:00
|
|
|
let mask = Mask::<$type, 8>::splat(false);
|
2021-02-04 19:47:13 -06:00
|
|
|
let _ = mask.test(8);
|
|
|
|
}
|
2021-03-07 23:48:18 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn any() {
|
2022-11-27 22:44:20 -06:00
|
|
|
assert!(!Mask::<$type, 8>::splat(false).any());
|
|
|
|
assert!(Mask::<$type, 8>::splat(true).any());
|
|
|
|
let mut v = Mask::<$type, 8>::splat(false);
|
2021-03-07 23:48:18 -06:00
|
|
|
v.set(2, true);
|
|
|
|
assert!(v.any());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn all() {
|
2022-11-27 22:44:20 -06:00
|
|
|
assert!(!Mask::<$type, 8>::splat(false).all());
|
|
|
|
assert!(Mask::<$type, 8>::splat(true).all());
|
|
|
|
let mut v = Mask::<$type, 8>::splat(false);
|
2021-03-07 23:48:18 -06:00
|
|
|
v.set(2, true);
|
|
|
|
assert!(!v.all());
|
|
|
|
}
|
2021-04-18 23:31:43 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn roundtrip_int_conversion() {
|
|
|
|
let values = [true, false, false, true, false, false, true, false];
|
2022-11-27 22:44:20 -06:00
|
|
|
let mask = Mask::<$type, 8>::from_array(values);
|
2021-04-18 23:31:43 -05:00
|
|
|
let int = mask.to_int();
|
|
|
|
assert_eq!(int.to_array(), [-1, 0, 0, -1, 0, 0, -1, 0]);
|
2022-11-27 22:44:20 -06:00
|
|
|
assert_eq!(Mask::<$type, 8>::from_int(int), mask);
|
2021-04-18 23:31:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-06-21 16:05:43 -05:00
|
|
|
fn roundtrip_bitmask_conversion() {
|
2022-11-27 22:44:20 -06:00
|
|
|
use core_simd::simd::ToBitMask;
|
2021-04-28 21:41:09 -05:00
|
|
|
let values = [
|
|
|
|
true, false, false, true, false, false, true, false,
|
2021-04-28 22:34:28 -05:00
|
|
|
true, true, false, false, false, false, false, true,
|
2021-04-28 21:41:09 -05:00
|
|
|
];
|
2022-11-27 22:44:20 -06:00
|
|
|
let mask = Mask::<$type, 16>::from_array(values);
|
2022-02-05 21:25:27 -06:00
|
|
|
let bitmask = mask.to_bitmask();
|
2022-01-13 20:20:17 -06:00
|
|
|
assert_eq!(bitmask, 0b1000001101001001);
|
2022-11-27 22:44:20 -06:00
|
|
|
assert_eq!(Mask::<$type, 16>::from_bitmask(bitmask), mask);
|
2021-04-18 23:31:43 -05:00
|
|
|
}
|
2022-03-16 19:27:32 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn roundtrip_bitmask_conversion_short() {
|
2022-11-27 22:44:20 -06:00
|
|
|
use core_simd::simd::ToBitMask;
|
2022-03-17 09:42:39 -05:00
|
|
|
|
2022-03-16 19:27:32 -05:00
|
|
|
let values = [
|
|
|
|
false, false, false, true,
|
|
|
|
];
|
2022-11-27 22:44:20 -06:00
|
|
|
let mask = Mask::<$type, 4>::from_array(values);
|
2022-03-16 19:27:32 -05:00
|
|
|
let bitmask = mask.to_bitmask();
|
|
|
|
assert_eq!(bitmask, 0b1000);
|
2022-11-27 22:44:20 -06:00
|
|
|
assert_eq!(Mask::<$type, 4>::from_bitmask(bitmask), mask);
|
2022-03-17 09:42:39 -05:00
|
|
|
|
|
|
|
let values = [true, false];
|
2022-11-27 22:44:20 -06:00
|
|
|
let mask = Mask::<$type, 2>::from_array(values);
|
2022-03-17 09:42:39 -05:00
|
|
|
let bitmask = mask.to_bitmask();
|
|
|
|
assert_eq!(bitmask, 0b01);
|
2022-11-27 22:44:20 -06:00
|
|
|
assert_eq!(Mask::<$type, 2>::from_bitmask(bitmask), mask);
|
2022-03-16 19:27:32 -05:00
|
|
|
}
|
2022-05-21 14:13:20 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cast() {
|
2022-11-27 22:44:20 -06:00
|
|
|
fn cast_impl<T: core_simd::simd::MaskElement>()
|
2022-05-21 14:13:20 -05:00
|
|
|
where
|
2022-11-27 22:44:20 -06:00
|
|
|
Mask<$type, 8>: Into<Mask<T, 8>>,
|
2022-05-21 14:13:20 -05:00
|
|
|
{
|
|
|
|
let values = [true, false, false, true, false, false, true, false];
|
2022-11-27 22:44:20 -06:00
|
|
|
let mask = Mask::<$type, 8>::from_array(values);
|
2022-05-21 14:13:20 -05:00
|
|
|
|
|
|
|
let cast_mask = mask.cast::<T>();
|
|
|
|
assert_eq!(values, cast_mask.to_array());
|
|
|
|
|
2022-11-27 22:44:20 -06:00
|
|
|
let into_mask: Mask<T, 8> = mask.into();
|
2022-05-21 14:13:20 -05:00
|
|
|
assert_eq!(values, into_mask.to_array());
|
|
|
|
}
|
|
|
|
|
|
|
|
cast_impl::<i8>();
|
|
|
|
cast_impl::<i16>();
|
|
|
|
cast_impl::<i32>();
|
|
|
|
cast_impl::<i64>();
|
|
|
|
cast_impl::<isize>();
|
|
|
|
}
|
2022-01-13 20:20:17 -06:00
|
|
|
|
2022-05-21 19:08:38 -05:00
|
|
|
#[cfg(feature = "generic_const_exprs")]
|
2022-01-13 20:20:17 -06:00
|
|
|
#[test]
|
|
|
|
fn roundtrip_bitmask_array_conversion() {
|
2022-11-27 22:44:20 -06:00
|
|
|
use core_simd::simd::ToBitMaskArray;
|
2022-01-13 20:20:17 -06:00
|
|
|
let values = [
|
|
|
|
true, false, false, true, false, false, true, false,
|
|
|
|
true, true, false, false, false, false, false, true,
|
|
|
|
];
|
2022-11-27 22:44:20 -06:00
|
|
|
let mask = Mask::<$type, 16>::from_array(values);
|
2022-01-13 20:20:17 -06:00
|
|
|
let bitmask = mask.to_bitmask_array();
|
|
|
|
assert_eq!(bitmask, [0b01001001, 0b10000011]);
|
2022-11-27 22:44:20 -06:00
|
|
|
assert_eq!(Mask::<$type, 16>::from_bitmask_array(bitmask), mask);
|
2022-01-13 20:20:17 -06:00
|
|
|
}
|
2021-02-04 19:47:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod mask_api {
|
2021-08-07 16:22:10 -05:00
|
|
|
test_mask_api! { i8 }
|
2021-10-18 18:20:31 -05:00
|
|
|
test_mask_api! { i16 }
|
|
|
|
test_mask_api! { i32 }
|
|
|
|
test_mask_api! { i64 }
|
|
|
|
test_mask_api! { isize }
|
2021-02-04 19:47:13 -06:00
|
|
|
}
|
2021-06-11 17:48:05 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn convert() {
|
2022-11-27 22:44:20 -06:00
|
|
|
use core_simd::simd::Mask;
|
2021-06-11 17:48:05 -05:00
|
|
|
let values = [true, false, false, true, false, false, true, false];
|
|
|
|
assert_eq!(
|
2022-11-27 22:44:20 -06:00
|
|
|
Mask::<i8, 8>::from_array(values),
|
|
|
|
Mask::<i32, 8>::from_array(values).into()
|
2021-06-11 17:48:05 -05:00
|
|
|
);
|
|
|
|
}
|