Skip to main content

lightweight_simd/
aligned.rs

1// Most or all contents generated by AI (model glm-5.3)
2
3//! Enforced-alignment vector types.
4//!
5//! [`Aligned4`] through [`Aligned64`] are siblings of [`Simd`]: fixed-length
6//! lane vectors over `[T; N]` that share one implementation with it — every
7//! impl block in this crate is written once and instantiated for `Simd` and
8//! the five aligned types together, so the whole operation surface is
9//! identical across the six types. The only difference is the enforced byte
10//! alignment of 4/8/16/32/64.
11//!
12//! The concrete aliases ([`f64x8`](crate::f64x8), …) pick the type whose
13//! alignment is the vector's byte size rounded up to a power of two, capped at
14//! 64 bytes. Conversions between plain and aligned vectors are `From`/`.0`/
15//! `to_simd`.
16//!
17//! Masks ([`Simd<bool, N>`]) are never aligned: they are register-resident
18//! temporaries. Comparisons on any vector type return plain masks, and
19//! per-lane selection is `value.mask_select(mask, other)`.
20
21use crate::Simd;
22
23#[duplicate::duplicate_item(
24    _vec_       _align_;
25    [Aligned4]  [4];
26    [Aligned8]  [8];
27    [Aligned16] [16];
28    [Aligned32] [32];
29    [Aligned64] [64];
30)]
31#[repr(align(_align_))]
32#[derive(Clone, Debug, PartialEq, Eq, Hash, Copy)]
33#[doc = concat!("A fixed-length vector of `N` lanes of element type `T` over `[T; N]` with enforced ", stringify!(_align_), "-byte alignment; see the [module](crate::aligned) docs.")]
34pub struct _vec_<T, const N: usize>(pub [T; N]);
35
36#[duplicate::duplicate_item(
37    _vec_;
38    [Aligned4];
39    [Aligned8];
40    [Aligned16];
41    [Aligned32];
42    [Aligned64];
43)]
44impl<T, const N: usize> From<Simd<T, N>> for _vec_<T, N> {
45    #[inline(always)]
46    fn from(v: Simd<T, N>) -> Self {
47        Self(v.0)
48    }
49}
50
51#[duplicate::duplicate_item(
52    _vec_;
53    [Aligned4];
54    [Aligned8];
55    [Aligned16];
56    [Aligned32];
57    [Aligned64];
58)]
59impl<T, const N: usize> From<_vec_<T, N>> for Simd<T, N> {
60    #[inline(always)]
61    fn from(v: _vec_<T, N>) -> Self {
62        Simd(v.0)
63    }
64}
65
66#[duplicate::duplicate_item(
67    _vec_;
68    [Aligned4];
69    [Aligned8];
70    [Aligned16];
71    [Aligned32];
72    [Aligned64];
73)]
74impl<T, const N: usize> _vec_<T, N> {
75    /// Returns the equivalent plain (naturally aligned) vector.
76    #[inline(always)]
77    pub fn to_simd(self) -> Simd<T, N> {
78        Simd(self.0)
79    }
80}