]>
git.proxmox.com Git - rustc.git/blob - vendor/packed_simd/src/api/swap_bytes.rs
1 //! Horizontal swap bytes
3 macro_rules
! impl_swap_bytes
{
4 ([$elem_ty
:ident
; $elem_count
:expr
]: $id
:ident
| $test_tt
:tt
) => {
6 /// Reverses the byte order of the vector.
8 pub fn swap_bytes(self) -> Self {
9 super::codegen
::swap_bytes
::SwapBytes
::swap_bytes(self)
12 /// Converts self to little endian from the target's endianness.
14 /// On little endian this is a no-op. On big endian the bytes are
17 pub fn to_le(self) -> Self {
18 #[cfg(target_endian = "little")]
22 #[cfg(not(target_endian = "little"))]
28 /// Converts self to big endian from the target's endianness.
30 /// On big endian this is a no-op. On little endian the bytes are
33 pub fn to_be(self) -> Self {
34 #[cfg(target_endian = "big")]
38 #[cfg(not(target_endian = "big"))]
44 /// Converts a vector from little endian to the target's endianness.
46 /// On little endian this is a no-op. On big endian the bytes are
49 pub fn from_le(x
: Self) -> Self {
50 #[cfg(target_endian = "little")]
54 #[cfg(not(target_endian = "little"))]
60 /// Converts a vector from big endian to the target's endianness.
62 /// On big endian this is a no-op. On little endian the bytes are
65 pub fn from_be(x
: Self) -> Self {
66 #[cfg(target_endian = "big")]
70 #[cfg(not(target_endian = "big"))]
79 paste
::item_with_macros
! {
80 pub mod [<$id _swap_bytes
>] {
83 const BYTES
: [u8; 64] = [
84 0, 1, 2, 3, 4, 5, 6, 7,
85 8, 9, 10, 11, 12, 13, 14, 15,
86 16, 17, 18, 19, 20, 21, 22, 23,
87 24, 25, 26, 27, 28, 29, 30, 31,
88 32, 33, 34, 35, 36, 37, 38, 39,
89 40, 41, 42, 43, 44, 45, 46, 47,
90 48, 49, 50, 51, 52, 53, 54, 55,
91 56, 57, 58, 59, 60, 61, 62, 63,
96 // catch possible future >512 vectors
97 assert
!(mem
::size_of
::<$id
>() <= 64);
99 let mut actual
= BYTES
;
100 let elems
: &mut [$elem_ty
] = unsafe {
101 slice
::from_raw_parts_mut(
102 actual
.as_mut_ptr() as *mut $elem_ty
,
107 let vec
= $id
::from_slice_unaligned(elems
);
108 $id
::$
func(vec
).write_to_slice_unaligned(elems
);
114 macro_rules
! test_swap
{
116 let actual
= swap
!($func
);
119 .skip(64 - crate::mem
::size_of
::<$id
>());
120 assert
!(actual
.iter().zip(expected
)
121 .all(|(x
, y
)| x
== y
));
125 macro_rules
! test_no_swap
{
127 let actual
= swap
!($func
);
128 let expected
= BYTES
.iter()
129 .take(mem
::size_of
::<$id
>());
131 assert
!(actual
.iter().zip(expected
)
132 .all(|(x
, y
)| x
== y
));
136 #[cfg_attr(not(target_arch = "wasm32"), test)] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
138 test_swap
!(swap_bytes
);
141 #[cfg_attr(not(target_arch = "wasm32"), test)] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
143 #[cfg(target_endian = "little")]
145 test_no_swap
!(to_le
);
147 #[cfg(not(target_endian = "little"))]
153 #[cfg_attr(not(target_arch = "wasm32"), test)] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
155 #[cfg(target_endian = "big")]
157 test_no_swap
!(to_be
);
159 #[cfg(not(target_endian = "big"))]
165 #[cfg_attr(not(target_arch = "wasm32"), test)] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
167 #[cfg(target_endian = "little")]
169 test_no_swap
!(from_le
);
171 #[cfg(not(target_endian = "little"))]
177 #[cfg_attr(not(target_arch = "wasm32"), test)] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
179 #[cfg(target_endian = "big")]
181 test_no_swap
!(from_be
);
183 #[cfg(not(target_endian = "big"))]