Skip to content

Commit 16765a1

Browse files
Introduce SimdArray trait
This provides a general framework for describing relationships between vector types and scalar types.
1 parent 57e67c9 commit 16765a1

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

crates/core_simd/src/array.rs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
use crate::masks::*;
2+
use crate::vector::*;
3+
4+
/// A representation of a vector as an "array" with indices, implementing
5+
/// operations applicable to any vector type based solely on "having lanes",
6+
/// and describing relationships between vector and scalar types.
7+
pub trait SimdArray<const LANES: usize>: crate::LanesAtMost32
8+
where
9+
SimdUsize<LANES>: crate::LanesAtMost32,
10+
SimdIsize<LANES>: crate::LanesAtMost32,
11+
MaskSize<LANES>: crate::Mask,
12+
Self: Sized,
13+
{
14+
/// The scalar type in every lane of this vector type.
15+
type Scalar: Copy + Sized;
16+
17+
/// Generates a SIMD vector with the same value in every lane.
18+
#[must_use]
19+
fn splat(val: Self::Scalar) -> Self;
20+
}
21+
22+
macro_rules! impl_simdarray_for {
23+
($simd:ident {type Scalar = $scalar:ident;}) => {
24+
impl<const LANES: usize> SimdArray<LANES> for $simd<LANES>
25+
where SimdUsize<LANES>: crate::LanesAtMost32,
26+
SimdIsize<LANES>: crate::LanesAtMost32,
27+
MaskSize<LANES>: crate::Mask,
28+
Self: crate::LanesAtMost32,
29+
{
30+
type Scalar = $scalar;
31+
32+
#[must_use]
33+
#[inline]
34+
fn splat(val: Self::Scalar) -> Self {
35+
[val; LANES].into()
36+
}
37+
}
38+
};
39+
40+
($simd:ident $impl:tt) => {
41+
impl<const LANES: usize> SimdArray<LANES> for $simd<LANES>
42+
where SimdUsize<LANES>: crate::LanesAtMost32,
43+
SimdIsize<LANES>: crate::LanesAtMost32,
44+
MaskSize<LANES>: crate::Mask,
45+
Self: crate::LanesAtMost32,
46+
$impl
47+
}
48+
}
49+
50+
impl_simdarray_for! {
51+
SimdUsize {
52+
type Scalar = usize;
53+
}
54+
}
55+
56+
impl_simdarray_for! {
57+
SimdIsize {
58+
type Scalar = isize;
59+
}
60+
}
61+
62+
impl_simdarray_for! {
63+
SimdI8 {
64+
type Scalar = i8;
65+
}
66+
}
67+
68+
impl_simdarray_for! {
69+
SimdI16 {
70+
type Scalar = i16;
71+
}
72+
}
73+
74+
impl_simdarray_for! {
75+
SimdI32 {
76+
type Scalar = i32;
77+
}
78+
}
79+
80+
impl_simdarray_for! {
81+
SimdI64 {
82+
type Scalar = i64;
83+
}
84+
}
85+
86+
impl_simdarray_for! {
87+
SimdU8 {
88+
type Scalar = u8;
89+
}
90+
}
91+
92+
impl_simdarray_for! {
93+
SimdU16 {
94+
type Scalar = u16;
95+
}
96+
}
97+
98+
impl_simdarray_for! {
99+
SimdU32 {
100+
type Scalar = u32;
101+
}
102+
}
103+
104+
impl_simdarray_for! {
105+
SimdU64 {
106+
type Scalar = u64;
107+
}
108+
}
109+
110+
impl_simdarray_for! {
111+
SimdF32 {
112+
type Scalar = f32;
113+
}
114+
}
115+
116+
impl_simdarray_for! {
117+
SimdF64 {
118+
type Scalar = f64;
119+
}
120+
}

crates/core_simd/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ pub use masks::*;
3535

3636
mod vector;
3737
pub use vector::*;
38+
39+
mod array;
40+
pub use array::SimdArray;

0 commit comments

Comments
 (0)