Update RageUtil::power_of_two

Replacing the SM5 power-of-two code with the implementation from Bit Twiddling Hacks, since the current implementation isn't correctly handling the edge case where the input is 0, and is a little overcomplicated.
This commit is contained in:
sukibaby
2025-02-08 22:59:36 -08:00
committed by teejusb
parent f2809028de
commit 4601da4ab9
2 changed files with 15 additions and 19 deletions
+14 -18
View File
@@ -122,25 +122,21 @@ float fmodfp(float x, float y)
return x;
}
int power_of_two( int input )
/* https://graphics.stanford.edu/%7Eseander/bithacks.html#RoundUpPowerOf2 */
int power_of_two( int v )
{
int exp = 31, i = input;
if (i >> 16)
i >>= 16;
else exp -= 16;
if (i >> 8)
i >>= 8;
else exp -= 8;
if (i >> 4)
i >>= 4;
else exp -= 4;
if (i >> 2)
i >>= 2;
else exp -= 2;
if (i >> 1 == 0)
exp -= 1;
int value = 1 << exp;
return (input == value) ? value : (value << 1);
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
/* Always be sure to return a value of at least 1. In the event of any edge
* cases, such as a zero or negative input, the returned value will be `1`. */
v += (v == 0);
return v;
}
bool IsAnInt( const RString &s )
+1 -1
View File
@@ -328,7 +328,7 @@ void fapproach( float& val, float other_val, float to_move );
/* Return a positive x mod y. */
float fmodfp( float x, float y );
int power_of_two( int input );
int power_of_two( int v );
bool IsAnInt( const RString &s );
bool IsHexVal( const RString &s );
RString BinaryToHex( const void *pData_, size_t iNumBytes );