@@ -630,6 +630,24 @@ where
630
630
unsafe { core:: intrinsics:: simd:: simd_gather ( or, source, enable. to_int ( ) ) }
631
631
}
632
632
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
+ /// ```
633
651
#[ inline]
634
652
pub fn masked_store ( self , slice : & mut [ T ] , mut enable : Mask < <T as SimdElement >:: Mask , N > ) {
635
653
enable &= mask_up_to ( slice. len ( ) ) ;
@@ -638,6 +656,26 @@ where
638
656
unsafe { self . masked_store_ptr ( slice. as_mut_ptr ( ) , enable) }
639
657
}
640
658
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
+ /// ```
641
679
#[ inline]
642
680
pub unsafe fn masked_store_unchecked (
643
681
self ,
@@ -649,6 +687,14 @@ where
649
687
unsafe { self . masked_store_ptr ( ptr, enable) }
650
688
}
651
689
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`].
652
698
#[ inline]
653
699
pub unsafe fn masked_store_ptr ( self , ptr : * mut T , enable : Mask < <T as SimdElement >:: Mask , N > ) {
654
700
// SAFETY: The safety of writing elements through `ptr` is ensured by the caller.
0 commit comments