diff --git a/crates/core_simd/src/vector.rs b/crates/core_simd/src/vector.rs index 1f6df533767e..ea1a732f2035 100644 --- a/crates/core_simd/src/vector.rs +++ b/crates/core_simd/src/vector.rs @@ -12,9 +12,85 @@ pub use uint::*; // Vectors of pointers are not for public use at the current time. pub(crate) mod ptr; +use crate::{LaneCount, SupportedLaneCount}; + +/// A SIMD vector of `LANES` elements of type `Element`. +#[repr(simd)] +pub struct Simd([Element; LANES]) +where + Element: SimdElement, + LaneCount: SupportedLaneCount; + mod sealed { pub trait Sealed {} } +use sealed::Sealed; + +/// Marker trait for types that may be used as SIMD vector elements. +pub unsafe trait SimdElement: Sealed { + /// The mask element type corresponding to this element type. + type Mask: SimdElement; +} + +impl Sealed for u8 {} +unsafe impl SimdElement for u8 { + type Mask = u8; +} + +impl Sealed for u16 {} +unsafe impl SimdElement for u16 { + type Mask = u16; +} + +impl Sealed for u32 {} +unsafe impl SimdElement for u32 { + type Mask = u32; +} + +impl Sealed for u64 {} +unsafe impl SimdElement for u64 { + type Mask = u64; +} + +impl Sealed for usize {} +unsafe impl SimdElement for usize { + type Mask = usize; +} + +impl Sealed for i8 {} +unsafe impl SimdElement for i8 { + type Mask = i8; +} + +impl Sealed for i16 {} +unsafe impl SimdElement for i16 { + type Mask = i16; +} + +impl Sealed for i32 {} +unsafe impl SimdElement for i32 { + type Mask = i32; +} + +impl Sealed for i64 {} +unsafe impl SimdElement for i64 { + type Mask = i64; +} + +impl Sealed for isize {} +unsafe impl SimdElement for isize { + type Mask = isize; +} + +impl Sealed for f32 {} +unsafe impl SimdElement for f32 { + type Mask = i32; +} + +impl Sealed for f64 {} +unsafe impl SimdElement for f64 { + type Mask = i64; +} /// A representation of a vector as an "array" with indices, implementing /// operations applicable to any vector type based solely on "having lanes", diff --git a/crates/core_simd/src/vector/float.rs b/crates/core_simd/src/vector/float.rs index bdeccd037a80..231fe590ada5 100644 --- a/crates/core_simd/src/vector/float.rs +++ b/crates/core_simd/src/vector/float.rs @@ -187,19 +187,12 @@ macro_rules! impl_float_vector { } /// A SIMD vector of containing `LANES` `f32` values. -#[repr(simd)] -pub struct SimdF32([f32; LANES]) -where - LaneCount: SupportedLaneCount; - -impl_float_vector! { SimdF32, f32, SimdU32, Mask32, SimdI32 } +pub type SimdF32 = crate::Simd; /// A SIMD vector of containing `LANES` `f64` values. -#[repr(simd)] -pub struct SimdF64([f64; LANES]) -where - LaneCount: SupportedLaneCount; +pub type SimdF64 = crate::Simd; +impl_float_vector! { SimdF32, f32, SimdU32, Mask32, SimdI32 } impl_float_vector! { SimdF64, f64, SimdU64, Mask64, SimdI64 } /// Vector of two `f32` values diff --git a/crates/core_simd/src/vector/int.rs b/crates/core_simd/src/vector/int.rs index 73c737762fb6..88a17daa2f4b 100644 --- a/crates/core_simd/src/vector/int.rs +++ b/crates/core_simd/src/vector/int.rs @@ -62,44 +62,25 @@ macro_rules! impl_integer_vector { } } -/// A SIMD vector of containing `LANES` `isize` values. -#[repr(simd)] -pub struct SimdIsize([isize; LANES]) -where - LaneCount: SupportedLaneCount; - -impl_integer_vector! { SimdIsize, isize, MaskSize, SimdIsize } +/// A SIMD vector of containing `LANES` `i8` values. +pub type SimdI8 = crate::Simd; /// A SIMD vector of containing `LANES` `i16` values. -#[repr(simd)] -pub struct SimdI16([i16; LANES]) -where - LaneCount: SupportedLaneCount; - -impl_integer_vector! { SimdI16, i16, Mask16, SimdI16 } +pub type SimdI16 = crate::Simd; /// A SIMD vector of containing `LANES` `i32` values. -#[repr(simd)] -pub struct SimdI32([i32; LANES]) -where - LaneCount: SupportedLaneCount; - -impl_integer_vector! { SimdI32, i32, Mask32, SimdI32 } +pub type SimdI32 = crate::Simd; /// A SIMD vector of containing `LANES` `i64` values. -#[repr(simd)] -pub struct SimdI64([i64; LANES]) -where - LaneCount: SupportedLaneCount; +pub type SimdI64 = crate::Simd; +/// A SIMD vector of containing `LANES` `isize` values. +pub type SimdIsize = crate::Simd; + +impl_integer_vector! { SimdIsize, isize, MaskSize, SimdIsize } +impl_integer_vector! { SimdI16, i16, Mask16, SimdI16 } +impl_integer_vector! { SimdI32, i32, Mask32, SimdI32 } impl_integer_vector! { SimdI64, i64, Mask64, SimdI64 } - -/// A SIMD vector of containing `LANES` `i8` values. -#[repr(simd)] -pub struct SimdI8([i8; LANES]) -where - LaneCount: SupportedLaneCount; - impl_integer_vector! { SimdI8, i8, Mask8, SimdI8 } /// Vector of two `isize` values diff --git a/crates/core_simd/src/vector/uint.rs b/crates/core_simd/src/vector/uint.rs index b19f694872ae..5bd1a7fd67fd 100644 --- a/crates/core_simd/src/vector/uint.rs +++ b/crates/core_simd/src/vector/uint.rs @@ -33,44 +33,25 @@ macro_rules! impl_unsigned_vector { } } -/// A SIMD vector of containing `LANES` `usize` values. -#[repr(simd)] -pub struct SimdUsize([usize; LANES]) -where - LaneCount: SupportedLaneCount; - -impl_unsigned_vector! { SimdUsize, usize } +/// A SIMD vector of containing `LANES` `u8` values. +pub type SimdU8 = crate::Simd; /// A SIMD vector of containing `LANES` `u16` values. -#[repr(simd)] -pub struct SimdU16([u16; LANES]) -where - LaneCount: SupportedLaneCount; - -impl_unsigned_vector! { SimdU16, u16 } +pub type SimdU16 = crate::Simd; /// A SIMD vector of containing `LANES` `u32` values. -#[repr(simd)] -pub struct SimdU32([u32; LANES]) -where - LaneCount: SupportedLaneCount; - -impl_unsigned_vector! { SimdU32, u32 } +pub type SimdU32 = crate::Simd; /// A SIMD vector of containing `LANES` `u64` values. -#[repr(simd)] -pub struct SimdU64([u64; LANES]) -where - LaneCount: SupportedLaneCount; +pub type SimdU64 = crate::Simd; +/// A SIMD vector of containing `LANES` `usize` values. +pub type SimdUsize = crate::Simd; + +impl_unsigned_vector! { SimdUsize, usize } +impl_unsigned_vector! { SimdU16, u16 } +impl_unsigned_vector! { SimdU32, u32 } impl_unsigned_vector! { SimdU64, u64 } - -/// A SIMD vector of containing `LANES` `u8` values. -#[repr(simd)] -pub struct SimdU8([u8; LANES]) -where - LaneCount: SupportedLaneCount; - impl_unsigned_vector! { SimdU8, u8 } /// Vector of two `usize` values