Skip to content

Instantly share code, notes, and snippets.

@zeux
Created April 16, 2012 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeux/2400940 to your computer and use it in GitHub Desktop.
Save zeux/2400940 to your computer and use it in GitHub Desktop.
MSVC10 optimization bug
// cl 16.00.40219.01 for 80x86
// Compile with /O2:
// pData - slcControlFlags = 48
// Assertion failed: (pData - slcControlFlags) == sizeof(slcControlFlags), file msvc10optbug.cpp, line 46
#include <stdint.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
const size_t kSlcSizeWithoutBoneArray = 96;
struct Locator { char data[16]; };
__declspec(noinline)
void WriteFlagsAt(uint8_t (&slcControlFlags)[kSlcSizeWithoutBoneArray], int controlSlot, const Locator& locator)
{
// create working handle
uint8_t* pData = slcControlFlags;
// locator
for ( int i = 0; i < 4; ++i )
{
if (i == controlSlot)
{
memcpy(pData, &locator, sizeof(locator));
}
pData += sizeof(locator);
}
// masks
for (int i = 0; i < 4; ++i)
{
if (i == controlSlot)
{
*((uint32_t*)pData) = 0;
}
pData += sizeof(uint32_t);
}
// indices
for (int i = 0; i < 4; ++i)
{
if (i == controlSlot)
{
*((uint32_t*)pData) = 0;
}
pData += sizeof(uint32_t);
}
printf("pData - slcControlFlags = %d\n", (int)(pData - slcControlFlags));
assert( (pData - slcControlFlags) == sizeof(slcControlFlags) );
}
int main()
{
uint8_t dest[kSlcSizeWithoutBoneArray];
WriteFlagsAt(dest, 1, Locator());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment