PRB: Bad Code Generated for "a+b == a" FP Comparison Expression

Last reviewed: July 17, 1997
Article ID: Q68559
6.00 6.00a 6.00ax 7.00 | 6.00 6.00a | 1.00 1.50
MS-DOS                 | OS/2       | WINDOWS
kbtool kbprb

The information in this article applies to:

  • The Microsoft C/C++ Compiler (CL.EXE) included with:

        - Microsoft C for MS-DOS, versions 6.0, 6.0a, and 6.0ax
        - Microsoft C for OS/2, versions 6.0 and 6.0a
        - Microsoft C/C++ for MS-DOS, version 7.0
        - Microsoft Visual C++ for Windows, versions 1.0 and 1.5
    

SYMPTOMS

The compiler optimizes expressions of the form "a+b == a" if a and b are floating-point variables (double or float type).

RESOLUTION

This is expected behavior for the compiler. The proper code is generated by compiling with the /Op option, which directs the compiler to use consistent floating-point calculations. If this is not a viable option, one of the following will work around the situation:

  • Enclose the expression "a+b" in parentheses.
  • Make the variables a and b integers, if applicable.
  • Compile with the quick compile (/qc) or fast compile (/f) options.

MORE INFORMATION

Sample code with floating-point expressions of the form "a+b == a" was compiled with optimization disabled (/Od) and the first few lines of the resulting assembly listing follows:

; double a,b; ; if (a+b == a) ;

    *** 00000b  9b d9 ee                fldz
    *** 00000e  9b dc 16 00 00          fcom    QWORD PTR _b
    *** 000013  9b dd d8                fstp    ST(0)
    *** (lines deleted)

; if ((a+b) == a) ;
    *** 000025  9b dd 06 00 00          fld     QWORD PTR _b
    *** 00002a  9b dc 06 00 00          fadd    QWORD PTR _a
    *** 00002f  9b dc 16 00 00          fcom    QWORD PTR _a
    *** 000034  9b dd d8                fstp    ST(0)
    *** (lines deleted)

Note that the first expression gets optimized to compare variable b to zero, rather than comparing a+b to a. In the second expression, a+b is correctly compared to a.

Compiling with the /Op option generates the correct code in both cases. The expression (a+b == a) might be used with floating point numbers to detect when b is negligibly small in relation to a.


Additional reference words: 1.00 1.50 6.00 6.00a 6.00ax 7.00 8.00 8.00c
conditional
KBCategory: kbtool kbprb
KBSubcategory: CodeGen
Keywords : kb16bitonly


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 17, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.