Skip to content

Instantly share code, notes, and snippets.

@zeux
Last active June 28, 2018 02:27
Show Gist options
  • Save zeux/d1c906bc792966c19db8631eb1256b02 to your computer and use it in GitHub Desktop.
Save zeux/d1c906bc792966c19db8631eb1256b02 to your computer and use it in GitHub Desktop.
An example how MSVC can change function argument evaluation order with the same compilation settings. Compile for x86 (32-bit) with MSVC 2015 or 2017 with /O2, with or without REORDER defined.
#include <stdio.h>
#include <stdint.h>
__declspec(noinline)
uint32_t giveMeRand()
{
static uint32_t result = 0xdeadbeef;
result *= 17;
return ++result;
}
#ifdef REORDER
__declspec(noinline) // <-- comment/uncomment this line
#endif
static float randUniform(float min, float max)
{
int granularity = 1000000;
return min + (max - min) * float(giveMeRand() % granularity) / granularity;
}
struct Vec2
{
float x, y;
Vec2(float x, float y): x(x), y(y)
{
}
};
int main()
{
Vec2 v(randUniform(-100.f, 100.f), randUniform(-100.f, 100.f));
// Output with MSVC 2015/2017 with /O2:
// No defines: 12.454597, -51.327999
// /DREORDER: -51.327999, 12.454597
printf("%f, %f\n", v.x, v.y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment