From 9ed3c51362958356639ecd9a8a0a4cb88a018b37 Mon Sep 17 00:00:00 2001 From: Caleb Zulawski Date: Mon, 6 Jun 2022 11:25:22 -0400 Subject: [PATCH] Add splat_lane --- crates/core_simd/src/swizzle.rs | 15 +++++++++++++++ crates/core_simd/tests/swizzle.rs | 10 ++++++++++ 2 files changed, 25 insertions(+) diff --git a/crates/core_simd/src/swizzle.rs b/crates/core_simd/src/swizzle.rs index 22999d24950..7a8b52dd3cf 100644 --- a/crates/core_simd/src/swizzle.rs +++ b/crates/core_simd/src/swizzle.rs @@ -188,6 +188,21 @@ where T: SimdElement, LaneCount: SupportedLaneCount, { + /// Splat a lane to the other lanes in the vector. + /// + /// Equivalent to `Simd::splat(self[LANE])`, but may be faster. + #[inline] + #[must_use = "method returns a new vector and does not mutate the original inputs"] + pub fn splat_lane(self) -> Self { + struct Splat; + + impl Swizzle for Splat { + const INDEX: [usize; LANES] = [LANE; LANES]; + } + + Splat::::swizzle(self) + } + /// Reverse the order of the lanes in the vector. #[inline] #[must_use = "method returns a new vector and does not mutate the original inputs"] diff --git a/crates/core_simd/tests/swizzle.rs b/crates/core_simd/tests/swizzle.rs index 51c63611aba..638aea539ff 100644 --- a/crates/core_simd/tests/swizzle.rs +++ b/crates/core_simd/tests/swizzle.rs @@ -23,6 +23,16 @@ fn swizzle() { assert_eq!(Index::swizzle(vector).to_array(), [4, 4]); } +#[test] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] +fn splat_lane() { + let a = Simd::from_array([1, 2, 3, 4]); + assert_eq!(a.splat_lane::<0>().to_array(), [1; 4]); + assert_eq!(a.splat_lane::<1>().to_array(), [2; 4]); + assert_eq!(a.splat_lane::<2>().to_array(), [3; 4]); + assert_eq!(a.splat_lane::<3>().to_array(), [4; 4]); +} + #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn reverse() {