Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

cranelift: x64: implement packed signed saturating subtraction - #3296 (#15179)

Nicolas Stiglerb76a1eea7cbc
diff --git a/cranelift/codegen/src/isa/x64/lower.isle b/cranelift/codegen/src/isa/x64/lower.isle
index 9fe014e325..654189182a 100644
--- a/cranelift/codegen/src/isa/x64/lower.isle
+++ b/cranelift/codegen/src/isa/x64/lower.isle
@@ -408,6 +408,32 @@
(rule (lower (ssub_sat (multi_lane 16 8) x y))
(x64_psubsw x y))
+;; There is no instruction for signed saturating subtraction of 32-bit or
+;; 64-bit lanes, so detect overflow and select the saturated value. A lane
+;; overflows exactly when the operands have different signs and the sign of the
+;; difference differs from the sign of the minuend, i.e. when the sign bit of
+;; `(x ^ y) & (x ^ diff)` is set. On overflow the difference's sign is the
+;; opposite of the minuend's, so the saturated value is `MAX` when the
+;; difference is negative and `MIN` otherwise.
+(rule (lower (ssub_sat ty @ (multi_lane 32 4) x y))
+ (let ((diff Xmm (x64_psubd x y)))
+ (lower_ssub_sat_wide ty x y diff)))
+
+(rule (lower (ssub_sat ty @ (multi_lane 64 2) x y))
+ (let ((diff Xmm (x64_psubq x y)))
+ (lower_ssub_sat_wide ty x y diff)))
+
+(decl lower_ssub_sat_wide (Type Xmm Xmm Xmm) Xmm)
+(rule (lower_ssub_sat_wide ty x y diff)
+ (let ((diff_sign Xmm (vec_sign_mask ty diff))
+ (sat Xmm (x64_pxor diff_sign (min_signed_const ty)))
+ (x_xor_y Xmm (x64_pxor x y))
+ (x_xor_diff Xmm (x64_pxor x diff))
+ (overflow Xmm (vec_sign_mask ty (x64_pand x_xor_y x_xor_diff)))
+ (change Xmm (x64_pxor diff sat))
+ (masked_change Xmm (x64_pand overflow change)))
+ (x64_pxor diff masked_change)))
+
;;;; Rules for `usub_sat` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(rule (lower (usub_sat (multi_lane 8 16) x y))
diff --git a/cranelift/filetests/filetests/runtests/simd-ssubsat-aarch64.clif b/cranelift/filetests/filetests/runtests/simd-ssubsat-aarch64.clif
deleted file mode 100644
index 705847b334..0000000000
--- a/cranelift/filetests/filetests/runtests/simd-ssubsat-aarch64.clif
+++ /dev/null
@@ -1,20 +0,0 @@
-test interpret
-test run
-target aarch64
-set enable_multi_ret_implicit_sret
-target riscv64 has_v
-target riscv64 has_v has_c has_zcb
-
-function %ssubsat_i32x4(i32x4, i32x4) -> i32x4 {
-block0(v0: i32x4, v1: i32x4):
- v2 = ssub_sat v0, v1
- return v2
-}
-; run: %ssubsat_i32x4([256 -2147483000 2147483000 2147483000], [256 1000 -1000 -1000]) == [0 -2147483648 2147483647 2147483647]
-
-function %ssubsat_i64x2(i64x2, i64x2) -> i64x2 {
-block0(v0: i64x2, v1: i64x2):
- v2 = ssub_sat v0, v1
- return v2
-}
-; run: %ssubsat_i64x2([-9223372036854775000 9223372036854775000], [1000 -1000]) == [-9223372036854775808 9223372036854775807]
diff --git a/cranelift/filetests/filetests/runtests/simd-ssubsat-wide.clif b/cranelift/filetests/filetests/runtests/simd-ssubsat-wide.clif
new file mode 100644
index 0000000000..8892e235d6
--- /dev/null
+++ b/cranelift/filetests/filetests/runtests/simd-ssubsat-wide.clif
@@ -0,0 +1,27 @@
+test interpret
+test run
+set enable_multi_ret_implicit_sret
+target aarch64
+target x86_64
+target x86_64 has_avx
+target riscv64 has_v
+target riscv64 has_v has_c has_zcb
+
+function %ssubsat_i32x4(i32x4, i32x4) -> i32x4 {
+block0(v0: i32x4, v1: i32x4):
+ v2 = ssub_sat v0, v1
+ return v2
+}
+; run: %ssubsat_i32x4([256 -2147483000 2147483000 2147483000], [256 1000 -1000 -1000]) == [0 -2147483648 2147483647 2147483647]
+; run: %ssubsat_i32x4([2147483647 -2147483648 123 -456], [0 0 1 -1]) == [2147483647 -2147483648 122 -455]
+; run: %ssubsat_i32x4([-1 -1 2147483647 -2147483648], [-2147483648 2147483647 -2147483648 2147483647]) == [2147483647 -2147483648 2147483647 -2147483648]
+
+function %ssubsat_i64x2(i64x2, i64x2) -> i64x2 {
+block0(v0: i64x2, v1: i64x2):
+ v2 = ssub_sat v0, v1
+ return v2
+}
+; run: %ssubsat_i64x2([-9223372036854775000 9223372036854775000], [1000 -1000]) == [-9223372036854775808 9223372036854775807]
+; run: %ssubsat_i64x2([-1 -1], [-9223372036854775808 9223372036854775807]) == [9223372036854775807 -9223372036854775808]
+; run: %ssubsat_i64x2([123 -456], [1 -1]) == [122 -455]
+; run: %ssubsat_i64x2([9223372036854775807 -9223372036854775808], [-9223372036854775808 9223372036854775807]) == [9223372036854775807 -9223372036854775808]
diff --git a/cranelift/fuzzgen/src/function_generator.rs b/cranelift/fuzzgen/src/function_generator.rs
index 4bdc4e44c3..aa8e507a3b 100644
--- a/cranelift/fuzzgen/src/function_generator.rs
+++ b/cranelift/fuzzgen/src/function_generator.rs
@@ -530,7 +530,7 @@ fn valid_for_target(triple: &Triple, op: Opcode, args: &[Type], rets: &[Type]) -
&([I8X16, I8X16] | [I16X8, I16X8] | [I32X4, I32X4] | [I64X2, I64X2])
),
(
- Opcode::UaddSat | Opcode::UsubSat | Opcode::SsubSat,
+ Opcode::UaddSat | Opcode::UsubSat,
&([I32X4, I32X4] | [I64X2, I64X2])
),
(Opcode::Fcopysign, &([F32X4, F32X4] | [F64X2, F64X2])),