]> git.lizzy.rs Git - rust.git/blob - library/portable-simd/crates/core_simd/src/to_bytes.rs
Rollup merge of #85766 - workingjubilee:file-options, r=yaahc
[rust.git] / library / portable-simd / crates / core_simd / src / to_bytes.rs
1 macro_rules! impl_to_bytes {
2     { $ty:ty, $size:literal } => {
3         impl<const LANES: usize> crate::simd::Simd<$ty, LANES>
4         where
5             crate::simd::LaneCount<LANES>: crate::simd::SupportedLaneCount,
6             crate::simd::LaneCount<{{ $size * LANES }}>: crate::simd::SupportedLaneCount,
7         {
8             /// Return the memory representation of this integer as a byte array in native byte
9             /// order.
10             pub fn to_ne_bytes(self) -> crate::simd::Simd<u8, {{ $size * LANES }}> {
11                 unsafe { core::mem::transmute_copy(&self) }
12             }
13
14             /// Create a native endian integer value from its memory representation as a byte array
15             /// in native endianness.
16             pub fn from_ne_bytes(bytes: crate::simd::Simd<u8, {{ $size * LANES }}>) -> Self {
17                 unsafe { core::mem::transmute_copy(&bytes) }
18             }
19         }
20     }
21 }
22
23 impl_to_bytes! { u8, 1 }
24 impl_to_bytes! { u16, 2 }
25 impl_to_bytes! { u32, 4 }
26 impl_to_bytes! { u64, 8 }
27 #[cfg(target_pointer_width = "32")]
28 impl_to_bytes! { usize, 4 }
29 #[cfg(target_pointer_width = "64")]
30 impl_to_bytes! { usize, 8 }
31
32 impl_to_bytes! { i8, 1 }
33 impl_to_bytes! { i16, 2 }
34 impl_to_bytes! { i32, 4 }
35 impl_to_bytes! { i64, 8 }
36 #[cfg(target_pointer_width = "32")]
37 impl_to_bytes! { isize, 4 }
38 #[cfg(target_pointer_width = "64")]
39 impl_to_bytes! { isize, 8 }