A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
cranelift: x64: implement packed signed saturating addition - #3295 (#15158)
Nicolas Stigler8f967ff7edd1
diff --git a/cranelift/codegen/src/isa/x64/lower.isle b/cranelift/codegen/src/isa/x64/lower.isleindex 4fde7e2220..9fe014e325 100644--- a/cranelift/codegen/src/isa/x64/lower.isle+++ b/cranelift/codegen/src/isa/x64/lower.isle@@ -294,6 +294,44 @@(rule (lower (sadd_sat (multi_lane 16 8) x y))(x64_paddsw x y))+;; There is no instruction for signed saturating addition of 32-bit or 64-bit+;; lanes, so detect overflow and select the saturated value. A lane overflows+;; exactly when the signs of both inputs differ from the sign of their wrapping+;; sum, i.e. when the sign bit of `(x ^ sum) & (y ^ sum)` is set. On overflow+;; the sum's sign is the opposite of the operands' sign, so the saturated value+;; is `MAX` when the sum is negative and `MIN` otherwise.+(rule (lower (sadd_sat ty @ (multi_lane 32 4) x y))+ (let ((sum Xmm (x64_paddd x y)))+ (lower_sadd_sat_wide ty x y sum)))++(rule (lower (sadd_sat ty @ (multi_lane 64 2) x y))+ (let ((sum Xmm (x64_paddq x y)))+ (lower_sadd_sat_wide ty x y sum)))++(decl lower_sadd_sat_wide (Type Xmm Xmm Xmm) Xmm)+(rule (lower_sadd_sat_wide ty x y sum)+ (let ((sum_sign Xmm (vec_sign_mask ty sum))+ (sat Xmm (x64_pxor sum_sign (min_signed_const ty)))+ (x_xor_sum Xmm (x64_pxor x sum))+ (y_xor_sum Xmm (x64_pxor y sum))+ (overflow Xmm (vec_sign_mask ty (x64_pand x_xor_sum y_xor_sum)))+ (diff Xmm (x64_pxor sum sat))+ (masked_diff Xmm (x64_pand overflow diff)))+ (x64_pxor sum masked_diff)))++(decl vec_sign_mask (Type Xmm) Xmm)+(rule (vec_sign_mask $I32X4 x)+ (x64_psrad x (xmi_imm 31)))+(rule (vec_sign_mask $I64X2 x)+ (let ((x Xmm (x64_psrad x (xmi_imm 31))))+ (x64_pshufd x 0b11_11_01_01)))++(decl min_signed_const (Type) XmmMem128)+(rule (min_signed_const $I32X4)+ (emit_xmm_mem_128_le_const 0x80000000_80000000_80000000_80000000))+(rule (min_signed_const $I64X2)+ (emit_xmm_mem_128_le_const 0x8000000000000000_8000000000000000))+;;;; Rules for `uadd_sat` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;(rule (lower (uadd_sat (multi_lane 8 16) x y))diff --git a/cranelift/filetests/filetests/runtests/simd-saddsat-wide.clif b/cranelift/filetests/filetests/runtests/simd-saddsat-wide.clifnew file mode 100644index 0000000000..dcb741b632--- /dev/null+++ b/cranelift/filetests/filetests/runtests/simd-saddsat-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 %saddsat_i32x4(i32x4, i32x4) -> i32x4 {+block0(v0: i32x4, v1: i32x4):+ v2 = sadd_sat v0, v1+ return v2+}+; run: %saddsat_i32x4([256 -2147483000 2147483000 2147483000], [256 -1000 1000 1000]) == [512 -2147483648 2147483647 2147483647]+; run: %saddsat_i32x4([2147483646 -2147483647 100 -100], [1 -1 200 100]) == [2147483647 -2147483648 300 0]+; run: %saddsat_i32x4([2147483647 -2147483648 -2147483648 2147483647], [2147483647 -2147483648 -1 2147483647]) == [2147483647 -2147483648 -2147483648 2147483647]++function %saddsat_i64x2(i64x2, i64x2) -> i64x2 {+block0(v0: i64x2, v1: i64x2):+ v2 = sadd_sat v0, v1+ return v2+}+; run: %saddsat_i64x2([-9223372036854775000 9223372036854775000], [-1000 1000]) == [-9223372036854775808 9223372036854775807]+; run: %saddsat_i64x2([9223372036854775806 -9223372036854775807], [1 -1]) == [9223372036854775807 -9223372036854775808]+; run: %saddsat_i64x2([123 -456], [1 -1]) == [124 -457]+; run: %saddsat_i64x2([9223372036854775807 -9223372036854775808], [1 -1]) == [9223372036854775807 -9223372036854775808]diff --git a/cranelift/fuzzgen/src/function_generator.rs b/cranelift/fuzzgen/src/function_generator.rsindex ec1bf6f78b..4bdc4e44c3 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::SaddSat | Opcode::UsubSat | Opcode::SsubSat,+ Opcode::UaddSat | Opcode::UsubSat | Opcode::SsubSat,&([I32X4, I32X4] | [I64X2, I64X2])),(Opcode::Fcopysign, &([F32X4, F32X4] | [F64X2, F64X2])),