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