int_pow.c 631 B

1234567891011121314151617181920212223242526272829303132
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * An integer based power function
  4. *
  5. * Derived from drivers/video/backlight/pwm_bl.c
  6. */
  7. #include <linux/export.h>
  8. #include <linux/kernel.h>
  9. #include <linux/types.h>
  10. /**
  11. * int_pow - computes the exponentiation of the given base and exponent
  12. * @base: base which will be raised to the given power
  13. * @exp: power to be raised to
  14. *
  15. * Computes: pow(base, exp), i.e. @base raised to the @exp power
  16. */
  17. u64 int_pow(u64 base, unsigned int exp)
  18. {
  19. u64 result = 1;
  20. while (exp) {
  21. if (exp & 1)
  22. result *= base;
  23. exp >>= 1;
  24. base *= base;
  25. }
  26. return result;
  27. }
  28. EXPORT_SYMBOL_GPL(int_pow);