option -ffloat-store has no effect

Nelson H. F. Beebe beebe@math.utah.edu
Sat Dec 3 00:53:00 GMT 2005

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:

     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  -

More information about the Gcc-help mailing list