pow.c 334 B

123456789101112131415161718192021
  1. #include <common/math.h>
  2. #include <common/stddef.h>
  3. int64_t pow(int64_t x, int y)
  4. {
  5. if (y == 0)
  6. return 1;
  7. if (y == 1)
  8. return x;
  9. if (y == 2)
  10. return x * x;
  11. int64_t res = 1;
  12. while (y != 0)
  13. {
  14. if (y & 1)
  15. res *= x;
  16. y >>= 1;
  17. x *= x;
  18. }
  19. return res;
  20. }