From d2e87281fcffbf26635c03a1060ca3fc18dcf418 Mon Sep 17 00:00:00 2001 From: Proloy Mishra <67726964+pro465@users.noreply.github.com> Date: Tue, 9 Nov 2021 06:58:43 +0530 Subject: [PATCH] add `Simd::from_slice` (#177) * add `Simd::from_slice` uses a zeroed initial array and loops so that it can be const. unfortunately, parameterizing the assert with slice length needs `#![feature(const_fn_fn_ptr_basics)]` to work. --- crates/core_simd/src/vector.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/crates/core_simd/src/vector.rs b/crates/core_simd/src/vector.rs index 893eff674ffb..7c5ec2bc314c 100644 --- a/crates/core_simd/src/vector.rs +++ b/crates/core_simd/src/vector.rs @@ -57,6 +57,24 @@ where self.0 } + /// Converts a slice to a SIMD vector containing `slice[..LANES]` + /// # Panics + /// `from_slice` will panic if the slice's `len` is less than the vector's `Simd::LANES`. + #[must_use] + pub const fn from_slice(slice: &[T]) -> Self { + assert!( + 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) + } + /// Reads from potentially discontiguous indices in `slice` to construct a SIMD vector. /// If an index is out-of-bounds, the lane is instead selected from the `or` vector. ///