mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-18 16:26:31 +00:00
将 kernel\common\math\pow.c 的求幂运算优化为快速幂
This commit is contained in:
@ -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;
|
||||
}
|
Reference in New Issue
Block a user