Skip to content

Commit 4458ff3

Browse files
rajeshgangamclaude
andcommitted
Add saturating add/sub intrinsics to RISC-V vector backend
Map Halide's saturating_add and saturating_sub operations to RVV instructions vsadd/vsaddu and vssub/vssubu respectively. These are commonly used in image processing for clamped arithmetic and were missing from the RISC-V intrinsic tables. Fixes #9184 Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 2fad88f commit 4458ff3

2 files changed

Lines changed: 8 additions & 0 deletions

File tree

src/CodeGen_RISCV.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ struct RISCVIntrinsic {
6262
const RISCVIntrinsic signed_intrinsic_defs[] = {
6363
{"vaadd", Type::Int, "halving_add", {Type::Int, Type::Int}, AddVLArg | RoundDown | Commutes},
6464
{"vaadd", Type::Int, "rounding_halving_add", {Type::Int, Type::Int}, AddVLArg | RoundUp | Commutes},
65+
{"vsadd", Type::Int, "saturating_add", {Type::Int, Type::Int}, AddVLArg | Commutes},
66+
{"vssub", Type::Int, "saturating_sub", {Type::Int, Type::Int}, AddVLArg},
6567
{"vwadd", {Type::Int, 2}, "widening_add", {Type::Int, Type::Int}, AddVLArg | MangleReturnType | Commutes},
6668
{"vwsub", {Type::Int, 2}, "widening_sub", {Type::Int, Type::Int}, AddVLArg | MangleReturnType},
6769
{"vwmul", {Type::Int, 2}, "widening_mul", {Type::Int, Type::Int}, AddVLArg | MangleReturnType | Commutes},
@@ -70,6 +72,8 @@ const RISCVIntrinsic signed_intrinsic_defs[] = {
7072
const RISCVIntrinsic unsigned_intrinsic_defs[] = {
7173
{"vaaddu", {Type::UInt}, "halving_add", {Type::UInt, Type::UInt}, AddVLArg | RoundDown | Commutes},
7274
{"vaaddu", {Type::UInt}, "rounding_halving_add", {Type::UInt, Type::UInt}, AddVLArg | RoundUp | Commutes},
75+
{"vsaddu", {Type::UInt}, "saturating_add", {Type::UInt, Type::UInt}, AddVLArg | Commutes},
76+
{"vssubu", {Type::UInt}, "saturating_sub", {Type::UInt, Type::UInt}, AddVLArg},
7377
{"vwaddu", {Type::UInt, 2}, "widening_add", {Type::UInt, Type::UInt}, AddVLArg | MangleReturnType | Commutes},
7478
{"vwsubu", {Type::UInt, 2}, "widening_sub", {Type::UInt, Type::UInt}, AddVLArg | MangleReturnType},
7579
{"vwmulu", {Type::UInt, 2}, "widening_mul", {Type::UInt, Type::UInt}, AddVLArg | MangleReturnType | Commutes},

test/correctness/simd_op_check_riscv.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ class SimdOpCheckRISCV : public SimdOpCheckTest {
9696
check("vaaddu.vv", lanes, halving_add(u_1, u_2));
9797
check("vaadd.vv", lanes, rounding_halving_add(i_1, i_2));
9898
check("vaaddu.vv", lanes, rounding_halving_add(u_1, u_2));
99+
check("vsadd.vv", lanes, saturating_add(i_1, i_2));
100+
check("vsaddu.vv", lanes, saturating_add(u_1, u_2));
101+
check("vssub.vv", lanes, saturating_sub(i_1, i_2));
102+
check("vssubu.vv", lanes, saturating_sub(u_1, u_2));
99103

100104
// Widening intrinsics
101105
if (base_bit_width < 64) {

0 commit comments

Comments
 (0)