Skip to content

Commit 782b5fe

Browse files
committed
Implement simd_saturating_{add,sub}
1 parent 0980596 commit 782b5fe

File tree

3 files changed

+39
-106
lines changed

3 files changed

+39
-106
lines changed

patches/0001-portable-simd-Disable-unsupported-tests.patch

-104
Original file line numberDiff line numberDiff line change
@@ -11,110 +11,6 @@ Subject: [PATCH] Disable unsupported tests
1111
crates/core_simd/tests/masks.rs | 3 ---
1212
5 files changed, 20 insertions(+), 3 deletions(-)
1313

14-
diff --git a/crates/core_simd/src/elements/int.rs b/crates/core_simd/src/elements/int.rs
15-
index 9b8c37e..ea95f08 100644
16-
--- a/crates/core_simd/src/elements/int.rs
17-
+++ b/crates/core_simd/src/elements/int.rs
18-
@@ -11,6 +11,7 @@ pub trait SimdInt: Copy + Sealed {
19-
/// Scalar type contained by this SIMD vector type.
20-
type Scalar;
21-
22-
+ /*
23-
/// Lanewise saturating add.
24-
///
25-
/// # Examples
26-
@@ -45,6 +46,7 @@ pub trait SimdInt: Copy + Sealed {
27-
/// assert_eq!(unsat, Simd::from_array([1, MAX, MIN, 0]));
28-
/// assert_eq!(sat, Simd::from_array([MIN, MIN, MIN, 0]));
29-
fn saturating_sub(self, second: Self) -> Self;
30-
+ */
31-
32-
/// Lanewise absolute value, implemented in Rust.
33-
/// Every lane becomes its absolute value.
34-
@@ -61,6 +63,7 @@ pub trait SimdInt: Copy + Sealed {
35-
/// ```
36-
fn abs(self) -> Self;
37-
38-
+ /*
39-
/// Lanewise saturating absolute value, implemented in Rust.
40-
/// As abs(), except the MIN value becomes MAX instead of itself.
41-
///
42-
@@ -96,6 +99,7 @@ pub trait SimdInt: Copy + Sealed {
43-
/// assert_eq!(sat, Simd::from_array([MAX, 2, -3, MIN + 1]));
44-
/// ```
45-
fn saturating_neg(self) -> Self;
46-
+ */
47-
48-
/// Returns true for each positive lane and false if it is zero or negative.
49-
fn is_positive(self) -> Self::Mask;
50-
@@ -199,6 +203,7 @@ macro_rules! impl_trait {
51-
type Mask = Mask<<$ty as SimdElement>::Mask, LANES>;
52-
type Scalar = $ty;
53-
54-
+ /*
55-
#[inline]
56-
fn saturating_add(self, second: Self) -> Self {
57-
// Safety: `self` is a vector
58-
@@ -210,6 +215,7 @@ macro_rules! impl_trait {
59-
// Safety: `self` is a vector
60-
unsafe { intrinsics::simd_saturating_sub(self, second) }
61-
}
62-
+ */
63-
64-
#[inline]
65-
fn abs(self) -> Self {
66-
@@ -218,6 +224,7 @@ macro_rules! impl_trait {
67-
(self^m) - m
68-
}
69-
70-
+ /*
71-
#[inline]
72-
fn saturating_abs(self) -> Self {
73-
// arith shift for -1 or 0 mask based on sign bit, giving 2s complement
74-
@@ -230,6 +237,7 @@ macro_rules! impl_trait {
75-
fn saturating_neg(self) -> Self {
76-
Self::splat(0).saturating_sub(self)
77-
}
78-
+ */
79-
80-
#[inline]
81-
fn is_positive(self) -> Self::Mask {
82-
diff --git a/crates/core_simd/src/elements/uint.rs b/crates/core_simd/src/elements/uint.rs
83-
index 21e7e76..0d6dee2 100644
84-
--- a/crates/core_simd/src/elements/uint.rs
85-
+++ b/crates/core_simd/src/elements/uint.rs
86-
@@ -6,6 +6,7 @@ pub trait SimdUint: Copy + Sealed {
87-
/// Scalar type contained by this SIMD vector type.
88-
type Scalar;
89-
90-
+ /*
91-
/// Lanewise saturating add.
92-
///
93-
/// # Examples
94-
@@ -40,6 +41,7 @@ pub trait SimdUint: Copy + Sealed {
95-
/// assert_eq!(unsat, Simd::from_array([3, 2, 1, 0]));
96-
/// assert_eq!(sat, Simd::splat(0));
97-
fn saturating_sub(self, second: Self) -> Self;
98-
+ */
99-
100-
/// Returns the sum of the lanes of the vector, with wrapping addition.
101-
fn reduce_sum(self) -> Self::Scalar;
102-
@@ -78,6 +80,7 @@ macro_rules! impl_trait {
103-
{
104-
type Scalar = $ty;
105-
106-
+ /*
107-
#[inline]
108-
fn saturating_add(self, second: Self) -> Self {
109-
// Safety: `self` is a vector
110-
@@ -89,6 +92,7 @@ macro_rules! impl_trait {
111-
// Safety: `self` is a vector
112-
unsafe { intrinsics::simd_saturating_sub(self, second) }
113-
}
114-
+ */
115-
116-
#[inline]
117-
fn reduce_sum(self) -> Self::Scalar {
11814
diff --git a/crates/core_simd/src/masks/full_masks.rs b/crates/core_simd/src/masks/full_masks.rs
11915
index adf0fcb..e7e657e 100644
12016
--- a/crates/core_simd/src/masks/full_masks.rs

src/intrinsics/mod.rs

+24
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,30 @@ fn simd_for_each_lane<'tcx>(
8484
}
8585
}
8686

87+
fn simd_pair_for_each_lane_typed<'tcx>(
88+
fx: &mut FunctionCx<'_, '_, 'tcx>,
89+
x: CValue<'tcx>,
90+
y: CValue<'tcx>,
91+
ret: CPlace<'tcx>,
92+
f: &dyn Fn(&mut FunctionCx<'_, '_, 'tcx>, CValue<'tcx>, CValue<'tcx>) -> CValue<'tcx>,
93+
) {
94+
assert_eq!(x.layout(), y.layout());
95+
let layout = x.layout();
96+
97+
let (lane_count, _lane_ty) = layout.ty.simd_size_and_type(fx.tcx);
98+
let (ret_lane_count, _ret_lane_ty) = ret.layout().ty.simd_size_and_type(fx.tcx);
99+
assert_eq!(lane_count, ret_lane_count);
100+
101+
for lane_idx in 0..lane_count {
102+
let x_lane = x.value_lane(fx, lane_idx);
103+
let y_lane = y.value_lane(fx, lane_idx);
104+
105+
let res_lane = f(fx, x_lane, y_lane);
106+
107+
ret.place_lane(fx, lane_idx).write_cvalue(fx, res_lane);
108+
}
109+
}
110+
87111
fn simd_pair_for_each_lane<'tcx>(
88112
fx: &mut FunctionCx<'_, '_, 'tcx>,
89113
x: CValue<'tcx>,

src/intrinsics/simd.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -730,9 +730,22 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
730730
ret.write_cvalue(fx, res);
731731
}
732732

733+
sym::simd_saturating_add | sym::simd_saturating_sub => {
734+
intrinsic_args!(fx, args => (x, y); intrinsic);
735+
736+
let bin_op = match intrinsic {
737+
sym::simd_saturating_add => BinOp::Add,
738+
sym::simd_saturating_sub => BinOp::Sub,
739+
_ => unreachable!(),
740+
};
741+
742+
// FIXME use vector instructions when possible
743+
simd_pair_for_each_lane_typed(fx, x, y, ret, &|fx, x_lane, y_lane| {
744+
crate::num::codegen_saturating_int_binop(fx, bin_op, x_lane, y_lane)
745+
});
746+
}
747+
733748
// simd_arith_offset
734-
// simd_saturating_add
735-
// simd_saturating_sub
736749
// simd_scatter
737750
// simd_gather
738751
// simd_select_bitmask

0 commit comments

Comments
 (0)