This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

-ffast-math and floating point reordering


OK, let's start a thread under the right title.

The question under consideration is how, if at all, the -ffast-math flag
should effect the evaluation of a+b+c or (a+b)+c .  It turns out that
there was a lot of confusion as to what the rules are, for various
languages.

Summary (credit to Joseph Myers, Gaby Dos Reis, and Toon Moene): for the
case where a, b, and c are really expressions, these expressions can be
evaluated in any order.  At that point:

Fortran:for a+b+c we can add in any order.  For (a+b)+c parentheses must
        be respected.

C++:	not only are parentheses respected, but a+b+c is (a+b)+c

Ada:	the 1983 LRM seems to say that the rule is the same as for C++:

	"... for a sequence of operators of the same precedence level, the
	operators are associated in textual order from left to right;
	parentheses can be used to impose specific associations"

	( http://archive.adaic.com/standards/83lrm/html/lrm-04-05.html#4.5 )

C:	if IEEE conformant behavior is promised (by defining
	__STDC_IEC_559__), it's like C++, but "the implementation can state
	that the accuracy of floating-point operations is unknown" if
	this symbol is not defined.

K&R C:	compilers could reorder FP operations at will; the user had to
        use explicit temporaries to defeat this.

Java:   not only do operators of equal precedence associate from left to
	right, but so does order of evaluation.

If -ffast-math is not specified, we should follow the language standards.
First question: can tree-ssa distinguish, for Fortran, whether parentheses
were present and so whether reordering is allowed?

If -ffast-math is specified, what should we do?  One option is to use the
K&R rule and free-associate at will.  I feel uncomfortable with that
because of the major loss in accuracy that can result.  If, however, we
implement support for the Fortran rules, one option would be to relax
order of evaluation when there are no parentheses (like Fortran), another
would be to just leave the order the same.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]