public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/107520] New: Optimize std::lerp(d, d, 0.5)
@ 2022-11-03 21:15 glisse at gcc dot gnu.org
  2022-11-03 23:31 ` [Bug tree-optimization/107520] " pinskia at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: glisse at gcc dot gnu.org @ 2022-11-03 21:15 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107520

            Bug ID: 107520
           Summary: Optimize std::lerp(d, d, 0.5)
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: enhancement
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: glisse at gcc dot gnu.org
  Target Milestone: ---

In some C++ code I have, it would be convenient if the compiler, possibly with
the help of the standard library, could make the following function cheap,
ideally just the identity. I'll probably end up wrapping lerp with a function
that first checks with __builtin_constant_p if the 2 bounds are equal, but I'll
post this in case people have ideas how to improve things.

#include <cmath>
double f(double d){
  return std::lerp(d, d, .5);
}

Currently, with -O3, we generate

        movapd  %xmm0, %xmm1
        pxor    %xmm0, %xmm0
        comisd  %xmm1, %xmm0
        jnb     .L7
        comisd  %xmm0, %xmm1
        jb      .L6
.L7:
        pxor    %xmm0, %xmm0
        ucomisd %xmm0, %xmm1
        jp      .L6
        je      .L11
.L6:
        movapd  %xmm1, %xmm0
        subsd   %xmm1, %xmm0
        mulsd   .LC1(%rip), %xmm0
        addsd   %xmm1, %xmm0
        maxsd   %xmm1, %xmm0
        ret
        .p2align 4,,10
        .p2align 3
.L11:
        mulsd   .LC1(%rip), %xmm1
        movapd  %xmm1, %xmm0
        addsd   %xmm1, %xmm0
        ret

(clang is better at avoiding the redundant comparison)

With -fno-trapping-math to help a bit, I see at the beginning

  if (d_2(D) == 0.0)
    goto <bb 3>; [34.00%]
  else
    goto <bb 4>; [66.00%]

  <bb 3> [local count: 475287355]:
  _7 = d_2(D) * 5.0e-1;
  _10 = _7 * 2.0e+0;

I think that even with the default -fsigned-zeros, simplifying to _10 = d_2(D)
is valid.

Adding -fno-signed-zeros

  <bb 2> [local count: 1073741824]:
  if (d_2(D) == 0.0)
    goto <bb 5>; [34.00%]
  else
    goto <bb 3>; [66.00%]

  <bb 3> [local count: 598454470]:
  _13 = d_2(D) - d_2(D);
  _14 = _13 * 5.0e-1;
  __x_15 = d_2(D) + _14;
  if (d_2(D) u>= __x_15)
    goto <bb 5>; [50.00%]
  else
    goto <bb 4>; [50.00%]

  <bb 4> [local count: 299227235]:

  <bb 5> [local count: 1073741825]:
  # _12 = PHI <d_2(D)(2), __x_15(4), d_2(D)(3)>
  return _12;

_13 is 0 or NaN, which doesn't change for _14, and __x_15 is just d_2, so we
always return d_2.

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug tree-optimization/107520] Optimize std::lerp(d, d, 0.5)
  2022-11-03 21:15 [Bug tree-optimization/107520] New: Optimize std::lerp(d, d, 0.5) glisse at gcc dot gnu.org
@ 2022-11-03 23:31 ` pinskia at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-11-03 23:31 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107520

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2022-11-03
                 CC|                            |aldyh at gcc dot gnu.org

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed. The VRP improvements I thought might catch this but nope.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-11-03 23:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-03 21:15 [Bug tree-optimization/107520] New: Optimize std::lerp(d, d, 0.5) glisse at gcc dot gnu.org
2022-11-03 23:31 ` [Bug tree-optimization/107520] " pinskia at gcc dot gnu.org

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).