optimize Pentium 4 and above bug!?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I beleave I have found a bug in the C++ compiler of Visual Studio 200

The correct output for the code below is "Color is: 99b2cce5" but if I turn on the Pentium 4 and above (/G7) options
I get the follow result "Color is: 80000000" which is completly wrong

I found this bug when upgrading from visual studio 6.0 and my colors turned black! :

/O2 /Ot /G7 /I "../../DevelopmentSystem" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /Gm /EHsc /MD /Fp".\Release/Europe.pch" /Fo"Release/" /Fd"Release/vc70.pdb" /FR"Release/" /W3 /nologo /c /Wp64 /Z

class Tes

public
float a, r, g, b

uint get(

uint c = a*255; c<<=8
c += r*255; c<<=8
c += g*255; c<<=8
c += b*255

return c

}

void main(int argc, const char **argv

Test test
test.a = .6
test.r = .7
test.g = .8
test.b = .9

uint c = test.get()

printf("%x\n", c)
 
The variable 'c' is being converted to a float during the += operation
and causing you some rounding errors on the conversion back to int.
Cast the expressions to (int) i.e.

c += r*255;

is equivalent to

c = c + r * 255;

which means the entire expression is promoted to a float type becuase of
r's type.

c += (unsigned char)(r*255);

I would probably recommend going the extra mile and use |= operator,
which will also catch any accidental use of float in this case as well.
 
I agree with your point about rounding errors and I will change my code
accordingly, but this still does not explain
why I get a calculation error when turing on optimizing for pentium 4.

Ludde
 
Back
Top