RE: [GD-Windows] forcing value to bool 'true' or 'false' (performance warning)
Brought to you by:
vexxed72
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 |