public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "glisse at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/107520] New: Optimize std::lerp(d, d, 0.5)
Date: Thu, 03 Nov 2022 21:15:50 +0000	[thread overview]
Message-ID: <bug-107520-4@http.gcc.gnu.org/bugzilla/> (raw)

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.

             reply	other threads:[~2022-11-03 21:16 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-03 21:15 glisse at gcc dot gnu.org [this message]
2022-11-03 23:31 ` [Bug tree-optimization/107520] " pinskia at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-107520-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).