|
| 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 } |
0 commit comments