Search

Optimization bug in x64 compiler when left shifting 64-bit value by Miika Virtanen

Closed
as Fixed Help for as Fixed

1
0
Sign in
to vote
Type: Bug
ID: 678524
Opened: 7/6/2011 1:32:51 AM
Access Restriction: Public
Moderator Decision: Sent to Engineering Team for consideration
0
Workaround(s)
0
User(s) can reproduce this bug
Left shifting 64-bit variable more than 30 bits in a function which gets inlined and is called inside a loop creates wrong results when build with x64 compiler and optimizations.

The problem disappears with any of the following changes:
1. Optimizations are removed.
2. Function() is declared with __declspec(noinline).
3. Comment away the line which prints LSHIFT so it is not used elsewhere than in the calculation.
4. The for-loop around Function() call is removed.

While the sample itself is meaningless it is derived from a more complex use case with your sample code which slightly modified exhibits the same problem.
http://msdn.microsoft.com/en-us/library/ms683194(v=vs.85).aspx
Details (expand)

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

Visual Studio 2010 SP1

Steps to reproduce

Create a default command line project and add x64 target to it. Compile it with release configuration and observe the printed value being 0 instead of 9223372036854775808.

#include <stdio.h>

void Function()
{
    unsigned __int64 LSHIFT = 63; // 0-30 works, 31-63 doesn't
    unsigned __int64 MASK = (unsigned __int64)(1) << LSHIFT;
    printf("LSHIFT: %I64u\n", LSHIFT);
    printf("MASK: %I64u (Correct: 9223372036854775808)\n", MASK);
}

int main()
{
    for(int i = 0; i < 1; ++i)
    {
        Function();
    }

    return 0;
}

Product Language

English

Operating System

Windows 7

Operating System Language

English

Actual results

The left shift results in wrong value.

Expected results

The left shift results in correct value and optimizations doesn't affect the result.
File Attachments
0 attachments
Sign in to post a comment.
Posted by Microsoft on 7/20/2011 at 3:55 PM
Miika, thank you for the bug report. We have confirmed that this is a bug. We will try to have this bug fixed in the next release.

Mark Levine
Visual C++
Posted by MS-Moderator07 [Feedback Moderator] on 7/7/2011 at 12:17 AM
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 7/6/2011 at 1:50 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.