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

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

View File

@ -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;
}