Skip to content

Commit 83c4eea

Browse files
committed
Document methods for masked store
1 parent 97af6d7 commit 83c4eea

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

crates/core_simd/src/vector.rs

+46
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,24 @@ where
630630
unsafe { core::intrinsics::simd::simd_gather(or, source, enable.to_int()) }
631631
}
632632

633+
/// Conditionally write contiguous elements to `slice`. The `enable` mask controls
634+
/// which elements are written, as long as they're in-bounds of the `slice`.
635+
/// If the element is disabled or out of bounds, no memory access to that location
636+
/// is made.
637+
///
638+
/// # Examples
639+
/// ```
640+
/// # #![feature(portable_simd)]
641+
/// # #[cfg(feature = "as_crate")] use core_simd::simd;
642+
/// # #[cfg(not(feature = "as_crate"))] use core::simd;
643+
/// # use simd::{Simd, Mask};
644+
/// let mut arr = [0i32; 4];
645+
/// let write = Simd::from_array([-5, -4, -3, -2]);
646+
/// let enable = Mask::from_array([false, true, true, true]);
647+
///
648+
/// write.masked_store(&mut arr[..3], enable);
649+
/// assert_eq!(arr, [0, -4, -3, 0]);
650+
/// ```
633651
#[inline]
634652
pub fn masked_store(self, slice: &mut [T], mut enable: Mask<<T as SimdElement>::Mask, N>) {
635653
enable &= mask_up_to(slice.len());
@@ -638,6 +656,26 @@ where
638656
unsafe { self.masked_store_ptr(slice.as_mut_ptr(), enable) }
639657
}
640658

659+
/// Conditionally write contiguous elements to `slice`. The `enable` mask controls
660+
/// which elements are written.
661+
///
662+
/// # Safety
663+
///
664+
/// Every enabled element must be in bounds for the `slice`.
665+
///
666+
/// # Examples
667+
/// ```
668+
/// # #![feature(portable_simd)]
669+
/// # #[cfg(feature = "as_crate")] use core_simd::simd;
670+
/// # #[cfg(not(feature = "as_crate"))] use core::simd;
671+
/// # use simd::{Simd, Mask};
672+
/// let mut arr = [0i32; 4];
673+
/// let write = Simd::from_array([-5, -4, -3, -2]);
674+
/// let enable = Mask::from_array([false, true, true, true]);
675+
///
676+
/// unsafe { write.masked_store_unchecked(&mut arr, enable) };
677+
/// assert_eq!(arr, [0, -4, -3, -3]);
678+
/// ```
641679
#[inline]
642680
pub unsafe fn masked_store_unchecked(
643681
self,
@@ -649,6 +687,14 @@ where
649687
unsafe { self.masked_store_ptr(ptr, enable) }
650688
}
651689

690+
/// Conditionally write contiguous elements starting from `ptr`.
691+
/// The `enable` mask controls which elements are written.
692+
/// When disabled, the memory location corresponding to that element is not accessed.
693+
///
694+
/// # Safety
695+
///
696+
/// Memory addresses for element are calculated [`core::ptr::wrapping_offset`] and
697+
/// each enabled element must satisfy the same conditions as [`core::ptr::write`].
652698
#[inline]
653699
pub unsafe fn masked_store_ptr(self, ptr: *mut T, enable: Mask<<T as SimdElement>::Mask, N>) {
654700
// SAFETY: The safety of writing elements through `ptr` is ensured by the caller.

0 commit comments

Comments
 (0)