Unreachable statmnt reached using gcc2.95 under Linux-Mdk

Martin v. Loewis martin@loewis.home.cs.tu-berlin.de
Mon Jan 17 16:37:00 GMT 2000


> Of course, I know that the comparison between floating-point values
> is not suitable in general. Here, the problem is that the same test
> (in source code) evaluates differently when called twice 
> consecutively (still in source file).

Thanks for your bug report. Please have a look at

http://gcc.gnu.org/faq.html#rounding

for a general discussion of this problem. In your specific example,
have a look at the sequence.

        sumt2+=(t[i]);

        if(sumt2>=sumt1) ...
        else
        {
            if(sumt2>=sumt1)

The cause of your problem is that the i386 processor internally
computes with a higher precision than is externally used to store
values. In particular, the internal computation uses 80 bits
precision; the external storage only 64 bits.

So the result sum2 of the addition is present in high precision when
making the first comparison. At the second comparison, it is
re-fetched from memory, resulting in a lower-precision value, and thus
a different outcome of the comparison.

To see the exact code, please have a look at the assembler code
generated. Roughly, the sequence is like

  fetch old sumt2 value into floating register (0)
  add t[i]
  store back result into new sumt2; keep sumt2 in register
  fetch sumt2 into another float register (1)
  compare register 0 and register 1
  If comparison fails,
    fetch sumt2 into register 0
    fetch sumt1 into register 1
    compare

I think the compiler is entirely entitled to execute your code in this
way. To get more consistent results, you can use the -ffloat-store
option of gcc, which forces a store-back for each assignment. This, of
course, is additional overhead as well.

Hope this helps,
Martin



More information about the Gcc-bugs mailing list