Skip to content

Commit

Permalink
Introduce trait-driven arithmetic
Browse files Browse the repository at this point in the history
This allows collapsing 5~10 instances of a function on the Simd type
into 1-3 copies, at least from the perspective of the docs.
The result is far more legible to a user.

Left deliberately unfinished to offer a taste of the difference
and allow the comparative angles to be discussed.
  • Loading branch information
workingjubilee committed Nov 14, 2021
1 parent 36e198b commit f8c373a
Show file tree
Hide file tree
Showing 4 changed files with 308 additions and 107 deletions.
3 changes: 2 additions & 1 deletion crates/core_simd/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ macro_rules! impl_uint_arith {
}
}

#[allow(unused)]
macro_rules! impl_int_arith {
($($ty:ty),+) => {
$( impl<const LANES: usize> Simd<$ty, LANES> where LaneCount<LANES>: SupportedLaneCount {
Expand Down Expand Up @@ -156,4 +157,4 @@ macro_rules! impl_int_arith {
}

impl_uint_arith! { u8, u16, u32, u64, usize }
impl_int_arith! { i8, i16, i32, i64, isize }
// impl_int_arith! { i8, i16, i32, i64, isize }
134 changes: 67 additions & 67 deletions crates/core_simd/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ macro_rules! impl_op {
{ impl Shl for $scalar:ty } => {
impl_op! { @binary $scalar, Shl::shl, ShlAssign::shl_assign, simd_shl }
};
{ impl Shr for $scalar:ty } => {
impl_op! { @binary $scalar, Shr::shr, ShrAssign::shr_assign, simd_shr }
};
// { impl Shr for $scalar:ty } => {
// impl_op! { @binary $scalar, Shr::shr, ShrAssign::shr_assign, simd_shr }
// };
{ impl BitAnd for $scalar:ty } => {
impl_op! { @binary $scalar, BitAnd::bitand, BitAndAssign::bitand_assign, simd_and }
};
Expand Down Expand Up @@ -561,70 +561,70 @@ macro_rules! impl_unsigned_int_ops {
}
}

impl_ref_ops! {
impl<const LANES: usize> core::ops::Shr<Self> for Simd<$scalar, LANES>
where
LaneCount<LANES>: SupportedLaneCount,
{
type Output = Self;

#[inline]
fn shr(self, rhs: Self) -> Self::Output {
// TODO there is probably a better way of doing this
if rhs.as_array()
.iter()
.copied()
.any(invalid_shift_rhs)
{
panic!("attempt to shift with overflow");
}
unsafe { intrinsics::simd_shr(self, rhs) }
}
}
}

impl_ref_ops! {
impl<const LANES: usize> core::ops::Shr<$scalar> for Simd<$scalar, LANES>
where
LaneCount<LANES>: SupportedLaneCount,
{
type Output = Self;

#[inline]
fn shr(self, rhs: $scalar) -> Self::Output {
if invalid_shift_rhs(rhs) {
panic!("attempt to shift with overflow");
}
let rhs = Self::splat(rhs);
unsafe { intrinsics::simd_shr(self, rhs) }
}
}
}


impl_ref_ops! {
impl<const LANES: usize> core::ops::ShrAssign<Self> for Simd<$scalar, LANES>
where
LaneCount<LANES>: SupportedLaneCount,
{
#[inline]
fn shr_assign(&mut self, rhs: Self) {
*self = *self >> rhs;
}
}
}

impl_ref_ops! {
impl<const LANES: usize> core::ops::ShrAssign<$scalar> for Simd<$scalar, LANES>
where
LaneCount<LANES>: SupportedLaneCount,
{
#[inline]
fn shr_assign(&mut self, rhs: $scalar) {
*self = *self >> rhs;
}
}
}
// impl_ref_ops! {
// impl<const LANES: usize> core::ops::Shr<Self> for Simd<$scalar, LANES>
// where
// LaneCount<LANES>: SupportedLaneCount,
// {
// type Output = Self;

// #[inline]
// fn shr(self, rhs: Self) -> Self::Output {
// // TODO there is probably a better way of doing this
// if rhs.as_array()
// .iter()
// .copied()
// .any(invalid_shift_rhs)
// {
// panic!("attempt to shift with overflow");
// }
// unsafe { intrinsics::simd_shr(self, rhs) }
// }
// }
// }

// impl_ref_ops! {
// impl<const LANES: usize> core::ops::Shr<$scalar> for Simd<$scalar, LANES>
// where
// LaneCount<LANES>: SupportedLaneCount,
// {
// type Output = Self;

// #[inline]
// fn shr(self, rhs: $scalar) -> Self::Output {
// if invalid_shift_rhs(rhs) {
// panic!("attempt to shift with overflow");
// }
// let rhs = Self::splat(rhs);
// unsafe { intrinsics::simd_shr(self, rhs) }
// }
// }
// }


// impl_ref_ops! {
// impl<const LANES: usize> core::ops::ShrAssign<Self> for Simd<$scalar, LANES>
// where
// LaneCount<LANES>: SupportedLaneCount,
// {
// #[inline]
// fn shr_assign(&mut self, rhs: Self) {
// *self = *self >> rhs;
// }
// }
// }

// impl_ref_ops! {
// impl<const LANES: usize> core::ops::ShrAssign<$scalar> for Simd<$scalar, LANES>
// where
// LaneCount<LANES>: SupportedLaneCount,
// {
// #[inline]
// fn shr_assign(&mut self, rhs: $scalar) {
// *self = *self >> rhs;
// }
// }
// }
)*
};
}
Expand Down
Loading

0 comments on commit f8c373a

Please sign in to comment.