Skip to content

Commit 9e96c8a

Browse files
committed
Add missing From implementation, add simple mask API tests
1 parent 92293af commit 9e96c8a

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

crates/core_simd/src/masks/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,24 @@ macro_rules! define_opaque_mask {
5959
}
6060
}
6161

62+
impl<const $lanes: usize> From<$inner_ty> for $name<$lanes>
63+
where
64+
BitMask<$lanes>: LanesAtMost64,
65+
{
66+
fn from(value: $inner_ty) -> Self {
67+
Self(value)
68+
}
69+
}
70+
71+
impl<const $lanes: usize> From<$name<$lanes>> for $inner_ty
72+
where
73+
BitMask<$lanes>: LanesAtMost64,
74+
{
75+
fn from(value: $name<$lanes>) -> Self {
76+
value.0
77+
}
78+
}
79+
6280
impl<const $lanes: usize> Copy for $name<$lanes> where BitMask<$lanes>: LanesAtMost64 {}
6381

6482
impl<const $lanes: usize> Clone for $name<$lanes> where BitMask<$lanes>: LanesAtMost64 {

crates/core_simd/tests/masks.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use core_simd::{BitMask, Mask8, SimdMask8, SimdI8};
2+
use core::convert::TryFrom;
3+
4+
#[cfg(target_arch = "wasm32")]
5+
use wasm_bindgen_test::*;
6+
7+
#[cfg(target_arch = "wasm32")]
8+
wasm_bindgen_test_configure!(run_in_browser);
9+
10+
#[test]
11+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
12+
fn mask_format_round_trip() {
13+
let ints = SimdI8::from_array([-1, 0, 0, -1]);
14+
15+
let simd_mask = SimdMask8::try_from(ints).unwrap();
16+
17+
let bitmask = BitMask::from(simd_mask);
18+
19+
let opaque_mask = Mask8::from(bitmask);
20+
21+
let simd_mask_returned = SimdMask8::from(opaque_mask);
22+
23+
let ints_returned = SimdI8::from(simd_mask_returned);
24+
25+
assert_eq!(ints_returned, ints);
26+
}
27+
28+
macro_rules! test_mask_api {
29+
{ $name:ident } => {
30+
#[allow(non_snake_case)]
31+
mod $name {
32+
#[test]
33+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
34+
fn set_and_test() {
35+
let values = [true, false, false, true, false, false, true, false];
36+
let mut mask = core_simd::$name::<8>::splat(false);
37+
for (lane, value) in values.iter().copied().enumerate() {
38+
mask.set(lane, value);
39+
}
40+
for (lane, value) in values.iter().copied().enumerate() {
41+
assert_eq!(mask.test(lane), value);
42+
}
43+
}
44+
45+
#[test]
46+
#[should_panic]
47+
fn set_invalid_lane() {
48+
let mut mask = core_simd::$name::<8>::splat(false);
49+
mask.set(8, true);
50+
let _ = mask;
51+
}
52+
53+
#[test]
54+
#[should_panic]
55+
fn test_invalid_lane() {
56+
let mask = core_simd::$name::<8>::splat(false);
57+
let _ = mask.test(8);
58+
}
59+
}
60+
}
61+
}
62+
63+
mod mask_api {
64+
test_mask_api!{ Mask8 }
65+
test_mask_api!{ SimdMask8 }
66+
test_mask_api!{ BitMask }
67+
}

0 commit comments

Comments
 (0)