Skip to content

Commit 054f25f

Browse files
committed
Convert all vectors to a single type
1 parent c36d17d commit 054f25f

File tree

4 files changed

+101
-70
lines changed

4 files changed

+101
-70
lines changed

crates/core_simd/src/vector.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,85 @@ pub use uint::*;
1212
// Vectors of pointers are not for public use at the current time.
1313
pub(crate) mod ptr;
1414

15+
use crate::{LaneCount, SupportedLaneCount};
16+
17+
/// A SIMD vector of `LANES` elements of type `Element`.
18+
#[repr(simd)]
19+
pub struct Simd<Element, const LANES: usize>([Element; LANES])
20+
where
21+
Element: SimdElement,
22+
LaneCount<LANES>: SupportedLaneCount;
23+
1524
mod sealed {
1625
pub trait Sealed {}
1726
}
27+
use sealed::Sealed;
28+
29+
/// Marker trait for types that may be used as SIMD vector elements.
30+
pub unsafe trait SimdElement: Sealed {
31+
/// The mask element type corresponding to this element type.
32+
type Mask: SimdElement;
33+
}
34+
35+
impl Sealed for u8 {}
36+
unsafe impl SimdElement for u8 {
37+
type Mask = u8;
38+
}
39+
40+
impl Sealed for u16 {}
41+
unsafe impl SimdElement for u16 {
42+
type Mask = u16;
43+
}
44+
45+
impl Sealed for u32 {}
46+
unsafe impl SimdElement for u32 {
47+
type Mask = u32;
48+
}
49+
50+
impl Sealed for u64 {}
51+
unsafe impl SimdElement for u64 {
52+
type Mask = u64;
53+
}
54+
55+
impl Sealed for usize {}
56+
unsafe impl SimdElement for usize {
57+
type Mask = usize;
58+
}
59+
60+
impl Sealed for i8 {}
61+
unsafe impl SimdElement for i8 {
62+
type Mask = i8;
63+
}
64+
65+
impl Sealed for i16 {}
66+
unsafe impl SimdElement for i16 {
67+
type Mask = i16;
68+
}
69+
70+
impl Sealed for i32 {}
71+
unsafe impl SimdElement for i32 {
72+
type Mask = i32;
73+
}
74+
75+
impl Sealed for i64 {}
76+
unsafe impl SimdElement for i64 {
77+
type Mask = i64;
78+
}
79+
80+
impl Sealed for isize {}
81+
unsafe impl SimdElement for isize {
82+
type Mask = isize;
83+
}
84+
85+
impl Sealed for f32 {}
86+
unsafe impl SimdElement for f32 {
87+
type Mask = i32;
88+
}
89+
90+
impl Sealed for f64 {}
91+
unsafe impl SimdElement for f64 {
92+
type Mask = i64;
93+
}
1894

1995
/// A representation of a vector as an "array" with indices, implementing
2096
/// operations applicable to any vector type based solely on "having lanes",

crates/core_simd/src/vector/float.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -187,19 +187,12 @@ macro_rules! impl_float_vector {
187187
}
188188

189189
/// A SIMD vector of containing `LANES` `f32` values.
190-
#[repr(simd)]
191-
pub struct SimdF32<const LANES: usize>([f32; LANES])
192-
where
193-
LaneCount<LANES>: SupportedLaneCount;
194-
195-
impl_float_vector! { SimdF32, f32, SimdU32, Mask32, SimdI32 }
190+
pub type SimdF32<const LANES: usize> = crate::Simd<f32, LANES>;
196191

197192
/// A SIMD vector of containing `LANES` `f64` values.
198-
#[repr(simd)]
199-
pub struct SimdF64<const LANES: usize>([f64; LANES])
200-
where
201-
LaneCount<LANES>: SupportedLaneCount;
193+
pub type SimdF64<const LANES: usize> = crate::Simd<f64, LANES>;
202194

195+
impl_float_vector! { SimdF32, f32, SimdU32, Mask32, SimdI32 }
203196
impl_float_vector! { SimdF64, f64, SimdU64, Mask64, SimdI64 }
204197

205198
/// Vector of two `f32` values

crates/core_simd/src/vector/int.rs

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -62,44 +62,25 @@ macro_rules! impl_integer_vector {
6262
}
6363
}
6464

65-
/// A SIMD vector of containing `LANES` `isize` values.
66-
#[repr(simd)]
67-
pub struct SimdIsize<const LANES: usize>([isize; LANES])
68-
where
69-
LaneCount<LANES>: SupportedLaneCount;
70-
71-
impl_integer_vector! { SimdIsize, isize, MaskSize, SimdIsize }
65+
/// A SIMD vector of containing `LANES` `i8` values.
66+
pub type SimdI8<const LANES: usize> = crate::Simd<i8, LANES>;
7267

7368
/// A SIMD vector of containing `LANES` `i16` values.
74-
#[repr(simd)]
75-
pub struct SimdI16<const LANES: usize>([i16; LANES])
76-
where
77-
LaneCount<LANES>: SupportedLaneCount;
78-
79-
impl_integer_vector! { SimdI16, i16, Mask16, SimdI16 }
69+
pub type SimdI16<const LANES: usize> = crate::Simd<i16, LANES>;
8070

8171
/// A SIMD vector of containing `LANES` `i32` values.
82-
#[repr(simd)]
83-
pub struct SimdI32<const LANES: usize>([i32; LANES])
84-
where
85-
LaneCount<LANES>: SupportedLaneCount;
86-
87-
impl_integer_vector! { SimdI32, i32, Mask32, SimdI32 }
72+
pub type SimdI32<const LANES: usize> = crate::Simd<i32, LANES>;
8873

8974
/// A SIMD vector of containing `LANES` `i64` values.
90-
#[repr(simd)]
91-
pub struct SimdI64<const LANES: usize>([i64; LANES])
92-
where
93-
LaneCount<LANES>: SupportedLaneCount;
75+
pub type SimdI64<const LANES: usize> = crate::Simd<i64, LANES>;
9476

95-
impl_integer_vector! { SimdI64, i64, Mask64, SimdI64 }
96-
97-
/// A SIMD vector of containing `LANES` `i8` values.
98-
#[repr(simd)]
99-
pub struct SimdI8<const LANES: usize>([i8; LANES])
100-
where
101-
LaneCount<LANES>: SupportedLaneCount;
77+
/// A SIMD vector of containing `LANES` `isize` values.
78+
pub type SimdIsize<const LANES: usize> = crate::Simd<isize, LANES>;
10279

80+
impl_integer_vector! { SimdIsize, isize, MaskSize, SimdIsize }
81+
impl_integer_vector! { SimdI16, i16, Mask16, SimdI16 }
82+
impl_integer_vector! { SimdI32, i32, Mask32, SimdI32 }
83+
impl_integer_vector! { SimdI64, i64, Mask64, SimdI64 }
10384
impl_integer_vector! { SimdI8, i8, Mask8, SimdI8 }
10485

10586
/// Vector of two `isize` values

crates/core_simd/src/vector/uint.rs

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,44 +33,25 @@ macro_rules! impl_unsigned_vector {
3333
}
3434
}
3535

36-
/// A SIMD vector of containing `LANES` `usize` values.
37-
#[repr(simd)]
38-
pub struct SimdUsize<const LANES: usize>([usize; LANES])
39-
where
40-
LaneCount<LANES>: SupportedLaneCount;
41-
42-
impl_unsigned_vector! { SimdUsize, usize }
36+
/// A SIMD vector of containing `LANES` `u8` values.
37+
pub type SimdU8<const LANES: usize> = crate::Simd<u8, LANES>;
4338

4439
/// A SIMD vector of containing `LANES` `u16` values.
45-
#[repr(simd)]
46-
pub struct SimdU16<const LANES: usize>([u16; LANES])
47-
where
48-
LaneCount<LANES>: SupportedLaneCount;
49-
50-
impl_unsigned_vector! { SimdU16, u16 }
40+
pub type SimdU16<const LANES: usize> = crate::Simd<u16, LANES>;
5141

5242
/// A SIMD vector of containing `LANES` `u32` values.
53-
#[repr(simd)]
54-
pub struct SimdU32<const LANES: usize>([u32; LANES])
55-
where
56-
LaneCount<LANES>: SupportedLaneCount;
57-
58-
impl_unsigned_vector! { SimdU32, u32 }
43+
pub type SimdU32<const LANES: usize> = crate::Simd<u32, LANES>;
5944

6045
/// A SIMD vector of containing `LANES` `u64` values.
61-
#[repr(simd)]
62-
pub struct SimdU64<const LANES: usize>([u64; LANES])
63-
where
64-
LaneCount<LANES>: SupportedLaneCount;
46+
pub type SimdU64<const LANES: usize> = crate::Simd<u64, LANES>;
6547

66-
impl_unsigned_vector! { SimdU64, u64 }
67-
68-
/// A SIMD vector of containing `LANES` `u8` values.
69-
#[repr(simd)]
70-
pub struct SimdU8<const LANES: usize>([u8; LANES])
71-
where
72-
LaneCount<LANES>: SupportedLaneCount;
48+
/// A SIMD vector of containing `LANES` `usize` values.
49+
pub type SimdUsize<const LANES: usize> = crate::Simd<usize, LANES>;
7350

51+
impl_unsigned_vector! { SimdUsize, usize }
52+
impl_unsigned_vector! { SimdU16, u16 }
53+
impl_unsigned_vector! { SimdU32, u32 }
54+
impl_unsigned_vector! { SimdU64, u64 }
7455
impl_unsigned_vector! { SimdU8, u8 }
7556

7657
/// Vector of two `usize` values

0 commit comments

Comments
 (0)