cmd/compile: simplify multiplication and division of Abs() #50126
Labels
compiler/runtime
Issues related to the Go compiler and/or runtime.
NeedsDecision
Feedback is required from experts, contributors, and/or the community before a change can be made.
Performance
Milestone
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes. See https://go.godbolt.org/z/1Gq4bPPnd for assembly output.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I noticed that the compile doesn't optimize calls to
math.Abs()
orcmplx.Abs()
to strip out unnecessary calls to those functions when multiplying them or dividing them. According to the absolute value rules,|A * B| = |A| * |B|
and|A / B| = |A| / |B|
, meaning that the compiler should be able to work the right two calls to eithermath.Abs()
orcmplx.Abs()
into a single call by moving the multiplication or division inside the call instead.This means that the first block of assembly can be simplified to the one after.
The above assembly was taken from the multiplication examples below:
This should also apply to complex numbers. It is important to note that it is only for multiplication and division, not addition and subtraction.
What did you expect to see?
I expected the compiler to strip out the duplicated calls to
math.Abs()
. Thus getting the following assembly for theAbsMult
function below.What did you see instead?
The compiler did not strip it out:
For more info, see: dominikh/go-tools#1135
The text was updated successfully, but these errors were encountered: