Skip to content

Commit cc3e99b

Browse files
committed
Implement to_array via as_array
1 parent 394a884 commit cc3e99b

File tree

1 file changed

+3
-26
lines changed

1 file changed

+3
-26
lines changed

crates/core_simd/src/vector.rs

+3-26
Original file line numberDiff line numberDiff line change
@@ -184,28 +184,16 @@ where
184184
/// # Safety
185185
/// Reading `ptr` must be safe, as if by `<*const [T; N]>::read_unaligned`.
186186
const unsafe fn load(ptr: *const [T; N]) -> Self {
187-
let mut tmp = core::mem::MaybeUninit::uninit();
187+
let mut tmp = core::mem::MaybeUninit::<Self>::uninit();
188188
// SAFETY: `Simd<T, N>` always contains `N` elements of type `T`. It may have padding
189189
// which does not need to be initialized. The safety of reading `ptr` is ensured by the
190190
// caller.
191191
unsafe {
192-
core::ptr::copy_nonoverlapping(ptr, tmp.as_mut_ptr() as *mut _, 1);
192+
core::ptr::copy_nonoverlapping(ptr, tmp.as_mut_ptr().cast(), 1);
193193
tmp.assume_init()
194194
}
195195
}
196196

197-
/// Store a vector to an array of `T`.
198-
///
199-
/// See `load` as to why this function is necessary.
200-
///
201-
/// # Safety
202-
/// Writing to `ptr` must be safe, as if by `<*mut [T; N]>::write_unaligned`.
203-
const unsafe fn store(self, ptr: *mut [T; N]) {
204-
// SAFETY: `Simd<T, N>` always contains `N` elements of type `T`. The safety of writing
205-
// `ptr` is ensured by the caller.
206-
unsafe { core::ptr::copy_nonoverlapping(self.as_array(), ptr, 1) }
207-
}
208-
209197
/// Converts an array to a SIMD vector.
210198
pub const fn from_array(array: [T; N]) -> Self {
211199
// SAFETY: `&array` is safe to read.
@@ -220,18 +208,7 @@ where
220208

221209
/// Converts a SIMD vector to an array.
222210
pub const fn to_array(self) -> [T; N] {
223-
let mut tmp = core::mem::MaybeUninit::uninit();
224-
// SAFETY: writing to `tmp` is safe and initializes it.
225-
//
226-
// FIXME: We currently use a pointer store instead of `transmute_copy` because `repr(simd)`
227-
// results in padding for non-power-of-2 vectors (so vectors are larger than arrays).
228-
//
229-
// NOTE: This deliberately doesn't just use `self.0`, see the comment
230-
// on the struct definition for details.
231-
unsafe {
232-
self.store(tmp.as_mut_ptr());
233-
tmp.assume_init()
234-
}
211+
*self.as_array()
235212
}
236213

237214
/// Converts a slice to a SIMD vector containing `slice[..N]`.

0 commit comments

Comments
 (0)