diff --git a/crates/core_simd/src/macros.rs b/crates/core_simd/src/macros.rs index ecf2b76ceb0..165029cca5e 100644 --- a/crates/core_simd/src/macros.rs +++ b/crates/core_simd/src/macros.rs @@ -207,6 +207,28 @@ macro_rules! base_vector_traits { } } +/// Implements additional integer traits (Eq, Ord, Hash) on the specified vector `$name`, holding multiple `$lanes` of `$type`. +macro_rules! integer_vector_traits { + { $name:path => [$type:ty; $lanes:literal] } => { + impl Eq for $name {} + + impl Ord for $name { + fn cmp(&self, other: &Self) -> core::cmp::Ordering { + AsRef::<[$type]>::as_ref(self).cmp(AsRef::<[$type]>::as_ref(other)) + } + } + + impl core::hash::Hash for $name { + fn hash(&self, state: &mut H) + where + H: core::hash::Hasher + { + AsRef::<[$type]>::as_ref(self).hash(state) + } + } + } +} + /// Defines a vector `$name` containing multiple `$lanes` of `$type`. macro_rules! define_vector { { $(#[$attr:meta])* struct $name:ident([$type:ty; $lanes:tt]); } => { @@ -242,6 +264,18 @@ macro_rules! define_vector { } } +/// Defines an integer vector `$name` containing multiple `$lanes` of integer `$type`. +macro_rules! define_integer_vector { + { $(#[$attr:meta])* struct $name:ident([$type:ty; $lanes:tt]); } => { + define_vector! { + $(#[$attr])* + struct $name([$type; $lanes]); + } + + integer_vector_traits! { $name => [$type; $lanes] } + } +} + /// Defines a mask vector `$name` containing multiple `$lanes` of `$type`, represented by the /// underlying type `$impl_type`. macro_rules! define_mask_vector { @@ -254,11 +288,11 @@ macro_rules! define_mask_vector { } base_vector_traits! { $name => [$type; $lanes] } + integer_vector_traits! { $name => [$type; $lanes] } }; { def $(#[$attr:meta])* | $name:ident | $($itype:ty)* } => { $(#[$attr])* #[allow(non_camel_case_types)] - #[derive(Eq, Ord)] #[repr(simd)] pub struct $name($($itype),*); }; diff --git a/crates/core_simd/src/vectors_i128.rs b/crates/core_simd/src/vectors_i128.rs index 588f4cffb90..5c8354070e8 100644 --- a/crates/core_simd/src/vectors_i128.rs +++ b/crates/core_simd/src/vectors_i128.rs @@ -1,12 +1,10 @@ -define_vector! { +define_integer_vector! { /// Vector of two `i128` values - #[derive(Eq, Ord, Hash)] struct i128x2([i128; 2]); } -define_vector! { +define_integer_vector! { /// Vector of four `i128` values - #[derive(Eq, Ord, Hash)] struct i128x4([i128; 4]); } diff --git a/crates/core_simd/src/vectors_i16.rs b/crates/core_simd/src/vectors_i16.rs index 52d9ef8379b..011820e19c7 100644 --- a/crates/core_simd/src/vectors_i16.rs +++ b/crates/core_simd/src/vectors_i16.rs @@ -1,30 +1,25 @@ -define_vector! { +define_integer_vector! { /// Vector of two `i16` values - #[derive(Eq, Ord, Hash)] struct i16x2([i16; 2]); } -define_vector! { +define_integer_vector! { /// Vector of four `i16` values - #[derive(Eq, Ord, Hash)] struct i16x4([i16; 4]); } -define_vector! { +define_integer_vector! { /// Vector of eight `i16` values - #[derive(Eq, Ord, Hash)] struct i16x8([i16; 8]); } -define_vector! { +define_integer_vector! { /// Vector of 16 `i16` values - #[derive(Eq, Ord, Hash)] struct i16x16([i16; 16]); } -define_vector! { +define_integer_vector! { /// Vector of 32 `i16` values - #[derive(Eq, Ord, Hash)] struct i16x32([i16; 32]); } diff --git a/crates/core_simd/src/vectors_i32.rs b/crates/core_simd/src/vectors_i32.rs index 34ecc169460..9aa9bc8e9dc 100644 --- a/crates/core_simd/src/vectors_i32.rs +++ b/crates/core_simd/src/vectors_i32.rs @@ -1,24 +1,20 @@ -define_vector! { +define_integer_vector! { /// Vector of two `i32` values - #[derive(Eq, Ord, Hash)] struct i32x2([i32; 2]); } -define_vector! { +define_integer_vector! { /// Vector of four `i32` values - #[derive(Eq, Ord, Hash)] struct i32x4([i32; 4]); } -define_vector! { +define_integer_vector! { /// Vector of eight `i32` values - #[derive(Eq, Ord, Hash)] struct i32x8([i32; 8]); } -define_vector! { +define_integer_vector! { /// Vector of 16 `i32` values - #[derive(Eq, Ord, Hash)] struct i32x16([i32; 16]); } diff --git a/crates/core_simd/src/vectors_i64.rs b/crates/core_simd/src/vectors_i64.rs index 58893c0dfed..ba66aba2095 100644 --- a/crates/core_simd/src/vectors_i64.rs +++ b/crates/core_simd/src/vectors_i64.rs @@ -1,18 +1,15 @@ -define_vector! { +define_integer_vector! { /// Vector of two `i64` values - #[derive(Eq, Ord, Hash)] struct i64x2([i64; 2]); } -define_vector! { +define_integer_vector! { /// Vector of four `i64` values - #[derive(Eq, Ord, Hash)] struct i64x4([i64; 4]); } -define_vector! { +define_integer_vector! { /// Vector of eight `i64` values - #[derive(Eq, Ord, Hash)] struct i64x8([i64; 8]); } diff --git a/crates/core_simd/src/vectors_i8.rs b/crates/core_simd/src/vectors_i8.rs index c0551fc285e..fb739bc44fa 100644 --- a/crates/core_simd/src/vectors_i8.rs +++ b/crates/core_simd/src/vectors_i8.rs @@ -1,36 +1,30 @@ -define_vector! { +define_integer_vector! { /// Vector of two `i8` values - #[derive(Eq, Ord, Hash)] struct i8x2([i8; 2]); } -define_vector! { +define_integer_vector! { /// Vector of four `i8` values - #[derive(Eq, Ord, Hash)] struct i8x4([i8; 4]); } -define_vector! { +define_integer_vector! { /// Vector of eight `i8` values - #[derive(Eq, Ord, Hash)] struct i8x8([i8; 8]); } -define_vector! { +define_integer_vector! { /// Vector of 16 `i8` values - #[derive(Eq, Ord, Hash)] struct i8x16([i8; 16]); } -define_vector! { +define_integer_vector! { /// Vector of 32 `i8` values - #[derive(Eq, Ord, Hash)] struct i8x32([i8; 32]); } -define_vector! { +define_integer_vector! { /// Vector of 64 `i8` values - #[derive(Eq, Ord, Hash)] struct i8x64([i8; 64]); } diff --git a/crates/core_simd/src/vectors_isize.rs b/crates/core_simd/src/vectors_isize.rs index 10a892bd090..35dac8bcbd4 100644 --- a/crates/core_simd/src/vectors_isize.rs +++ b/crates/core_simd/src/vectors_isize.rs @@ -1,18 +1,15 @@ -define_vector! { +define_integer_vector! { /// Vector of two `isize` values - #[derive(Eq, Ord, Hash)] struct isizex2([isize; 2]); } -define_vector! { +define_integer_vector! { /// Vector of four `isize` values - #[derive(Eq, Ord, Hash)] struct isizex4([isize; 4]); } -define_vector! { +define_integer_vector! { /// Vector of eight `isize` values - #[derive(Eq, Ord, Hash)] struct isizex8([isize; 8]); } diff --git a/crates/core_simd/src/vectors_u128.rs b/crates/core_simd/src/vectors_u128.rs index 1412dfdc85d..eec7bde1722 100644 --- a/crates/core_simd/src/vectors_u128.rs +++ b/crates/core_simd/src/vectors_u128.rs @@ -1,12 +1,10 @@ -define_vector! { +define_integer_vector! { /// Vector of two `u128` values - #[derive(Eq, Ord, Hash)] struct u128x2([u128; 2]); } -define_vector! { +define_integer_vector! { /// Vector of four `u128` values - #[derive(Eq, Ord, Hash)] struct u128x4([u128; 4]); } diff --git a/crates/core_simd/src/vectors_u16.rs b/crates/core_simd/src/vectors_u16.rs index 6fd7c64f4b3..9e846a32efa 100644 --- a/crates/core_simd/src/vectors_u16.rs +++ b/crates/core_simd/src/vectors_u16.rs @@ -1,30 +1,25 @@ -define_vector! { +define_integer_vector! { /// Vector of two `u16` values - #[derive(Eq, Ord, Hash)] struct u16x2([u16; 2]); } -define_vector! { +define_integer_vector! { /// Vector of four `u16` values - #[derive(Eq, Ord, Hash)] struct u16x4([u16; 4]); } -define_vector! { +define_integer_vector! { /// Vector of eight `u16` values - #[derive(Eq, Ord, Hash)] struct u16x8([u16; 8]); } -define_vector! { +define_integer_vector! { /// Vector of 16 `u16` values - #[derive(Eq, Ord, Hash)] struct u16x16([u16; 16]); } -define_vector! { +define_integer_vector! { /// Vector of 32 `u16` values - #[derive(Eq, Ord, Hash)] struct u16x32([u16; 32]); } diff --git a/crates/core_simd/src/vectors_u32.rs b/crates/core_simd/src/vectors_u32.rs index d4e1fd439ff..b00c63d9058 100644 --- a/crates/core_simd/src/vectors_u32.rs +++ b/crates/core_simd/src/vectors_u32.rs @@ -1,24 +1,20 @@ -define_vector! { +define_integer_vector! { /// Vector of two `u32` values - #[derive(Eq, Ord, Hash)] struct u32x2([u32; 2]); } -define_vector! { +define_integer_vector! { /// Vector of four `u32` values - #[derive(Eq, Ord, Hash)] struct u32x4([u32; 4]); } -define_vector! { +define_integer_vector! { /// Vector of eight `u32` values - #[derive(Eq, Ord, Hash)] struct u32x8([u32; 8]); } -define_vector! { +define_integer_vector! { /// Vector of 16 `u32` values - #[derive(Eq, Ord, Hash)] struct u32x16([u32; 16]); } diff --git a/crates/core_simd/src/vectors_u64.rs b/crates/core_simd/src/vectors_u64.rs index 6a2b0ef7630..0bcf28ebc26 100644 --- a/crates/core_simd/src/vectors_u64.rs +++ b/crates/core_simd/src/vectors_u64.rs @@ -1,18 +1,15 @@ -define_vector! { +define_integer_vector! { /// Vector of two `u64` values - #[derive(Eq, Ord, Hash)] struct u64x2([u64; 2]); } -define_vector! { +define_integer_vector! { /// Vector of four `u64` values - #[derive(Eq, Ord, Hash)] struct u64x4([u64; 4]); } -define_vector! { +define_integer_vector! { /// Vector of eight `u64` values - #[derive(Eq, Ord, Hash)] struct u64x8([u64; 8]); } diff --git a/crates/core_simd/src/vectors_u8.rs b/crates/core_simd/src/vectors_u8.rs index 487a25e2d66..d70de1a24c9 100644 --- a/crates/core_simd/src/vectors_u8.rs +++ b/crates/core_simd/src/vectors_u8.rs @@ -1,36 +1,30 @@ -define_vector! { +define_integer_vector! { /// Vector of two `u8` values - #[derive(Eq, Ord, Hash)] struct u8x2([u8; 2]); } -define_vector! { +define_integer_vector! { /// Vector of four `u8` values - #[derive(Eq, Ord, Hash)] struct u8x4([u8; 4]); } -define_vector! { +define_integer_vector! { /// Vector of eight `u8` values - #[derive(Eq, Ord, Hash)] struct u8x8([u8; 8]); } -define_vector! { +define_integer_vector! { /// Vector of 16 `u8` values - #[derive(Eq, Ord, Hash)] struct u8x16([u8; 16]); } -define_vector! { +define_integer_vector! { /// Vector of 32 `u8` values - #[derive(Eq, Ord, Hash)] struct u8x32([u8; 32]); } -define_vector! { +define_integer_vector! { /// Vector of 64 `u8` values - #[derive(Eq, Ord, Hash)] struct u8x64([u8; 64]); } diff --git a/crates/core_simd/src/vectors_usize.rs b/crates/core_simd/src/vectors_usize.rs index 2318d63a001..84a4b8e509b 100644 --- a/crates/core_simd/src/vectors_usize.rs +++ b/crates/core_simd/src/vectors_usize.rs @@ -1,18 +1,15 @@ -define_vector! { +define_integer_vector! { /// Vector of two `usize` values - #[derive(Eq, Ord, Hash)] struct usizex2([usize; 2]); } -define_vector! { +define_integer_vector! { /// Vector of four `usize` values - #[derive(Eq, Ord, Hash)] struct usizex4([usize; 4]); } -define_vector! { +define_integer_vector! { /// Vector of eight `usize` values - #[derive(Eq, Ord, Hash)] struct usizex8([usize; 8]); }