Skip to content

Commit 3ba0687

Browse files
committed
Add lane count marker type
1 parent 3743805 commit 3ba0687

21 files changed

+367
-411
lines changed

crates/core_simd/src/comparisons.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
use crate::Vector;
1+
use crate::{LaneCount, SupportedLaneCount};
22

33
macro_rules! implement_mask_ops {
44
{ $($vector:ident => $mask:ident ($inner_ty:ident),)* } => {
55
$(
66
impl<const LANES: usize> crate::$vector<LANES>
77
where
8-
crate::$vector<LANES>: Vector,
9-
crate::$inner_ty<LANES>: Vector,
10-
crate::$mask<LANES>: crate::Mask,
8+
LaneCount<LANES>: SupportedLaneCount,
119
{
1210
/// Test if each lane is equal to the corresponding lane in `other`.
1311
#[inline]

crates/core_simd/src/fmt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ macro_rules! impl_fmt_trait {
3535
$( // repeat trait
3636
impl<const LANES: usize> core::fmt::$trait for crate::$type<LANES>
3737
where
38-
Self: crate::Vector,
38+
crate::LaneCount<LANES>: crate::SupportedLaneCount,
3939
{
4040
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
4141
$format(self.as_ref(), f)

crates/core_simd/src/iter.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
use crate::{LaneCount, SupportedLaneCount};
2+
13
macro_rules! impl_traits {
24
{ $type:ident } => {
35
impl<const LANES: usize> core::iter::Sum<Self> for crate::$type<LANES>
46
where
5-
Self: crate::Vector,
7+
LaneCount<LANES>: SupportedLaneCount,
68
{
79
fn sum<I: core::iter::Iterator<Item = Self>>(iter: I) -> Self {
810
iter.fold(Default::default(), core::ops::Add::add)
@@ -11,7 +13,7 @@ macro_rules! impl_traits {
1113

1214
impl<const LANES: usize> core::iter::Product<Self> for crate::$type<LANES>
1315
where
14-
Self: crate::Vector,
16+
LaneCount<LANES>: SupportedLaneCount,
1517
{
1618
fn product<I: core::iter::Iterator<Item = Self>>(iter: I) -> Self {
1719
iter.fold(Default::default(), core::ops::Mul::mul)
@@ -20,7 +22,7 @@ macro_rules! impl_traits {
2022

2123
impl<'a, const LANES: usize> core::iter::Sum<&'a Self> for crate::$type<LANES>
2224
where
23-
Self: crate::Vector,
25+
LaneCount<LANES>: SupportedLaneCount,
2426
{
2527
fn sum<I: core::iter::Iterator<Item = &'a Self>>(iter: I) -> Self {
2628
iter.fold(Default::default(), core::ops::Add::add)
@@ -29,7 +31,7 @@ macro_rules! impl_traits {
2931

3032
impl<'a, const LANES: usize> core::iter::Product<&'a Self> for crate::$type<LANES>
3133
where
32-
Self: crate::Vector,
34+
LaneCount<LANES>: SupportedLaneCount,
3335
{
3436
fn product<I: core::iter::Iterator<Item = &'a Self>>(iter: I) -> Self {
3537
iter.fold(Default::default(), core::ops::Mul::mul)

crates/core_simd/src/lane_count.rs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
mod sealed {
2+
pub trait Sealed {}
3+
}
4+
use sealed::Sealed;
5+
6+
/// A type representing a vector lane count.
7+
pub struct LaneCount<const LANES: usize>;
8+
9+
/// Helper trait for vector lane counts.
10+
pub trait SupportedLaneCount: Sealed {
11+
/// The bitmask representation of a mask.
12+
type BitMask: Copy + Default + AsRef<[u8]> + AsMut<[u8]>;
13+
14+
#[doc(hidden)]
15+
type IntBitMask;
16+
}
17+
18+
impl<const LANES: usize> Sealed for LaneCount<LANES> {}
19+
20+
impl SupportedLaneCount for LaneCount<1> {
21+
type BitMask = [u8; 1];
22+
type IntBitMask = u8;
23+
}
24+
impl SupportedLaneCount for LaneCount<2> {
25+
type BitMask = [u8; 1];
26+
type IntBitMask = u8;
27+
}
28+
impl SupportedLaneCount for LaneCount<4> {
29+
type BitMask = [u8; 1];
30+
type IntBitMask = u8;
31+
}
32+
impl SupportedLaneCount for LaneCount<8> {
33+
type BitMask = [u8; 1];
34+
type IntBitMask = u8;
35+
}
36+
impl SupportedLaneCount for LaneCount<16> {
37+
type BitMask = [u8; 2];
38+
type IntBitMask = u16;
39+
}
40+
impl SupportedLaneCount for LaneCount<32> {
41+
type BitMask = [u8; 4];
42+
type IntBitMask = u32;
43+
}

crates/core_simd/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ mod comparisons;
2121
mod fmt;
2222
mod intrinsics;
2323
mod iter;
24+
mod math;
2425
mod ops;
2526
mod round;
2627

27-
mod math;
28+
mod lane_count;
29+
pub use lane_count::*;
2830

2931
mod masks;
3032
pub use masks::*;

0 commit comments

Comments
 (0)