public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: * Re: What is acceptable for -ffast-math? (Was: associative lawin combine)
@ 2001-07-29 12:52 Linus Torvalds
  2001-07-29 14:03 ` What is acceptable for -ffast-math? (Was: associative law incombine) Stephen L Moshier
  0 siblings, 1 reply; 119+ messages in thread
From: Linus Torvalds @ 2001-07-29 12:52 UTC (permalink / raw)
  To: moshier; +Cc: tprince, gcc

On Sun, 29 Jul 2001, Stephen L Moshier wrote:
>
> > Why not make this easier for the user:
> > - default to non-IEEE, but "reasonably safe".
>
> The default GCC behavior is not to allow any optimization
> that could change the value of an expression.  That rule is easy
> for users to understand and surely it is a good rule to keep.
>
> Floating point arithmetic, IEEE or otherwise, does not obey the
> associative or distributive laws.

This is YOUR opinion.

But most people who write floating point code do not think that way. They
consider FP to be "mathematically exact" (which _does_ obey associative
and distributive laws), and they write their programs that way.

And that is exactly my point: to the people who do not care about the
exact details of what it means from a mathematical standpoint, using
associative and distributive rules is _fine_. Their algorithms do not
care, simply because the person writing the code doesn't even _know_ about
the issues of limited precision and range of FP code.

See?

What I'm saying is that the compiler cannot fix "thinking errors" of the
programmer. Trying to fix them is self-defeating and ultimately stupid.
I'm claiming that 99%+ of all programmers do not know or care about the
fact that

	(a+b)*c

is not necessarily the same as

	a*c+b*c

when we're talking about floating point.

My _second_ point is that the ones that DO KNOW and DO CARE are also aware
enough that _they_ can be counted on to add a "-mieee" or other flags.

They are also knowledgeable enough that they know what "associative law"
means, which is already a step up from most programmers, I suspect. So
they can understand why the compiler has done an optimization, and can (if
it is documented) be instructed to use the appropriate switch for not
doing that optimization.

So: put the onus of understanding different FP optimizations on the people
who CAN understand them. Don't make the average person who wants to do his
own small project have to worry about it. By the time the person
understands about the range of FP arithmetic enough to care, he will also
have the capability to do something about it.

		Linus

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-29 14:22 dewar
  0 siblings, 0 replies; 119+ messages in thread
From: dewar @ 2001-07-29 14:22 UTC (permalink / raw)
  To: moshier, torvalds; +Cc: gcc, tprince

<<Further, GCC is supposed to be standards compliant by default.  You
seem to be advocating that GCC should, without warning, generate
programs that violate the standard and are mathematically wrong.
>>

and that violate the expectations of programmers who *do* know what they
are doing!

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law incombine)
  2001-07-29 14:22 dewar
@ 2001-07-29 21:33 Linus Torvalds
  2001-07-30 14:43 ` What is acceptable for -ffast-math? (Was: associative law in combine) Alexandre Oliva
  -1 siblings, 1 reply; 119+ messages in thread
From: Linus Torvalds @ 2001-07-29 21:33 UTC (permalink / raw)
  To: dewar; +Cc: moshier, gcc, tprince

On Sun, 29 Jul 2001 dewar@gnat.com wrote:
>
> <<Further, GCC is supposed to be standards compliant by default.  You
> seem to be advocating that GCC should, without warning, generate
> programs that violate the standard and are mathematically wrong.
> >>
>
> and that violate the expectations of programmers who *do* know what they
> are doing!

Note that most of the people I know who _do_ know what they are doing also
want to have the option o ftelling the compiler when they don't care, and
want to allow the compiler to do "illegal" things if that allows the
compiler to generate better code.

It's sad that C99 doesn't seem to allow the Fortran kind of "you go and
make the best you can of this expression unless I tell you otherwise".
Which means that we'd _have_ to be non-standard to allow the optimization
at all. Which obviously means a flag (one way or the other).

Rather than have a good way to do it on a case-by-case basis, which is
probably what the people who _really_ care would prefer.

In C, you can't reasonably use the Fortran "parenthesis are significant"
approach - the way the C pre-processor works you often have lots of extra
parenthesis for that reason. However, C does have the well-established
sequence point rule, so it would make sense to have that kind of
extension.

And hey, if you want to make it be an extra flag, just make it a _simple_
one. Like "-ffast-math". Don't make people have to know 100 esoteric flags
that are compiler-release-specific. That's completely useless.

		Linus

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-30  5:57 dewar
  0 siblings, 0 replies; 119+ messages in thread
From: dewar @ 2001-07-30  5:57 UTC (permalink / raw)
  To: moshier, torvalds; +Cc: gcc, tprince

<<In 99% of all cases, the information of the subtleties of floating point
arithmetic simply was never there in the program gcc tries to compile.
Why? Becuase the programmer didn't even _intend_ any of the subtleties, or
even knew about them.

Gcc trying to enforce them doesn't add any value.
>>

The 99% is a highly excessive estimate here. Many many people understand
and expect that the compiler will NOT mess around with their floating-point
expressions. It has always been the case in Fortran that you can depend
on parentheses being respected, and programmers do indeed count on this.

Especially on an IEEE machine, fpt is not just some vague approximation
of real numbers, it is a well defined arithmetic system with well defined
rules, and in the world of numerical programming, people are not as
incompetent as you claim. Sure, a casual C programmer using an occasional
bit of fpt arithmetic may well not know what they are doing, but a compiler
should aim its implementation of fpt at those writing numeric codes. Giving
wrong answers fast is definitely not useful.

Part of the trouble here is that many compiler writers and systems people
simply don't know floating-point semantics well enough, and have often made
improper assumptions about optimizations etc, which have caused a lot of trouble
in the past. 

I cannot believe that any expert in floating-point would agree that it is just
fine to do the kind of optimizations you suggest by default, and indeed I have
trouble seeing that this kind of "optimization" is ever anything but a bug!

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-30  6:00 dewar
  2001-07-30 13:08 ` Toon Moene
  0 siblings, 1 reply; 119+ messages in thread
From: dewar @ 2001-07-30  6:00 UTC (permalink / raw)
  To: dewar, torvalds; +Cc: gcc, moshier, tprince

<<It's sad that C99 doesn't seem to allow the Fortran kind of "you go and
make the best you can of this expression unless I tell you otherwise".
>>

But if you look at real Fortran codes, they are full of parentheses to stop
the compiler from messing around, and in fact most Fortran compilers do only
very limited mucking even when no parens are present. I would be shocked
to find a FOrtraqn compiler that changed

a*c + b*c

into

(a+b)*c

a totally diffrerent expression!

I would have written the second expression if I had wanted that effect.

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-30  6:01 dewar
  2001-07-30  6:53 ` Tim Hollebeek
  0 siblings, 1 reply; 119+ messages in thread
From: dewar @ 2001-07-30  6:01 UTC (permalink / raw)
  To: dewar, torvalds; +Cc: gcc, moshier, tprince

It certainly would be interesting to hear from someone with extensive
experience in writing numerical codes supporting Linus' position (I have
that extensive experience, but for sure I do not support the position that
a compiler should by default do completely different things from what it
is told.

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-30  6:14 dewar
  2001-07-30  8:30 ` Kevin Handy
  0 siblings, 1 reply; 119+ messages in thread
From: dewar @ 2001-07-30  6:14 UTC (permalink / raw)
  To: fjh, gdr; +Cc: gcc, moshier, torvalds, tprince

Another issue here is that the optimizations in question are unlikely to
be that significant in practice. In real programs where performance is an
issue, there will be relatively few opportunities for this kind of
optimization, and assuming that the program was reasonably competently
written, those few cases are likely to be just those that should not
be molested.

Consdier again the assoicative case. The expression

a*b + a*c

Now there are three cases

1. The really competent programmer, who knows that the associat9ive 
transformation will blow up the error analysis, and wrote it that way
for a good reason.

2. The somewhat competent programmer, who knows nothing about fpt details,
but who wants their program to run fast. Such a programmer is likely to
rewrite this manually as

  a +(b*c)

anyway, because that's obviously "faster".

3. The incompetent programmer who knows nothing about fpt details, wants
their program to run fast, would be perfectly happy to have that
transformation done, but does not do it themselves.

These optimizations are aimed only at class 3 programmers. I think that

a) there are fewer such programmers than people think, certainly nothing
like Linus' 99%

b) even for them, it will be rare for these kind of improper optimizations
to make a difference (that's why Fortran compilers don't really mess around
that much, even though the standard allows them to).

Floating-point is always tricky. If you see

a*a*a*a

in a program, it may seem very tempting to "optimize" this
as 

T := a*a;
T*T

but even though the latter has only two operations instead of three, it
has significantly worse error bounds.

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-30  8:59 mike stump
  0 siblings, 0 replies; 119+ messages in thread
From: mike stump @ 2001-07-30  8:59 UTC (permalink / raw)
  To: moshier, torvalds; +Cc: gcc, tprince

> Date: Sun, 29 Jul 2001 17:03:16 -0400 (EDT)
> From: Stephen L Moshier <moshier@mediaone.net>

> Further, GCC is supposed to be standards compliant by default.

Really?

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law incombine)
  2001-07-30  0:23     ` Gabriel Dos Reis
@ 2001-07-30 11:37 Linus Torvalds
  2001-07-30 11:53 ` What is acceptable for -ffast-math? (Was: associative law in combine) Gabriel Dos Reis
  -1 siblings, 1 reply; 119+ messages in thread
From: Linus Torvalds @ 2001-07-30 11:37 UTC (permalink / raw)
  To: Gabriel Dos Reis; +Cc: Fergus Henderson, moshier, tprince, gcc

On 30 Jul 2001, Gabriel Dos Reis wrote:

> Fergus Henderson <fjh@cs.mu.oz.au> writes:
> |
> | I think it would be nice if GCC was standards compliant by default,
> | but that is certainly not the status quo.
>
> Agreed.  But that should be the ideal we should strive for.
> And also, I don't think that is an argument for not computing
> correctly, even if we're asked to compute fast.

Note that "a/b/c" _is_ the same as "a/(b*c)" when seen as a mathematical
expression.

So it's certainly very very debatable whether you can say "not computing
correctly" for it - the user has not actually shown a strong preference
for the double division, and it is entirely possible that the double
division has come about as a result of macro expansion and/or inline
function expansion.

Who are you to say that the user wouldn't like the code to be run faster?
I don't think this was well-defined in C before C99, for example.

Note that with the current -ffast-math on x86, for example, gcc will
already inline things like "sin()" etc using the x87 instruction directly.
Despite the fact that that instruction is well-defined only for the range
-2**63 to +2**63.

And now there are people that say that something simple as "a/b/c" should
not be optimized to "a/(b*c)" even with -ffast-math.

What planet are you people from?

We are already in the situation that -ffast-math gives a value for "sin()"
that is not even _close_ to the range of [-1,1] under various
circumstances. And you're complaining about the fact that b*c can
overflow? We've already effectively limited our argument range to +-2**63,
and there's no way a*b can overflow, guys.

So even if you don't want to make it an optimization by default with the
C99 argument (hey, I don't really disagree), there is absolutely _no_
valid argument for not doing it with -ffast-math.

		Linus

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-30 11:52 dewar
  0 siblings, 0 replies; 119+ messages in thread
From: dewar @ 2001-07-30 11:52 UTC (permalink / raw)
  To: dewar, torvalds; +Cc: gcc, moshier, tprince

<<What if I told you that it's actually legal? Go check it out.
>>

Of course it's legal - I *do* know the Fortran standard :-)

Indeed, read what I said more carefully, what I was saying was that *even
though* the Fortran standard allows this transformation, I would be shocked
at a compiler that actually did this particular one, since it is seriously
misguided to do so. Permission in the standard to do unwise things does
not mean that you have to do them.

<<Fortran, while being a pretty horrible language for anything else, does
exactly this for FP math. It doesn't do it for much anything else, of
course.
>>

Fortran is by no means such a horrible language for numerical calculations
(I say this having written many hundreds of thousands of lines of Fortran
numerical code). Yes, I know perfectly well that the standard allows this,
and that is why competent Fortran programmers use parentheses extensively,
but at least in my experience, Fortran compilers are fairly judicious about
taking advantage of the permissions in the absence of parens. Most certainly
one would expect that a Fortran compiler would NOT bother to preserve the
"proper" negative zero semantics in the case where parens are not used,
and so would for example feel free to transform 

a-b

into

-(b-a)

which is of course not a valid transformation from a strict IEEE point of view.

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-30 12:26 dewar
  0 siblings, 0 replies; 119+ messages in thread
From: dewar @ 2001-07-30 12:26 UTC (permalink / raw)
  To: dewar, kth; +Cc: gcc

<<I would hope that a competent programmer would not transform the
expression into this. I would hope he would transform it into

    a*(b+c)

instead, unless my algebra has become hopelessly munged since
high school.
>>

:-)

oops, yes indeed

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-30 13:10 dewar
  0 siblings, 0 replies; 119+ messages in thread
From: dewar @ 2001-07-30 13:10 UTC (permalink / raw)
  To: dewar, toon; +Cc: gcc, moshier, torvalds, tprince

>>Well, I won't be so rash as to say that it should do that *by default*,

But that of course is the issue under discussoin :-)

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-30 15:29 dewar
  2001-07-30 15:39 ` Toon Moene
  0 siblings, 1 reply; 119+ messages in thread
From: dewar @ 2001-07-30 15:29 UTC (permalink / raw)
  To: dewar, toon; +Cc: gcc, moshier, torvalds, tprince

<<No, you should have written (a*c) + (b*c) if you wanted the first
expression *instead* of the second one - heck, you say so yourself, in
the above (... full of parentheses ...)
>>

I really don't understand the "No" here. Please reread my message. Yes
of course, to fully follow the standard you put in the parens, as I noted
earlier, Fortran code tends to be full of parens for this reason.

But ... my point was that *EVEN IF* no parens were present, I would be shocked
to find the Fortran compiler using the associative law to rewrite my 
expression.

After all, if you really believe that a Fortran compiler is not only allowed
(true) but reasonably may (false) perform any kind of such "optimization",
there are some pretty weird results that can occur in the extreme.

Actually most Fortran programmers, even those not doing careful error
analysis, tend to be quite disturbed if optimization changes results,
even if this is allowed by the standard.

If you write

  A = B * C

  IF (A.EQ.B*C) ...

it is a surprise for the IF to yield False, even though the standard permits
it. Kahane in fact declares this to be a bug, and cannot be mollified by
being told that the standard permits this "bug".

I know that IBM in their Fortran compiler for the Power architecture (and
remember IBM is targetting knowledgable numeric programmers there) decided
that in optimization mode they should still round to single precision when
calculations are done in registers, and avoid the phenomenon of getting
unexpected extra accuracy for register calculations [the reason incidentally
behind the misbehavior of the IF above].

The x86 of course is quite a nightmare from this point of view (at least
the traditional 80-bit stack based arithmetic is a nightmare, the newer
fpt on the later Pentiums and Athlons is better).

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-30 16:11 dewar
  2001-07-30 16:29 ` Alexandre Oliva
  0 siblings, 1 reply; 119+ messages in thread
From: dewar @ 2001-07-30 16:11 UTC (permalink / raw)
  To: aoliva, neil; +Cc: dewar, gcc, moshier, torvalds, tprince

<<I am serious.  The preprocessor should protect individual parentheses
from becoming consecutive by introducing whitespace.  We should tell:

>>

Sometimes I must say that people seem to work hard to dream up the most
infeasible and ugly syntactical ideas that one could possibly imagine :-)

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-30 18:00 dewar
  2001-07-30 18:25 ` Joe Buck
  0 siblings, 1 reply; 119+ messages in thread
From: dewar @ 2001-07-30 18:00 UTC (permalink / raw)
  To: moshier, mrs; +Cc: gcc

<<Well, then I'd like to understand the actual policy better.  You have
GNU extensions but otherwise seem to include what's in the language
definition.  The apparent conflicts arise when you say the
GNU extensions have to be turned off in order for the compiler
to be strictly compliant with the standard.
>>

Does the standard forbid upwards compatible extensions?

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-30 18:02 dewar
  0 siblings, 0 replies; 119+ messages in thread
From: dewar @ 2001-07-30 18:02 UTC (permalink / raw)
  To: aoliva, dewar; +Cc: gcc, moshier, neil, torvalds, tprince

<<I'd much rather write ((a*b)) + ((a*c)), which will even compile with
other compilers, and might be picked up in a future revision of the C
and C++ Standards.
>>

I very much doubt that the standards committee would even consider, let
alone adopt such a lexically dubious extension :-)

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-30 18:08 dewar
  0 siblings, 0 replies; 119+ messages in thread
From: dewar @ 2001-07-30 18:08 UTC (permalink / raw)
  To: aoliva, jsm28; +Cc: gcc

<<I'd rather you use #pragma STDC FP_CONTRACT OFF - while this seems to be
intended to disable optimisations that give more accurate results, it
should probably disable optimisations here that may give less accurate
results as well.
>>

Well to be fair, Alexandre is trying to provide a mechanism for very
specifically controlling on an operation by operation basis whether
"optimizations" of the type we are discussing are permitted.

My own view is that, if you absolutely must persue this, then just use
parentheses as in Fortran. So what if some PP output is unnecessarily
signalled as not being optimizable, this is a second order effect (my
own guess is that even the first order effect of such optimizations is
small in any case).

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-30 18:38 dewar
  0 siblings, 0 replies; 119+ messages in thread
From: dewar @ 2001-07-30 18:38 UTC (permalink / raw)
  To: dewar, jbuck; +Cc: gcc, moshier, mrs

<<The standard specifies that some names, including names beginning with
two underscores, are reserved to the implementation.  Extensions that
use reserved names don't conflict with the standard.  But extensions that
introduce new keywords, where the keywords aren't in the implementation
namespace, do conflict with the standard because a programmer can legally
use that word as an identifier.
>>

OK, yes of course new reserved words are not allowed (COBOL is the only
standard language in which new keywords are allowed, well at least this
was in 74 COBOL, not sure if this was retained in later standards).

But it is interesting that many of the GNU extensions do not conflict with
the standard (e.g. nested procedures, dynamic arrays), so these are not
examples of GNU-C not meeting the criterion of being standard by default.

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-30 18:39 dewar
  0 siblings, 0 replies; 119+ messages in thread
From: dewar @ 2001-07-30 18:39 UTC (permalink / raw)
  To: dewar, jbuck; +Cc: gcc, moshier, mrs

Note incidentally that by contrast, no extensions of any kind are permitted
in Ada, and the Ada test suite contains a number of tests that try to guess
at possible extensions and make sure they are not accepted :-)

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-30 19:46 dewar
  2001-07-30 20:00 ` Gabriel Dos Reis
  0 siblings, 1 reply; 119+ messages in thread
From: dewar @ 2001-07-30 19:46 UTC (permalink / raw)
  To: amylaar, aoliva; +Cc: dewar, gcc, moshier, torvalds, tprince

> ({a+b;})*c

OK, I admit, the (( .. )) suggestion was not the most awful one after all :-)

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-30 20:54 dewar
  2001-07-30 21:11 ` Gabriel Dos Reis
  2001-07-30 21:39 ` Joern Rennecke
  0 siblings, 2 replies; 119+ messages in thread
From: dewar @ 2001-07-30 20:54 UTC (permalink / raw)
  To: aoliva, gdr; +Cc: amylaar, dewar, gcc, moshier, torvalds, tprince

<<The C/C++ Standard, for example, say that a/b/c is evaluated as
(a/b)/c. Someone proposed that it should be evaluated as a/(b*c)
-- which of course is incorrect -- when optimizing.
>>

Well now, let's be a little careful here ....

When the C standard says a/b/c is evaluated as (a/b)/c, it means that the
result is *as if* it were computed that way, and a compiler is of course
free to compute a/(b*c) if the result is still correct.

Now for the integer case we know exactly what "still correct" means, it
means that if we can prove that a/(b*c) does not overflow, and always
gives the same result as (a/b)/c, then of course the compiler can compute
it the first way.

What we are discussing is what "still correct" means for floating-point.
Most certainly the C and C++ standards do not give an exact prescription
for floating-point values. You actually have to say quite a bit to even
get near to the point where you can really answer the question accurately.

Let's look at the exact rules in Ada for example. Of course Ada requires
that a/b/c be computed as if it were (a/b)/c. But Ada also defines a
concept of model numbers and model intervals, and any evaluation scheme
that provides a guarantee that the result is in the appropriate model
interval is permissible. Let's give an example.

If you see  a / 2.0 / 2.0

it is absolutely definition OK, to compute this particular expression
as a / 4.0 in any language no matter WHAT the standard says, since the
result is exactly the same. But for other divisors this is not necessarily
true.

So the "which of course is incorrect" is a bit too glib here.

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-30 21:13 dewar
  2001-07-30 21:34 ` Gabriel Dos Reis
  0 siblings, 1 reply; 119+ messages in thread
From: dewar @ 2001-07-30 21:13 UTC (permalink / raw)
  To: dewar, gdr; +Cc: amylaar, aoliva, gcc, moshier, torvalds, tprince

<<What I intended to mean is that the compiler has, first, to prove that
such transformmation yields the same result.  The only argument I've
seen so far is that, GCC should make the program run fast...
>>

Not quite, "the same result" is too strong. Instead, the rule is that
the transformation must yield a result that is still conforming to the
requirements of the standard. Since the standard is (deliberately) not
100% precise in saying what this result should be, there is some legitimate
wiggle room.

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-31  7:26 dewar
  2001-07-31 15:57 ` Toon Moene
  2001-08-03  6:12 ` Per Abrahamsen
  0 siblings, 2 replies; 119+ messages in thread
From: dewar @ 2001-07-31  7:26 UTC (permalink / raw)
  To: gdr, torvalds; +Cc: fjh, gcc, moshier, tprince

> The _only_ person who can judge whether it's wrong to do so it the person
> who wrote the program. He may be happy with the faster code. He's almost
> certainly happy if he used "-ffast-math".

Well it sure would be nice to here from some of these mythical numerical
programmers (I don't care if they are writing games or nuclear reactor codes)
who would be happier, so far we haven't heard this! And in my experience,
even quite inexperienced floating-point numerical programmers are very
disturbed when optimization changes the results of their programs.

> Your arguments about "numerical computation" are just silly, as you don't
> seem to realize that there are tons of problems where your theoretical
> issues are nothing more than noise.

If you think the arguments are silly, then I really fear you lack the full
context for this discussion, a discussion that has, as you should know raged
for well over thirty years.

> Go back and look at why it was added. Right - exactly so that people could
> get _fast_ math, when they do not care about the inexactness that isn't
> even an issue for most people.

Sure -ffast-math is precisely intended to allow transformations that would
not otherwise be allowed (let's not call them optimizations, that's just
too controversial a word in the context of this argument).

The question is what is the boundary of allowable transformations. No one
agrees that there should be no boundaries (even you don't like the change
results to zero, though note that abandoning denormals has exactly this
effect, and might be considered acceptable).

So, what is the boundary, can one for instance forget about denormals and
flush to zero to save a bit of time, can one truncate instead of round,
can one ignore negative zeroes, or infinity semantics, can one ignore
intermediate overflow (note: nearly all the discussed transformations are
implicitly saying yes to this last question).

I have not seen anyone writing from the point of view of serious numerical
coding saying "sure, go ahead and transform a*b + a*c, I write that all
the time and would be happy if the compiler would speed it up in the
obvious manner". So there is real doubt as to whether this particular
transformation (which can introduce overflow where none existed before)
is one that we can agree to include.

It seems to me that the criteria for including a transformation are

  a) it is well understood and clear
  b) it really makes a difference in performance
  c) it does not introduce surprises

Unfortunately, these are often in conflict. For instance, in C, we usually
degrade the precision of the x86 floating-point unit to minimize surprises,
even though operating in full precision would be more efficient in some
cases. Should -ffast-math allow full precision operation? I would think so,
since it definitely improves performance, and reduces surprises.

Similarly, I would go for ignoring accuracy in negative zero handling, since
that so clearly meets criteria a) and c). 

But really it would be very valuable to have quantitative data that show that
a particular optimization is worth while. I will say again that the particular
case that has been discussed, which is associative law redistribution, has not
carried that burden for me AT ALL. It is a dubious transformation, violating
condition c) above, and you can see that it is dubious, given the reaction of
many, not just me, on this list. Furthermore, promoting something like this
gives an impression to serious numerical analysts that "those GCC guys" simply
don't understand the requirements of floating-point arithmetic, and that's a
cost one does not want to freely pay unless you are sure you are getting 
something in return.

We can't look to the standard here, because the question is one of taste. 
Even in Ada, in relaxed mode (the equivalent of -ffast-math, but acknowledged
and defined somewhat in the stadard), the standard leaves these kind of
decisions to the implementor. However, I cannot imagine any Ada implementor
being as cavalier as you would suggest.

By the way, I said I would be shocked to find a Fortran compiler that did
associative redistribution in the absence of parens. I am somewhat surprised
that no one stepped forward with a counter-example, but I suspect in fact that
there may not be any shocking Fortran implementations around.

It is an old argument, the one that says that fpt is approximate, so why bother
to be persnickety about it. Seymour Cray always tool this viewpoint, and it
did not bother him that 81.0/3.0 did not give exactly 27.0 on the CDC 6000
class machines.

But the advent, and essentially universal adoption, of IEEE arithmetic, really
signifies that Seymour's viewpoint (and yours in this discussion) have lost the
argument and that indeed we want floating-point to be well behaved even though
we know many programmers won't know what this means. Our experience is that
even these inexperienced programmers gain from a better floating-point model,
and that, surprise! the cost in hardware to give such a model turns out to be
minimal.

Now compiler writers always get carried away with the supposed importance
of miscellaneous optimizations, and every compiler writer has the experience
of working hard on some optimization, only to find that it has little or
no effect.

If indeed we could show that associative redistribution had a large effect
on important programs (be they games, SPEC tests, or nuclear reactor codes),
then we would have a different situation from the hardware folks. But I suspect
that careful measurement would show that we are in the same boat, and that the
cost of providing decent floating-point semantics is too small for it to be
worth worrying about very much.


Robert

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-31  7:59 mike stump
  0 siblings, 0 replies; 119+ messages in thread
From: mike stump @ 2001-07-31  7:59 UTC (permalink / raw)
  To: moshier; +Cc: gcc

> Date: Mon, 30 Jul 2001 18:45:49 -0400 (EDT)
> From: Stephen L Moshier <moshier@mediaone.net>
> To: mike stump <mrs@windriver.com>
> cc: gcc@gcc.gnu.org

> > > Further, GCC is supposed to be standards compliant by default.
> > 
> > Really?

> Well, then I'd like to understand the actual policy better.  You
> have GNU extensions but otherwise seem to include what's in the
> language definition.  The apparent conflicts arise when you say the
> GNU extensions have to be turned off in order for the compiler to be
> strictly compliant with the standard.

Sounds like you have a pretty good idea already.  Mostly compliant
isn't the same as compliant.  If you want to understand the
differences better, take a look at all the code that -mieee, -ansi,
-std... and -pedantic-errors changes.  I think I got all the right
flags, but I may have missed one, also, I'm not up on the ieee stuff,
so the first one could be wrong.

It seems that the extensions fall on enough peoples radars for this to
be an issue, which rises this to a level higher than just an
insignificant nit; I tend to think of it as a minor nit.

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-31  8:19 mike stump
  0 siblings, 0 replies; 119+ messages in thread
From: mike stump @ 2001-07-31  8:19 UTC (permalink / raw)
  To: dewar, moshier; +Cc: gcc

> From: dewar@gnat.com
> To: moshier@moshier.ne.mediaone.net, mrs@windriver.com
> Date: Mon, 30 Jul 2001 21:00:23 -0400 (EDT)

> Does the standard forbid upwards compatible extensions?

Roughly speaking, when something is undefined, we are allowed to
define it.  When behavior is defined, we are not allow to behave as if
we differed from it.  When something is unspecified, we are allowed to
specify it.

For example:

#include <alloca.h>

isn't defined, therefore, we can define it to do anything.

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-31  8:35 dewar
  0 siblings, 0 replies; 119+ messages in thread
From: dewar @ 2001-07-31  8:35 UTC (permalink / raw)
  To: dewar, moshier, mrs; +Cc: gcc

>isn't defined, therefore, we can define it to do anything.

Right, that's what I mean by an upwards compatible extension, i.e. one that
cannot affect any legal defined program. So the answer is indeed for C and
C++ that extensions are allowed freely if they are upwards compatible (this
is not the case in COBOL and Ada), where the standard requires a mode in 
which no extensions are made.

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-31  8:36 dewar
  0 siblings, 0 replies; 119+ messages in thread
From: dewar @ 2001-07-31  8:36 UTC (permalink / raw)
  To: moshier, mrs; +Cc: gcc

surely -mieee is not required to be standards compliant?

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-31  8:36 mike stump
  0 siblings, 0 replies; 119+ messages in thread
From: mike stump @ 2001-07-31  8:36 UTC (permalink / raw)
  To: dewar, jbuck; +Cc: gcc, moshier

> From: dewar@gnat.com
> To: dewar@gnat.com, jbuck@synopsys.COM
> Cc: gcc@gcc.gnu.org, moshier@moshier.ne.mediaone.net, mrs@windriver.com
> Date: Mon, 30 Jul 2001 21:38:24 -0400 (EDT)

> But it is interesting that many of the GNU extensions do not
> conflict with the standard (e.g. nested procedures, dynamic arrays),
> so these are not examples of GNU-C not meeting the criterion of
> being standard by default.

?

When there is wording that states that the value inside the [] shall
be a constant greater than 0 and non-negative, there is absolutely no
room for gcc to extend the sematics to include dynamicly sized arrays,
sorry.

  If the constant-expression (5.19) is present, it shall be an
  integral constant expression and its value shall be greater than
  zero.

From:

1 In a declaration  T  D where  D has the form

          D1 [constant-expressionopt]

  and the type of the identifier in the declaration  T  D1 is   derived-
  declarator-type-list    T, then the type of the identifier of  D is an
  array type.   T is called the array  element  type;  this  type  shall
  not  be  a  reference  type,  the  type    void, a function type or an
  abstract class type.  If the  constant-expression (5.19)  is  present,
  it  shall  be  an  integral constant expression and its value shall be
  greater than zero.  The constant expression specifies the    bound  of
  (number  of  elements  in)  the  array.   If the value of the constant
  expression is  N, the array has  N elements numbered  0 to   N-1,  and
  the type of the identifier of  D is derived-declarator-type-list array
  of  N  T.  An object of array type contains a  contiguously  allocated


  8.3.4  Arrays           DRAFT: 2 December 1996       Declarators  8-11

  non-empty  set  of    N  sub-objects  of  type    T.   If the constant
  expression is omitted, the type of the identifier of   D  is  derived-
  declarator-type-list  array  of  unknown  bound  of   T, an incomplete
  object type.  The type derived-declarator-type-list array of  N  T  is
  a  different  type from the type derived-declarator-type-list array of
  unknown bound of  T, see 3.9.  Any type of the  form  cv-qualifier-seq
  array  of    N   T is adjusted to array of  N cv-qualifier-seq  T, and
  similarly for array of unknown bound of  T.  [Example:

          typedef int A[5], AA[2][3];
          typedef const A CA;     // type is ``array of 5 const int''
          typedef const AA CAA;   // type is ``array of 2 array of 3 const int'\
'

   -end example] [Note: an array of   N  cv-qualifier-seq    T  has  cv-
  qualified  type;  such an array has internal linkage unless explicitly
  declared  extern (7.1.5.1) and must be  initialized  as  specified  in
  8.5.  ]

I don't have any clue how you were so misled on this.  Similar I
suspect for nested procedures.  When the standard says that violation
of a mandate of the standard must result in a diagnostic:

2 Every conforming C++ implementation shall, within its resource limits,
  accept and correctly execute well-formed C++ programs, and shall issue
  at least one diagnostic message when  presented  with  any  ill-formed
  program  that contains a violation of any diagnosable semantic rule or
  of any syntax rule.

this _does_ result in failure to comply with the standard and gcc does
fail to meet a criterion of the standard, as you put it.  These are
reasonably safe extensions, as no conforming program can use the
standard to complain about the semantics then, but that is all.

#include <alloca.h> is an example of an allowed extension.  It isn't
defined in the standard, and the standard doesn't mandate a
diagnostic.

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-31  8:37 dewar
  0 siblings, 0 replies; 119+ messages in thread
From: dewar @ 2001-07-31  8:37 UTC (permalink / raw)
  To: dewar, jbuck, mrs; +Cc: gcc, moshier

<<When there is wording that states that the value inside the [] shall
be a constant greater than 0 and non-negative, there is absolutely no
room for gcc to extend the sematics to include dynamicly sized arrays,
sorry.
>>

I disagree, this is giving the rules for legal, well-define programs, and
the standard has nothing to say about the behavior of a program that does
not follow the above rules.

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-31  9:22 mike stump
  0 siblings, 0 replies; 119+ messages in thread
From: mike stump @ 2001-07-31  9:22 UTC (permalink / raw)
  To: dewar, moshier; +Cc: gcc

> From: dewar@gnat.com
> To: dewar@gnat.com, moshier@moshier.ne.mediaone.net, mrs@windriver.com
> Cc: gcc@gcc.gnu.org
> Date: Tue, 31 Jul 2001 11:35:04 -0400 (EDT)

> >isn't defined, therefore, we can define it to do anything.

> Right, that's what I mean by an upwards compatible extension, i.e. one that
> cannot affect any legal defined program. So the answer is indeed for C and
> C++ that extensions are allowed freely if they are upwards compatible (this
> is not the case in COBOL and Ada), where the standard requires a mode in 
> which no extensions are made.

What did you mean by `are allowed freely'?  Did you mean are permitted
by the standard?  If so, then you are wrong.  They are not allowed,
there is a requirement upon all processors of the language that they
issue a diagnostic.  This requirement of a diagnostic is not what I
would call `are allowed freely'.  If you meant anything else, you can
ignore this message.

> From: dewar@gnat.com
> To: dewar@gnat.com, jbuck@synopsys.COM, mrs@windriver.com
> Cc: gcc@gcc.gnu.org, moshier@moshier.ne.mediaone.net
> Date: Tue, 31 Jul 2001 11:37:02 -0400 (EDT)

> <<When there is wording that states that the value inside the [] shall
> be a constant greater than 0 and non-negative, there is absolutely no
> room for gcc to extend the sematics to include dynamicly sized arrays,
> sorry.
> >>

> I disagree, this is giving the rules for legal, well-define
> programs, and the standard has nothing to say about the behavior of
> a program that does not follow the above rules.

In case it wasn't perfectly clean in the above, I meant, there is no
room within the confines of the standard.  If you don't disagree with
that, you can stop reading now; however, if you do, read on...

The standard says nothing about the behavior of any program, you
misunderstand the standard.  The standard only give requirements on
processors of the language.  Any violation of any requirement of the
standard upon a processor of the lanuage, is a violation of the
standard.  There is no difference in the standard wether the
requirement is for a diagnostic, or for some behavior of running code.
Any differentiation made is extra-standard.

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-31 16:38 dewar
  0 siblings, 0 replies; 119+ messages in thread
From: dewar @ 2001-07-31 16:38 UTC (permalink / raw)
  To: dewar, toon; +Cc: fjh, gcc, gdr, moshier, torvalds, tprince

<<Well, that might be because we're on a public list and I, certainly, am
not going to name names of competitors.  Granted, I do not have an
actual proof of a Fortran compiler that substituted (a+b)*c for a*b+a*c,
but I have seen those that:
>>

Well you should certainly not hesitate to name compilers here, this is hardly
a sensitive issue, and indeed there are those on this list who think that
associative rearrangement *is* an appopriate "optimization" even if it
introduces overflow where none existed for the canonical evaluation.

But in fact, as you make clear, you don't in fact know of any Fortran compiler
that does associative rearrangement, and I repeat my point ... I would be
surprised if there is any such in existence.

<<2. Included an option to use floating point induction variables, i.e.,
   change:

      DO I = 1, N
         A(I) = I * 0.1
      ENDDO

   into:

      T = 0.0
      DO I = 1, N
         T = T + 0.1
         A(I) = T
      ENDDO
>>

Must be an old compiler. I can't think of any modern machine where such a
change would make a difference. Floating-point IEEE addition is typically
at least as difficult, sometimes one pipeline stage harder, than floating-point
multiplication, so that's a pretty pointless transformation on modern machines
as well as being a real horror (this not a case where the error is limited
to a few ULP!)

And that's really the point. For modern machines, are we really talking
about transformations that make any difference to the performance? Not
once in this long discussoin has anyone provided even the tiniest smidgeon
of quantitative support for the position that you do see a real performance
difference in real programs. Linus may declare that he knows that this is
a significant issue, and call people silly if they disagree, but that hardly
counts as quantitative data :-)

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-31 17:37 dewar
  0 siblings, 0 replies; 119+ messages in thread
From: dewar @ 2001-07-31 17:37 UTC (permalink / raw)
  To: dewar, torvalds; +Cc: fjh, gcc, gdr, moshier, tprince

I still see *no* quantitative data showing that the transformation most
discussed here (associative redistribution) is effective in improving
performance.

Linus seems to be getting a little emotional in this discussion but swearing
does not replace data.

My viewpoint, which I will repeat briefly is that we have to look at each
proposed transformation individually, and determine, by quantitative
measurement (many of these measurements are in the literature) how great
the gain is, and then the gain has to be traded off against the effect of
the transformation.

This seems such a reasonable position that I really have a heck of a time
understanding why Linus is so incensed by it.

Note that the requirement that there be a significant gain is a gate. If a
given transformation cannot be shown to be an effective contributor to
performance, then we don't need to discuss the effect of the transformation.
I suspect that in practice, many of the proposed transformations simply won't
get past this gate, and that thus a lot of hot air is expended uselessly 
arguing about the second stage.

In the case that a given transformation *is* effective, then we can discuss.

Let's take an example, which is multiplication by a reciprocal. This of
course does give "wrong" results. But division is often quite slow. On the
other hand, I can't imagine floating-point programmers, including those
doing game stuff, not knowing this, and thus writing the multiplication
in the first place. 

<<Oh, round-to-zero is definitely acceptable in the world of "who cares
about IEEE, we want fast math, and we'll use fixed arithmetic if the FP
code is too slow".
>>

That's probably a bad idea, on most modern machines, full IEEE floating-point
is faster than integer arithmetic (particularly in the case of multiplication)

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law incombine)
  2001-07-30 21:11 ` Gabriel Dos Reis
@ 2001-07-31 18:12 Linus Torvalds
  2001-07-31 20:55 ` What is acceptable for -ffast-math? (Was: associative law in combine) Gabriel Dos Reis
  -1 siblings, 1 reply; 119+ messages in thread
From: Linus Torvalds @ 2001-07-31 18:12 UTC (permalink / raw)
  To: Gabriel Dos Reis; +Cc: dewar, aoliva, amylaar, gcc, moshier, tprince

On 31 Jul 2001, Gabriel Dos Reis wrote:
>
> What I intended to mean is that the compiler has, first, to prove that
> such transformmation yields the same result.

Ehh..

You're _wrong_.

A lot of compiler optimizations yield different results. The question is
whether it is acceptable or not.

It _clearly_ is acceptable to do when the user has explicitly asked for
"-ffast-math". The whole meaning of the flag is to say "I want it fast,
even if you might lose some range or precision".

It's only a matter of defining the semantics.

There are other optimizations like that. Signed integer overflow is
undefined, so you can optimize integer operations in ways that do not give
the same results for all integers.

Alias analysis was added several years ago, even though it was _clearly_ a
"illegal optimization" from a traditional C standpoint. That one broke
tons of real programs. Again, it was a question of re-defining the
semantics, and allowing people to take advantage of them.

Already, the _default_ behaviour for gcc at least on x86 with floating
point has very little to do with IEEE. It's been discussed several times
how to fix it - and very little real work on it has been done for the last
several years. Why? Because it really doesn't matter for most people, and
the current work-arounds are too much of a performance hit.

So gcc _already_ has "better fast than correct to the last ULP in IEEE".
Live with it. Even by default. Your argument that it cannot do the same
even when the user asks for it is completely ludicrous.

Your continual insistence that it is "wrong" only shows that you have
built a cage around your brain.

Have you even _looked_ at what the gcc definition for "fast-math" are?
Look again:

  {"fast-math", &flag_fast_math, 1,
   "Improve FP speed by violating ANSI & IEEE rules" },

and realize that even something simple as "double" doesn't historcially
have an exact meaning in C (ie you can _not_ assume specific ranges or
specific IEEE rounding in traditional C - it was left up to the
implementor to do as best he could with what hardware was available).

That means that traditionally, you don't even know what overflows or not,
or what precision you get. It's historically "undefined". Which means that
the semantics are not clear.

Now, this is obviously why a lot of computational people disliked,
distrusted and didn't use C historically, preferring to use Fortran which
had more defined rules. And which is, of course, why C99 integrated the
math extensions that some people have been working on for a loong time.

But you have to realize that this is (a) fairly recent and (b) not
everybody needs or wants it.

And you should also realize that YOU are not the grand poobah that gets to
tell people what semantics they want.

[ Neither am I, of course, but I'm arguing for choice and freedom, while
  you're arguing for small-mindedness everywhere. ]

			Linus

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-31 18:15 dewar
  0 siblings, 0 replies; 119+ messages in thread
From: dewar @ 2001-07-31 18:15 UTC (permalink / raw)
  To: gdr, torvalds; +Cc: amylaar, aoliva, dewar, gcc, moshier, tprince

[ Neither am I, of course, but I'm arguing for choice and freedom, while
  you're arguing for small-mindedness everywhere. ]

Well that's not really the case. Because if your argument leads to
-ffast-math going overboard and going too far in giving what most people
regard as wrong results, then you HAVE taken away choice and freedom,
because -ffast-math is no longer usable for these people. That's why it
is perfectly reasonable to have a technical argument on a case by case
basis.

Trying to argue from a general ideological point of view (I favor freedom,
you do not, therfore you are wrong) is not particularly helpful in this
discussion.

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-31 18:20 dewar
  2001-07-31 18:50 ` Joern Rennecke
  0 siblings, 1 reply; 119+ messages in thread
From: dewar @ 2001-07-31 18:20 UTC (permalink / raw)
  To: amylaar, torvalds; +Cc: aoliva, dewar, gcc, gdr, moshier, tprince

<<The _definition_ of "-ffast-math" is "fast and loose". It says so. Try
this:
>>

I hardly thing "fast and loose" is a definition. We know perfectly well 
that there are transformations that Linus would find unacceptable, so no
one is in favor of unrestrained looseness. We are simply (trying to) have
a technical discussion about how loose is loose.

By the way, this entire discussion has been played out in the Ada context,
since in the Ada standard, the two modes are recognized at the standard
level, but the standard does not attempt to define fast and loose.

Note that in classical C, there was no statement about floating-point
accuracy at all. So from a definitional point of view, the compiler could
do anything, but people had some general agreement over what was or was
not appropriate. We are simply trying to reach that general agreement in
this -ffast-math context.

By the way, going back to an earlier post, Linus claims that GCC by default
allows extra precision. That's interesting, since it would mean that on
Power, optimization changes the results. I mentioned earlier that IBM decided
after careful study that this was not acceptable to their numeric users, so
it is interesting for gcc to take an opposite point of view (I suspect that
this was done without careful study :-)

Actually what I had in mind was 80-bit on x86, and I don't think gcc operates
that way by default?

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-31 18:23 dewar
  0 siblings, 0 replies; 119+ messages in thread
From: dewar @ 2001-07-31 18:23 UTC (permalink / raw)
  To: gdr, torvalds; +Cc: fjh, gcc, moshier, tprince

<<You want to bet?


I would take that bet ... think of all the spread sheet stuff in the world,
and all the image processing independent of games. The percentage of PC's
used to play 3D games is smaller than you think. Even most home computers
are used for email etc more than games.

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-07-31 19:10 dewar
  0 siblings, 0 replies; 119+ messages in thread
From: dewar @ 2001-07-31 19:10 UTC (permalink / raw)
  To: dewar, torvalds; +Cc: fjh, gcc, gdr, moshier, tprince

by the way, I find that flush to zero is a perfectly reasonable semantics
for a C compiler, especially on machines that do this naturally. For example,
on an Alpha, I would do this by default, and only do real denormals in -mieee
mode, because otherwise the cost is indeed high, same think for the R10K.
There is certainly nothing in the C or C++ standard requiring denormals.

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-08-01  3:02 Vincent Penquerc'h
  0 siblings, 0 replies; 119+ messages in thread
From: Vincent Penquerc'h @ 2001-08-01  3:02 UTC (permalink / raw)
  To: Gcc@Gcc. Gnu. Org

Hi,

I'd like to point out that it seems right to me to allow
non-perfect results when using -ffast-math, for the simple
reason that if we get two methods for computing FP operations
and one is faster, there is no point using the slowest (unless
you happen to use -Os and this one is smaller, etc, but this
is special case). Therefore, the very existence of such a
-ffast-math option acknowledges that its results are less
perfect than the default behavior, be it on average error,
domain, etc. This has been mentionned already, but I wanted
to state it clearly.
As long as the differences and gotchas between these two
modes is well documented, I see little reason to dismiss
-ffast-math on the grounds that it is less than perfect
compared to the default behavior.
Moreover, if the drawbacks of some optimizations are too
dangerous (very reduced domain, etc), a solution could be
(yes, I realize there are too many already) to allow the
user to specify how hard one wants the compiler to try and
speed up the code using -ffast-math-<N>, as in -O<N>, or
adding --fmath-allow-domain-shrinking, or -fmath-ignore-nans
or whatever could be used by the programmer to tell the
compiler what is important to him/her and what can be
sacrificed for sped.

-- 
Lyrian 

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-08-01  6:04 dewar
  2001-08-01  6:48 ` Vincent Penquerc'h
  2001-08-03  0:46 ` Eric W. Biederman
  0 siblings, 2 replies; 119+ messages in thread
From: dewar @ 2001-08-01  6:04 UTC (permalink / raw)
  To: gcc, vincent

<<As long as the differences and gotchas between these two
modes is well documented, I see little reason to dismiss
-ffast-math on the grounds that it is less than perfect
compared to the default behavior.
>>

No one is dismissing -ffast-math on any grounds, we are just discussing what
it should allow. Clearly everyone has a limit on what is reasonable, we need
to discuss to get a common agreement on reasonable limits. What we are talking
about here is just how tolerant we want to be. For example, can one tolerate
computing ** using log and exp? The result can be hundreds of ULP wrong, but
perhaps this is still tolerable IF it really saves time.

The point I keep plugging is that there is no point in providing less
accurate results unless you get some significant gain from the transformation.
For example, providing true -mieee results is really expensive on the Alpha,
so it is well worth compromising. Similarly, providing exact Java semantics
on the x86 is very expensive so it is worth compromising. 

Whether the specific transformation

 a/b/c => a/(b*c)

is worthwhile is unproven. In practice divisions are far less frequent than
multiplications and additions in any case, and the number of cases where
an expression with two divisions is written in the form a/b/c is small.
Furthermore, one would expect that since programmers obviously know that
division is in general slow, they would tend to rewrite as a/(b*c) anyway,
so perhaps it is in fact quite likely that many of the cases in which
a/b/c is written are deliberate.

Yes, Linus points to a case in which due to pipeline overlap the division
is faster, but compiler writers and systems programmers are always good
at finding theoretical cases. For me, the only justification for the
transformation above is if tests show that real programs noticably benefit.
This is a fairly stiff burden.

When I was actively working on SPITBOL, I solicited optimization suggestions
for SNOBOL-4 for the SPITBOL newsletter. I got many submissions. Then I put
into place a rule that said that you must be able to show one program that
speeds up by at least 5% (people were allowed to construct a program for the
purposes). The number of suggestions dropped to close to zero :-)

^ permalink raw reply	[flat|nested] 119+ messages in thread
* RE: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-08-01  6:52 dewar
  0 siblings, 0 replies; 119+ messages in thread
From: dewar @ 2001-08-01  6:52 UTC (permalink / raw)
  To: dewar, gcc, vincent

<<Such flags would be well suited to cover different optimizations
with different architectures (as the -O flags already trigger
different optimizations depending on the architecture).
>>

The trouble with proliferation of flags is that it gets complicated for
people to even learn about them, let alone figure out what they do or
do not want.

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law incombine)
@ 2001-08-01  9:58 Gabriel Dos_Reis
  2001-08-01 10:08 ` Wolfgang Bangerth
  0 siblings, 1 reply; 119+ messages in thread
From: Gabriel Dos_Reis @ 2001-08-01  9:58 UTC (permalink / raw)
  To: Wolfgang Bangerth; +Cc: gcc, tim

The transformations in question aren't documented.  So, those having
"valid reasons to request them" don't even know what there are
requesting with -fdubious-math.

Right now, we're discussing the ability of the compiler to transform
something like a/b/c into a/(b*c).  Some argue on the ground of speed
considerations, that the programmer would expect the compiler to do
that. But then, if they would have written that form in the first 
place.

The issue isn't about "freedom vs. no-freedom".

-- Gaby

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-08-01  9:59 dewar
  0 siblings, 0 replies; 119+ messages in thread
From: dewar @ 2001-08-01  9:59 UTC (permalink / raw)
  To: Theodore.Papadopoulo, gdr
  Cc: amylaar, aoliva, dewar, gcc, moshier, torvalds, tprince

<<Full safety is IMHO impossible with floating point... Only the
programmer knows the accuracy of its input data (Linus called that
noise) and the accuracy he expects on its result.
>>

Now that statement really cannot be let go by.

Of course it is possible to write safe and reliable floating-point code, but
in general this is only feasible if you have a sensible floating-point model
(e.g. IEEE), and you have a well understood mapping between the high level
language and the floating-point model.

THe viewpoint that floating-point is inherently sloppy is an unhelpful
and archaic one. It is the sort of thing that gives rise to idiotic advice
like "never use absolute equality on floating-point values".

Now of course a lot of code is written in a very much less safe and reliable
manner, and indeed, one can considerably relax the model much of the time.

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-08-01 10:04 dewar
  2001-08-01 10:28 ` Gabriel Dos_Reis
  0 siblings, 1 reply; 119+ messages in thread
From: dewar @ 2001-08-01 10:04 UTC (permalink / raw)
  To: Theodore.Papadopoulo, gdr
  Cc: amylaar, aoliva, dewar, gcc, moshier, torvalds, tprince

<<We're talking here of transformations which we do know able to
drastically change the results.
>>

Words like drastically are not particularly helpful in this discussion,
plain "change" would be just fine.

THe issue is whether the "change" is worth the speed up.

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-08-01 10:05 dewar
  0 siblings, 0 replies; 119+ messages in thread
From: dewar @ 2001-08-01 10:05 UTC (permalink / raw)
  To: Theodore.Papadopoulo, tim
  Cc: amylaar, aoliva, dewar, gcc, gdr, moshier, torvalds, tprince

<<Exactly ... and it isn't at all rare that the programmer knows exactly
how much noise is in the input data (little or none) and the how much
accuracy he expects in the result (as much as possible!).
>>

No, no, "as much as possible" is often quite wrong. There are stable algorithms
that become unstable with unexpected extra precision.

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-08-01 10:13 dewar
  0 siblings, 0 replies; 119+ messages in thread
From: dewar @ 2001-08-01 10:13 UTC (permalink / raw)
  To: tim, torvalds
  Cc: Theodore.Papadopoulo, amylaar, aoliva, dewar, gcc, gdr, moshier, tprince

a/2.0/2.0 will never yield zero if a is non-zero
a/4.0 may indeed yield zero if a is the smallest number

Indeed if one saw a/2.0/2.0 that's pretty peculiar, and it would not be
unreasonable to guess that the properly of not yielding zero was being
exploited :-)

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-08-01 10:38 dewar
  0 siblings, 0 replies; 119+ messages in thread
From: dewar @ 2001-08-01 10:38 UTC (permalink / raw)
  To: dewar, torvalds
  Cc: Theodore.Papadopoulo, amylaar, aoliva, gcc, gdr, moshier, tim, tprince

<<The above rules, btw, only apply for exact IEEE math, and on a number of
machines you _will_ see zero for the first one.
>>

Hello! it was you who restricted the discussion to the x86. If we are allowed
to go to other machines, we have lots more examples up our sleeves :-)

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-08-01 10:39 dewar
  0 siblings, 0 replies; 119+ messages in thread
From: dewar @ 2001-08-01 10:39 UTC (permalink / raw)
  To: gdosreis, torvalds
  Cc: Theodore.Papadopoulo, amylaar, aoliva, dewar, gcc, gdr, moshier,
	tim, tprince

>>Your arguments are getting weaker and weaker, and dishonest to boot.


I really see no reason for ad hominem remarks, I am now killing the thread,
when things deteriorate that far, no further useful exchange is likely.

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-08-01 12:06 Phil Edwards
  0 siblings, 0 replies; 119+ messages in thread
From: Phil Edwards @ 2001-08-01 12:06 UTC (permalink / raw)
  To: Linus Torvalds
  Cc: dewar, tim, Theodore.Papadopoulo, amylaar, aoliva, gcc, gdr,
	moshier, tprince

  To contribute, or not to contribute, that is the question
  Whether it is better in the long run
  To continue to post messages to this thread
  Which goeth round in circles, further and further off-topic.
  Or to stop posting to it
  And allow silly arguments to go unchallenged
  Or on the other hand to stop reading it altogether
  And by ignoring it, end it.

(apologies to Andrew Rilstone)

I'm just sort of wondering if there's going to be a patch posted here or on
gcc-patches, something in the way of a concrete suggestion.  Thus far there's
been some name calling, lots of heat, very little light.  Maybe someone
would care to summarize the topic (e.g., are we restricting the discussion
to a particular platform, what exactly is the source of disagreement, etc)?

If I recall my flamewar protocol correctly, the next step is ad hominem
attacks.  Those are always entertaining.


Phil
...I wrote "exactly" in a FP thread... that was a mistake...

-- 
Would I had phrases that are not known, utterances that are strange, in
new language that has not been used, free from repetition, not an utterance
which has grown stale, which men of old have spoken.
                                     - anonymous Egyptian scribe, c.1700 BC

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-08-01 19:04 Carlo Wood
  0 siblings, 0 replies; 119+ messages in thread
From: Carlo Wood @ 2001-08-01 19:04 UTC (permalink / raw)
  To: Linus Torvalds
  Cc: Gabriel Dos Reis, Theodore Papadopoulo, dewar, amylaar, aoliva,
	gcc, moshier, tprince

When I see a thread as long as this one, I always think:
here is some inefficient "communication" going on :p.
Allow me to give my 0.02.

> > Changing 0.125 to 0.0 is a dramatic change.

Some may think that dramatic changes are acceptable.

When an optimization results in

99%   of practical cases it is faster,
0.99% of practical cases it is slower and
0.01% of the cases it results in a dramatic change;

then that might be acceptable because the number
of cases were the difference is not dramatic and also
not close to the normal FP accuracy is 0%.

A dramatic change will be noted.  Developers have to watch
for overflow and underflow already anyway.

My definition of what is unacceptable is when an optimization
causes possibly errors for example in the 32th bit, while the
expected accuracy is 64 bit...

Imho, this difference (dramatic versus subtile) is would you
tried to discuss, but failed to make clear.

-- 
Carlo Wood <carlo@alinoe.com>

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in combine)
@ 2001-08-02  3:37 Vincent Penquerc'h
  0 siblings, 0 replies; 119+ messages in thread
From: Vincent Penquerc'h @ 2001-08-02  3:37 UTC (permalink / raw)
  To: Gcc@Gcc. Gnu. Org

May I suggest breaking this into two separate topics, in an
attempt to avoid attribution of some arguments to the wrong
category.
I think everyone is in agreement that -ffast-math should
not be enabled by default by -O. So I propose:

1: Should -ffast-math include transformations that can
   under certain circumstances yield substantially different
   results than the original code ?

2: What transformations should be allowed when -ffast-math
   is *not* specified (eg the a/b/c example).

-- 
Lyrian 

^ permalink raw reply	[flat|nested] 119+ messages in thread
* Re: What is acceptable for -ffast-math? (Was: associative law in    combine)
@ 2001-08-03 14:48 dewar
  0 siblings, 0 replies; 119+ messages in thread
From: dewar @ 2001-08-03 14:48 UTC (permalink / raw)
  To: gdr, nick; +Cc: amylaar, aoliva, dewar, gcc, moshier, torvalds, tprince

Stephen says

> I'm writing agricultural simulations[1], and I'd be happy to get
> transformations enabled with -ffast-math that would be safe with real
> numbers, but not with floating point numbers.

Well that's at least a coherent position, but I am afraid that no one would
really accept this criterion. for example, this would allow replacing:

  (a*b) / (c*d)

by

  (a * b * 10.0**1000) / (c * d * 10.0**10000)

and introduce overflow where none was expected, and if you think such
a transformation is just obviously unreasonable consider:

   x := b * 10.0**1000;
   y := d * 10.0**1000;

   now compute (a*b) / (c*d)

and to improve efficiency transform this to (a*x)/(c*y), since x and y
happen to be available in registers

Now you might be inclined to say, OK, "transformations that would be safe
with real numbers providing they do not introduce overflow".

But you don't mean that, because many of the transformations we have
discussed introduce overflow in "marginal" cases, and now we are back in
undefined territory.

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

end of thread, other threads:[~2001-08-03 14:48 UTC | newest]

Thread overview: 119+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20010716224423.F29145@atrey.karlin.mff.cuni.cz>
     [not found] ` <3B547DF4.7F39BF61@moene.indiv.nluug.nl>
     [not found]   ` <20010717200629.E5166@atrey.karlin.mff.cuni.cz>
     [not found]     ` <3B5489F1.54DE59CE@moene.indiv.nluug.nl>
     [not found]       ` <20010717205837.I5166@atrey.karlin.mff.cuni.cz>
     [not found]         ` <3B548CBF.DDD59F97@moene.indiv.nluug.nl>
     [not found]           ` <20010717211516.N5166@atrey.karlin.mff.cuni.cz>
     [not found]             ` <3B548FAA.E174E765@moene.indiv.nluug.nl>
2001-07-17 12:23               ` associative law in combine Jan Hubicka
2001-07-17 15:41                 ` Dima Volodin
2001-07-17 15:59                 ` Joern Rennecke
2001-07-18  1:01                   ` Toon Moene
2001-07-18  1:47                     ` What is acceptable for -ffast-math? (Was: associative law in combine) Jan Hubicka
2001-07-28 23:04                       ` Tim Prince
2001-07-29  6:33                         ` Jan Hubicka
2001-07-29 10:18                           ` Tim Prince
2001-07-29 10:26                             ` Jan Hubicka
2001-07-29 12:11                               ` Tim Prince
2001-07-29 12:17                                 ` Jan Hubicka
2001-07-29 10:50                         ` Linus Torvalds
2001-07-18 11:30                     ` associative law in combine Joern Rennecke
2001-07-29 12:52 * Re: What is acceptable for -ffast-math? (Was: associative lawin combine) Linus Torvalds
2001-07-29 14:03 ` What is acceptable for -ffast-math? (Was: associative law incombine) Stephen L Moshier
2001-07-29 21:17   ` What is acceptable for -ffast-math? (Was: associative law in combine) Fergus Henderson
2001-07-30  0:23     ` Gabriel Dos Reis
2001-07-29 14:22 dewar
2001-07-29 21:33 What is acceptable for -ffast-math? (Was: associative law incombine) Linus Torvalds
2001-07-30 14:43 ` What is acceptable for -ffast-math? (Was: associative law in combine) Alexandre Oliva
2001-07-30 15:45   ` Neil Booth
2001-07-30 16:03     ` Alexandre Oliva
2001-07-30 16:11       ` Neil Booth
2001-07-30 16:28         ` Alexandre Oliva
2001-07-30 19:08   ` Joern Rennecke
2001-07-30 19:22     ` Alexandre Oliva
2001-07-30 19:29       ` Gabriel Dos Reis
2001-07-30 19:34         ` Alexandre Oliva
2001-07-30 19:54           ` Gabriel Dos Reis
2001-07-30 19:27     ` Gabriel Dos Reis
2001-07-30  5:57 dewar
2001-07-30  6:00 dewar
2001-07-30 13:08 ` Toon Moene
2001-07-30  6:01 dewar
2001-07-30  6:53 ` Tim Hollebeek
2001-07-30  6:14 dewar
2001-07-30  8:30 ` Kevin Handy
2001-07-30  8:59 mike stump
2001-07-30 11:37 What is acceptable for -ffast-math? (Was: associative law incombine) Linus Torvalds
2001-07-30 11:53 ` What is acceptable for -ffast-math? (Was: associative law in combine) Gabriel Dos Reis
2001-07-30 18:40   ` Olivier Galibert
2001-07-30 19:06     ` Gabriel Dos Reis
2001-07-31  1:35   ` Linus Torvalds
2001-07-31  2:04     ` Gabriel Dos Reis
2001-07-31  2:35       ` Olivier Galibert
2001-07-31  2:58         ` Gabriel Dos Reis
2001-07-31 18:10       ` Linus Torvalds
2001-07-30 11:52 dewar
2001-07-30 12:26 dewar
2001-07-30 13:10 dewar
2001-07-30 15:29 dewar
2001-07-30 15:39 ` Toon Moene
2001-07-30 16:11 dewar
2001-07-30 16:29 ` Alexandre Oliva
2001-07-31  8:13   ` Kevin Handy
2001-07-30 18:00 dewar
2001-07-30 18:25 ` Joe Buck
2001-07-30 18:02 dewar
2001-07-30 18:08 dewar
2001-07-30 18:38 dewar
2001-07-30 18:39 dewar
2001-07-30 19:46 dewar
2001-07-30 20:00 ` Gabriel Dos Reis
2001-07-30 20:20   ` Alexandre Oliva
2001-07-30 20:25     ` Gabriel Dos Reis
2001-07-30 20:54 dewar
2001-07-30 21:11 ` Gabriel Dos Reis
2001-07-30 21:39 ` Joern Rennecke
2001-07-30 21:13 dewar
2001-07-30 21:34 ` Gabriel Dos Reis
2001-07-30 21:43   ` Joern Rennecke
2001-07-30 21:53     ` Gabriel Dos Reis
2001-08-03  7:12       ` Nick Ing-Simmons
2001-08-01  8:55   ` Theodore Papadopoulo
2001-08-01  9:15     ` Gabriel Dos Reis
2001-08-01 11:21       ` Theodore Papadopoulo
2001-08-01 11:44         ` Gabriel Dos Reis
2001-08-01  9:24     ` Tim Hollebeek
2001-08-01  9:54       ` What is acceptable for -ffast-math? (Was: associative law incombine) Linus Torvalds
2001-08-01 10:26         ` Gabriel Dos_Reis
2001-08-01 11:13           ` What is acceptable for -ffast-math? (Was: associative law in combine) Alexandre Oliva
2001-08-01 11:36             ` Gabriel Dos Reis
2001-08-01 12:07               ` Alexandre Oliva
2001-08-01 13:21                 ` Gabriel Dos_Reis
2001-08-01 14:20                   ` Toon Moene
2001-07-31  7:26 dewar
2001-07-31 15:57 ` Toon Moene
2001-07-31 21:55   ` Tim Prince
2001-08-03  6:12 ` Per Abrahamsen
2001-07-31  7:59 mike stump
2001-07-31  8:19 mike stump
2001-07-31  8:35 dewar
2001-07-31  8:36 dewar
2001-07-31  8:36 mike stump
2001-07-31  8:37 dewar
2001-07-31  9:22 mike stump
2001-07-31 16:38 dewar
2001-07-31 17:37 dewar
2001-07-31 18:12 What is acceptable for -ffast-math? (Was: associative law incombine) Linus Torvalds
2001-07-31 20:55 ` What is acceptable for -ffast-math? (Was: associative law in combine) Gabriel Dos Reis
2001-07-31 18:15 dewar
2001-07-31 18:20 dewar
2001-07-31 18:50 ` Joern Rennecke
2001-07-31 21:27   ` Tim Prince
2001-07-31 18:23 dewar
2001-07-31 19:10 dewar
2001-08-01  3:02 Vincent Penquerc'h
2001-08-01  6:04 dewar
2001-08-01  6:48 ` Vincent Penquerc'h
2001-08-03  0:46 ` Eric W. Biederman
2001-08-01  6:52 dewar
2001-08-01  9:58 What is acceptable for -ffast-math? (Was: associative law incombine) Gabriel Dos_Reis
2001-08-01 10:08 ` Wolfgang Bangerth
2001-08-01 11:12   ` Gabriel Dos_Reis
2001-08-01 11:27     ` What is acceptable for -ffast-math? (Was: associative law in combine) Theodore Papadopoulo
2001-08-01 11:47       ` Gabriel Dos_Reis
2001-08-03  7:32         ` Nick Ing-Simmons
2001-08-03  6:01     ` Per Abrahamsen
2001-08-01  9:59 dewar
2001-08-01 10:04 dewar
2001-08-01 10:28 ` Gabriel Dos_Reis
2001-08-01 10:05 dewar
2001-08-01 10:13 dewar
2001-08-01 10:38 dewar
2001-08-01 10:39 dewar
2001-08-01 12:06 Phil Edwards
2001-08-01 19:04 Carlo Wood
2001-08-02  3:37 Vincent Penquerc'h
2001-08-03 14:48 dewar

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