From: Joachim Wesner <joachim.wesner@frankfurt.netsurf.de>
To: axp-list@redhat.com
Subject: Re: Floating point problems
Date: Mon, 30 Nov 1998 18:06:33 +0100 [thread overview]
Message-ID: <3662D099.2CA026FA@frankfurt.netsurf.de> (raw)
In-Reply-To: 199811300816.IAA24959@ppp393.ts.rz.tu-bs.de
y0000218@rzsrv2.rz.tu-bs.de wrote:
>
> Heinz Ulrich Stille <Heinz.Ulrich.Stille@mbi-os.de> wrote:
> : Hi!
>
> : What's wrong with the following program?
>
> : main() {
> : double x = 1.0;
> : x *= 1e-310;
> : }
>
> : This results in a floating point exception on my Ruffian (for values
> : somwhere between 1e-300 and 1e-325, independent of kernel and compiler),
> : but not on any intel. The assembler output seems to indicate that the
> : two values are just loaded into registers and multiplied.
> : What is going on?
>
and Alvin Starr wrote:
>try the following
> main() {
> double x = 1.0;
> double y = 1e-155;
> x *=(1e-155*y);
> printf("%e %e %e\n",(1e-155*y),x,1e-310);
> }
>
>this generates the same result as the original program but will not >cause a trap.
This starts to look confusing........
However, I studied the Alpha Architecture Handbook again and - hopefully
- begin to understand what is happening und a way to find the real cause
of this and a way out
We have to distinguish 2 cases here:
1) Denormals as OPERANDS to a fp op
2) Denormals as RESULT of an fp op
to 1)
The Alpha hardware definitely cannot handle denormals as input to fp
ops. This has to be fixed in software, by the trap handler AND for this
to REALLY work in REAL code, due to the (damned !!) imprecise trap
handling will require extensive use of trapb instructions, that
basically serialize the fpu and make a scalar machine out of a pipelined
one, basically what -mieee does, it slows down the code a lot.
Basically, the idea of the dec engineers was that in critical code you
might be able to identify the few ops that are really critical and put a
trapb only here, but it seems that neither gcc/egcs or even the DEC cc
is smart enough to do this efiiciently, instead, you have to clutter
your code with those instructions.
to 2)
Here the DEC documentation is a bit misleading, it seems I was wrong !
The fp ops have an additional qualifier /U that determines the underflow
behavior. HOWEVER, at a first look (what I had done till now,, it seems,
sorry), you only have two possibilities
- either you do not use /U, then the documentation says any fp op will
trap with all the above problems when a denormal is a result of the op
- or you use /U, then no trap will happen, but also, the denormal will
not be replaced by a true zero and the code will trap via 1) the next
time the result is used in further calculations.
HOWEVER, reading on reveals a bit more, 1st, there is an additional
underflow trap disable bit in the fp control register, and 2nd, when the
trap happens, the setting by zero that I missed, will already happen by
the hardware !!! Huh, why not with /U but instead vice-versa ???
This seems, that the way to go would be "enable" the underflow trap
(with replacing by zero) by NOT using /u (as is usually already done),
but later again DISABLE that very same trap in the fp control register.
Yet, and it seems, LINUX AXP is already configured to do just this !!
This would explain the two codes :
In the code with 1e-310, which already IS a denormal 1) applies and you
get a trap and as long you did not compile the stuff with trapb
instructions, even a probably better trap handler could not fix it.
BTW, the very same should happen also on DEC Unix, when reading 1e-310
from a binary file or if you code it as a hex number (Somebody could try
that ?). DEC CC seems to try to avoid such by never allowing you to
input such numbers in a regular way, what explains the warning in the
DEC output and that it did not print 1e-310 as last result.
In the code with 1e-155**2 instead, all things seems to go as they
should, 2) applies.
BUT, so why do still many people see so many problems with denormals
under AXP Linux ?? When you never input a denormal and all fp op never
return a denormal, NO problems should happen !!
I have the impression that might be the function routines that are not
written to NOT return denormals, for example exp of a large negative
number could return a denormal. This should be changed.
Incidentially - <Advertisement on> the new version of libffm 0.28 is out
that now fully handles illegal arguments and results. It should never
return a denormal (ONLY in the linear regions for small args of sin,
tan, asin, atan, where the arg is simply returned as is, but then you
already HAD the problem earlier on) and anybody with the above problems
should give it a try if it fixes the probs. As is does not yet contain
ALL libm functions, it still could happen, in such case could somebody
try to trace which function would be missing to be fixed/replaced first.
Actually, I HAD been wondering when designing libffm, if exp needs to
return a denormal for very small arguments, and now I see, it was a good
decision (to be lazy and) not to add that !! <Advertisement OFF>
Hope this turns out to be right and helps !!
If you still find a code that does not seem to fit to the above
explanation, please send it to me.
Joachim
next prev parent reply other threads:[~1998-11-30 17:09 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
1998-11-30 8:16 Floating point problems y0000218
1998-11-30 9:08 ` Carles Perello
1998-11-30 9:53 ` Joachim Wesner
1998-11-30 14:22 ` Alvin Starr
1998-11-30 13:27 ` krebs
1998-11-30 17:06 ` Joachim Wesner [this message]
1998-12-03 0:48 ` Richard Henderson
1998-12-03 16:59 ` Greg Lindahl
1998-12-03 19:35 ` Joachim Wesner
1998-12-03 20:04 ` Richard Henderson
1998-12-03 20:16 ` Greg Lindahl
1998-12-03 23:44 ` Joachim Wesner
1998-12-01 8:42 ` Jochen Kuepper
-- strict thread matches above, loose matches on Subject: below --
1998-11-30 10:24 y0000218
1998-11-30 13:30 ` Alvin Starr
1998-12-03 0:48 ` Richard Henderson
1998-11-28 13:15 Heinz Ulrich Stille
1998-11-28 14:24 ` Johannes G.D. Hausmann
1998-11-28 13:58 ` Heinz Ulrich Stille
1998-11-28 14:48 ` Stefan Schroepfer
1998-11-28 14:57 ` Heinz Ulrich Stille
1998-11-29 13:15 ` Johannes G.D. Hausmann
1998-11-29 13:02 ` Heinz Ulrich Stille
1998-11-29 18:01 ` B. James Phillippe
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox