1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//! Horizontal swap bytes

macro_rules! impl_swap_bytes {
    ([$elem_ty:ident; $elem_count:expr]: $id:ident | $test_tt:tt) => {
        impl $id {
            /// Reverses the byte order of the vector.
            #[inline]
            pub fn swap_bytes(self) -> Self {
                super::codegen::swap_bytes::SwapBytes::swap_bytes(self)
            }

            /// Converts self to little endian from the target's endianness.
            ///
            /// On little endian this is a no-op. On big endian the bytes are
            /// swapped.
            #[inline]
            pub fn to_le(self) -> Self {
                #[cfg(target_endian = "little")]
                {
                    self
                }
                #[cfg(not(target_endian = "little"))]
                {
                    self.swap_bytes()
                }
            }

            /// Converts self to big endian from the target's endianness.
            ///
            /// On big endian this is a no-op. On little endian the bytes are
            /// swapped.
            #[inline]
            pub fn to_be(self) -> Self {
                #[cfg(target_endian = "big")]
                {
                    self
                }
                #[cfg(not(target_endian = "big"))]
                {
                    self.swap_bytes()
                }
            }

            /// Converts a vector from little endian to the target's endianness.
            ///
            /// On little endian this is a no-op. On big endian the bytes are
            /// swapped.
            #[inline]
            pub fn from_le(x: Self) -> Self {
                #[cfg(target_endian = "little")]
                {
                    x
                }
                #[cfg(not(target_endian = "little"))]
                {
                    x.swap_bytes()
                }
            }

            /// Converts a vector from big endian to the target's endianness.
            ///
            /// On big endian this is a no-op. On little endian the bytes are
            /// swapped.
            #[inline]
            pub fn from_be(x: Self) -> Self {
                #[cfg(target_endian = "big")]
                {
                    x
                }
                #[cfg(not(target_endian = "big"))]
                {
                    x.swap_bytes()
                }
            }
        }

        test_if! {
            $test_tt:
            paste::item_with_macros! {
                pub mod [<$id _swap_bytes>] {
                    use super::*;

                    const BYTES: [u8; 64] = [
                        0, 1, 2, 3, 4, 5, 6, 7,
                        8, 9, 10, 11, 12, 13, 14, 15,
                        16, 17, 18, 19, 20, 21, 22, 23,
                        24, 25, 26, 27, 28, 29, 30, 31,
                        32, 33, 34, 35, 36, 37, 38, 39,
                        40, 41, 42, 43, 44, 45, 46, 47,
                        48, 49, 50, 51, 52, 53, 54, 55,
                        56, 57, 58, 59, 60, 61, 62, 63,
                    ];

                    macro_rules! swap {
                        ($func: ident) => {{
                            // catch possible future >512 vectors
                            assert!(mem::size_of::<$id>() <= 64);

                            let mut actual = BYTES;
                            let elems: &mut [$elem_ty] = unsafe {
                                slice::from_raw_parts_mut(
                                    actual.as_mut_ptr() as *mut $elem_ty,
                                    $id::lanes(),
                                )
                            };

                            let vec = $id::from_slice_unaligned(elems);
                            $id::$func(vec).write_to_slice_unaligned(elems);

                            actual
                        }};
                    }

                    macro_rules! test_swap {
                        ($func: ident) => {{
                            let actual = swap!($func);
                            let expected =
                                BYTES.iter().rev()
                                .skip(64 - crate::mem::size_of::<$id>());
                            assert!(actual.iter().zip(expected)
                                    .all(|(x, y)| x == y));
                        }};
                    }

                    macro_rules! test_no_swap {
                        ($func: ident) => {{
                            let actual = swap!($func);
                            let expected = BYTES.iter()
                                .take(mem::size_of::<$id>());

                            assert!(actual.iter().zip(expected)
                                    .all(|(x, y)| x == y));
                        }};
                    }

                    #[cfg_attr(not(target_arch = "wasm32"), test)] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
                    fn swap_bytes() {
                        test_swap!(swap_bytes);
                    }

                    #[cfg_attr(not(target_arch = "wasm32"), test)] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
                    fn to_le() {
                        #[cfg(target_endian = "little")]
                        {
                            test_no_swap!(to_le);
                        }
                        #[cfg(not(target_endian = "little"))]
                        {
                            test_swap!(to_le);
                        }
                    }

                    #[cfg_attr(not(target_arch = "wasm32"), test)] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
                    fn to_be() {
                        #[cfg(target_endian = "big")]
                        {
                            test_no_swap!(to_be);
                        }
                        #[cfg(not(target_endian = "big"))]
                        {
                            test_swap!(to_be);
                        }
                    }

                    #[cfg_attr(not(target_arch = "wasm32"), test)] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
                    fn from_le() {
                        #[cfg(target_endian = "little")]
                        {
                            test_no_swap!(from_le);
                        }
                        #[cfg(not(target_endian = "little"))]
                        {
                            test_swap!(from_le);
                        }
                    }

                    #[cfg_attr(not(target_arch = "wasm32"), test)] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
                    fn from_be() {
                        #[cfg(target_endian = "big")]
                        {
                            test_no_swap!(from_be);
                        }
                        #[cfg(not(target_endian = "big"))]
                        {
                            test_swap!(from_be);
                        }
                    }
                }
            }
        }
    };
}