Skip to contentSkip to conversation

math broken on mips

DISCUSSION Zhang Fuxin 2 participants 2 messages Feb 20, 2002 MIPS

lore
Share & tools
lore
#0 lore raw

hi,
a quick example:
main()
{
float t,zero=0.0;
t = 0.0/0.0;
printf("t=%08lx\n",*(unsigned long*)&t); (0x7fc00000) t = zero/zero;
printf("t=%08lx\n",*(unsigned long*)&t); (0x7fbfffff)
}

you should see different output,because the first one is optimized by gcc to a QNaN,but it's signalling for MIPS.

在 2002-02-19 16:37:00 you wrote:

Quoted 9 lines

Hello Zhang,

I am going to test your reported bugs on the current SDE compiler.

Do you have some test example for the first problem (Nan handling)? If you have found it in the sources, and that source is actually being used by the MIPS compiler generated, it must be possible to provide a small example showing wrong behaviour from the compiler?

Quoted 95 lines

/Hartvig

Quoted 91 lines · Zhang Fuxin

hi,
I find that you are asking only gcc related parts,that is related less,
here they are(from posts on linux-mips,i cc them to the list in case it is useful,sorry if it brings you inconvenience):

1.about NaN handling
-----------------begin of the first problem-------------------------------- I am sorry but it seems i can't fix this without ugly changes. since i am not familiar with gcc code, i decide to leave it to you, but provide some information instead.

In gcc there are 3 spaces where the NaN handling is assumed the
Intel way.

1. gcc/real.c (the most important one)
here the author seems to have known the NaN pattern problem,so
he leaves a interface macro for defining non intel NaN patterns:
(comment of function "make_nan()",at about line 6219)

/* Output a binary NaN bit pattern in the target machine's format. */

/* If special NaN bit patterns are required, define them in tm.h
as arrays of unsigned 16-bit shorts. Otherwise, use the default patterns here. */

I have read through this file and decided that the follow defined should
be enough for mips:

/* NaN pattern,mips QNAN & SNAN is different from intel's
* DFMODE_NAN and SFMODE_NAN is used in real.c */

#define DFMODE_NAN \
        unsigned short DFbignan[4] = {0x7ff7, 0xffff, 0xffff, 0xffff}; \
        unsigned short DFlittlenan[4] = {0xffff, 0xffff, 0xffff, 0xfff7}
#define SFMODE_NAN \
        unsigned short SFbignan[2] = {0x7fbf, 0xffff}; \
        unsigned short SFlittlenan[2] = {0xffff, 0xffbf}

But the problem is where to put them:(. Obviously it is target specified
definitions and should be in config/mips/. Documents say tm.h is a symbol link and included in config.h,but it is no long true.If i add them to xm-mips.h then for native compilation it is ok but it fails for cross-compile.

   2.gcc/reg-stack.c (H.J. tell me it is not used on mips)
     There is a hardcoded QNaN used around line 477:
      nan = gen_lowpart (SFmode, GEN_INT (0x7fc00000));
     I sugest defining a macro QNAN_HAS_1ST_FRACBIT_CLEARED for mips and change
     it to,just don't know where to put it:
      #ifndef QNAN_HAS_1ST_FRACBIT_CLEARED
         nan = gen_lowpart (SFmode, GEN_INT (0x7fc00000));
      #else
         nan = gen_lowpart (SFmode, GEN_INT (0x7fbfffff));
      #endif

3. config/fp-bit.c (H.J. said it is not mean to target specified) this is for machine having no fpu hardware.
again i susgest define QNAN_HAS_1ST_FRACBIT_CLEARED and then apply this patch:

190d189
< #ifndef QNAN_HAS_1ST_FRACBIT_CLEARED
192,195d190
< #else
< fraction &= ~QUIET_NAN;
< #endif
<
379,380d373
<
< #ifndef QNAN_HAS_1ST_FRACBIT_CLEARED
382,384d374
< #else
< if (!(fraction & QUIET_NAN))
< #endif

在 2002-02-03 22:54:00 you wrote:

Quoted 16 lines · Zhang Fuxin
Quoted 8 lines

hi,

Gcc (2.96 20000731,H.J.LU's rh port for mips) think 0x7fc00000 is QNaN and optimize 0.0/0.0 as 0x7fc00000 for single precision ops,while for my cpu (maybe most mips cpu) is a SNaN. R4k user's manual and "See Mips Run" both
say so.And experiments confirm this.

Should we correct it?

Yes. Do you have a patch?

Thanks.

H.J.

Regards
Zhang Fuxin
[email protected]

#1 lore raw

Zhang,

Quoted 10 lines

a quick example:
main()

{
   float t,zero=0.0;
   t = 0.0/0.0;
   printf("t=%08lx\n",*(unsigned long*)&t); (0x7fc00000)
   t = zero/zero;
   printf("t=%08lx\n",*(unsigned long*)&t); (0x7fbfffff)
}
Quoted 2 lines

you should see different output,because the first one is optimized by gcc to a QNaN,but it's signalling for MIPS.

On my RedHat 7.1 x86 machine you get something of the same kind. The constant expression ("0.0/0.0") is always computed at compile time, but the "zero/zero" is only resolved as a constant once you turn on the optimiser.

So I see two different values printed without optimisation, but the same value twice with -O2.

(Note that the 0.0/0.0 will be computed as a double, while zero/zero will be computed as a float - but I don't think that makes any difference in this case).

A floating point purist would say that the compiler should never try to propagate floating point constants where the calculation might produce any exception or exceptional result.

That's standards-correct, but unacceptable in practice. If I write

  float pi = 3.1415926535897932384626433832795029;

the decimal->binary conversion will be inexact and might produce an exception (if that flag is set: it almost never is, but the compiler doesn't know that). But I'll be annoyed if my compiler doesn't generate a binary floating point value for 'pi' at compile time.

Once you open this door and allow the compiler to work on some floating point constants, life gets tougher. It might be a good rule that if an expression produces an exceptional value (NaN, infinity), you leave it to run-time... but it may mean some ugly back-tracking.

In this case gcc for x86 (and MIPS) has had no inhibitions: it's just seen a constant expression and reduced it. With no way to predict the FP mode settings which will be in effect it has no way of knowing what kind of exceptional result to produce in this case.

The compiler is now kind of outside the specs: but it's still better style to produce a signalling NaN (anyone who wanted their NaNs quiet probably has exceptions turned off so won't know the difference; anyone who didn't want their NaNs quiet should probably get an exception). But frankly, it's not important.

In expecting floating point maths to "just work" in corner cases, you're expecting too much. If you want to be appropriately frightened, here's a paper I came across fairly recently - David Goldberg's "What Every Computer Scientist should Know about Floating Point":

http://cch.loria.fr/documentation/IEEE754/ACM/goldberg.pdf

--
Dominic Sweetman
Algorithmics Ltd
The Fruit Farm, Ely Road, Chittering, CAMBS CB5 9PH, ENGLAND phone +44 1223 706200/fax +44 1223 706250/direct +44 1223 706205
http://www.algor.co.uk