simdeez/base/
transmute.rs

1#[cfg(target_arch = "aarch64")]
2use core::arch::aarch64::*;
3#[cfg(target_arch = "wasm32")]
4use core::arch::wasm32::v128;
5#[cfg(target_arch = "x86")]
6use core::arch::x86::*;
7#[cfg(target_arch = "x86_64")]
8use core::arch::x86_64::*;
9
10macro_rules! make_simd_transmute {
11    ($name:ident, $scalar:ident, $sse:ident, $avx:ident, $neon:ident, $wasm:ident) => {
12        pub trait $name: Sized {
13            /// Tries to transmute the value into its underlying scalar type. Panics if the value is not a scalar.
14            fn try_transmute_scalar(&self) -> $scalar {
15                panic!("Invalid transmute: tried to transmute non-scalar into scalar");
16            }
17
18            /// Tries to create the value from its underlying scalar type. Panics if the value is not a scalar.
19            fn try_transmute_from_scalar(_scalar: $scalar) -> Self {
20                panic!("Invalid transmute: tried to transmute non-scalar into scalar");
21            }
22
23            #[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
24            /// Tries to transmute the value into its underlying Sse2 type. Panics if the value is not a Sse2.
25            fn try_transmute_sse2(&self) -> $sse {
26                panic!("Invalid transmute: tried to transmute non-sse2 into sse2");
27            }
28
29            #[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
30            /// Tries to create the value from its underlying Sse2 type. Panics if the value is not a Sse2.
31            fn try_transmute_from_sse2(_sse2: $sse) -> Self {
32                panic!("Invalid transmute: tried to transmute non-sse2 into sse2");
33            }
34
35            #[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
36            /// Tries to transmute the value into its underlying Sse4.1 type. Panics if the value is not a Sse4.1.
37            fn try_transmute_sse41(&self) -> $sse {
38                panic!("Invalid transmute: tried to transmute non-sse41 into sse41");
39            }
40
41            #[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
42            /// Tries to create the value from its underlying Sse4.1 type. Panics if the value is not a Sse4.1.
43            fn try_transmute_from_sse41(_sse41: $sse) -> Self {
44                panic!("Invalid transmute: tried to transmute non-sse41 into sse41");
45            }
46
47            #[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
48            /// Tries to transmute the value into its underlying Avx2 type. Panics if the value is not a Avx2.
49            fn try_transmute_avx2(&self) -> $avx {
50                panic!("Invalid transmute: tried to transmute non-avx2 into avx2");
51            }
52
53            #[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
54            /// Tries to create the value from its underlying Avx2 type. Panics if the value is not a Avx2.
55            fn try_transmute_from_avx2(_avx2: $avx) -> Self {
56                panic!("Invalid transmute: tried to transmute non-avx2 into avx2");
57            }
58
59            #[cfg(target_arch = "aarch64")]
60            /// Tries to transmute the value into its underlying Neon type. Panics if the value is not a Neon.
61            fn try_transmute_neon(&self) -> $neon {
62                panic!("Invalid transmute: tried to transmute non-neon into neon");
63            }
64
65            #[cfg(target_arch = "aarch64")]
66            /// Tries to create the value from its underlying Neon type. Panics if the value is not a Neon.
67            fn try_transmute_from_neon(_neon: $neon) -> Self {
68                panic!("Invalid transmute: tried to transmute non-neon into neon");
69            }
70
71            #[cfg(target_arch = "wasm32")]
72            /// Tries to transmute the value into its underlying Wasm type. Panics if the value is not a Wasm.
73            fn try_transmute_wasm(&self) -> $wasm {
74                panic!("Invalid transmute: tried to transmute non-wasm into wasm");
75            }
76
77            #[cfg(target_arch = "wasm32")]
78            /// Tries to create the value from its underlying Wasm type. Panics if the value is not a Wasm.
79            fn try_transmute_from_wasm(_wasm: $wasm) -> Self {
80                panic!("Invalid transmute: tried to transmute non-wasm into wasm");
81            }
82        }
83    };
84}
85
86make_simd_transmute!(SimdTransmuteF32, f32, __m128, __m256, float32x4_t, v128);
87make_simd_transmute!(SimdTransmuteF64, f64, __m128d, __m256d, float64x2_t, v128);
88make_simd_transmute!(SimdTransmuteI8, i8, __m128i, __m256i, int8x16_t, v128);
89make_simd_transmute!(SimdTransmuteI16, i16, __m128i, __m256i, int16x8_t, v128);
90make_simd_transmute!(SimdTransmuteI32, i32, __m128i, __m256i, int32x4_t, v128);
91make_simd_transmute!(SimdTransmuteI64, i64, __m128i, __m256i, int64x2_t, v128);