Skip to content

Commit d2e8728

Browse files
authored
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.
1 parent 772bf20 commit d2e8728

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

crates/core_simd/src/vector.rs

+18
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,24 @@ where
5757
self.0
5858
}
5959

60+
/// Converts a slice to a SIMD vector containing `slice[..LANES]`
61+
/// # Panics
62+
/// `from_slice` will panic if the slice's `len` is less than the vector's `Simd::LANES`.
63+
#[must_use]
64+
pub const fn from_slice(slice: &[T]) -> Self {
65+
assert!(
66+
slice.len() >= LANES,
67+
"slice length must be at least the number of lanes"
68+
);
69+
let mut array = [slice[0]; LANES];
70+
let mut i = 0;
71+
while i < LANES {
72+
array[i] = slice[i];
73+
i += 1;
74+
}
75+
Self(array)
76+
}
77+
6078
/// Reads from potentially discontiguous indices in `slice` to construct a SIMD vector.
6179
/// If an index is out-of-bounds, the lane is instead selected from the `or` vector.
6280
///

0 commit comments

Comments
 (0)