about summary refs log tree commit diff
diff options
context:
space:
mode:
authorProloy Mishra <67726964+pro465@users.noreply.github.com>2021-11-09 06:58:43 +0530
committerGitHub <noreply@github.com>2021-11-08 17:28:43 -0800
commitd2e87281fcffbf26635c03a1060ca3fc18dcf418 (patch)
treebf8d7eebe671ddcbd50805a17e83abfc4654ce79
parent772bf2090e04db68790549bae36e5180423f9f65 (diff)
downloadrust-d2e87281fcffbf26635c03a1060ca3fc18dcf418.tar.gz
rust-d2e87281fcffbf26635c03a1060ca3fc18dcf418.zip
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.
-rw-r--r--crates/core_simd/src/vector.rs18
1 files changed, 18 insertions, 0 deletions
diff --git a/crates/core_simd/src/vector.rs b/crates/core_simd/src/vector.rs
index 893eff674ff..7c5ec2bc314 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.
     ///