core/stdarch/crates/core_arch/src/
macros.rs

1//! Utility macros.
2
3#[allow(unused)]
4macro_rules! static_assert {
5    ($e:expr) => {
6        const {
7            assert!($e);
8        }
9    };
10    ($e:expr, $msg:expr) => {
11        const {
12            assert!($e, $msg);
13        }
14    };
15}
16
17#[allow(unused_macros)]
18macro_rules! static_assert_uimm_bits {
19    ($imm:ident, $bits:expr) => {
20        // `0 <= $imm` produces a warning if the immediate has an unsigned type
21        #[allow(unused_comparisons)]
22        {
23            static_assert!(
24                0 <= $imm && $imm < (1 << $bits),
25                concat!(
26                    stringify!($imm),
27                    " doesn't fit in ",
28                    stringify!($bits),
29                    " bits",
30                )
31            )
32        }
33    };
34}
35
36#[allow(unused_macros)]
37macro_rules! static_assert_simm_bits {
38    ($imm:ident, $bits:expr) => {
39        static_assert!(
40            (-1 << ($bits - 1)) - 1 <= $imm && $imm < (1 << ($bits - 1)),
41            concat!(
42                stringify!($imm),
43                " doesn't fit in ",
44                stringify!($bits),
45                " bits",
46            )
47        )
48    };
49}
50
51#[allow(unused)]
52macro_rules! types {
53    (
54        #![$stability_first:meta]
55        $(
56            #![$stability_more:meta]
57        )*
58
59        $(
60            $(#[$doc:meta])*
61            $(stability: [$stability_already: meta])*
62            pub struct $name:ident($len:literal x $v:vis $elem_type:ty);
63        )*
64    ) => (types! {
65        $(
66            #![$stability_more]
67        )*
68
69        $(
70            $(#[$doc])*
71            $(stability: [$stability_already])*
72            stability: [$stability_first]
73            pub struct $name($len x $v $elem_type);
74        )*
75    });
76
77    (
78        $(
79            $(#[$doc:meta])*
80            $(stability: [$stability: meta])+
81            pub struct $name:ident($len:literal x $v:vis $elem_type:ty);
82        )*
83    ) => ($(
84        $(#[$doc])*
85        $(#[$stability])+
86        #[derive(Copy, Clone)]
87        #[allow(non_camel_case_types)]
88        #[repr(simd)]
89        #[allow(clippy::missing_inline_in_public_items)]
90        pub struct $name($v [$elem_type; $len]);
91
92        impl $name {
93            /// Using `my_simd([x; N])` seemingly fails tests,
94            /// so use this internal helper for it instead.
95            #[inline(always)]
96            $v fn splat(value: $elem_type) -> $name {
97                #[derive(Copy, Clone)]
98                #[repr(simd)]
99                struct JustOne([$elem_type; 1]);
100                let one = JustOne([value]);
101                // SAFETY: 0 is always in-bounds because we're shuffling
102                // a simd type with exactly one element.
103                unsafe { simd_shuffle!(one, one, [0; $len]) }
104            }
105        }
106
107        $(#[$stability])+
108        impl crate::fmt::Debug for $name {
109            #[inline]
110            fn fmt(&self, f: &mut crate::fmt::Formatter<'_>) -> crate::fmt::Result {
111                crate::core_arch::simd::debug_simd_finish(f, stringify!($name), self.0)
112            }
113        }
114    )*);
115}
116
117#[allow(unused)]
118#[repr(simd)]
119pub(crate) struct SimdShuffleIdx<const LEN: usize>(pub(crate) [u32; LEN]);
120
121#[allow(unused)]
122macro_rules! simd_shuffle {
123    ($x:expr, $y:expr, $idx:expr $(,)?) => {{
124        $crate::intrinsics::simd::simd_shuffle(
125            $x,
126            $y,
127            const { $crate::core_arch::macros::SimdShuffleIdx($idx) },
128        )
129    }};
130}
131
132#[allow(unused)]
133macro_rules! simd_insert {
134    ($x:expr, $idx:expr, $val:expr $(,)?) => {{
135        $crate::intrinsics::simd::simd_insert($x, const { $idx }, $val)
136    }};
137}
138
139#[allow(unused)]
140macro_rules! simd_extract {
141    ($x:expr, $idx:expr $(,)?) => {{
142        $crate::intrinsics::simd::simd_extract($x, const { $idx })
143    }};
144    ($x:expr, $idx:expr, $ty:ty $(,)?) => {{
145        $crate::intrinsics::simd::simd_extract::<_, $ty>($x, const { $idx })
146    }};
147}