Skip to main content

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            /// Put the same value in every lane.
94            #[inline(always)]
95            $v fn splat(value: $elem_type) -> $name {
96                unsafe { $crate::intrinsics::simd::simd_splat(value) }
97            }
98
99            /// Returns an array reference containing the entire SIMD vector.
100            $v const fn as_array(&self) -> &[$elem_type; $len] {
101                // SAFETY: this type is just an overaligned `[T; N]` with
102                // potential padding at the end, so pointer casting to a
103                // `&[T; N]` is safe.
104                //
105                // NOTE: This deliberately doesn't just use `&self.0` because it may soon be banned
106                // see https://github.com/rust-lang/compiler-team/issues/838
107                unsafe { &*(self as *const Self as *const [$elem_type; $len]) }
108
109            }
110
111            /// Returns a mutable array reference containing the entire SIMD vector.
112            #[inline]
113            $v fn as_mut_array(&mut self) -> &mut [$elem_type; $len] {
114                // SAFETY: this type is just an overaligned `[T; N]` with
115                // potential padding at the end, so pointer casting to a
116                // `&mut [T; N]` is safe.
117                //
118                // NOTE: This deliberately doesn't just use `&mut self.0` because it may soon be banned
119                // see https://github.com/rust-lang/compiler-team/issues/838
120                unsafe { &mut *(self as *mut Self as *mut [$elem_type; $len]) }
121            }
122        }
123
124        $(#[$stability])+
125        impl crate::fmt::Debug for $name {
126            #[inline]
127            fn fmt(&self, f: &mut crate::fmt::Formatter<'_>) -> crate::fmt::Result {
128                crate::core_arch::simd::debug_simd_finish(f, stringify!($name), self.as_array())
129            }
130        }
131
132        $(#[$stability])+
133        impl crate::convert::From<crate::core_arch::simd::Simd<$elem_type, $len>> for $name {
134            #[inline(always)]
135            fn from(simd: crate::core_arch::simd::Simd<$elem_type, $len>) -> Self {
136                unsafe { crate::mem::transmute(simd) }
137            }
138        }
139
140        $(#[$stability])+
141        impl crate::convert::From<$name> for crate::core_arch::simd::Simd<$elem_type, $len> {
142            #[inline(always)]
143            fn from(simd: $name) -> Self {
144                unsafe { crate::mem::transmute(simd) }
145            }
146        }
147    )*);
148}
149
150#[allow(unused)]
151#[repr(simd)]
152pub(crate) struct SimdShuffleIdx<const LEN: usize>(pub(crate) [u32; LEN]);
153
154#[allow(unused)]
155macro_rules! simd_shuffle {
156    ($x:expr, $y:expr, $idx:expr $(,)?) => {{
157        $crate::intrinsics::simd::simd_shuffle(
158            $x,
159            $y,
160            const { $crate::core_arch::macros::SimdShuffleIdx($idx) },
161        )
162    }};
163}
164
165#[allow(unused)]
166macro_rules! simd_insert {
167    ($x:expr, $idx:expr, $val:expr $(,)?) => {{ $crate::intrinsics::simd::simd_insert($x, const { $idx }, $val) }};
168}
169
170#[allow(unused)]
171macro_rules! simd_extract {
172    ($x:expr, $idx:expr $(,)?) => {{ $crate::intrinsics::simd::simd_extract($x, const { $idx }) }};
173    ($x:expr, $idx:expr, $ty:ty $(,)?) => {{ $crate::intrinsics::simd::simd_extract::<_, $ty>($x, const { $idx }) }};
174}
175
176#[allow(unused)]
177macro_rules! simd_masked_load {
178    ($align:expr, $mask:expr, $ptr:expr, $default:expr) => {
179        $crate::intrinsics::simd::simd_masked_load::<_, _, _, { $align }>($mask, $ptr, $default)
180    };
181}
182
183#[allow(unused)]
184macro_rules! simd_masked_store {
185    ($align:expr, $mask:expr, $ptr:expr, $default:expr) => {
186        $crate::intrinsics::simd::simd_masked_store::<_, _, _, { $align }>($mask, $ptr, $default)
187    };
188}