Re: Idiotic benchmark

From: Anatoly Vorobey (avorobey_at_nonexisting.hamakor.org.il)
Date: Tue 09 Aug 2005 - 13:02:47 IDT


On Tue, Aug 09, 2005 at 11:52:35AM +0300, Ariel Biener wrote:
> On Monday 08 August 2005 21:30, Shachar Shemesh wrote:
> >
> > Actually, something extremely weird it going on here. The result change,
>
> Not weird, Anatoly didn't read what I sent through, see gcc man page for what
> -fno-math-errno does.

What makes you think I didn't read it? You just didn't realize it wasn't
the whole picture.

When you were invoking gcc with -O2 -fno-math-errno and without using
the results of the computation in any way, gcc was optimising away the
sqrt computation inside the loop completely; that, and not the speed
of inlining sqrt computation, explained the huge speed increase you
were seeing.

The reason why gcc *could afford* to eliminate computing sqrt was given
by you correctly: it is that -fno-math-errno freed gcc from worrying
over the side-effects of sqrt, which are precisely the possible setting
of ERRNO.

Basically, gcc has four distinct ways to compile the loop into machine
code (disregarding loop unrolling and such):

1) compile the loop, calling library sqrt() each time. That's what it
does when you don't ask it to optimize at all.

2) compile the loop, using inlined FPU fsqrt instructions inside the
loop, but then checking the result for exceptions (like for example
if you tried to take a square root of a negative number) and in case
of an exception calling sqrt() on that same argument, thereby ensuring
that it sets ERRNO correctly.

That's what it does if you specify -O2 but do not specify
-fno-math-errno.

3) compile the loop, using online inlined FPU fsqrt instructions
inside the loop, and never bothering with library sqrt() or errno.

That's what it does if you specify -O2 and -fno-math-errno, *and in
addition* you let the compiler know you're going to use the results
of the computation in some way.

4) compile the loop, and do nothing inside it, not computing sqrt
in any way.

That's what it does if you specify -O2 and -fno-math-errno, and the
compiler realises you're throwing the result of the computation away.

There's also

5) do not even compile the loop at all. Do nothing.

I couldn't find enough optimisation options to make gcc do that, though
there may be some combination I missed.

You saw a huge difference between 1) and 4), but what you should have
been comparing if you wanted to gauge the effect of -fno-math-errno
on that loop is between 1) and 3).

-- 
avva
"There's nothing simply good, nor ill alone" -- John Donne
=================================================================
To unsubscribe, send mail to linux-il-request_at_linux.org.il with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail linux-il-request_at_linux.org.il


This archive was generated by hypermail 2.1.7 : Tue 09 Aug 2005 - 13:18:16 IDT