Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit f5fea57

Browse files
committed
Change API to accept a padding argument
1 parent 55b4b74 commit f5fea57

File tree

2 files changed

+28
-40
lines changed

2 files changed

+28
-40
lines changed

crates/core_simd/src/swizzle.rs

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,11 @@ where
251251
Rotate::<OFFSET>::swizzle(self)
252252
}
253253

254-
/// Shifts the vector elements to the left by `OFFSET`, padding by the
255-
/// default value (e.g., zero) to the right.
254+
/// Shifts the vector elements to the left by `OFFSET`, filling in with
255+
/// `padding` from the right.
256256
#[inline]
257257
#[must_use = "method returns a new vector and does not mutate the original inputs"]
258-
pub fn shift_elements_left<const OFFSET: usize>(self) -> Self
259-
where
260-
T: Default,
261-
{
258+
pub fn shift_elements_left<const OFFSET: usize>(self, padding: T) -> Self {
262259
struct Shift<const OFFSET: usize>;
263260

264261
impl<const OFFSET: usize, const N: usize> Swizzle<N> for Shift<OFFSET> {
@@ -273,17 +270,14 @@ where
273270
};
274271
}
275272

276-
Shift::<OFFSET>::concat_swizzle(self, Self::default())
273+
Shift::<OFFSET>::concat_swizzle(self, Simd::splat(padding))
277274
}
278275

279-
/// Shifts the vector elements to the right by `OFFSET`, padding by the
280-
/// default value (e.g., zero) from the left.
276+
/// Shifts the vector elements to the right by `OFFSET`, filling in with
277+
/// `padding` from the left.
281278
#[inline]
282279
#[must_use = "method returns a new vector and does not mutate the original inputs"]
283-
pub fn shift_elements_right<const OFFSET: usize>(self) -> Self
284-
where
285-
T: Default,
286-
{
280+
pub fn shift_elements_right<const OFFSET: usize>(self, padding: T) -> Self {
287281
struct Shift<const OFFSET: usize>;
288282

289283
impl<const OFFSET: usize, const N: usize> Swizzle<N> for Shift<OFFSET> {
@@ -298,7 +292,7 @@ where
298292
};
299293
}
300294

301-
Shift::<OFFSET>::concat_swizzle(self, Self::default())
295+
Shift::<OFFSET>::concat_swizzle(self, Simd::splat(padding))
302296
}
303297

304298
/// Interleave two vectors.
@@ -501,28 +495,22 @@ where
501495
unsafe { Self::from_int_unchecked(self.to_int().rotate_elements_right::<OFFSET>()) }
502496
}
503497

504-
/// Shifts the mask elements to the left by `OFFSET`, padding by the
505-
/// default value (e.g., zero) to the right.
498+
/// Shifts the mask elements to the left by `OFFSET`, filling in with
499+
/// `padding` from the right.
506500
#[inline]
507501
#[must_use = "method returns a new vector and does not mutate the original inputs"]
508-
pub fn shift_elements_left<const OFFSET: usize>(self) -> Self
509-
where
510-
T: Default,
511-
{
502+
pub fn shift_elements_left<const OFFSET: usize>(self, padding: T) -> Self {
512503
// Safety: swizzles are safe for masks
513-
unsafe { Self::from_int_unchecked(self.to_int().shift_elements_left::<OFFSET>()) }
504+
unsafe { Self::from_int_unchecked(self.to_int().shift_elements_left::<OFFSET>(padding)) }
514505
}
515506

516-
/// Shifts the mask elements to the right by `OFFSET`, padding by the
517-
/// default value (e.g., `false`) from the left.
507+
/// Shifts the mask elements to the right by `OFFSET`, filling in with
508+
/// `padding` from the left.
518509
#[inline]
519510
#[must_use = "method returns a new vector and does not mutate the original inputs"]
520-
pub fn shift_elements_right<const OFFSET: usize>(self) -> Self
521-
where
522-
T: Default,
523-
{
511+
pub fn shift_elements_right<const OFFSET: usize>(self, padding: T) -> Self {
524512
// Safety: swizzles are safe for masks
525-
unsafe { Self::from_int_unchecked(self.to_int().shift_elements_right::<OFFSET>()) }
513+
unsafe { Self::from_int_unchecked(self.to_int().shift_elements_right::<OFFSET>(padding)) }
526514
}
527515

528516
/// Interleave two masks.

crates/core_simd/tests/swizzle.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,18 @@ fn rotate() {
5252
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
5353
fn shift() {
5454
let a = Simd::from_array([1, 2, 3, 4]);
55-
assert_eq!(a.shift_elements_left::<0>().to_array(), [1, 2, 3, 4]);
56-
assert_eq!(a.shift_elements_left::<1>().to_array(), [2, 3, 4, 0]);
57-
assert_eq!(a.shift_elements_left::<2>().to_array(), [3, 4, 0, 0]);
58-
assert_eq!(a.shift_elements_left::<3>().to_array(), [4, 0, 0, 0]);
59-
assert_eq!(a.shift_elements_left::<4>().to_array(), [0, 0, 0, 0]);
60-
assert_eq!(a.shift_elements_left::<5>().to_array(), [0, 0, 0, 0]);
61-
assert_eq!(a.shift_elements_right::<0>().to_array(), [1, 2, 3, 4]);
62-
assert_eq!(a.shift_elements_right::<1>().to_array(), [0, 1, 2, 3]);
63-
assert_eq!(a.shift_elements_right::<2>().to_array(), [0, 0, 1, 2]);
64-
assert_eq!(a.shift_elements_right::<3>().to_array(), [0, 0, 0, 1]);
65-
assert_eq!(a.shift_elements_right::<4>().to_array(), [0, 0, 0, 0]);
66-
assert_eq!(a.shift_elements_right::<5>().to_array(), [0, 0, 0, 0]);
55+
assert_eq!(a.shift_elements_left::<0>(0).to_array(), [1, 2, 3, 4]);
56+
assert_eq!(a.shift_elements_left::<1>(0).to_array(), [2, 3, 4, 0]);
57+
assert_eq!(a.shift_elements_left::<2>(9).to_array(), [3, 4, 9, 9]);
58+
assert_eq!(a.shift_elements_left::<3>(8).to_array(), [4, 8, 8, 8]);
59+
assert_eq!(a.shift_elements_left::<4>(7).to_array(), [7, 7, 7, 7]);
60+
assert_eq!(a.shift_elements_left::<5>(6).to_array(), [6, 6, 6, 6]);
61+
assert_eq!(a.shift_elements_right::<0>(0).to_array(), [1, 2, 3, 4]);
62+
assert_eq!(a.shift_elements_right::<1>(0).to_array(), [0, 1, 2, 3]);
63+
assert_eq!(a.shift_elements_right::<2>(-1).to_array(), [-1, -1, 1, 2]);
64+
assert_eq!(a.shift_elements_right::<3>(-2).to_array(), [-2, -2, -2, 1]);
65+
assert_eq!(a.shift_elements_right::<4>(-3).to_array(), [-3, -3, -3, -3]);
66+
assert_eq!(a.shift_elements_right::<5>(-4).to_array(), [-4, -4, -4, -4]);
6767
}
6868

6969
#[test]

0 commit comments

Comments
 (0)