将 kernel\common\math\pow.c 的求幂运算优化为快速幂

This commit is contained in:
Eugene
2022-08-14 21:27:21 +08:00
parent fb1144fbda
commit 618b612754
4 changed files with 18 additions and 5 deletions

View File

@ -3,8 +3,19 @@
int64_t pow(int64_t x, int y)
{
if (y == 0)
return 1;
if (y == 1)
return x;
if (y == 2)
return x * x;
int64_t res = 1;
for (int i = 0; i < y; ++i)
res *= x;
while (y != 0)
{
if (y & 1)
res *= x;
y >>= 1;
x *= x;
}
return res;
}