www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - optimizer bug ?

<Dcode>
import std.c.stdio;


ubyte[4] arr;

void foo()
{
 *cast(float*)(&arr[0]) = 3.14;
}

uint bar()
{
 uint result = *cast(uint*)&arr[0];
 return result;
}

float baz()
{
 assert (float.size == uint.size);
 uint result = bar();
 result &= 0xffffffff;  // try to remove this line in optimize (-O) mode !
 return *cast(float*)&result;
}

void main()
{
 foo();
 float x = baz();

 printf("value: %f", x);
 getchar();
}
</Dcode>

guess what happens when the commented line is removed in optimized mode. ok,
i'll tell you ;) instead of printing "3.140000" the program outputs "-nan".
i guess its not an optimization in that case ;)

why "result &= 0xffffffff;" ? it could be anything, like increment and then
decrement. seems like the value just needs to be "touched" in some way,
otherwise the compiler optimizes it out...

compiler version: 0.91
platform: win xp sp1

thanks for help,
Tom
Jun 05 2004