Skip to content

Commit 54b6f69

Browse files
committed
Avoid a scalar loop in Simd::from_slice
1 parent 6e30c6e commit 54b6f69

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

crates/core_simd/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![no_std]
22
#![feature(
3+
const_ptr_read,
34
convert_float_to_int,
45
decl_macro,
56
intra_doc_pointers,

crates/core_simd/src/vector.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,10 @@ where
174174
slice.len() >= LANES,
175175
"slice length must be at least the number of lanes"
176176
);
177-
let mut array = [slice[0]; LANES];
178-
let mut i = 0;
179-
while i < LANES {
180-
array[i] = slice[i];
181-
i += 1;
182-
}
183-
Self(array)
177+
// Safety:
178+
// - We've checked the length is sufficient.
179+
// - `T` and `Simd<T, N>` are Copy types.
180+
unsafe { slice.as_ptr().cast::<Self>().read_unaligned() }
184181
}
185182

186183
/// Performs lanewise conversion of a SIMD vector's elements to another SIMD-valid type.

0 commit comments

Comments
 (0)