lightweight-simd 0.1.1

Lightweight generic fixed-array vector types relying on compiler auto-vectorization
Documentation
// Most or all contents generated by AI (model glm-5.3)

//! Slice load/store operations for the vector types ([`Simd`] and its
//! [`aligned`](crate::aligned) siblings).

use num_traits::Zero;

use crate::{Aligned4, Aligned8, Aligned16, Aligned32, Aligned64, Simd};

#[duplicate::duplicate_item(
    _mod_        _vec_;
    [simd_impls] [Simd];
    [impls_a4]   [Aligned4];
    [impls_a8]   [Aligned8];
    [impls_a16]  [Aligned16];
    [impls_a32]  [Aligned32];
    [impls_a64]  [Aligned64];
)]
mod _mod_ {
    use super::*;

    impl<T: Copy, const N: usize> _vec_<T, N> {
        /// Loads a vector from the first `N` elements of `src`.
        ///
        /// # Panics
        ///
        /// Panics if `src.len() < N`.
        #[inline(always)]
        pub fn from_slice(src: &[T]) -> Self {
            assert!(src.len() >= N, "from_slice: src.len() = {} < lane count {N}", src.len());
            Self::from_fn(|i| src[i])
        }

        /// Stores all lanes to the first `N` elements of `dst`.
        ///
        /// # Panics
        ///
        /// Panics if `dst.len() < N`.
        #[inline(always)]
        pub fn store_slice(&self, dst: &mut [T]) {
            assert!(dst.len() >= N, "store_slice: dst.len() = {} < lane count {N}", dst.len());
            dst[..N].copy_from_slice(&self.0);
        }

        /// Stores at most the first `min(dst.len(), N)` lanes to `dst`.
        #[inline(always)]
        pub fn store_slice_partial(&self, dst: &mut [T]) {
            let n = dst.len().min(N);
            dst[..n].copy_from_slice(&self.0[..n]);
        }
    }

    impl<T: Zero + Copy, const N: usize> _vec_<T, N> {
        /// Loads a vector from `src`, zero-filling lanes past `src.len()`.
        #[inline(always)]
        pub fn from_slice_pad(src: &[T]) -> Self {
            let mut out = Self::zero();
            let n = src.len().min(N);
            out.0[..n].copy_from_slice(&src[..n]);
            out
        }
    }
}