Skip to content

Commit e449630

Browse files
committed
Add copy_to_slice
1 parent 9bd30e7 commit e449630

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

crates/core_simd/src/vector.rs

+30-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ where
159159
///
160160
/// Panics if the slice's length is less than the vector's `Simd::LANES`.
161161
///
162-
/// # Examples
162+
/// # Example
163163
///
164164
/// ```
165165
/// # #![feature(portable_simd)]
@@ -180,6 +180,35 @@ where
180180
unsafe { slice.as_ptr().cast::<Self>().read_unaligned() }
181181
}
182182

183+
/// Writes a SIMD vector to the first `LANES` elements of a slice.
184+
///
185+
/// # Panics
186+
///
187+
/// Panics if the slice's length is less than the vector's `Simd::LANES`.
188+
///
189+
/// # Example
190+
///
191+
/// ```
192+
/// # #![feature(portable_simd)]
193+
/// # #[cfg(feature = "as_crate")] use core_simd::simd;
194+
/// # #[cfg(not(feature = "as_crate"))] use core::simd;
195+
/// # use simd::u32x4;
196+
/// let mut dest = vec![0; 6];
197+
/// let v = u32x4::from_array([1, 2, 3, 4]);
198+
/// v.copy_to_slice(&mut dest);
199+
/// assert_eq!(&dest, &[1, 2, 3, 4, 0, 0]);
200+
/// ```
201+
pub fn copy_to_slice(self, slice: &mut [T]) {
202+
assert!(
203+
slice.len() >= LANES,
204+
"slice length must be at least the number of lanes"
205+
);
206+
// Safety:
207+
// - We've checked the length is sufficient
208+
// - `T` and `Simd<T, N>` are Copy types.
209+
unsafe { slice.as_mut_ptr().cast::<Self>().write_unaligned(self) }
210+
}
211+
183212
/// Performs lanewise conversion of a SIMD vector's elements to another SIMD-valid type.
184213
///
185214
/// This follows the semantics of Rust's `as` conversion for casting

0 commit comments

Comments
 (0)