2021-05-24 07:37:15 -05:00
|
|
|
macro_rules! impl_to_bytes {
|
2021-08-07 14:28:27 -05:00
|
|
|
{ $ty:ty, $size:literal } => {
|
2021-09-18 20:31:49 -05:00
|
|
|
impl<const LANES: usize> crate::simd::Simd<$ty, LANES>
|
2021-05-24 07:37:15 -05:00
|
|
|
where
|
2021-09-18 20:31:49 -05:00
|
|
|
crate::simd::LaneCount<LANES>: crate::simd::SupportedLaneCount,
|
|
|
|
crate::simd::LaneCount<{{ $size * LANES }}>: crate::simd::SupportedLaneCount,
|
2021-05-24 07:37:15 -05:00
|
|
|
{
|
|
|
|
/// Return the memory representation of this integer as a byte array in native byte
|
|
|
|
/// order.
|
2021-09-18 20:31:49 -05:00
|
|
|
pub fn to_ne_bytes(self) -> crate::simd::Simd<u8, {{ $size * LANES }}> {
|
2021-07-27 23:19:31 -05:00
|
|
|
unsafe { core::mem::transmute_copy(&self) }
|
|
|
|
}
|
2021-05-24 07:37:15 -05:00
|
|
|
|
|
|
|
/// Create a native endian integer value from its memory representation as a byte array
|
|
|
|
/// in native endianness.
|
2021-09-18 20:31:49 -05:00
|
|
|
pub fn from_ne_bytes(bytes: crate::simd::Simd<u8, {{ $size * LANES }}>) -> Self {
|
2021-07-27 23:19:31 -05:00
|
|
|
unsafe { core::mem::transmute_copy(&bytes) }
|
|
|
|
}
|
2021-05-24 07:37:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-07 14:28:27 -05:00
|
|
|
impl_to_bytes! { u8, 1 }
|
|
|
|
impl_to_bytes! { u16, 2 }
|
|
|
|
impl_to_bytes! { u32, 4 }
|
|
|
|
impl_to_bytes! { u64, 8 }
|
2021-05-24 07:37:15 -05:00
|
|
|
#[cfg(target_pointer_width = "32")]
|
2021-08-07 14:28:27 -05:00
|
|
|
impl_to_bytes! { usize, 4 }
|
2021-05-24 07:37:15 -05:00
|
|
|
#[cfg(target_pointer_width = "64")]
|
2021-08-07 14:28:27 -05:00
|
|
|
impl_to_bytes! { usize, 8 }
|
2021-05-24 07:37:15 -05:00
|
|
|
|
2021-08-07 14:28:27 -05:00
|
|
|
impl_to_bytes! { i8, 1 }
|
|
|
|
impl_to_bytes! { i16, 2 }
|
|
|
|
impl_to_bytes! { i32, 4 }
|
|
|
|
impl_to_bytes! { i64, 8 }
|
2021-05-24 07:37:15 -05:00
|
|
|
#[cfg(target_pointer_width = "32")]
|
2021-08-07 14:28:27 -05:00
|
|
|
impl_to_bytes! { isize, 4 }
|
2021-05-24 07:37:15 -05:00
|
|
|
#[cfg(target_pointer_width = "64")]
|
2021-08-07 14:28:27 -05:00
|
|
|
impl_to_bytes! { isize, 8 }
|