public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/114363] New: inconsistent optimization of pow(x,2)+pow(y,2)
@ 2024-03-16 9:31 vincenzo.innocente at cern dot ch
2024-03-16 10:37 ` [Bug tree-optimization/114363] " harald at gigawatt dot nl
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: vincenzo.innocente at cern dot ch @ 2024-03-16 9:31 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114363
Bug ID: 114363
Summary: inconsistent optimization of pow(x,2)+pow(y,2)
Product: gcc
Version: 14.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: vincenzo.innocente at cern dot ch
Target Milestone: ---
while pow(x,2) is optimized in x*x (float x)
in pow(x,2)+pow(y,2) x and y are first promoted to double
which I find inconsistent
see
https://godbolt.org/z/rYfoaxr89
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug tree-optimization/114363] inconsistent optimization of pow(x,2)+pow(y,2)
2024-03-16 9:31 [Bug tree-optimization/114363] New: inconsistent optimization of pow(x,2)+pow(y,2) vincenzo.innocente at cern dot ch
@ 2024-03-16 10:37 ` harald at gigawatt dot nl
2024-03-16 11:26 ` xry111 at gcc dot gnu.org
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: harald at gigawatt dot nl @ 2024-03-16 10:37 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114363
Harald van Dijk <harald at gigawatt dot nl> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |harald at gigawatt dot nl
--- Comment #1 from Harald van Dijk <harald at gigawatt dot nl> ---
This is, I believe, correct. Before C++11, calling std::pow with float and int
arguments, it returned a float. As of C++11, it returns a double.
If the result of pow(x,2) is immediately converted to float, then it is a valid
optimisation to convert it to x*x: that is guaranteed to produce the exact same
result. But if it isn't, then converting to x*x loses accuracy and alters the
result.
You can call std::powf instead of std::pow to avoid the promotion to double.
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug tree-optimization/114363] inconsistent optimization of pow(x,2)+pow(y,2)
2024-03-16 9:31 [Bug tree-optimization/114363] New: inconsistent optimization of pow(x,2)+pow(y,2) vincenzo.innocente at cern dot ch
2024-03-16 10:37 ` [Bug tree-optimization/114363] " harald at gigawatt dot nl
@ 2024-03-16 11:26 ` xry111 at gcc dot gnu.org
2024-03-16 12:58 ` jakub at gcc dot gnu.org
2024-03-16 15:06 ` vincenzo.innocente at cern dot ch
3 siblings, 0 replies; 5+ messages in thread
From: xry111 at gcc dot gnu.org @ 2024-03-16 11:26 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114363
Xi Ruoyao <xry111 at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Resolution|--- |INVALID
CC| |xry111 at gcc dot gnu.org
Status|UNCONFIRMED |RESOLVED
--- Comment #2 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
(In reply to Harald van Dijk from comment #1)
> This is, I believe, correct. Before C++11, calling std::pow with float and
> int arguments, it returned a float. As of C++11, it returns a double.
>
> If the result of pow(x,2) is immediately converted to float, then it is a
> valid optimisation to convert it to x*x: that is guaranteed to produce the
> exact same result. But if it isn't, then converting to x*x loses accuracy
> and alters the result.
Thus invalid.
> You can call std::powf instead of std::pow to avoid the promotion to double.
Or add -std=c++98.
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug tree-optimization/114363] inconsistent optimization of pow(x,2)+pow(y,2)
2024-03-16 9:31 [Bug tree-optimization/114363] New: inconsistent optimization of pow(x,2)+pow(y,2) vincenzo.innocente at cern dot ch
2024-03-16 10:37 ` [Bug tree-optimization/114363] " harald at gigawatt dot nl
2024-03-16 11:26 ` xry111 at gcc dot gnu.org
@ 2024-03-16 12:58 ` jakub at gcc dot gnu.org
2024-03-16 15:06 ` vincenzo.innocente at cern dot ch
3 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-03-16 12:58 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114363
Jakub Jelinek <jakub at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |jakub at gcc dot gnu.org
--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Or use pow(x,2.0f)+pow(y,2.0f).
Anyway, see https://eel.is/c++draft/c.math#cmath.syn-3
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug tree-optimization/114363] inconsistent optimization of pow(x,2)+pow(y,2)
2024-03-16 9:31 [Bug tree-optimization/114363] New: inconsistent optimization of pow(x,2)+pow(y,2) vincenzo.innocente at cern dot ch
` (2 preceding siblings ...)
2024-03-16 12:58 ` jakub at gcc dot gnu.org
@ 2024-03-16 15:06 ` vincenzo.innocente at cern dot ch
3 siblings, 0 replies; 5+ messages in thread
From: vincenzo.innocente at cern dot ch @ 2024-03-16 15:06 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114363
--- Comment #4 from vincenzo Innocente <vincenzo.innocente at cern dot ch> ---
Thanks Harald, I missed the point that float z = pow(double(x),2) and
float z = x*x would indeed produce exactly the same result, while in all other
cases of course not.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2024-03-16 15:06 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-16 9:31 [Bug tree-optimization/114363] New: inconsistent optimization of pow(x,2)+pow(y,2) vincenzo.innocente at cern dot ch
2024-03-16 10:37 ` [Bug tree-optimization/114363] " harald at gigawatt dot nl
2024-03-16 11:26 ` xry111 at gcc dot gnu.org
2024-03-16 12:58 ` jakub at gcc dot gnu.org
2024-03-16 15:06 ` vincenzo.innocente at cern dot ch
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).