|
| 1 | +//! Auxiliary crate for tests that need SIMD types. |
| 2 | +//! |
| 3 | +//! Historically the tests just made their own, but projections into simd types |
| 4 | +//! was banned by <https://github.com/rust-lang/compiler-team/issues/838>, which |
| 5 | +//! breaks `derive(Clone)`, so this exists to give easily-usable types that can |
| 6 | +//! be used without copy-pasting the definitions of the helpers everywhere. |
| 7 | +//! |
| 8 | +//! This makes no attempt to guard against ICEs. Using it with proper types |
| 9 | +//! and such is your responsibility in the tests you write. |
| 10 | +
|
| 11 | +#![allow(unused)] |
| 12 | +#![allow(non_camel_case_types)] |
| 13 | + |
| 14 | +// The field is currently left `pub` for convenience in porting tests, many of |
| 15 | +// which attempt to just construct it directly. That still works; it's just the |
| 16 | +// `.0` projection that doesn't. |
| 17 | +#[repr(simd)] |
| 18 | +#[derive(Copy, Eq)] |
| 19 | +pub struct Simd<T, const N: usize>(pub [T; N]); |
| 20 | + |
| 21 | +impl<T: Copy, const N: usize> Clone for Simd<T, N> { |
| 22 | + fn clone(&self) -> Self { |
| 23 | + *self |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +impl<T: PartialEq, const N: usize> PartialEq for Simd<T, N> { |
| 28 | + fn eq(&self, other: &Self) -> bool { |
| 29 | + self.as_array() == other.as_array() |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +impl<T: core::fmt::Debug, const N: usize> core::fmt::Debug for Simd<T, N> { |
| 34 | + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> { |
| 35 | + <[T; N] as core::fmt::Debug>::fmt(self.as_array(), f) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl<T, const N: usize> core::ops::Index<usize> for Simd<T, N> { |
| 40 | + type Output = T; |
| 41 | + fn index(&self, i: usize) -> &T { |
| 42 | + &self.as_array()[i] |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl<T, const N: usize> Simd<T, N> { |
| 47 | + pub const fn from_array(a: [T; N]) -> Self { |
| 48 | + Simd(a) |
| 49 | + } |
| 50 | + pub fn as_array(&self) -> &[T; N] { |
| 51 | + let p: *const Self = self; |
| 52 | + unsafe { &*p.cast::<[T; N]>() } |
| 53 | + } |
| 54 | + pub fn into_array(self) -> [T; N] |
| 55 | + where |
| 56 | + T: Copy, |
| 57 | + { |
| 58 | + *self.as_array() |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +pub type u8x2 = Simd<u8, 2>; |
| 63 | +pub type u8x4 = Simd<u8, 4>; |
| 64 | +pub type u8x8 = Simd<u8, 8>; |
| 65 | +pub type u8x16 = Simd<u8, 16>; |
| 66 | +pub type u8x32 = Simd<u8, 32>; |
| 67 | +pub type u8x64 = Simd<u8, 64>; |
| 68 | + |
| 69 | +pub type u16x2 = Simd<u16, 2>; |
| 70 | +pub type u16x4 = Simd<u16, 4>; |
| 71 | +pub type u16x8 = Simd<u16, 8>; |
| 72 | +pub type u16x16 = Simd<u16, 16>; |
| 73 | +pub type u16x32 = Simd<u16, 32>; |
| 74 | + |
| 75 | +pub type u32x2 = Simd<u32, 2>; |
| 76 | +pub type u32x4 = Simd<u32, 4>; |
| 77 | +pub type u32x8 = Simd<u32, 8>; |
| 78 | +pub type u32x16 = Simd<u32, 16>; |
| 79 | + |
| 80 | +pub type u64x2 = Simd<u64, 2>; |
| 81 | +pub type u64x4 = Simd<u64, 4>; |
| 82 | +pub type u64x8 = Simd<u64, 8>; |
| 83 | + |
| 84 | +pub type u128x2 = Simd<u128, 2>; |
| 85 | +pub type u128x4 = Simd<u128, 4>; |
| 86 | + |
| 87 | +pub type i8x2 = Simd<i8, 2>; |
| 88 | +pub type i8x4 = Simd<i8, 4>; |
| 89 | +pub type i8x8 = Simd<i8, 8>; |
| 90 | +pub type i8x16 = Simd<i8, 16>; |
| 91 | +pub type i8x32 = Simd<i8, 32>; |
| 92 | +pub type i8x64 = Simd<i8, 64>; |
| 93 | + |
| 94 | +pub type i16x2 = Simd<i16, 2>; |
| 95 | +pub type i16x4 = Simd<i16, 4>; |
| 96 | +pub type i16x8 = Simd<i16, 8>; |
| 97 | +pub type i16x16 = Simd<i16, 16>; |
| 98 | +pub type i16x32 = Simd<i16, 32>; |
| 99 | + |
| 100 | +pub type i32x2 = Simd<i32, 2>; |
| 101 | +pub type i32x4 = Simd<i32, 4>; |
| 102 | +pub type i32x8 = Simd<i32, 8>; |
| 103 | +pub type i32x16 = Simd<i32, 16>; |
| 104 | + |
| 105 | +pub type i64x2 = Simd<i64, 2>; |
| 106 | +pub type i64x4 = Simd<i64, 4>; |
| 107 | +pub type i64x8 = Simd<i64, 8>; |
| 108 | + |
| 109 | +pub type i128x2 = Simd<i128, 2>; |
| 110 | +pub type i128x4 = Simd<i128, 4>; |
| 111 | + |
| 112 | +pub type f32x2 = Simd<f32, 2>; |
| 113 | +pub type f32x4 = Simd<f32, 4>; |
| 114 | +pub type f32x8 = Simd<f32, 8>; |
| 115 | +pub type f32x16 = Simd<f32, 16>; |
| 116 | + |
| 117 | +pub type f64x2 = Simd<f64, 2>; |
| 118 | +pub type f64x4 = Simd<f64, 4>; |
| 119 | +pub type f64x8 = Simd<f64, 8>; |
| 120 | + |
| 121 | +// The field is currently left `pub` for convenience in porting tests, many of |
| 122 | +// which attempt to just construct it directly. That still works; it's just the |
| 123 | +// `.0` projection that doesn't. |
| 124 | +#[repr(simd, packed)] |
| 125 | +#[derive(Copy)] |
| 126 | +pub struct PackedSimd<T, const N: usize>(pub [T; N]); |
| 127 | + |
| 128 | +impl<T: Copy, const N: usize> Clone for PackedSimd<T, N> { |
| 129 | + fn clone(&self) -> Self { |
| 130 | + *self |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +impl<T: PartialEq, const N: usize> PartialEq for PackedSimd<T, N> { |
| 135 | + fn eq(&self, other: &Self) -> bool { |
| 136 | + self.as_array() == other.as_array() |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +impl<T: core::fmt::Debug, const N: usize> core::fmt::Debug for PackedSimd<T, N> { |
| 141 | + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> { |
| 142 | + <[T; N] as core::fmt::Debug>::fmt(self.as_array(), f) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +impl<T, const N: usize> PackedSimd<T, N> { |
| 147 | + pub const fn from_array(a: [T; N]) -> Self { |
| 148 | + PackedSimd(a) |
| 149 | + } |
| 150 | + pub fn as_array(&self) -> &[T; N] { |
| 151 | + let p: *const Self = self; |
| 152 | + unsafe { &*p.cast::<[T; N]>() } |
| 153 | + } |
| 154 | + pub fn into_array(self) -> [T; N] |
| 155 | + where |
| 156 | + T: Copy, |
| 157 | + { |
| 158 | + *self.as_array() |
| 159 | + } |
| 160 | +} |
0 commit comments