core_simd/
lane_count.rs

1mod sealed {
2    pub trait Sealed {}
3}
4use sealed::Sealed;
5
6/// Specifies the number of lanes in a SIMD vector as a type.
7pub struct LaneCount<const N: usize>;
8
9impl<const N: usize> LaneCount<N> {
10    /// The number of bytes in a bitmask with this many lanes.
11    pub const BITMASK_LEN: usize = N.div_ceil(8);
12}
13
14/// Statically guarantees that a lane count is marked as supported.
15///
16/// This trait is *sealed*: the list of implementors below is total.
17/// Users do not have the ability to mark additional `LaneCount<N>` values as supported.
18/// Only SIMD vectors with supported lane counts are constructable.
19pub trait SupportedLaneCount: Sealed {
20    #[doc(hidden)]
21    type BitMask: Copy + AsRef<[u8]> + AsMut<[u8]>;
22    #[doc(hidden)]
23    const EMPTY_BIT_MASK: Self::BitMask;
24    #[doc(hidden)]
25    const FULL_BIT_MASK: Self::BitMask;
26}
27
28impl<const N: usize> Sealed for LaneCount<N> {}
29
30macro_rules! supported_lane_count {
31    ($($lanes:literal),+) => {
32        $(
33            impl SupportedLaneCount for LaneCount<$lanes> {
34                type BitMask = [u8; Self::BITMASK_LEN];
35                const EMPTY_BIT_MASK: Self::BitMask = [0; Self::BITMASK_LEN];
36                const FULL_BIT_MASK: Self::BitMask = {
37                    let mut array = [!0u8; Self::BITMASK_LEN];
38                    if $lanes % 8 > 0 {
39                        array[Self::BITMASK_LEN - 1] = (!0) >> (8 - $lanes % 8);
40                    }
41                    array
42                };
43            }
44        )+
45    };
46}
47
48supported_lane_count!(
49    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
50    27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
51    51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64
52);