diff options
| author | Samuel Shepard <vfn4@cdc.gov> | 2024-09-27 19:18:30 -0400 |
|---|---|---|
| committer | Samuel Shepard <vfn4@cdc.gov> | 2024-09-27 19:18:30 -0400 |
| commit | f5fea5702863eb9ff30b42ee4e8942d9f70eea54 (patch) | |
| tree | dfa30a5dc29e4d0e1e912e27f57e4c23885f836a | |
| parent | 55b4b74a120dc83317f143b8a7ce292f6afd34ea (diff) | |
| download | rust-f5fea5702863eb9ff30b42ee4e8942d9f70eea54.tar.gz rust-f5fea5702863eb9ff30b42ee4e8942d9f70eea54.zip | |
Change API to accept a `padding` argument
| -rw-r--r-- | crates/core_simd/src/swizzle.rs | 44 | ||||
| -rw-r--r-- | crates/core_simd/tests/swizzle.rs | 24 |
2 files changed, 28 insertions, 40 deletions
diff --git a/crates/core_simd/src/swizzle.rs b/crates/core_simd/src/swizzle.rs index cf1e08aa668..6353196e4cf 100644 --- a/crates/core_simd/src/swizzle.rs +++ b/crates/core_simd/src/swizzle.rs @@ -251,14 +251,11 @@ where Rotate::<OFFSET>::swizzle(self) } - /// Shifts the vector elements to the left by `OFFSET`, padding by the - /// default value (e.g., zero) to the right. + /// Shifts the vector elements to the left by `OFFSET`, filling in with + /// `padding` from the right. #[inline] #[must_use = "method returns a new vector and does not mutate the original inputs"] - pub fn shift_elements_left<const OFFSET: usize>(self) -> Self - where - T: Default, - { + pub fn shift_elements_left<const OFFSET: usize>(self, padding: T) -> Self { struct Shift<const OFFSET: usize>; impl<const OFFSET: usize, const N: usize> Swizzle<N> for Shift<OFFSET> { @@ -273,17 +270,14 @@ where }; } - Shift::<OFFSET>::concat_swizzle(self, Self::default()) + Shift::<OFFSET>::concat_swizzle(self, Simd::splat(padding)) } - /// Shifts the vector elements to the right by `OFFSET`, padding by the - /// default value (e.g., zero) from the left. + /// Shifts the vector elements to the right by `OFFSET`, filling in with + /// `padding` from the left. #[inline] #[must_use = "method returns a new vector and does not mutate the original inputs"] - pub fn shift_elements_right<const OFFSET: usize>(self) -> Self - where - T: Default, - { + pub fn shift_elements_right<const OFFSET: usize>(self, padding: T) -> Self { struct Shift<const OFFSET: usize>; impl<const OFFSET: usize, const N: usize> Swizzle<N> for Shift<OFFSET> { @@ -298,7 +292,7 @@ where }; } - Shift::<OFFSET>::concat_swizzle(self, Self::default()) + Shift::<OFFSET>::concat_swizzle(self, Simd::splat(padding)) } /// Interleave two vectors. @@ -501,28 +495,22 @@ where unsafe { Self::from_int_unchecked(self.to_int().rotate_elements_right::<OFFSET>()) } } - /// Shifts the mask elements to the left by `OFFSET`, padding by the - /// default value (e.g., zero) to the right. + /// Shifts the mask elements to the left by `OFFSET`, filling in with + /// `padding` from the right. #[inline] #[must_use = "method returns a new vector and does not mutate the original inputs"] - pub fn shift_elements_left<const OFFSET: usize>(self) -> Self - where - T: Default, - { + pub fn shift_elements_left<const OFFSET: usize>(self, padding: T) -> Self { // Safety: swizzles are safe for masks - unsafe { Self::from_int_unchecked(self.to_int().shift_elements_left::<OFFSET>()) } + unsafe { Self::from_int_unchecked(self.to_int().shift_elements_left::<OFFSET>(padding)) } } - /// Shifts the mask elements to the right by `OFFSET`, padding by the - /// default value (e.g., `false`) from the left. + /// Shifts the mask elements to the right by `OFFSET`, filling in with + /// `padding` from the left. #[inline] #[must_use = "method returns a new vector and does not mutate the original inputs"] - pub fn shift_elements_right<const OFFSET: usize>(self) -> Self - where - T: Default, - { + pub fn shift_elements_right<const OFFSET: usize>(self, padding: T) -> Self { // Safety: swizzles are safe for masks - unsafe { Self::from_int_unchecked(self.to_int().shift_elements_right::<OFFSET>()) } + unsafe { Self::from_int_unchecked(self.to_int().shift_elements_right::<OFFSET>(padding)) } } /// Interleave two masks. diff --git a/crates/core_simd/tests/swizzle.rs b/crates/core_simd/tests/swizzle.rs index 98045fc5c54..7001e5f6bf8 100644 --- a/crates/core_simd/tests/swizzle.rs +++ b/crates/core_simd/tests/swizzle.rs @@ -52,18 +52,18 @@ fn rotate() { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn shift() { let a = Simd::from_array([1, 2, 3, 4]); - assert_eq!(a.shift_elements_left::<0>().to_array(), [1, 2, 3, 4]); - assert_eq!(a.shift_elements_left::<1>().to_array(), [2, 3, 4, 0]); - assert_eq!(a.shift_elements_left::<2>().to_array(), [3, 4, 0, 0]); - assert_eq!(a.shift_elements_left::<3>().to_array(), [4, 0, 0, 0]); - assert_eq!(a.shift_elements_left::<4>().to_array(), [0, 0, 0, 0]); - assert_eq!(a.shift_elements_left::<5>().to_array(), [0, 0, 0, 0]); - assert_eq!(a.shift_elements_right::<0>().to_array(), [1, 2, 3, 4]); - assert_eq!(a.shift_elements_right::<1>().to_array(), [0, 1, 2, 3]); - assert_eq!(a.shift_elements_right::<2>().to_array(), [0, 0, 1, 2]); - assert_eq!(a.shift_elements_right::<3>().to_array(), [0, 0, 0, 1]); - assert_eq!(a.shift_elements_right::<4>().to_array(), [0, 0, 0, 0]); - assert_eq!(a.shift_elements_right::<5>().to_array(), [0, 0, 0, 0]); + assert_eq!(a.shift_elements_left::<0>(0).to_array(), [1, 2, 3, 4]); + assert_eq!(a.shift_elements_left::<1>(0).to_array(), [2, 3, 4, 0]); + assert_eq!(a.shift_elements_left::<2>(9).to_array(), [3, 4, 9, 9]); + assert_eq!(a.shift_elements_left::<3>(8).to_array(), [4, 8, 8, 8]); + assert_eq!(a.shift_elements_left::<4>(7).to_array(), [7, 7, 7, 7]); + assert_eq!(a.shift_elements_left::<5>(6).to_array(), [6, 6, 6, 6]); + assert_eq!(a.shift_elements_right::<0>(0).to_array(), [1, 2, 3, 4]); + assert_eq!(a.shift_elements_right::<1>(0).to_array(), [0, 1, 2, 3]); + assert_eq!(a.shift_elements_right::<2>(-1).to_array(), [-1, -1, 1, 2]); + assert_eq!(a.shift_elements_right::<3>(-2).to_array(), [-2, -2, -2, 1]); + assert_eq!(a.shift_elements_right::<4>(-3).to_array(), [-3, -3, -3, -3]); + assert_eq!(a.shift_elements_right::<5>(-4).to_array(), [-4, -4, -4, -4]); } #[test] |
