Skip to content

Commit 1529ed4

Browse files
Document and test doubled writes in scatter
1 parent f38659a commit 1529ed4

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

crates/core_simd/src/array.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,15 @@ where
8888

8989
/// SIMD scatter: write a SIMD vector's values into a slice, using potentially discontiguous indices.
9090
/// Out-of-bounds indices are not written.
91+
/// `scatter` writes "in order", so if an index receives two writes, only the last is guaranteed.
9192
/// ```
9293
/// # use core_simd::*;
9394
/// let mut vec: Vec<i32> = vec![10, 11, 12, 13, 14, 15, 16, 17, 18];
94-
/// let idxs = SimdUsize::<4>::from_array([9, 3, 0, 5]);
95-
/// let vals = SimdI32::from_array([-5, -4, -3, -2]);
95+
/// let idxs = SimdUsize::<4>::from_array([9, 3, 0, 0]);
96+
/// let vals = SimdI32::from_array([-27, 82, -41, 124]);
9697
///
97-
/// vals.scatter(&mut vec, idxs);
98-
/// assert_eq!(vec, vec![-3, 11, 12, -4, 14, -2, 16, 17, 18]);
98+
/// vals.scatter(&mut vec, idxs); // index 0 receives two writes.
99+
/// assert_eq!(vec, vec![124, 11, 12, 82, 14, 15, 16, 17, 18]);
99100
/// ```
100101
#[inline]
101102
fn scatter(self, slice: &mut [Self::Scalar], idxs: SimdUsize<LANES>) {
@@ -104,15 +105,16 @@ where
104105

105106
/// SIMD scatter: write a SIMD vector's values into a slice, using potentially discontiguous indices.
106107
/// Out-of-bounds or masked indices are not written.
108+
/// `scatter_select` writes "in order", so if an index receives two writes, only the last is guaranteed.
107109
/// ```
108110
/// # use core_simd::*;
109111
/// let mut vec: Vec<i32> = vec![10, 11, 12, 13, 14, 15, 16, 17, 18];
110-
/// let idxs = SimdUsize::<4>::from_array([9, 3, 0, 5]);
111-
/// let vals = SimdI32::from_array([-5, -4, -3, -2]);
112+
/// let idxs = SimdUsize::<4>::from_array([9, 3, 0, 0]);
113+
/// let vals = SimdI32::from_array([-27, 82, -41, 124]);
112114
/// let mask = MaskSize::from_array([true, true, true, false]); // Note the mask of the last lane.
113115
///
114-
/// vals.scatter_select(&mut vec, mask, idxs);
115-
/// assert_eq!(vec, vec![-3, 11, 12, -4, 14, 15, 16, 17, 18]);
116+
/// vals.scatter_select(&mut vec, mask, idxs); // index 0's second write is masked, thus omitted.
117+
/// assert_eq!(vec, vec![-41, 11, 12, 82, 14, 15, 16, 17, 18]);
116118
/// ```
117119
#[inline]
118120
fn scatter_select(

0 commit comments

Comments
 (0)