2 Free Software Foundation, Inc.
7 IEEE floating point comparisons
9 Ian Dall <ian@beware.dropbear.id.au>
10 ------------------------------------
12 The ns32x81 fpu handles most operands in hardware, but traps on NaN,
13 Inf and Denormalized numbers. The correct behavior can be handled by
14 the trap handler. This is mostly transparent to the compiler, but in
15 the case of floating point comparisons, the trap handler and the
16 compiler must co-operate.
18 Comparing a Nan with anything (including another Nan) is an unordered
19 comparison and a NE test should be true and any other test should be
20 false. There is nothing the trap handler can do to the condition codes
21 to make, for example ble and bgt (the machine instructions, not the
22 gcc insn's) both fail.
24 The L flag (normally used for unsigned comparisons) is cleared by a floating
25 point compare. So, it is possible for the trap handler to communicate that
26 it has seen a NaN by setting this flag.
28 This can get confusing so the following documents the reasoning. There
29 are only 3 flags of significance, N, Z and L. In what follows AB is
30 the conjunction of A and B ("and") A+B is the disjunction of A and B
31 ("or"), upper case represents true and lower case represents false. So
32 "Zl" is "Z AND (NOT L)".
34 Also, we can require that the trap handler clears N and Z, whenever it
35 sets L. This means that in many cases, we do not need to test if L is
36 set or clear. The operations we require are:
38 Operator Expression Check L
47 For example, the emitted code for the case of LT is
53 which is, in effect, "branch if ordered and less than."
55 We also need insns for the reverse branches. These have the PC and
56 the label ref operands reversed. Thus the reverse bgt has a pattern:
59 (if_then_else (gt (cc0)
62 (label_ref (match_operand 0 "" ""))))
64 This is identical to a normal branch with the test complimented:
67 (if_then_else (not (gt (cc0)
69 (label_ref (match_operand 0 "" "")
72 Thus we need a family of (NOT cond) tests. For integers this is easy,
73 a reverse blt becomes bge. However, the possibility of unordered
74 comparison complicates the floating point case. So, we need to
75 compliment the above expressions, using deMorgan's theorem, for the reverse
78 Operator Expression Check L
86 For example the emitted code for the case of RLT is
91 which is, in effect "branch if not less than and not unordered."
93 These extra comparisons are safe if the trap handler doesn't set the
94 L flag, since in that case the additional "bhi" instructions are never
95 taken. Also, these extra branch instructions are controlled by the
96 "-mieee-compare" option.