Skip to main content

lightweight_simd/
memory.rs

1// Most or all contents generated by AI (model glm-5.3)
2
3//! Slice load/store operations for the vector types ([`Simd`] and its
4//! [`aligned`](crate::aligned) siblings).
5
6use num_traits::Zero;
7
8use crate::{Aligned4, Aligned8, Aligned16, Aligned32, Aligned64, Simd};
9
10#[duplicate::duplicate_item(
11    _mod_        _vec_;
12    [simd_impls] [Simd];
13    [impls_a4]   [Aligned4];
14    [impls_a8]   [Aligned8];
15    [impls_a16]  [Aligned16];
16    [impls_a32]  [Aligned32];
17    [impls_a64]  [Aligned64];
18)]
19mod _mod_ {
20    use super::*;
21
22    impl<T: Copy, const N: usize> _vec_<T, N> {
23        /// Loads a vector from the first `N` elements of `src`.
24        ///
25        /// # Panics
26        ///
27        /// Panics if `src.len() < N`.
28        #[inline(always)]
29        pub fn from_slice(src: &[T]) -> Self {
30            assert!(src.len() >= N, "from_slice: src.len() = {} < lane count {N}", src.len());
31            Self::from_fn(|i| src[i])
32        }
33
34        /// Stores all lanes to the first `N` elements of `dst`.
35        ///
36        /// # Panics
37        ///
38        /// Panics if `dst.len() < N`.
39        #[inline(always)]
40        pub fn store_slice(&self, dst: &mut [T]) {
41            assert!(dst.len() >= N, "store_slice: dst.len() = {} < lane count {N}", dst.len());
42            dst[..N].copy_from_slice(&self.0);
43        }
44
45        /// Stores at most the first `min(dst.len(), N)` lanes to `dst`.
46        #[inline(always)]
47        pub fn store_slice_partial(&self, dst: &mut [T]) {
48            let n = dst.len().min(N);
49            dst[..n].copy_from_slice(&self.0[..n]);
50        }
51    }
52
53    impl<T: Zero + Copy, const N: usize> _vec_<T, N> {
54        /// Loads a vector from `src`, zero-filling lanes past `src.len()`.
55        #[inline(always)]
56        pub fn from_slice_pad(src: &[T]) -> Self {
57            let mut out = Self::zero();
58            let n = src.len().min(N);
59            out.0[..n].copy_from_slice(&src[..n]);
60            out
61        }
62    }
63}