Search

Invalid C code result with optimization by SolidSnake2011

Closed
as Fixed Help for as Fixed

1
0
Sign in
to vote
Type: Bug
ID: 676931
Opened: 6/25/2011 1:18:00 AM
Access Restriction: Public
Moderator Decision: Sent to Engineering Team for consideration
0
Workaround(s)
0
User(s) can reproduce this bug
The following code gives invalid result with optimization (Release configuration).

#include <stdio.h>
#include <stdlib.h>

typedef char T; // char or short
#define N 4 // any from 4 to 10

void
int2Buf (T * buf, int value, int nBits)
{
    int i;
    for (i = 0 ; i < nBits; i++) {
        buf[i] = value & 1;
        value >>= 1;
    }
}

void
printBuf (T * buf, int nBits)
{
    int i;
    printf ("bits: ");
    for (i = 0; i < nBits; i++) {
        printf ("%d", (int)buf[i]);
    }
    printf ("\n");
}

int
main (void)
{
    T buf[32];
    int2Buf (buf, 0x55555555, N);
    printBuf (buf, N);
    int2Buf (buf, abs(0x55555555), N);
    printBuf (buf, N);
    return 0;
}
Details (expand)

Visual Studio/Team Foundation Server/.NET Framework Tooling version

Visual Studio 2010 SP1

Steps to reproduce

Create default empty console Win32 project, run under Release configuration.
here is a compilation command line:
/Zi /nologo /W3 /WX- /O2 /Oi /Oy- /GL /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /Gm- /EHsc /GS /Gy /fp:precise /Zc:wchar_t /Zc:forScope /Fp"Release\test2.pch" /Fa"Release\" /Fo"Release\" /Fd"Release\vc100.pdb" /Gd /analyze- /errorReport:queue

Product Language

English

Operating System

Windows 7

Operating System Language

English

Actual results

bits: 1010
bits: 1011

Expected results

bits: 1010
bits: 1010
File Attachments
0 attachments
Sign in to post a comment.
Posted by Microsoft on 6/28/2011 at 1:01 PM
Thanks for reporting this issue. This will be address in the next release of Visual Studio. You can work around optimizations such as this by disabling optimizations around the function that is misbehaving. Documentation on doing this can be found on MSDN here:
http://msdn.microsoft.com/en-us/library/chh3fb0k(v=VS.100).aspx

thanks,
ian Bearman
VC++ Code Generation and Optimization Team
Posted by MS-Moderator10 [Feedback Moderator] on 6/26/2011 at 7:29 PM
Thanks for your feedback.

We are rerouting 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/25/2011 at 1:49 AM
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)
Sign in to post a workaround.