bug in cl (c compiler) - by Dmitry Volkinshtein

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 676584 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 6/22/2011 10:00:50 PM
Access Restriction Public


The following program gives incorrect output when compiled with optimization on x86 machine

#include <stdio.h>
#include <limits.h>

int main(int argc, char *argv[])
    volatile int i10 = 10;
    printf("%d\n", -(INT_MIN/i10));

The problem seems to be the following:
During the optimization process, the compiler tries to save the '-' at the end of the calculation by changing the sign of the constant dividend first instead. It is ok for all constant ints except INT_MIN because of the 2's complement feature: -INT_MIN==INT_MIN and the result of (-INT_MIN)/10 is negative. The  output of the program is correct for other constants, and when the optimization if off (Debug)
Sign in to post a comment.
Posted by Microsoft on 6/23/2011 at 3:50 PM
Dimitry, my original response seems to have gotten lost in the ether. I wanted to thank for reporting all code generation issues that you've been submitting. This kind of feedback is valuable to us.

As you've correctly surmised in your report the optimizer is ignoring the case of MIN_INT when distrubuting the negation over the division. This will be fixed in the next major release of Visual Studio.

In the meantime this should be easy enough to identify and work around.

ian Bearman
VC++ Code Generation and Optimization Team
Posted by MS-Moderator08 [Feedback Moderator] on 6/23/2011 at 2:17 AM
Thank you for reporting the issue.
We are routing this issue to the appropriate group within the Visual Studio Product Team for triage and resolution.These specialized experts will follow-up with your issue.
Posted by MS-Moderator01 on 6/22/2011 at 10:50 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)