From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (qmail 16620 invoked from network); 30 Nov 1998 17:09:36 -0000 Received: from mail.redhat.com (199.183.24.239) by lists.redhat.com with SMTP; 30 Nov 1998 17:09:35 -0000 Received: from beach.frankfurt.netsurf.de (root@beach.frankfurt.netsurf.de [194.64.181.2]) by mail.redhat.com (8.8.7/8.8.7) with ESMTP id MAA04544 for ; Mon, 30 Nov 1998 12:07:25 -0500 Received: from Othello (deck-178.frankfurt.netsurf.de [194.64.181.210]) by beach.frankfurt.netsurf.de (8.8.5/8.8.5) with SMTP id SAA06477; Mon, 30 Nov 1998 18:07:22 +0100 (MET) Sender: fabwes@beach.frankfurt.netsurf.de Message-ID: <3662D099.2CA026FA@frankfurt.netsurf.de> Date: Mon, 30 Nov 1998 18:06:33 +0100 From: Joachim Wesner X-Mailer: Mozilla 3.04Gold (X11; I; Linux 2.0.33 i586) MIME-Version: 1.0 To: axp-list@redhat.com Subject: Re: Floating point problems References: <199811300816.IAA24959@ppp393.ts.rz.tu-bs.de> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit y0000218@rzsrv2.rz.tu-bs.de wrote: > > Heinz Ulrich Stille 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 - 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 !! 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