diff --git a/crates/core_simd/src/vector.rs b/crates/core_simd/src/vector.rs index bef3a16c02e..c75e88f413e 100644 --- a/crates/core_simd/src/vector.rs +++ b/crates/core_simd/src/vector.rs @@ -630,6 +630,24 @@ where unsafe { core::intrinsics::simd::simd_gather(or, source, enable.to_int()) } } + /// Conditionally write contiguous elements to `slice`. The `enable` mask controls + /// which elements are written, as long as they're in-bounds of the `slice`. + /// If the element is disabled or out of bounds, no memory access to that location + /// is made. + /// + /// # Examples + /// ``` + /// # #![feature(portable_simd)] + /// # #[cfg(feature = "as_crate")] use core_simd::simd; + /// # #[cfg(not(feature = "as_crate"))] use core::simd; + /// # use simd::{Simd, Mask}; + /// let mut arr = [0i32; 4]; + /// let write = Simd::from_array([-5, -4, -3, -2]); + /// let enable = Mask::from_array([false, true, true, true]); + /// + /// write.masked_store(&mut arr[..3], enable); + /// assert_eq!(arr, [0, -4, -3, 0]); + /// ``` #[inline] pub fn masked_store(self, slice: &mut [T], mut enable: Mask<::Mask, N>) { enable &= mask_up_to(slice.len()); @@ -638,6 +656,26 @@ where unsafe { self.masked_store_ptr(slice.as_mut_ptr(), enable) } } + /// Conditionally write contiguous elements to `slice`. The `enable` mask controls + /// which elements are written. + /// + /// # Safety + /// + /// Every enabled element must be in bounds for the `slice`. + /// + /// # Examples + /// ``` + /// # #![feature(portable_simd)] + /// # #[cfg(feature = "as_crate")] use core_simd::simd; + /// # #[cfg(not(feature = "as_crate"))] use core::simd; + /// # use simd::{Simd, Mask}; + /// let mut arr = [0i32; 4]; + /// let write = Simd::from_array([-5, -4, -3, -2]); + /// let enable = Mask::from_array([false, true, true, true]); + /// + /// unsafe { write.masked_store_unchecked(&mut arr, enable) }; + /// assert_eq!(arr, [0, -4, -3, -3]); + /// ``` #[inline] pub unsafe fn masked_store_unchecked( self, @@ -649,6 +687,14 @@ where unsafe { self.masked_store_ptr(ptr, enable) } } + /// Conditionally write contiguous elements starting from `ptr`. + /// The `enable` mask controls which elements are written. + /// When disabled, the memory location corresponding to that element is not accessed. + /// + /// # Safety + /// + /// Memory addresses for element are calculated [`core::ptr::wrapping_offset`] and + /// each enabled element must satisfy the same conditions as [`core::ptr::write`]. #[inline] pub unsafe fn masked_store_ptr(self, ptr: *mut T, enable: Mask<::Mask, N>) { // SAFETY: The safety of writing elements through `ptr` is ensured by the caller.