Variable assignment and truncation of floats on x86
Wed Apr 18 15:32:00 GMT 2001
Remove the -O2 from the compile line. The program is simple and the compiler
optimization precomputes the variables and you can't see the problem.
If you look at the assembly with optimization it will simple write the
results. In any reasonable program where all values can not be precomputed
the bug is evident.
From: Toshi Morita [ mailto:email@example.com ]
Sent: Wednesday, April 18, 2001 6:28 PM
Subject: Re: Variable assignment and truncation of floats on x86
> A routine that demonstrates the problem is below. I can provide several
> other examples. It was suggested previously that the floating point unit
> being put into truncate mode from its default round-to mode. I don't know
> how to overcome this without major ramifications on the code baseline.
> 3 results in: 7.0000000000 (actually probably 6.999999? rounded to
> 4 results in: 6 <------- PROBLEM WITH INPLACE ASSIGNMENT
> 5 results in: 7 (correct answer)
[tm@localhost tm]$ uname -a
Linux localhost.localdomain 2.2.17-14smp #1 SMP Mon Feb 5 15:48:47 EST 2001
[tm@localhost tm]$ gcc -v
Reading specs from /usr/local/lib/gcc-lib/i686-linux/2.95.3/specs
gcc version 2.95.3 20010315 (release)
[tm@localhost tm]$ gcc -O2 test2.c
[tm@localhost tm]$ ./a.out
Direct assign float to float = 7.0000000000
Inplace assign float to int = 7
Direct assign float to int = 7
This doesn't seem to occur on all versions of gcc.
It would probably help if you specified an exact version
which fails, instead of "gcc on x86".
More information about the Gcc-bugs