Skip to content

Commit c14f2fc

Browse files
authoredMar 18, 2025··
Merge pull request #452 from folkertdev/element-rotate-example
add examples for `shift_elements_{left, right}` and `rotate_elements_{left, right}`
2 parents abd5d05 + 85a494b commit c14f2fc

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
 

‎crates/core_simd/src/swizzle.rs

+44
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,17 @@ where
214214
/// Rotates the vector such that the first `OFFSET` elements of the slice move to the end
215215
/// while the last `self.len() - OFFSET` elements move to the front. After calling `rotate_elements_left`,
216216
/// the element previously at index `OFFSET` will become the first element in the slice.
217+
/// ```
218+
/// # #![feature(portable_simd)]
219+
/// # #[cfg(feature = "as_crate")] use core_simd::simd::Simd;
220+
/// # #[cfg(not(feature = "as_crate"))] use core::simd::Simd;
221+
/// let a = Simd::from_array([0, 1, 2, 3]);
222+
/// let x = a.rotate_elements_left::<3>();
223+
/// assert_eq!(x.to_array(), [3, 0, 1, 2]);
224+
///
225+
/// let y = a.rotate_elements_left::<7>();
226+
/// assert_eq!(y.to_array(), [3, 0, 1, 2]);
227+
/// ```
217228
#[inline]
218229
#[must_use = "method returns a new vector and does not mutate the original inputs"]
219230
pub fn rotate_elements_left<const OFFSET: usize>(self) -> Self {
@@ -238,6 +249,17 @@ where
238249
/// Rotates the vector such that the first `self.len() - OFFSET` elements of the vector move to
239250
/// the end while the last `OFFSET` elements move to the front. After calling `rotate_elements_right`,
240251
/// the element previously at index `self.len() - OFFSET` will become the first element in the slice.
252+
/// ```
253+
/// # #![feature(portable_simd)]
254+
/// # #[cfg(feature = "as_crate")] use core_simd::simd::Simd;
255+
/// # #[cfg(not(feature = "as_crate"))] use core::simd::Simd;
256+
/// let a = Simd::from_array([0, 1, 2, 3]);
257+
/// let x = a.rotate_elements_right::<3>();
258+
/// assert_eq!(x.to_array(), [1, 2, 3, 0]);
259+
///
260+
/// let y = a.rotate_elements_right::<7>();
261+
/// assert_eq!(y.to_array(), [1, 2, 3, 0]);
262+
/// ```
241263
#[inline]
242264
#[must_use = "method returns a new vector and does not mutate the original inputs"]
243265
pub fn rotate_elements_right<const OFFSET: usize>(self) -> Self {
@@ -261,6 +283,17 @@ where
261283

262284
/// Shifts the vector elements to the left by `OFFSET`, filling in with
263285
/// `padding` from the right.
286+
/// ```
287+
/// # #![feature(portable_simd)]
288+
/// # #[cfg(feature = "as_crate")] use core_simd::simd::Simd;
289+
/// # #[cfg(not(feature = "as_crate"))] use core::simd::Simd;
290+
/// let a = Simd::from_array([0, 1, 2, 3]);
291+
/// let x = a.shift_elements_left::<3>(255);
292+
/// assert_eq!(x.to_array(), [3, 255, 255, 255]);
293+
///
294+
/// let y = a.shift_elements_left::<7>(255);
295+
/// assert_eq!(y.to_array(), [255, 255, 255, 255]);
296+
/// ```
264297
#[inline]
265298
#[must_use = "method returns a new vector and does not mutate the original inputs"]
266299
pub fn shift_elements_left<const OFFSET: usize>(self, padding: T) -> Self {
@@ -283,6 +316,17 @@ where
283316

284317
/// Shifts the vector elements to the right by `OFFSET`, filling in with
285318
/// `padding` from the left.
319+
/// ```
320+
/// # #![feature(portable_simd)]
321+
/// # #[cfg(feature = "as_crate")] use core_simd::simd::Simd;
322+
/// # #[cfg(not(feature = "as_crate"))] use core::simd::Simd;
323+
/// let a = Simd::from_array([0, 1, 2, 3]);
324+
/// let x = a.shift_elements_right::<3>(255);
325+
/// assert_eq!(x.to_array(), [255, 255, 255, 0]);
326+
///
327+
/// let y = a.shift_elements_right::<7>(255);
328+
/// assert_eq!(y.to_array(), [255, 255, 255, 255]);
329+
/// ```
286330
#[inline]
287331
#[must_use = "method returns a new vector and does not mutate the original inputs"]
288332
pub fn shift_elements_right<const OFFSET: usize>(self, padding: T) -> Self {

0 commit comments

Comments
 (0)
Please sign in to comment.