Skip to content

Commit 0ac057a

Browse files
committed
Add integer tests
1 parent d5c2279 commit 0ac057a

File tree

3 files changed

+208
-4
lines changed

3 files changed

+208
-4
lines changed

crates/core_simd/tests/float.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ macro_rules! impl_op_test {
88
test_helpers::test_unary_elementwise(
99
<$vector as core::ops::$trait>::$fn,
1010
<$scalar as core::ops::$trait>::$fn,
11+
|_| true,
1112
);
1213
}
1314
}
@@ -21,34 +22,39 @@ macro_rules! impl_op_test {
2122
test_helpers::test_binary_elementwise(
2223
<$vector as core::ops::$trait>::$fn,
2324
<$scalar as core::ops::$trait>::$fn,
25+
|_, _| true,
2426
);
2527
}
2628

2729
fn scalar_rhs<const LANES: usize>() {
2830
test_helpers::test_binary_scalar_rhs_elementwise(
2931
<$vector as core::ops::$trait<$scalar>>::$fn,
3032
<$scalar as core::ops::$trait>::$fn,
33+
|_, _| true,
3134
);
3235
}
3336

3437
fn scalar_lhs<const LANES: usize>() {
3538
test_helpers::test_binary_scalar_lhs_elementwise(
3639
<$scalar as core::ops::$trait<$vector>>::$fn,
3740
<$scalar as core::ops::$trait>::$fn,
41+
|_, _| true,
3842
);
3943
}
4044

4145
fn assign<const LANES: usize>() {
4246
test_helpers::test_binary_elementwise(
4347
|mut a, b| { <$vector as core::ops::$trait_assign>::$fn_assign(&mut a, b); a },
4448
|mut a, b| { <$scalar as core::ops::$trait_assign>::$fn_assign(&mut a, b); a },
49+
|_, _| true,
4550
)
4651
}
4752

4853
fn assign_scalar_rhs<const LANES: usize>() {
4954
test_helpers::test_binary_scalar_rhs_elementwise(
5055
|mut a, b| { <$vector as core::ops::$trait_assign<$scalar>>::$fn_assign(&mut a, b); a },
5156
|mut a, b| { <$scalar as core::ops::$trait_assign>::$fn_assign(&mut a, b); a },
57+
|_, _| true,
5258
)
5359
}
5460
}
@@ -62,7 +68,7 @@ macro_rules! impl_tests {
6268
type Vector<const LANES: usize> = core_simd::$vector<LANES>;
6369
type Scalar = $scalar;
6470
type IntScalar = $int_scalar;
65-
71+
6672
impl_op_test! { unary, Vector<LANES>, Scalar, Neg::neg }
6773
impl_op_test! { binary, Vector<LANES>, Scalar, Add::add, AddAssign::add_assign }
6874
impl_op_test! { binary, Vector<LANES>, Scalar, Sub::sub, SubAssign::sub_assign }
@@ -75,27 +81,31 @@ macro_rules! impl_tests {
7581
test_helpers::test_unary_elementwise(
7682
Vector::<LANES>::abs,
7783
Scalar::abs,
84+
|_| true,
7885
)
7986
}
8087

8188
fn ceil<const LANES: usize>() {
8289
test_helpers::test_unary_elementwise(
8390
Vector::<LANES>::ceil,
8491
Scalar::ceil,
92+
|_| true,
8593
)
8694
}
8795

8896
fn floor<const LANES: usize>() {
8997
test_helpers::test_unary_elementwise(
9098
Vector::<LANES>::floor,
9199
Scalar::floor,
100+
|_| true,
92101
)
93102
}
94103

95104
fn round_from_int<const LANES: usize>() {
96105
test_helpers::test_unary_elementwise(
97106
Vector::<LANES>::round_from_int,
98107
|x| x as Scalar,
108+
|_| true,
99109
)
100110
}
101111

crates/core_simd/tests/integer.rs

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
#[cfg(target_arch = "wasm32")]
2+
wasm_bindgen_test_configure!(run_in_browser);
3+
4+
macro_rules! impl_unary_op_test {
5+
{ $vector:ty, $scalar:ty, $trait:ident :: $fn:ident, $scalar_fn:expr } => {
6+
test_helpers::test_lanes! {
7+
fn $fn<const LANES: usize>() {
8+
test_helpers::test_unary_elementwise(
9+
<$vector as core::ops::$trait>::$fn,
10+
$scalar_fn,
11+
|_| true,
12+
);
13+
}
14+
}
15+
};
16+
{ $vector:ty, $scalar:ty, $trait:ident :: $fn:ident } => {
17+
impl_unary_op_test! { $vector, $scalar, $trait::$fn, <$scalar as core::ops::$trait>::$fn }
18+
};
19+
}
20+
21+
macro_rules! impl_binary_op_test {
22+
{ $vector:ty, $scalar:ty, $trait:ident :: $fn:ident, $trait_assign:ident :: $fn_assign:ident, $scalar_fn:expr } => {
23+
mod $fn {
24+
use super::*;
25+
26+
test_helpers::test_lanes! {
27+
fn normal<const LANES: usize>() {
28+
test_helpers::test_binary_elementwise(
29+
<$vector as core::ops::$trait>::$fn,
30+
$scalar_fn,
31+
|_, _| true,
32+
);
33+
}
34+
35+
fn scalar_rhs<const LANES: usize>() {
36+
test_helpers::test_binary_scalar_rhs_elementwise(
37+
<$vector as core::ops::$trait<$scalar>>::$fn,
38+
$scalar_fn,
39+
|_, _| true,
40+
);
41+
}
42+
43+
fn scalar_lhs<const LANES: usize>() {
44+
test_helpers::test_binary_scalar_lhs_elementwise(
45+
<$scalar as core::ops::$trait<$vector>>::$fn,
46+
$scalar_fn,
47+
|_, _| true,
48+
);
49+
}
50+
51+
fn assign<const LANES: usize>() {
52+
test_helpers::test_binary_elementwise(
53+
|mut a, b| { <$vector as core::ops::$trait_assign>::$fn_assign(&mut a, b); a },
54+
$scalar_fn,
55+
|_, _| true,
56+
)
57+
}
58+
59+
fn assign_scalar_rhs<const LANES: usize>() {
60+
test_helpers::test_binary_scalar_rhs_elementwise(
61+
|mut a, b| { <$vector as core::ops::$trait_assign<$scalar>>::$fn_assign(&mut a, b); a },
62+
$scalar_fn,
63+
|_, _| true,
64+
)
65+
}
66+
}
67+
}
68+
};
69+
{ $vector:ty, $scalar:ty, $trait:ident :: $fn:ident, $trait_assign:ident :: $fn_assign:ident } => {
70+
impl_binary_op_test! { $vector, $scalar, $trait::$fn, $trait_assign::$fn_assign, <$scalar as core::ops::$trait>::$fn }
71+
};
72+
}
73+
74+
macro_rules! impl_binary_checked_op_test {
75+
{ $vector:ty, $scalar:ty, $trait:ident :: $fn:ident, $trait_assign:ident :: $fn_assign:ident, $scalar_fn:expr, $check_fn:expr } => {
76+
mod $fn {
77+
use super::*;
78+
79+
test_helpers::test_lanes! {
80+
fn normal<const LANES: usize>() {
81+
test_helpers::test_binary_elementwise(
82+
<$vector as core::ops::$trait>::$fn,
83+
$scalar_fn,
84+
|x, y| x.iter().zip(y.iter()).all(|(x, y)| $check_fn(*x, *y)),
85+
);
86+
}
87+
88+
fn scalar_rhs<const LANES: usize>() {
89+
test_helpers::test_binary_scalar_rhs_elementwise(
90+
<$vector as core::ops::$trait<$scalar>>::$fn,
91+
$scalar_fn,
92+
|x, y| x.iter().all(|x| $check_fn(*x, y)),
93+
);
94+
}
95+
96+
fn scalar_lhs<const LANES: usize>() {
97+
test_helpers::test_binary_scalar_lhs_elementwise(
98+
<$scalar as core::ops::$trait<$vector>>::$fn,
99+
$scalar_fn,
100+
|x, y| y.iter().all(|y| $check_fn(x, *y)),
101+
);
102+
}
103+
104+
fn assign<const LANES: usize>() {
105+
test_helpers::test_binary_elementwise(
106+
|mut a, b| { <$vector as core::ops::$trait_assign>::$fn_assign(&mut a, b); a },
107+
$scalar_fn,
108+
|x, y| x.iter().zip(y.iter()).all(|(x, y)| $check_fn(*x, *y)),
109+
)
110+
}
111+
112+
fn assign_scalar_rhs<const LANES: usize>() {
113+
test_helpers::test_binary_scalar_rhs_elementwise(
114+
|mut a, b| { <$vector as core::ops::$trait_assign<$scalar>>::$fn_assign(&mut a, b); a },
115+
$scalar_fn,
116+
|x, y| x.iter().all(|x| $check_fn(*x, y)),
117+
)
118+
}
119+
}
120+
}
121+
};
122+
{ $vector:ty, $scalar:ty, $trait:ident :: $fn:ident, $trait_assign:ident :: $fn_assign:ident, $check_fn:expr } => {
123+
impl_binary_nonzero_rhs_op_test! { $vector, $scalar, $trait::$fn, $trait_assign::$fn_assign, <$scalar as core::ops::$trait>::$fn, $check_fn }
124+
};
125+
}
126+
127+
macro_rules! impl_signed_tests {
128+
{ $vector:ident, $scalar:tt } => {
129+
mod $scalar {
130+
type Vector<const LANES: usize> = core_simd::$vector<LANES>;
131+
type Scalar = $scalar;
132+
133+
test_helpers::test_lanes! {
134+
fn neg<const LANES: usize>() {
135+
test_helpers::test_unary_elementwise(
136+
<Vector<LANES> as core::ops::Neg>::neg,
137+
<Scalar as core::ops::Neg>::neg,
138+
|x| !x.contains(&Scalar::MIN),
139+
);
140+
}
141+
}
142+
143+
impl_binary_op_test!(Vector<LANES>, Scalar, Add::add, AddAssign::add_assign, Scalar::wrapping_add);
144+
impl_binary_op_test!(Vector<LANES>, Scalar, Sub::sub, SubAssign::sub_assign, Scalar::wrapping_sub);
145+
impl_binary_op_test!(Vector<LANES>, Scalar, Mul::mul, MulAssign::mul_assign, Scalar::wrapping_mul);
146+
impl_binary_checked_op_test!(Vector<LANES>, Scalar, Div::div, DivAssign::div_assign, Scalar::wrapping_div, |x, y| y != 0 && !(x == Scalar::MIN && y == -1));
147+
impl_binary_checked_op_test!(Vector<LANES>, Scalar, Rem::rem, RemAssign::rem_assign, Scalar::wrapping_rem, |x, y| y != 0 && !(x == Scalar::MIN && y == -1));
148+
149+
impl_unary_op_test!(Vector<LANES>, Scalar, Not::not);
150+
impl_binary_op_test!(Vector<LANES>, Scalar, BitAnd::bitand, BitAndAssign::bitand_assign);
151+
impl_binary_op_test!(Vector<LANES>, Scalar, BitOr::bitor, BitOrAssign::bitor_assign);
152+
impl_binary_op_test!(Vector<LANES>, Scalar, BitXor::bitxor, BitXorAssign::bitxor_assign);
153+
}
154+
}
155+
}
156+
157+
macro_rules! impl_unsigned_tests {
158+
{ $vector:ident, $scalar:tt } => {
159+
mod $scalar {
160+
type Vector<const LANES: usize> = core_simd::$vector<LANES>;
161+
type Scalar = $scalar;
162+
163+
impl_binary_op_test!(Vector<LANES>, Scalar, Add::add, AddAssign::add_assign, Scalar::wrapping_add);
164+
impl_binary_op_test!(Vector<LANES>, Scalar, Sub::sub, SubAssign::sub_assign, Scalar::wrapping_sub);
165+
impl_binary_op_test!(Vector<LANES>, Scalar, Mul::mul, MulAssign::mul_assign, Scalar::wrapping_mul);
166+
impl_binary_checked_op_test!(Vector<LANES>, Scalar, Div::div, DivAssign::div_assign, Scalar::wrapping_div, |_, y| y != 0);
167+
impl_binary_checked_op_test!(Vector<LANES>, Scalar, Rem::rem, RemAssign::rem_assign, Scalar::wrapping_rem, |_, y| y != 0);
168+
169+
impl_unary_op_test!(Vector<LANES>, Scalar, Not::not);
170+
impl_binary_op_test!(Vector<LANES>, Scalar, BitAnd::bitand, BitAndAssign::bitand_assign);
171+
impl_binary_op_test!(Vector<LANES>, Scalar, BitOr::bitor, BitOrAssign::bitor_assign);
172+
impl_binary_op_test!(Vector<LANES>, Scalar, BitXor::bitxor, BitXorAssign::bitxor_assign);
173+
}
174+
}
175+
}
176+
177+
impl_signed_tests! { SimdI8, i8 }
178+
impl_signed_tests! { SimdI16, i16 }
179+
impl_signed_tests! { SimdI32, i32 }
180+
impl_signed_tests! { SimdI64, i64 }
181+
impl_signed_tests! { SimdI128, i128 }
182+
impl_signed_tests! { SimdIsize, isize }
183+
184+
impl_unsigned_tests! { SimdU8, u8 }
185+
impl_unsigned_tests! { SimdU16, u16 }
186+
impl_unsigned_tests! { SimdU32, u32 }
187+
impl_unsigned_tests! { SimdU64, u64 }
188+
impl_unsigned_tests! { SimdU128, u128 }
189+
impl_unsigned_tests! { SimdUsize, usize }

crates/test_helpers/src/lib.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,15 @@ pub fn test_2<A: core::fmt::Debug + DefaultStrategy, B: core::fmt::Debug + Defau
6262
pub fn test_unary_elementwise<Scalar, ScalarResult, Vector, VectorResult, const LANES: usize>(
6363
fv: impl Fn(Vector) -> VectorResult,
6464
fs: impl Fn(Scalar) -> ScalarResult,
65+
check: impl Fn([Scalar; LANES]) -> bool,
6566
) where
6667
Scalar: Copy + Default + core::fmt::Debug + DefaultStrategy,
6768
ScalarResult: Copy + Default + biteq::BitEq + core::fmt::Debug + DefaultStrategy,
6869
Vector: Into<[Scalar; LANES]> + From<[Scalar; LANES]> + Copy,
6970
VectorResult: Into<[ScalarResult; LANES]> + From<[ScalarResult; LANES]> + Copy,
7071
{
7172
test_1(|x: [Scalar; LANES]| {
73+
proptest::prop_assume!(check(x));
7274
let result_1: [ScalarResult; LANES] = fv(x.into()).into();
7375
let result_2: [ScalarResult; LANES] = {
7476
let mut result = [ScalarResult::default(); LANES];
@@ -93,6 +95,7 @@ pub fn test_binary_elementwise<
9395
>(
9496
fv: impl Fn(Vector1, Vector2) -> VectorResult,
9597
fs: impl Fn(Scalar1, Scalar2) -> ScalarResult,
98+
check: impl Fn([Scalar1; LANES], [Scalar2; LANES]) -> bool,
9699
) where
97100
Scalar1: Copy + Default + core::fmt::Debug + DefaultStrategy,
98101
Scalar2: Copy + Default + core::fmt::Debug + DefaultStrategy,
@@ -102,6 +105,7 @@ pub fn test_binary_elementwise<
102105
VectorResult: Into<[ScalarResult; LANES]> + From<[ScalarResult; LANES]> + Copy,
103106
{
104107
test_2(|x: [Scalar1; LANES], y: [Scalar2; LANES]| {
108+
proptest::prop_assume!(check(x, y));
105109
let result_1: [ScalarResult; LANES] = fv(x.into(), y.into()).into();
106110
let result_2: [ScalarResult; LANES] = {
107111
let mut result = [ScalarResult::default(); LANES];
@@ -125,6 +129,7 @@ pub fn test_binary_scalar_rhs_elementwise<
125129
>(
126130
fv: impl Fn(Vector, Scalar2) -> VectorResult,
127131
fs: impl Fn(Scalar1, Scalar2) -> ScalarResult,
132+
check: impl Fn([Scalar1; LANES], Scalar2) -> bool,
128133
) where
129134
Scalar1: Copy + Default + core::fmt::Debug + DefaultStrategy,
130135
Scalar2: Copy + Default + core::fmt::Debug + DefaultStrategy,
@@ -133,6 +138,7 @@ pub fn test_binary_scalar_rhs_elementwise<
133138
VectorResult: Into<[ScalarResult; LANES]> + From<[ScalarResult; LANES]> + Copy,
134139
{
135140
test_2(|x: [Scalar1; LANES], y: Scalar2| {
141+
proptest::prop_assume!(check(x, y));
136142
let result_1: [ScalarResult; LANES] = fv(x.into(), y).into();
137143
let result_2: [ScalarResult; LANES] = {
138144
let mut result = [ScalarResult::default(); LANES];
@@ -156,6 +162,7 @@ pub fn test_binary_scalar_lhs_elementwise<
156162
>(
157163
fv: impl Fn(Scalar1, Vector) -> VectorResult,
158164
fs: impl Fn(Scalar1, Scalar2) -> ScalarResult,
165+
check: impl Fn(Scalar1, [Scalar2; LANES]) -> bool,
159166
) where
160167
Scalar1: Copy + Default + core::fmt::Debug + DefaultStrategy,
161168
Scalar2: Copy + Default + core::fmt::Debug + DefaultStrategy,
@@ -164,6 +171,7 @@ pub fn test_binary_scalar_lhs_elementwise<
164171
VectorResult: Into<[ScalarResult; LANES]> + From<[ScalarResult; LANES]> + Copy,
165172
{
166173
test_2(|x: Scalar1, y: [Scalar2; LANES]| {
174+
proptest::prop_assume!(check(x, y));
167175
let result_1: [ScalarResult; LANES] = fv(x, y.into()).into();
168176
let result_2: [ScalarResult; LANES] = {
169177
let mut result = [ScalarResult::default(); LANES];
@@ -215,9 +223,6 @@ macro_rules! test_lanes {
215223
lanes_8 => 8,
216224
lanes_16 => 16,
217225
lanes_32 => 32,
218-
lanes_64 => 64,
219-
lanes_128 => 128,
220-
lanes_256 => 256,
221226
}
222227
)*
223228
}

0 commit comments

Comments
 (0)