FP comparisons, C, X86, and NaN's
Gabriel Dos Reis
gdr@acm.org
Sun Jun 20 04:35:00 GMT 2004
You Wrote Chris Lattner
>
> I'm trying to understand the standards relating to floating point
> comparisons in C, particularly with respect to NaN's. From my
> interpretation of the whole mess, in C99, standard relational operations
> (like X < Y) should trap on NaN's, including SNaNs.
QNaNs don't trap, but would obey non-standard logic. SNaNs would
trap. More specifically, the C99 standard says:
5.2.4.2.2/3
[#3] In addition to normalized floating-point numbers (f1>0
if x!=0), floating types may be able to contain other kinds
of floating-point numbers, such as subnormal floating-point
numbers (x!=0, e=emin, f1=0) and unnormalized floating-point
numbers (x!=0, e>emin, f1=0), and values that are not
floating-point numbers, such as infinities and NaNs. A NaN
is an encoding signifying Not-a-Number. A quiet NaN
propagates through almost every arithmetic operation without
raising a floating-point exception; a signaling NaN
generally raises a floating-point exception when occurring
as an arithmetic operand.17)
(and it seems to have a very interesting view of what a floating-point
exception would be).
>
> When looking into this, I noticed that GCC compiles standard relational
> comparisons to the fucom* instructions on the X86 target (which ignore
> nans), but the ICC compiler compiles them to fcom* (which traps on nans).
>
> Which is right? Roger pointed out to me that there would be no reason for
> the C99 isgreaterequal (and family) functions if comparisons didn't trap.
> Based on this, I see two alternatives:
Roger is right.
>
> 1. GCC has a bug, it should be producing fcom* instead of fucom*
> 2. C does not define whether or not standard relational operations trap.
> If this is the case, there is no bug in GCC.
the type generic macros isxxx are explicilty defined as non-trapping.
7.12.14 Comparison macros
[#1] The relational and equality operators support the usual
mathematical relationships between numeric values. For any
ordered pair of numeric values exactly one of the
relationships -- less, greater, and equal
-- is true. Relational operators may raise the
``invalid'' floating-point exception when argument values
are NaNs. For a NaN and a numeric value, or for two NaNs,
just the unordered relationship is true.206) The following
subclauses provide macros that are quiet (non floating-point
exception raising) versions of the relational operators, and
other comparison macros that facilitate writing efficient
code that accounts for NaNs without suffering the
``invalid'' floating-point exception. In the synopses in
this subclause, real-floating indicates that the argument
shall be an expression of real floating type.
>
> Does anyone have any thoughts on this matter?
>
> -Chris
>
> --
> http://llvm.cs.uiuc.edu/
> http://www.nondot.org/~sabre/Projects/
>
>
More information about the Gcc
mailing list