Merge pull request #318 from thomcc/simd_from_slice

Avoid a scalar loop in `Simd::from_slice`
This commit is contained in:
Caleb Zulawski 2022-11-28 09:17:02 -05:00 committed by GitHub
commit 1547dd66f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 7 deletions

View File

@ -1,5 +1,6 @@
#![no_std]
#![feature(
const_ptr_read,
convert_float_to_int,
decl_macro,
intra_doc_pointers,

View File

@ -174,13 +174,10 @@ pub const fn from_slice(slice: &[T]) -> Self {
slice.len() >= LANES,
"slice length must be at least the number of lanes"
);
let mut array = [slice[0]; LANES];
let mut i = 0;
while i < LANES {
array[i] = slice[i];
i += 1;
}
Self(array)
// Safety:
// - We've checked the length is sufficient.
// - `T` and `Simd<T, N>` are Copy types.
unsafe { slice.as_ptr().cast::<Self>().read_unaligned() }
}
/// Performs lanewise conversion of a SIMD vector's elements to another SIMD-valid type.