Thread: Re: [GD-Windows] forcing value to bool 'true' or 'false' (performance warning)
Brought to you by:
vexxed72
From: Rich <leg...@xm...> - 2002-04-23 01:04:21
|
In article <000901c1ea5a$ca4377e0$dc56533e@aurora>, =?iso-8859-1?Q?Ignacio_Casta=F1o?= <cas...@ya...> writes: > does somebody knows why msvc complains about a performance warning when > converting an integer into a bool value? Is there really any performance > hit? Well, the compiler will generate code to convert an int (4 bytes) to a bool (usually 1 byte), whether or not it matters for performance is dubious. You can eliminate the warning by doing 'bool p = i ? true : false;' -- Ask me about my upcoming book on Direct3D from Addison-Wesley! Direct3D Book <http://www.xmission.com/~legalize/book/> izfree: Open source tools for Windows Installer <http://izfree.sourceforge.net> |
From: Brian S. <bs...@mi...> - 2002-04-23 01:17:39
|
If you look at the disassembly you'll see it's a little bit more than that, it also has to clamp any integer to the value of "true", which is 1 in the VC++ implementation. Essentially, it does implicitly what your "?" expression does explicitly. --brian > -----Original Message----- > From: Rich [mailto:leg...@xm...]=20 > Sent: Monday, April 22, 2002 6:04 PM > To: gam...@li... > Subject: Re: [GD-Windows] forcing value to bool 'true' or=20 > 'false' (performance warning)=20 >=20 >=20 >=20 > In article <000901c1ea5a$ca4377e0$dc56533e@aurora>, > =3D?iso-8859-1?Q?Ignacio_Casta=3DF1o?=3D <cas...@ya...> = writes: >=20 > > does somebody knows why msvc complains about a performance=20 > warning when > > converting an integer into a bool value? Is there really=20 > any performance > > hit? >=20 > Well, the compiler will generate code to convert an int (4 bytes) to a > bool (usually 1 byte), whether or not it matters for performance is > dubious. >=20 > You can eliminate the warning by doing 'bool p =3D i ? true : false;' > --=20 > Ask me about my upcoming book on Direct3D from Addison-Wesley! > Direct3D Book <http://www.xmission.com/~legalize/book/> > izfree: Open source tools for Windows Installer > <http://izfree.sourceforge.net> >=20 > _______________________________________________ > Gamedevlists-windows mailing list > Gam...@li... > https://lists.sourceforge.net/lists/listinfo/gamedevlists-windows > Archives: > http://sourceforge.net/mailarchive/forum.php?forum_id=3D555 >=20 |
From: Brian S. <bs...@mi...> - 2002-04-23 01:28:50
|
The compiler generated version doesn't cause a branch (this is VC 7): 752: int a =3D 1000; 0003A370 mov dword ptr [a],3E8h 753: bool b =3D a; 0003A377 cmp dword ptr [a],0 0003A37B setne al 0003A37E mov byte ptr [b],al But it is using a register so that doesn't do the optimizer any favors. --brian > -----Original Message----- > From: Jon Watte [mailto:hp...@mi...]=20 > Sent: Monday, April 22, 2002 6:16 PM > To: Rich; gam...@li... > Subject: RE: [GD-Windows] forcing value to bool 'true' or=20 > 'false' (performance warning)=20 >=20 >=20 >=20 > It's more than that. The problem is that any bytes in the int must=20 > count towards a "true" value. The re-write you propose will generate=20 > a branch, which will not predict well, which is a REALLY bad thing. >=20 > Perhaps MSVC7 generates CMOV for this; I've never gotten MSVC 6 to=20 > do it unfortunately :-( Another branchless alternative (assuming=20 > they're OK with any non-0 value for "true" inside a bool, which may=20 > not be true) is to do some shifts and ors. >=20 > int i; bool b; >=20 > unsigned short temp16 =3D (i>>16) | i; > *(unsigned char *)&b =3D (temp16>>8) | temp16; // horrible! >=20 > Cheers, >=20 > / h+ >=20 > > Well, the compiler will generate code to convert an int (4=20 > bytes) to a > > bool (usually 1 byte), whether or not it matters for performance is > > dubious. > >=20 > > You can eliminate the warning by doing 'bool p =3D i ? true : = false;' >=20 >=20 >=20 > _______________________________________________ > Gamedevlists-windows mailing list > Gam...@li... > https://lists.sourceforge.net/lists/listinfo/gamedevlists-windows > Archives: > http://sourceforge.net/mailarchive/forum.php?forum_id=3D555 >=20 |
From: Rich <leg...@xm...> - 2002-04-23 02:30:21
|
In article <HEE...@mi...>, "Jon Watte" <hp...@mi...> writes: > The re-write you propose will generate > a branch, which will not predict well, which is a REALLY bad thing. Nonsense. Nothing can universally be declared bad. "Bad things" can only be measured in context. You won't know if this is good or bad (or even significant) until you profile your app and determine that this is the limiting factor. I'm highly dubious that it ever will be -the- limiting factor, but even if it ever is, you can't say that until you measure it and determine that to be the case. -- Ask me about my upcoming book on Direct3D from Addison-Wesley! Direct3D Book <http://www.xmission.com/~legalize/book/> izfree: Open source tools for Windows Installer <http://izfree.sourceforge.net> |
From: Rich <leg...@xm...> - 2002-04-23 02:31:32
|
In article <5.1...@ma...>, Tom Hubina <to...@3d...> writes: > You don't need conditionals for this. I didn't assert anything of the sort. I simply said you can eliminate the warning that way. I did not say it was the only way to eliminate the warning. I did not say it was the best way to eliminate the warning. I simply said it was -a- way. -- Ask me about my upcoming book on Direct3D from Addison-Wesley! Direct3D Book <http://www.xmission.com/~legalize/book/> izfree: Open source tools for Windows Installer <http://izfree.sourceforge.net> |
From: Jon W. <hp...@mi...> - 2002-04-23 01:15:43
|
It's more than that. The problem is that any bytes in the int must count towards a "true" value. The re-write you propose will generate a branch, which will not predict well, which is a REALLY bad thing. Perhaps MSVC7 generates CMOV for this; I've never gotten MSVC 6 to do it unfortunately :-( Another branchless alternative (assuming they're OK with any non-0 value for "true" inside a bool, which may not be true) is to do some shifts and ors. int i; bool b; unsigned short temp16 = (i>>16) | i; *(unsigned char *)&b = (temp16>>8) | temp16; // horrible! Cheers, / h+ > Well, the compiler will generate code to convert an int (4 bytes) to a > bool (usually 1 byte), whether or not it matters for performance is > dubious. > > You can eliminate the warning by doing 'bool p = i ? true : false;' |