vc compiler (Version 17.00.50727.1 for x86) optimization issue - by Tango1979

Status : 

  Duplicate<br /><br />
		This item appears to be a duplicate of another existing Connect or internal item.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.


1
0
Sign in
to vote
ID 776379 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 1/11/2013 3:25:41 PM
Access Restriction Public

Description

Wrong optimization for following file:
===file begin===
#include <cstdio>
typedef unsigned char GLubyte;

#define width  64
#define height 64

static GLubyte red    [height][width][4];

static void makeCubeImage(void)
{
  for(GLubyte i=0; i< height; i++){
    for(GLubyte j=0; j<width; j++){
	  red[i][j][0]= ((i<height/4 && j<width/4)? 255:0) + ((i>height/4 && i<(3*height)/4)? ((j>width/4 && j<(3*width)/4)? 255:0):0);
    }
  }
}

void print_array(GLubyte* ptr)
{
	printf("%02x\n", ptr[4420]);
}

int main()
{
	makeCubeImage();
	print_array(reinterpret_cast<GLubyte*>(red));
}
===file end===

The loop internal is compiled into:
	  red[i][j][0]= ((i<height/4 && j<width/4)? 255:0) + ((i>height/4 && i<(3*height)/4)? ((j>width/4 && j<(3*width)/4)? 255:0):0);
00201012 80 FB 10             cmp         bl,10h  
00201015 73 0C                jae         makeCubeImage+23h (0201023h)  
00201017 80 FA 10             cmp         dl,10h  
0020101A 73 07                jae         makeCubeImage+23h (0201023h)  
0020101C B9 FF 00 00 00       mov         ecx,0FFh  
00201021 EB 02                jmp         makeCubeImage+25h (0201025h)  
00201023 33 C9                xor         ecx,ecx  
00201025 8A C3                mov         al,bl  
00201027 2C 11                sub         al,11h  
00201029 3C 1E                cmp         al,1Eh  
0020102B 77 15                ja          makeCubeImage+42h (0201042h)  
0020102D B8 1E 00 00 00       mov         eax,1Eh  
00201032 3A E0                cmp         ah,al  
00201034 1B C0                sbb         eax,eax  
00201036 25 01 FF FF FF       and         eax,0FFFFFF01h  
0020103B 05 FF 00 00 00       add         eax,0FFh  
00201040 EB 02                jmp         makeCubeImage+44h (0201044h)  
00201042 33 C0                xor         eax,eax  
00201044 02 C8                add         cl,al  
00201046 88 0E                mov         byte ptr [esi],cl  
00201048 FE C2                inc         dl  
0020104A 83 C6 04             add         esi,4  
0020104D 80 FA 40             cmp         dl,40h  
00201050 72 C0                jb          makeCubeImage+12h (0201012h)  
  for(GLubyte i=0; i< height; i++){
00201052 FE C3                inc         bl  
The internal sequence this is wrong:
0020102D B8 1E 00 00 00       mov         eax,1Eh  
00201032 3A E0                cmp         ah,al  
00201034 1B C0                sbb         eax,eax  
00201036 25 01 FF FF FF       and         eax,0FFFFFF01h  
0020103B 05 FF 00 00 00       add         eax,0FFh  
Should be compared with dl, instead of ah cmp with al since it is a constant that just assigned through mov         eax,1Eh .

Hope to get the fix soon, or tell us a version that the issue has been fixed.

Thanks,
Tango
Sign in to post a comment.
Posted by Tango1979 on 1/18/2013 at 1:20 PM
Thanks for your feedback!
We will use workaround now.
Posted by Microsoft on 1/17/2013 at 7:42 PM
Hi, expect this fix in the first 18 release.

Eric
Posted by Tango1979 on 1/17/2013 at 5:03 PM
Thanks for your confirmation and workaround!

A small question: may I know what the next major release means?
Just the compiler shipped in vs2012 sp1/sp2 or cl version 18.00 that is expected to be shipped in next generation of visual studio?

Thanks,
Tango
Posted by Microsoft on 1/17/2013 at 10:37 AM
Hello, thanks for the bug report. This is a compiler optimization bug, and will be fixed in the next major release of the C++ compiler.

Like Mike Danes suggested, you can use "unsigned int" instead of "unsigned char" as a source code workaround for this.

This is the same compiler bug as reported here: http://connect.microsoft.com/VisualStudio/feedback/details/768459/miscompilation-of-char-string-manipulation-on-ox#details

If you feel you would like a hotfix for VS 2012, please visit the support site:

http://support.microsoft.com/common/international.aspx?RDPATH=dm;en-us;selectassist&target=assistance

Thanks,
Eric Brumer - Microsoft Visual C++
Posted by Microsoft on 1/13/2013 at 10:58 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 Mike Danes on 1/12/2013 at 7:17 AM
You can avoid the bug by using 'unsigned int' instead of 'unsigned char' for the iteration variables i and j.
Posted by Microsoft on 1/11/2013 at 3:52 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)