public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "ubizjak at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug target/103008] poor inlined builtin_fmod on x86_64
Date: Thu, 10 Feb 2022 18:09:21 +0000	[thread overview]
Message-ID: <bug-103008-4-qHaBeLIkKg@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-103008-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #10 from Uroš Bizjak <ubizjak at gmail dot com> ---
FYI, the following testcase:

--cut here--
#include <math.h>

float
__attribute__((noinline))
_fmodf (float x, float y)
{
  return x - truncf (x/y) * y;
}

int
main ()
{

  float a, b;
  volatile float z;

  for (a = -1000.0f; a < 1000.0f; a += 0.01f)
    for (b = -1000.0f; b < 1000.0f; b += 0.1f)
      z = fmodf (a, b);

  return 0;
}
--cut here--

$ gcc -Ofast -lm fmod-bench.c

      22,127092116 seconds time elapsed

      22,125111000 seconds user
       0,000999000 seconds sys


$ gcc -Ofast -fno-builtin-fmodf -lm fmod-bench.c

      32,751589079 seconds time elapsed

      32,746156000 seconds user
       0,000999000 seconds sys


Which points that the x87 code is considerably faster on my target
(Ivybridge-E) on Fedora-34 with glibc-2.33.

For reference, when the above _fmodf is called, I get:

$ gcc -Ofast -lm fmod-bench.c

      10,706189749 seconds time elapsed

      10,704859000 seconds user
       0,000999000 seconds sys

$ gcc -Ofast -lm -msse4 fmod-bench.c

      11,391062747 seconds time elapsed

      11,390771000 seconds user
       0,000000000 seconds sys

So, considerable faster!

It looks that with -ffast-math it is not inlined x87 code that is problematic,
but the missing fmod transformation. As shown above, the SSE2 code for truncf
is on par with SSE4 roundss instruction, so if the target can provide optimized
truncf code, the fmodf should definitely be converted to "a - trunc (a/p) * p".

  parent reply	other threads:[~2022-02-10 18:09 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-30 18:51 [Bug target/103008] New: " fx at gnu dot org
2021-10-30 18:52 ` [Bug target/103008] " fx at gnu dot org
2021-10-30 18:55 ` fx at gnu dot org
2021-10-30 18:56 ` fx at gnu dot org
2021-10-30 20:15 ` fx at gnu dot org
2021-10-30 20:39 ` anlauf at gcc dot gnu.org
2021-10-31 20:05 ` ubizjak at gmail dot com
2021-11-01  8:23 ` ubizjak at gmail dot com
2022-02-10 14:18 ` rguenth at gcc dot gnu.org
2022-02-10 14:22 ` rguenth at gcc dot gnu.org
2022-02-10 18:09 ` ubizjak at gmail dot com [this message]
2022-02-10 20:50 ` joseph at codesourcery dot com
2022-02-11  7:59 ` rguenth at gcc dot gnu.org
2022-02-11 15:47 ` ubizjak at gmail dot com
2022-02-12 22:07 ` ubizjak at gmail dot com
2022-02-13 21:00 ` Dave.Love at manchester dot ac.uk
2022-02-14  7:12 ` rguenther at suse dot de
2022-02-14  7:35 ` rguenth 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-103008-4-qHaBeLIkKg@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).