Skip to content

Commit f8c373a

Browse files
Introduce trait-driven arithmetic
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.
1 parent 36e198b commit f8c373a

File tree

4 files changed

+308
-107
lines changed

4 files changed

+308
-107
lines changed

crates/core_simd/src/math.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ macro_rules! impl_uint_arith {
4747
}
4848
}
4949

50+
#[allow(unused)]
5051
macro_rules! impl_int_arith {
5152
($($ty:ty),+) => {
5253
$( impl<const LANES: usize> Simd<$ty, LANES> where LaneCount<LANES>: SupportedLaneCount {
@@ -156,4 +157,4 @@ macro_rules! impl_int_arith {
156157
}
157158

158159
impl_uint_arith! { u8, u16, u32, u64, usize }
159-
impl_int_arith! { i8, i16, i32, i64, isize }
160+
// impl_int_arith! { i8, i16, i32, i64, isize }

crates/core_simd/src/ops.rs

+67-67
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ macro_rules! impl_op {
174174
{ impl Shl for $scalar:ty } => {
175175
impl_op! { @binary $scalar, Shl::shl, ShlAssign::shl_assign, simd_shl }
176176
};
177-
{ impl Shr for $scalar:ty } => {
178-
impl_op! { @binary $scalar, Shr::shr, ShrAssign::shr_assign, simd_shr }
179-
};
177+
// { impl Shr for $scalar:ty } => {
178+
// impl_op! { @binary $scalar, Shr::shr, ShrAssign::shr_assign, simd_shr }
179+
// };
180180
{ impl BitAnd for $scalar:ty } => {
181181
impl_op! { @binary $scalar, BitAnd::bitand, BitAndAssign::bitand_assign, simd_and }
182182
};
@@ -561,70 +561,70 @@ macro_rules! impl_unsigned_int_ops {
561561
}
562562
}
563563

564-
impl_ref_ops! {
565-
impl<const LANES: usize> core::ops::Shr<Self> for Simd<$scalar, LANES>
566-
where
567-
LaneCount<LANES>: SupportedLaneCount,
568-
{
569-
type Output = Self;
570-
571-
#[inline]
572-
fn shr(self, rhs: Self) -> Self::Output {
573-
// TODO there is probably a better way of doing this
574-
if rhs.as_array()
575-
.iter()
576-
.copied()
577-
.any(invalid_shift_rhs)
578-
{
579-
panic!("attempt to shift with overflow");
580-
}
581-
unsafe { intrinsics::simd_shr(self, rhs) }
582-
}
583-
}
584-
}
585-
586-
impl_ref_ops! {
587-
impl<const LANES: usize> core::ops::Shr<$scalar> for Simd<$scalar, LANES>
588-
where
589-
LaneCount<LANES>: SupportedLaneCount,
590-
{
591-
type Output = Self;
592-
593-
#[inline]
594-
fn shr(self, rhs: $scalar) -> Self::Output {
595-
if invalid_shift_rhs(rhs) {
596-
panic!("attempt to shift with overflow");
597-
}
598-
let rhs = Self::splat(rhs);
599-
unsafe { intrinsics::simd_shr(self, rhs) }
600-
}
601-
}
602-
}
603-
604-
605-
impl_ref_ops! {
606-
impl<const LANES: usize> core::ops::ShrAssign<Self> for Simd<$scalar, LANES>
607-
where
608-
LaneCount<LANES>: SupportedLaneCount,
609-
{
610-
#[inline]
611-
fn shr_assign(&mut self, rhs: Self) {
612-
*self = *self >> rhs;
613-
}
614-
}
615-
}
616-
617-
impl_ref_ops! {
618-
impl<const LANES: usize> core::ops::ShrAssign<$scalar> for Simd<$scalar, LANES>
619-
where
620-
LaneCount<LANES>: SupportedLaneCount,
621-
{
622-
#[inline]
623-
fn shr_assign(&mut self, rhs: $scalar) {
624-
*self = *self >> rhs;
625-
}
626-
}
627-
}
564+
// impl_ref_ops! {
565+
// impl<const LANES: usize> core::ops::Shr<Self> for Simd<$scalar, LANES>
566+
// where
567+
// LaneCount<LANES>: SupportedLaneCount,
568+
// {
569+
// type Output = Self;
570+
571+
// #[inline]
572+
// fn shr(self, rhs: Self) -> Self::Output {
573+
// // TODO there is probably a better way of doing this
574+
// if rhs.as_array()
575+
// .iter()
576+
// .copied()
577+
// .any(invalid_shift_rhs)
578+
// {
579+
// panic!("attempt to shift with overflow");
580+
// }
581+
// unsafe { intrinsics::simd_shr(self, rhs) }
582+
// }
583+
// }
584+
// }
585+
586+
// impl_ref_ops! {
587+
// impl<const LANES: usize> core::ops::Shr<$scalar> for Simd<$scalar, LANES>
588+
// where
589+
// LaneCount<LANES>: SupportedLaneCount,
590+
// {
591+
// type Output = Self;
592+
593+
// #[inline]
594+
// fn shr(self, rhs: $scalar) -> Self::Output {
595+
// if invalid_shift_rhs(rhs) {
596+
// panic!("attempt to shift with overflow");
597+
// }
598+
// let rhs = Self::splat(rhs);
599+
// unsafe { intrinsics::simd_shr(self, rhs) }
600+
// }
601+
// }
602+
// }
603+
604+
605+
// impl_ref_ops! {
606+
// impl<const LANES: usize> core::ops::ShrAssign<Self> for Simd<$scalar, LANES>
607+
// where
608+
// LaneCount<LANES>: SupportedLaneCount,
609+
// {
610+
// #[inline]
611+
// fn shr_assign(&mut self, rhs: Self) {
612+
// *self = *self >> rhs;
613+
// }
614+
// }
615+
// }
616+
617+
// impl_ref_ops! {
618+
// impl<const LANES: usize> core::ops::ShrAssign<$scalar> for Simd<$scalar, LANES>
619+
// where
620+
// LaneCount<LANES>: SupportedLaneCount,
621+
// {
622+
// #[inline]
623+
// fn shr_assign(&mut self, rhs: $scalar) {
624+
// *self = *self >> rhs;
625+
// }
626+
// }
627+
// }
628628
)*
629629
};
630630
}

0 commit comments

Comments
 (0)