Skip to content

Missed optimization in math expression: sqrt(a) * sqrt(b) == sqrt(a*b) #34952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
zamazan4ik opened this issue Dec 10, 2017 · 5 comments
Closed
Labels
bugzilla Issues migrated from bugzilla llvm:codegen

Comments

@zamazan4ik
Copy link

zamazan4ik commented Dec 10, 2017

Bugzilla Link 35604
Resolution FIXED
Resolved on Feb 03, 2019 23:23
Version trunk
OS All
Blocks #34959
CC @hfinkel,@rotateright

Extended Description

clang(trunk) with '--std=c++17 -O3 -march=native -ffast-math' flags for this code:

#include <cmath>

double test(double a, double b)
{
    return sqrt(a) * sqrt(b);
}

generates this assembly:

test(double, double, double): # @test(double, double, double)
  vsqrtsd xmm0, xmm0, xmm0
  vsqrtsd xmm1, xmm1, xmm1
  vmulsd xmm0, xmm1, xmm0
  ret

gcc(trunk) with '--std=c++17 -O3 -march=native -ffast-math' flags generates this:

test(double, double, double):
        vmulsd  xmm0, xmm0, xmm1
        vsqrtsd xmm0, xmm0, xmm0
        ret

Also if you will increase count of multiplication on sqrt(varible_name), gcc continues generate more optimal code:

#include <cmath>

double test(double a, double b, double c)
{
    return sqrt(a) * sqrt(b) * sqrt(c);
}

clang:

test(double, double, double): # @test(double, double, double)
  vsqrtsd xmm0, xmm0, xmm0
  vsqrtsd xmm1, xmm1, xmm1
  vmulsd xmm0, xmm1, xmm0
  vsqrtsd xmm1, xmm2, xmm2
  vmulsd xmm0, xmm0, xmm1
  ret

gcc:

test(double, double, double):
        vmulsd  xmm2, xmm1, xmm2
        vmulsd  xmm0, xmm2, xmm0
        vsqrtsd xmm0, xmm0, xmm0
        ret
@zamazan4ik
Copy link
Author

@llvmbot
Copy link
Member

llvmbot commented Dec 10, 2017

Thanks for all these reports. May I ask you to open a meta/umbrella ticket, something like "missing fast-math optimizations" and link all them there? I think I might consider picking up some. Thanks!

@hfinkel
Copy link
Collaborator

hfinkel commented Dec 10, 2017

Thanks for all these reports. May I ask you to open a meta/umbrella ticket,
something like "missing fast-math optimizations" and link all them there? I
think I might consider picking up some. Thanks!

#34959

@llvmbot
Copy link
Member

llvmbot commented Feb 4, 2019

Fixed in https://reviews.llvm.org/D41322. Commit: rL321637. See: https://godbolt.org/z/Ffks6m

@RKSimon
Copy link
Collaborator

RKSimon commented Nov 27, 2021

mentioned in issue #34959

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 10, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla llvm:codegen
Projects
None yet
Development

No branches or pull requests

4 participants