public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: hjstein@bfr.co.il (Harvey J. Stein)
To: moshier@mediaone.net
Cc: hjstein@bfr.co.il
Subject: Re: FWD: FLOATING-POINT CONSISTENCY, -FFLOAT-STORE, AND X86
Date: Mon, 14 Dec 1998 01:52:00 -0000	[thread overview]
Message-ID: <m2af0r1278.fsf@blinky.bfr.co.il> (raw)
In-Reply-To: <Pine.LNX.4.05.9812132106250.30706-100000@moshier.ne.mediaone.net>

Stephen L Moshier <moshier@mediaone.net> writes:

 > The extra-precise registers are supposed to be a feature, not a bug.
 > Neither the computer language nor the compiler has a way to say
 > "this is an extra-precise register" so there is some inconvenience
 > using the feature.  It can't be made consistent.  The harder you look,
 > the more contradictions you find.
 > 
 > If you don't believe that, the alternative that makes sense is to
 > ask for straight IEEE behavior.  You can't get IEEE behavior without
 > setting the coprocessor rounding precision.  After you set the
 > rounding precision, all the other bugs disappear except for a rare
 > hardware bug or two.  The hardware bugs are dealt with by a trap
 > handler in the operating system, in the time honored fashion
 > of Intel, Borland, or Microsoft.
 > 
 > So there could be a straightforward plan to make x86 obey IEEE.
 > It's doubtful there could be a workable plan to fix the extra-precise
 > registers; anyway, they are a feature, no fix is needed!

I agree wholeheartedly.

Reasonable floating point code should expect that reordering
operations will produce slightly different results due to round off
error, and should be tolerant of the optimizer doing such.  Especially
given how little control the programmer has over exactly how
computations are ordered.

What floating point code *doesn't* expect is that there are multiple
underlying representations with different precisions, and that
identical computations might yield different results depending on
which representation happens to get used.  For example, most code will
expect that after:

   x = 1.0;
   y = 1.0;

that f(x) and f(y) will always be equal (for an f with no side
effects).

It will also expect that after

   x = 1.0/3.0;
   y = 1.0/3.0;

that x and y will always be equal.

Both of these assumptions break on ix86 because of the extended
precision FPU registers.

These are very reasonable things to expect, and much code breaks when
they're not satisfied.  One would think that this can be fixed by the
programmer with careful use of tolerances in comparisons, but this is
not the case.  Even if *very* carefully (i.e. - with knowledge of the
precision of the underlying representations), it will often just cause
discontinuities which create worse problems elsewhere.  But, it
*can't* be done carefully, because values in registers and values in
memory can accumulate differently, thus affecting more than just the
excess precision bits.

C and Fortran give little control over order of operations and
virtually no control over where such variances in precision might crop
up.  The compiler is free to use registers as it sees fit, and the
programmer has no control over it aside from recourse to assembler
programming.

So, well behaved, well written floating point code might behave well
on motorola, sparc & alpha CPUs but fail miserably on ix86 CPUs.  In
particular, the problems that have been discussed don't have to do
with compiler reordering & optimization.  They only have to do with
the fact that registers can contain excess precision, which AFAIK,
only happens on the ix86 CPUs.

As was pointed out, -ffloat-store only goes part way to fixing the
problem, because it doesn't affect compiler generated temporaries.  It
also makes the code slower.

I think the option of setting the fpu precision to 53 bits is a great
solution.  Even the Intel assember manual says:

   The double precision and single precision settings reduce the size
   of the significand to 53 and 24 bits, respectively.  These settings
   are providied to support the IEEE standard and to allow exact
   replication of calculations which were done using the lower
   precision data types.

I think Intel's comments about how great it is to use the extra
precision is just propaganda.  I think that it maybe helps *slightly*
with badly written code.  But it can make it impossible to write good
code.  Maybe it was just a poorly thought out clever idea for fixing
bad code.  Maybe it's to avoid having to have single precision
and mixed single/double register instructions, coupled with lots of
marketing propaganda to prevent seeing this shortcomming.

I'd think the only drawback to using the FPU in double precision mode
instead of extended precision mode would be for carefully hand
optimized numerical routines in assembler which can possibly do things
quicker by utilizing the 64 bit extended precision mode.  For such
code the programmer could explicitly muck with the FPU control word -
save its value, set it to extended precision, do the computations &
restore the value.  The OS will have to save and restore the FPU
control word when context switching, but it has to do this now anyway
& the ix86 FPU state save & restore instructions include the FPU
control word.

Of course, it's also *not* going to solve problems with single
precision unless the compiler is careful to not mix single precision &
double precision operations on the FPU stack & to always keep the FPU
control word set to the appropriate value.  It'd seem, actually, that
this would be the only way to get IEEE conformance with code that uses
both single and double precision values.  Fortunately, it seems that
people are tending to just use double precision, so maybe this can be
put off for later.

-- 
Harvey J. Stein
BFM Financial Research
hjstein@bfr.co.il

  reply	other threads:[~1998-12-14  1:52 UTC|newest]

Thread overview: 65+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-12-13 18:23 Stephen L Moshier
1998-12-14  1:52 ` Harvey J. Stein [this message]
1998-12-14 14:56   ` Edward Jason Riedy
1998-12-14 17:20     ` Joe Buck
1998-12-14 18:51       ` Edward Jason Riedy
1998-12-14 21:54         ` Craig Burley
1998-12-15 14:31           ` Edward Jason Riedy
1998-12-15 17:11         ` Jamie Lokier
1998-12-16  0:26           ` Harvey J. Stein
1998-12-16  9:33             ` Craig Burley
1998-12-16 12:18               ` Harvey J. Stein
1998-12-16  9:38           ` Craig Burley
1998-12-16 12:25           ` Marc Lehmann
1998-12-16 12:50             ` Tim Hollebeek
1998-12-16 13:04               ` Harvey J. Stein
1998-12-16 14:01               ` Marc Lehmann
1998-12-17 11:26                 ` Dave Love
1998-12-17 15:06                   ` Marc Lehmann
1998-12-18 12:50                     ` Dave Love
1998-12-19 14:09                       ` Marc Lehmann
1998-12-20 11:28                         ` Dave Love
1998-12-20 11:24               ` Dave Love
1998-12-16 23:11           ` Joern Rennecke
1998-12-17  6:07             ` Jamie Lokier
1998-12-14 22:54       ` Craig Burley
1998-12-15  1:45 Geert Bosch
1998-12-15  3:34 ` Harvey J. Stein
1998-12-16 10:36   ` Craig Burley
1998-12-16 12:47     ` Harvey J. Stein
1998-12-17 10:22       ` Craig Burley
1998-12-17 14:54         ` Marc Lehmann
1998-12-19  0:27           ` Craig Burley
1998-12-19  5:06             ` Stephen L Moshier
1998-12-15  6:43 ` Stephen L Moshier
1998-12-16 10:14   ` Craig Burley
1998-12-15  9:29 ` Joe Buck
1998-12-15 10:14   ` Jeffrey A Law
1998-12-16  8:32     ` Sylvain Pion
1998-12-16  9:20       ` Craig Burley
1998-12-15 12:10 Geert Bosch
1998-12-15 13:09 ` Jeffrey A Law
1998-12-15 12:24 Toon Moene
1998-12-15 12:55 ` Joe Buck
1998-12-15 15:05 ` Edward Jason Riedy
1998-12-16 10:05 ` Craig Burley
1998-12-16 13:52 Toon Moene
1998-12-17 10:06 ` Craig Burley
1998-12-17 12:16   ` Harvey J. Stein
1998-12-19  0:29     ` Craig Burley
1998-12-17 11:20 ` Dave Love
1998-12-17 11:27 Brad Lucier
1998-12-17 14:51 ` Marc Lehmann
1998-12-19  0:17   ` Craig Burley
1998-12-19  6:42     ` Emil Hallin
1998-12-19 14:26       ` Dave Love
1998-12-17 14:37 tprince
1998-12-17 15:15 ` Stephen L Moshier
1998-12-17 14:38 Toon Moene
1998-12-17 15:30 ` Harvey J. Stein
1998-12-18  1:54   ` Toon Moene
1998-12-18  3:05     ` Harvey J. Stein
1998-12-18  9:01       ` Toon Moene
1998-12-18 15:59       ` Richard Henderson
1998-12-18 13:26   ` Marc Lehmann
1998-12-18 12:50 ` Dave Love

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=m2af0r1278.fsf@blinky.bfr.co.il \
    --to=hjstein@bfr.co.il \
    --cc=moshier@mediaone.net \
    /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).