summaryrefslogtreecommitdiffstats
path: root/library/portable-simd/crates/core_simd/tests/swizzle_dyn.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-18 02:49:50 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-18 02:49:50 +0000
commit9835e2ae736235810b4ea1c162ca5e65c547e770 (patch)
tree3fcebf40ed70e581d776a8a4c65923e8ec20e026 /library/portable-simd/crates/core_simd/tests/swizzle_dyn.rs
parentReleasing progress-linux version 1.70.0+dfsg2-1~progress7.99u1. (diff)
downloadrustc-9835e2ae736235810b4ea1c162ca5e65c547e770.tar.xz
rustc-9835e2ae736235810b4ea1c162ca5e65c547e770.zip
Merging upstream version 1.71.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/portable-simd/crates/core_simd/tests/swizzle_dyn.rs')
-rw-r--r--library/portable-simd/crates/core_simd/tests/swizzle_dyn.rs74
1 files changed, 74 insertions, 0 deletions
diff --git a/library/portable-simd/crates/core_simd/tests/swizzle_dyn.rs b/library/portable-simd/crates/core_simd/tests/swizzle_dyn.rs
new file mode 100644
index 000000000..646cd5f33
--- /dev/null
+++ b/library/portable-simd/crates/core_simd/tests/swizzle_dyn.rs
@@ -0,0 +1,74 @@
+#![feature(portable_simd)]
+use core::{fmt, ops::RangeInclusive};
+use proptest;
+use test_helpers::{self, biteq, make_runner, prop_assert_biteq};
+
+fn swizzle_dyn_scalar_ver<const N: usize>(values: [u8; N], idxs: [u8; N]) -> [u8; N] {
+ let mut array = [0; N];
+ for (i, k) in idxs.into_iter().enumerate() {
+ if (k as usize) < N {
+ array[i] = values[k as usize];
+ };
+ }
+ array
+}
+
+test_helpers::test_lanes! {
+ fn swizzle_dyn<const N: usize>() {
+ match_simd_with_fallback(
+ &core_simd::simd::Simd::<u8, N>::swizzle_dyn,
+ &swizzle_dyn_scalar_ver,
+ &|_, _| true,
+ );
+ }
+}
+
+fn match_simd_with_fallback<Scalar, ScalarResult, Vector, VectorResult, const N: usize>(
+ fv: &dyn Fn(Vector, Vector) -> VectorResult,
+ fs: &dyn Fn([Scalar; N], [Scalar; N]) -> [ScalarResult; N],
+ check: &dyn Fn([Scalar; N], [Scalar; N]) -> bool,
+) where
+ Scalar: Copy + fmt::Debug + SwizzleStrategy,
+ ScalarResult: Copy + biteq::BitEq + fmt::Debug + SwizzleStrategy,
+ Vector: Into<[Scalar; N]> + From<[Scalar; N]> + Copy,
+ VectorResult: Into<[ScalarResult; N]> + From<[ScalarResult; N]> + Copy,
+{
+ test_swizzles_2(&|x: [Scalar; N], y: [Scalar; N]| {
+ proptest::prop_assume!(check(x, y));
+ let result_v: [ScalarResult; N] = fv(x.into(), y.into()).into();
+ let result_s: [ScalarResult; N] = fs(x, y);
+ crate::prop_assert_biteq!(result_v, result_s);
+ Ok(())
+ });
+}
+
+fn test_swizzles_2<A: fmt::Debug + SwizzleStrategy, B: fmt::Debug + SwizzleStrategy>(
+ f: &dyn Fn(A, B) -> proptest::test_runner::TestCaseResult,
+) {
+ let mut runner = make_runner();
+ runner
+ .run(
+ &(A::swizzled_strategy(), B::swizzled_strategy()),
+ |(a, b)| f(a, b),
+ )
+ .unwrap();
+}
+
+pub trait SwizzleStrategy {
+ type Strategy: proptest::strategy::Strategy<Value = Self>;
+ fn swizzled_strategy() -> Self::Strategy;
+}
+
+impl SwizzleStrategy for u8 {
+ type Strategy = RangeInclusive<u8>;
+ fn swizzled_strategy() -> Self::Strategy {
+ 0..=64
+ }
+}
+
+impl<T: fmt::Debug + SwizzleStrategy, const N: usize> SwizzleStrategy for [T; N] {
+ type Strategy = test_helpers::array::UniformArrayStrategy<T::Strategy, Self>;
+ fn swizzled_strategy() -> Self::Strategy {
+ Self::Strategy::new(T::swizzled_strategy())
+ }
+}