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-11 22:55:29 -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; 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; v--;
if (i >> 16) v |= v >> 1;
i >>= 16; v |= v >> 2;
else exp -= 16; v |= v >> 4;
if (i >> 8) v |= v >> 8;
i >>= 8; v |= v >> 16;
else exp -= 8; v++;
if (i >> 4)
i >>= 4; /* Always be sure to return a value of at least 1. In the event of any edge
else exp -= 4; * cases, such as a zero or negative input, the returned value will be `1`. */
if (i >> 2) v += (v == 0);
i >>= 2; return v;
else exp -= 2;
if (i >> 1 == 0)
exp -= 1;
int value = 1 << exp;
return (input == value) ? value : (value << 1);
} }
bool IsAnInt( const RString &s ) 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. */ /* Return a positive x mod y. */
float fmodfp( float x, float 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 IsAnInt( const RString &s );
bool IsHexVal( const RString &s ); bool IsHexVal( const RString &s );
RString BinaryToHex( const void *pData_, size_t iNumBytes ); RString BinaryToHex( const void *pData_, size_t iNumBytes );