From 2cdcc4150bc577e0b40a9cedaaa7c8301f2860cd Mon Sep 17 00:00:00 2001 From: Meng Zhuo Date: Fri, 14 Nov 2025 12:47:35 +0800 Subject: cmd/compile: fold negation into multiplication MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit goos: linux goarch: riscv64 pkg: cmd/compile/internal/test cpu: Spacemit(R) X60 │ /root/mul.base.log │ /root/mul.new.log │ │ sec/op │ sec/op vs base │ MulNeg 6.426µ ± 0% 4.501µ ± 0% -29.96% (p=0.000 n=10) Mul2Neg 9.000µ ± 0% 6.431µ ± 0% -28.54% (p=0.000 n=10) Mul2 1.263µ ± 0% 1.263µ ± 0% ~ (p=1.000 n=10) MulNeg2 1.577µ ± 0% 1.577µ ± 0% ~ (p=0.211 n=10) geomean 3.276µ 2.756µ -15.89% goos: linux goarch: amd64 pkg: cmd/compile/internal/test cpu: AMD EPYC 7532 32-Core Processor │ /root/base │ /root/new │ │ sec/op │ sec/op vs base │ MulNeg 691.9n ± 1% 319.4n ± 0% -53.83% (p=0.000 n=10) Mul2Neg 630.0n ± 0% 629.6n ± 0% -0.07% (p=0.000 n=10) Mul2 438.1n ± 0% 438.1n ± 0% ~ (p=0.728 n=10) MulNeg2 439.3n ± 0% 439.4n ± 0% ~ (p=0.656 n=10) geomean 538.2n 443.6n -17.58% Change-Id: Ice8e6c8d1e8e3009ba8a0b1b689205174e199019 Reviewed-on: https://go-review.googlesource.com/c/go/+/720180 Reviewed-by: abner chenc Reviewed-by: Keith Randall Reviewed-by: Junyang Shao Reviewed-by: Joel Sing Reviewed-by: Keith Randall LUCI-TryBot-Result: Go LUCI Auto-Submit: Keith Randall --- test/codegen/arithmetic.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'test/codegen') diff --git a/test/codegen/arithmetic.go b/test/codegen/arithmetic.go index 42d5d2ef65..6b2c5529e1 100644 --- a/test/codegen/arithmetic.go +++ b/test/codegen/arithmetic.go @@ -318,13 +318,19 @@ func MergeMuls5(a, n int) int { // Multiplications folded negation func FoldNegMul(a int) int { - // loong64:"SUBVU" "ALSLV [$]2" "ALSLV [$]1" - return (-a) * 11 + // amd64:"IMUL3Q [$]-11" -"NEGQ" + // arm64:"MOVD [$]-11" "MUL" -"NEG" + // loong64:"ALSLV [$]2" "SUBVU" "ALSLV [$]4" + // riscv64:"MOV [$]-11" "MUL" -"NEG" + return -a * 11 } func Fold2NegMul(a, b int) int { + // amd64:"IMULQ" -"NEGQ" + // arm64:"MUL" -"NEG" // loong64:"MULV" -"SUBVU R[0-9], R0," - return (-a) * (-b) + // riscv64:"MUL" -"NEG" + return -a * -b } // -------------- // -- cgit v1.3