public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Geert Bosch <bosch@adacore.com>
To: Jeroen Van Der Bossche <jeroenvanderbossche@gmail.com>
Cc: gcc@gcc.gnu.org
Subject: Re: Optimizing floating point *(2^c) and /(2^c)
Date: Mon, 29 Mar 2010 20:30:00 -0000	[thread overview]
Message-ID: <8369F83F-8021-4654-8266-7D2A89920FED@adacore.com> (raw)
In-Reply-To: <f60407751003291019j5faf4febk861f7c25ea0cb4a0@mail.gmail.com>


On Mar 29, 2010, at 13:19, Jeroen Van Der Bossche wrote:

> 've recently written a program where taking the average of 2 floating
> point numbers was a real bottleneck. I've looked into the assembly
> generated by gcc -O3 and apparently gcc treats multiplication and
> division by a hard-coded 2 like any other multiplication with a
> constant. I think, however, that *(2^c) and /(2^c) for floating
> points, where the c is known at compile-time, should be able to be
> optimized with the following pseudo-code:
> 
> e = exponent bits of the number
> if (e > c && e < (0b111...11)-c) {
> e += c or e -= c
> } else {
> do regular multiplication
> }
> 
> Even further optimizations may be possible, such as bitshifting the
> significand when e=0. However, that would require checking for a lot
> of special cases and require so many conditional jumps that it's most
> likely not going to be any faster.
> 
> I'm not skilled enough with assembly to write this myself and test if
> this actually performs faster than how it's implemented now. Its
> performance will most likely also depend on the processor
> architecture, and I could only test this code on one machine.
> Therefore I ask to those who are familiar with gcc's optimization
> routines to give this 2 seconds of thought, as this is probably rather
> easy to implement and many programs could benefit from this.

For any optimization suggestions, you should start with showing some real, compilable, code with a performance problem that you think the compiler could address. Please include details about compilation options, GCC versions and target hardware, as well as observed performance numbers. How do you see that averaging two floating point numbers is a bottleneck? This should only be a single addition and multiplication, and will execute in a nanosecond or so on a moderately modern system.

Your particular suggestion is flawed. Floating-point multiplication is very fast on most targets. It is hard to see how on any target with floating-point hardware, manual mucking with the representation can be a win. In particular, your sketch doesn't at all address underflow and overflow. Likely a complete implementation would be many times slower than a floating-point multiply.

  -Geert

  reply	other threads:[~2010-03-29 17:51 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-29 17:38 Jeroen Van Der Bossche
2010-03-29 20:30 ` Geert Bosch [this message]
2010-03-29 22:43   ` Tim Prince
2010-03-31 15:29     ` Geert Bosch
2010-03-30 19:03   ` Jeroen Van Der Bossche
2010-03-31  6:07     ` Vincent Lefevre
2010-03-31  9:25       ` Marc Glisse
2010-03-31 12:03         ` Vincent Lefevre
2010-04-01 16:21           ` Paolo Bonzini

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=8369F83F-8021-4654-8266-7D2A89920FED@adacore.com \
    --to=bosch@adacore.com \
    --cc=gcc@gcc.gnu.org \
    --cc=jeroenvanderbossche@gmail.com \
    /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).