2
0
mirror of https://github.com/alliedmodders/amxmodx.git synced 2025-04-10 11:40:06 +03:00

Use exponentiation by squaring instead of n-times multiplications in power() ()

This commit is contained in:
Alexander Petrov 2016-08-27 18:12:02 +10:00 committed by Vincent Herbet
parent ea43a61094
commit a9557fe53d

@ -19,10 +19,19 @@ static cell power(AMX *amx, cell *params)
/* power(value, exponent);
* params[1] = value
* params[2] = exponent
* Exponentiation by squaring
*/
cell result = 1;
while (params[2]-- > 0)
result *= params[1];
cell value = params[1];
cell exp = params[2];
while (exp > 0) {
if (exp & 1) {
result *= value;
} /* if */
value *= value;
exp >>= 1;
} /* while */
return result;
}