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