57
57
self . 0
58
58
}
59
59
60
- /// SIMD gather: construct a SIMD vector by reading from a slice, using potentially discontiguous indices.
61
- /// If an index is out of bounds, that lane instead selects the value from the "or" vector.
60
+ /// Reads from potentially discontiguous indices in `slice` to construct a SIMD vector.
61
+ /// Lanes given an out-of-bounds index instead select values from the `or` vector.
62
+ ///
63
+ /// # Examples
62
64
/// ```
63
65
/// # #![feature(portable_simd)]
64
66
/// # #[cfg(feature = "std")] use core_simd::Simd;
76
78
Self :: gather_select ( slice, Mask :: splat ( true ) , idxs, or)
77
79
}
78
80
79
- /// SIMD gather: construct a SIMD vector by reading from a slice, using potentially discontiguous indices.
80
- /// Out-of-bounds indices instead use the default value for that lane (0).
81
+ /// Reads from potentially discontiguous indices in `slice` to construct a SIMD vector.
82
+ /// Lanes given an out-of-bounds index instead are set the default value for the type.
83
+ ///
84
+ /// # Examples
81
85
/// ```
82
86
/// # #![feature(portable_simd)]
83
87
/// # #[cfg(feature = "std")] use core_simd::Simd;
@@ -97,69 +101,82 @@ where
97
101
Self :: gather_or ( slice, idxs, Self :: splat ( T :: default ( ) ) )
98
102
}
99
103
100
- /// SIMD gather: construct a SIMD vector by reading from a slice, using potentially discontiguous indices.
101
- /// Out-of-bounds or masked indices instead select the value from the "or" vector.
104
+ /// Reads from potentially discontiguous indices in `slice` to construct a SIMD vector.
105
+ /// The mask `enable`s all `true` lanes and disables all `false` lanes.
106
+ /// If an index is disabled or is out-of-bounds, the lane is selected from the `or` vector.
107
+ ///
108
+ /// # Examples
102
109
/// ```
103
110
/// # #![feature(portable_simd)]
104
111
/// # #[cfg(feature = "std")] use core_simd::{Simd, Mask};
105
112
/// # #[cfg(not(feature = "std"))] use core::simd::{Simd, Mask};
106
113
/// let vec: Vec<i32> = vec![10, 11, 12, 13, 14, 15, 16, 17, 18];
107
114
/// let idxs = Simd::from_array([9, 3, 0, 5]);
108
115
/// let alt = Simd::from_array([-5, -4, -3, -2]);
109
- /// let mask = Mask::from_array([true, true, true, false]); // Note the mask of the last lane.
116
+ /// let enable = Mask::from_array([true, true, true, false]); // Note the mask of the last lane.
110
117
///
111
- /// let result = Simd::gather_select(&vec, mask , idxs, alt); // Note the lane that is out-of-bounds.
118
+ /// let result = Simd::gather_select(&vec, enable , idxs, alt); // Note the lane that is out-of-bounds.
112
119
/// assert_eq!(result, Simd::from_array([-5, 13, 10, -2]));
113
120
/// ```
114
121
#[ must_use]
115
122
#[ inline]
116
123
pub fn gather_select (
117
124
slice : & [ T ] ,
118
- mask : Mask < isize , LANES > ,
125
+ enable : Mask < isize , LANES > ,
119
126
idxs : Simd < usize , LANES > ,
120
127
or : Self ,
121
128
) -> Self {
122
- let mask : Mask < isize , LANES > = mask & idxs. lanes_lt ( Simd :: splat ( slice. len ( ) ) ) ;
129
+ let enable : Mask < isize , LANES > = enable & idxs. lanes_lt ( Simd :: splat ( slice. len ( ) ) ) ;
123
130
// SAFETY: We have masked-off out-of-bounds lanes.
124
- unsafe { Self :: gather_select_unchecked ( slice, mask , idxs, or) }
131
+ unsafe { Self :: gather_select_unchecked ( slice, enable , idxs, or) }
125
132
}
126
133
127
- /// Unsafe SIMD gather: construct a SIMD vector by reading from a slice, using potentially discontiguous indices.
128
- /// Masked indices instead select the value from the "or" vector.
129
- /// `gather_select_unchecked` is unsound if any unmasked index is out-of-bounds of the slice.
134
+ /// Reads from potentially discontiguous indices in `slice` to construct a SIMD vector.
135
+ /// The mask `enable`s all `true` lanes and disables all `false` lanes.
136
+ /// If an index is disabled, the lane is selected from the `or` vector.
137
+ ///
138
+ /// # Safety
139
+ ///
140
+ /// Calling this function with an `enable`d out-of-bounds index is *[undefined behavior]*
141
+ /// even if the resulting value is not used.
142
+ ///
143
+ /// # Examples
130
144
/// ```
131
145
/// # #![feature(portable_simd)]
132
146
/// # #[cfg(feature = "std")] use core_simd::{Simd, Mask};
133
147
/// # #[cfg(not(feature = "std"))] use core::simd::{Simd, Mask};
134
148
/// let vec: Vec<i32> = vec![10, 11, 12, 13, 14, 15, 16, 17, 18];
135
149
/// let idxs = Simd::from_array([9, 3, 0, 5]);
136
150
/// let alt = Simd::from_array([-5, -4, -3, -2]);
137
- /// let mask = Mask::from_array([true, true, true, false]); // Note the final mask lane.
151
+ /// let enable = Mask::from_array([true, true, true, false]); // Note the final mask lane.
138
152
/// // If this mask was used to gather, it would be unsound. Let's fix that.
139
- /// let mask = mask & idxs.lanes_lt(Simd::splat(vec.len()));
153
+ /// let enable = enable & idxs.lanes_lt(Simd::splat(vec.len()));
140
154
///
141
155
/// // We have masked the OOB lane, so it's safe to gather now.
142
- /// let result = unsafe { Simd::gather_select_unchecked(&vec, mask , idxs, alt) };
156
+ /// let result = unsafe { Simd::gather_select_unchecked(&vec, enable , idxs, alt) };
143
157
/// assert_eq!(result, Simd::from_array([-5, 13, 10, -2]));
144
158
/// ```
159
+ /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
145
160
#[ must_use]
146
161
#[ inline]
147
162
pub unsafe fn gather_select_unchecked (
148
163
slice : & [ T ] ,
149
- mask : Mask < isize , LANES > ,
164
+ enable : Mask < isize , LANES > ,
150
165
idxs : Simd < usize , LANES > ,
151
166
or : Self ,
152
167
) -> Self {
153
168
let base_ptr = crate :: simd:: ptr:: SimdConstPtr :: splat ( slice. as_ptr ( ) ) ;
154
169
// Ferris forgive me, I have done pointer arithmetic here.
155
170
let ptrs = base_ptr. wrapping_add ( idxs) ;
156
171
// SAFETY: The ptrs have been bounds-masked to prevent memory-unsafe reads insha'allah
157
- unsafe { intrinsics:: simd_gather ( or, ptrs, mask . to_int ( ) ) }
172
+ unsafe { intrinsics:: simd_gather ( or, ptrs, enable . to_int ( ) ) }
158
173
}
159
174
160
- /// SIMD scatter: write a SIMD vector's values into a slice, using potentially discontiguous indices.
161
- /// Out-of-bounds indices are not written.
162
- /// `scatter` writes "in order", so if an index receives two writes, only the last is guaranteed.
175
+ /// Writes the values in a SIMD vector to potentially discontiguous indices in `slice`.
176
+ /// If two lanes in the scattered vector would write to the same index
177
+ /// only the last lane is guaranteed to actually be written.
178
+ ///
179
+ /// # Examples
163
180
/// ```
164
181
/// # #![feature(portable_simd)]
165
182
/// # #[cfg(feature = "std")] use core_simd::Simd;
@@ -176,58 +193,70 @@ where
176
193
self . scatter_select ( slice, Mask :: splat ( true ) , idxs)
177
194
}
178
195
179
- /// SIMD scatter: write a SIMD vector's values into a slice, using potentially discontiguous indices.
180
- /// Out-of-bounds or masked indices are not written.
181
- /// `scatter_select` writes "in order", so if an index receives two writes, only the last is guaranteed.
196
+ /// Writes the values in a SIMD vector to multiple potentially discontiguous indices in `slice`.
197
+ /// The mask `enable`s all `true` lanes and disables all `false` lanes.
198
+ /// If an enabled index is out-of-bounds, the lane is not written.
199
+ /// If two enabled lanes in the scattered vector would write to the same index,
200
+ /// only the last lane is guaranteed to actually be written.
201
+ ///
202
+ /// # Examples
182
203
/// ```
183
204
/// # #![feature(portable_simd)]
184
205
/// # #[cfg(feature = "std")] use core_simd::{Simd, Mask};
185
206
/// # #[cfg(not(feature = "std"))] use core::simd::{Simd, Mask};
186
207
/// let mut vec: Vec<i32> = vec![10, 11, 12, 13, 14, 15, 16, 17, 18];
187
208
/// let idxs = Simd::from_array([9, 3, 0, 0]);
188
209
/// let vals = Simd::from_array([-27, 82, -41, 124]);
189
- /// let mask = Mask::from_array([true, true, true, false]); // Note the mask of the last lane.
210
+ /// let enable = Mask::from_array([true, true, true, false]); // Note the mask of the last lane.
190
211
///
191
- /// vals.scatter_select(&mut vec, mask , idxs); // index 0's second write is masked, thus omitted.
212
+ /// vals.scatter_select(&mut vec, enable , idxs); // index 0's second write is masked, thus omitted.
192
213
/// assert_eq!(vec, vec![-41, 11, 12, 82, 14, 15, 16, 17, 18]);
193
214
/// ```
194
215
#[ inline]
195
216
pub fn scatter_select (
196
217
self ,
197
218
slice : & mut [ T ] ,
198
- mask : Mask < isize , LANES > ,
219
+ enable : Mask < isize , LANES > ,
199
220
idxs : Simd < usize , LANES > ,
200
221
) {
201
- let mask : Mask < isize , LANES > = mask & idxs. lanes_lt ( Simd :: splat ( slice. len ( ) ) ) ;
222
+ let enable : Mask < isize , LANES > = enable & idxs. lanes_lt ( Simd :: splat ( slice. len ( ) ) ) ;
202
223
// SAFETY: We have masked-off out-of-bounds lanes.
203
- unsafe { self . scatter_select_unchecked ( slice, mask , idxs) }
224
+ unsafe { self . scatter_select_unchecked ( slice, enable , idxs) }
204
225
}
205
226
206
- /// Unsafe SIMD scatter: write a SIMD vector's values into a slice, using potentially discontiguous indices.
207
- /// Out-of-bounds or masked indices are not written.
208
- /// `scatter_select_unchecked` is unsound if any unmasked index is out of bounds of the slice.
209
- /// `scatter_select_unchecked` writes "in order", so if the same index receives two writes, only the last is guaranteed.
227
+ /// Writes the values in a SIMD vector to multiple potentially discontiguous indices in `slice`.
228
+ /// The mask `enable`s all `true` lanes and disables all `false` lanes.
229
+ /// If two enabled lanes in the scattered vector would write to the same index,
230
+ /// only the last lane is guaranteed to actually be written.
231
+ ///
232
+ /// # Safety
233
+ ///
234
+ /// Calling this function with an enabled out-of-bounds index is *[undefined behavior]*,
235
+ /// and may lead to memory corruption.
236
+ ///
237
+ /// # Examples
210
238
/// ```
211
239
/// # #![feature(portable_simd)]
212
240
/// # #[cfg(feature = "std")] use core_simd::{Simd, Mask};
213
241
/// # #[cfg(not(feature = "std"))] use core::simd::{Simd, Mask};
214
242
/// let mut vec: Vec<i32> = vec![10, 11, 12, 13, 14, 15, 16, 17, 18];
215
243
/// let idxs = Simd::from_array([9, 3, 0, 0]);
216
244
/// let vals = Simd::from_array([-27, 82, -41, 124]);
217
- /// let mask = Mask::from_array([true, true, true, false]); // Note the mask of the last lane.
245
+ /// let enable = Mask::from_array([true, true, true, false]); // Note the mask of the last lane.
218
246
/// // If this mask was used to scatter, it would be unsound. Let's fix that.
219
- /// let mask = mask & idxs.lanes_lt(Simd::splat(vec.len()));
247
+ /// let enable = enable & idxs.lanes_lt(Simd::splat(vec.len()));
220
248
///
221
249
/// // We have masked the OOB lane, so it's safe to scatter now.
222
- /// unsafe { vals.scatter_select_unchecked(&mut vec, mask , idxs); }
250
+ /// unsafe { vals.scatter_select_unchecked(&mut vec, enable , idxs); }
223
251
/// // index 0's second write is masked, thus was omitted.
224
252
/// assert_eq!(vec, vec![-41, 11, 12, 82, 14, 15, 16, 17, 18]);
225
253
/// ```
254
+ /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
226
255
#[ inline]
227
256
pub unsafe fn scatter_select_unchecked (
228
257
self ,
229
258
slice : & mut [ T ] ,
230
- mask : Mask < isize , LANES > ,
259
+ enable : Mask < isize , LANES > ,
231
260
idxs : Simd < usize , LANES > ,
232
261
) {
233
262
// SAFETY: This block works with *mut T derived from &mut 'a [T],
@@ -237,7 +266,7 @@ where
237
266
// to prevent invalidating the raw ptrs while they're live.
238
267
// Thus, entering this block requires all values to use being already ready:
239
268
// 0. idxs we want to write to, which are used to construct the mask.
240
- // 1. mask , which depends on an initial &'a [T] and the idxs.
269
+ // 1. enable , which depends on an initial &'a [T] and the idxs.
241
270
// 2. actual values to scatter (self).
242
271
// 3. &mut [T] which will become our base ptr.
243
272
unsafe {
@@ -246,7 +275,7 @@ where
246
275
// Ferris forgive me, I have done pointer arithmetic here.
247
276
let ptrs = base_ptr. wrapping_add ( idxs) ;
248
277
// The ptrs have been bounds-masked to prevent memory-unsafe writes insha'allah
249
- intrinsics:: simd_scatter ( self , ptrs, mask . to_int ( ) )
278
+ intrinsics:: simd_scatter ( self , ptrs, enable . to_int ( ) )
250
279
// Cleared ☢️ *mut T Zone
251
280
}
252
281
}
0 commit comments