mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-21 10:42:07 +00:00
Fix calculation of 2**30
In exponent(), 'base *= base;' should not run when base is 65536, since it overflows an int32_t. This also optimizes exponent() based on gcc and clang -O3 test cases in godbolt.org.
This commit is contained in:
@@ -283,14 +283,16 @@ static int32_t shift(int32_t shiftee, int32_t amount)
|
||||
}
|
||||
}
|
||||
|
||||
static int32_t exponent(int32_t base, int32_t power)
|
||||
static int32_t exponent(int32_t base, uint32_t power)
|
||||
{
|
||||
int32_t result = 1;
|
||||
|
||||
while (power) {
|
||||
for (;;) {
|
||||
if (power % 2)
|
||||
result *= base;
|
||||
power /= 2;
|
||||
if (!power)
|
||||
break;
|
||||
base *= base;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user