From 16765a10218bbdfc06375961d70477460a53fc01 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Tue, 15 Jun 2021 10:32:53 -0700 Subject: [PATCH] Introduce SimdArray trait This provides a general framework for describing relationships between vector types and scalar types. --- crates/core_simd/src/array.rs | 120 ++++++++++++++++++++++++++++++++++ crates/core_simd/src/lib.rs | 3 + 2 files changed, 123 insertions(+) create mode 100644 crates/core_simd/src/array.rs diff --git a/crates/core_simd/src/array.rs b/crates/core_simd/src/array.rs new file mode 100644 index 00000000000..d2f944d1e53 --- /dev/null +++ b/crates/core_simd/src/array.rs @@ -0,0 +1,120 @@ +use crate::masks::*; +use crate::vector::*; + +/// A representation of a vector as an "array" with indices, implementing +/// operations applicable to any vector type based solely on "having lanes", +/// and describing relationships between vector and scalar types. +pub trait SimdArray: crate::LanesAtMost32 +where + SimdUsize: crate::LanesAtMost32, + SimdIsize: crate::LanesAtMost32, + MaskSize: crate::Mask, + Self: Sized, +{ + /// The scalar type in every lane of this vector type. + type Scalar: Copy + Sized; + + /// Generates a SIMD vector with the same value in every lane. + #[must_use] + fn splat(val: Self::Scalar) -> Self; +} + +macro_rules! impl_simdarray_for { + ($simd:ident {type Scalar = $scalar:ident;}) => { + impl SimdArray for $simd + where SimdUsize: crate::LanesAtMost32, + SimdIsize: crate::LanesAtMost32, + MaskSize: crate::Mask, + Self: crate::LanesAtMost32, + { + type Scalar = $scalar; + + #[must_use] + #[inline] + fn splat(val: Self::Scalar) -> Self { + [val; LANES].into() + } + } + }; + + ($simd:ident $impl:tt) => { + impl SimdArray for $simd + where SimdUsize: crate::LanesAtMost32, + SimdIsize: crate::LanesAtMost32, + MaskSize: crate::Mask, + Self: crate::LanesAtMost32, + $impl + } +} + +impl_simdarray_for! { + SimdUsize { + type Scalar = usize; + } +} + +impl_simdarray_for! { + SimdIsize { + type Scalar = isize; + } +} + +impl_simdarray_for! { + SimdI8 { + type Scalar = i8; + } +} + +impl_simdarray_for! { + SimdI16 { + type Scalar = i16; + } +} + +impl_simdarray_for! { + SimdI32 { + type Scalar = i32; + } +} + +impl_simdarray_for! { + SimdI64 { + type Scalar = i64; + } +} + +impl_simdarray_for! { + SimdU8 { + type Scalar = u8; + } +} + +impl_simdarray_for! { + SimdU16 { + type Scalar = u16; + } +} + +impl_simdarray_for! { + SimdU32 { + type Scalar = u32; + } +} + +impl_simdarray_for! { + SimdU64 { + type Scalar = u64; + } +} + +impl_simdarray_for! { + SimdF32 { + type Scalar = f32; + } +} + +impl_simdarray_for! { + SimdF64 { + type Scalar = f64; + } +} diff --git a/crates/core_simd/src/lib.rs b/crates/core_simd/src/lib.rs index 7fe7d666e8d..1e68cf5d428 100644 --- a/crates/core_simd/src/lib.rs +++ b/crates/core_simd/src/lib.rs @@ -35,3 +35,6 @@ mod vector; pub use vector::*; + +mod array; +pub use array::SimdArray;