* Re: option -ffloat-store has no effect
@ 2005-12-03 0:53 Nelson H. F. Beebe
0 siblings, 0 replies; 2+ messages in thread
From: Nelson H. F. Beebe @ 2005-12-03 0:53 UTC (permalink / raw)
To: Petr Savicky; +Cc: beebe, gcc-help
Petr Savicky <savicky@cs.cas.cz> writes on Sat, 3 Dec 2005 01:35:24
+0100 to ask about why the code snippet
x = i*(a/i);
if (x != (i*(a/i))) printf("%f\n",i);
when compiled with produces output in a loop, even with -ffloat-store.
I believe that the compiler is working according to its documentation:
`-ffloat-store'
Do not store floating point variables in registers, and inhibit
other options that might change whether a floating point value is
taken from a register or memory.
...
The option applies only to VARIABLES, not to INTERMEDIATE EXPRESSIONS
such as (i*(a/i)).
In the sample code that Petr posted, on an IA-32 system with several
different gcc versions, I get output, and that happens because x has
been stored into memory as a 64-bit value, while (i*(a/i)) is retained
in a register as a 80-bit value, and the results differ slightly.
You need to rewrite the code so that both sides of the comparison are
guaranteed to be the same width by forcing them into memory:
volatile x, y;
x = ...;
y = ...;
if (x == y) ...
The volatile qualifier here is an extremely important coding technique
for floating-point software that is run on platforms with registers
that are longer than normal memory formats (e.g., Intel IA-32 and
IA-64, Motorola 68K, ancient Honeywell systems, ...).
-------------------------------------------------------------------------------
- Nelson H. F. Beebe Tel: +1 801 581 5254 -
- University of Utah FAX: +1 801 581 4148 -
- Department of Mathematics, 110 LCB Internet e-mail: beebe@math.utah.edu -
- 155 S 1400 E RM 233 beebe@acm.org beebe@computer.org -
- Salt Lake City, UT 84112-0090, USA URL: http://www.math.utah.edu/~beebe -
-------------------------------------------------------------------------------
^ permalink raw reply [flat|nested] 2+ messages in thread
* option -ffloat-store has no effect
@ 2005-12-03 0:35 Petr Savicky
0 siblings, 0 replies; 2+ messages in thread
From: Petr Savicky @ 2005-12-03 0:35 UTC (permalink / raw)
To: gcc-help
Dear gcc-help,
the option -ffloat-store has no effect on the following program.
If the program
#include <stdio.h>
double a,i,x;
int main(int argc, char *argv[])
{
a = 1;
for (i=1; i<100; i++) {
x = i*(a/i);
if (x != (i*(a/i))) printf("%f\n",i);
}
}
is compiled with gcc -O0 -ffloat-store
or with gcc -O0 -fno-float-store,
then the obtained binary executable program is the same (verified by diff)
and produces the output
41.000000
45.000000
55.000000
61.000000
82.000000
90.000000
99.000000
So, there are two questions:
1. Why -ffloat-store has no effect?
2. Is there some other option needed to guarantee that after
double a,i,x;
...
x = i*(a/i);
the test
(x == i*(a/i))
is satisfied?
I use Fedora Core release 2 on Xeon processor.
gcc (GCC) 3.3.3 20040412 (Red Hat Linux 3.3.3-7)
Thank you in advance for any help.
Petr
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2005-12-03 0:53 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-12-03 0:53 option -ffloat-store has no effect Nelson H. F. Beebe
-- strict thread matches above, loose matches on Subject: below --
2005-12-03 0:35 Petr Savicky
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).