xref: /linux/lib/math/int_pow.c (revision cdd38c5f1ce4398ec58fec95904b75824daab7b5)
19f615894SAndy Shevchenko // SPDX-License-Identifier: GPL-2.0
29f615894SAndy Shevchenko /*
39f615894SAndy Shevchenko  * An integer based power function
49f615894SAndy Shevchenko  *
59f615894SAndy Shevchenko  * Derived from drivers/video/backlight/pwm_bl.c
69f615894SAndy Shevchenko  */
79f615894SAndy Shevchenko 
89f615894SAndy Shevchenko #include <linux/export.h>
9*aa6159abSAndy Shevchenko #include <linux/math.h>
109f615894SAndy Shevchenko #include <linux/types.h>
119f615894SAndy Shevchenko 
129f615894SAndy Shevchenko /**
139f615894SAndy Shevchenko  * int_pow - computes the exponentiation of the given base and exponent
149f615894SAndy Shevchenko  * @base: base which will be raised to the given power
159f615894SAndy Shevchenko  * @exp: power to be raised to
169f615894SAndy Shevchenko  *
179f615894SAndy Shevchenko  * Computes: pow(base, exp), i.e. @base raised to the @exp power
189f615894SAndy Shevchenko  */
int_pow(u64 base,unsigned int exp)199f615894SAndy Shevchenko u64 int_pow(u64 base, unsigned int exp)
209f615894SAndy Shevchenko {
219f615894SAndy Shevchenko 	u64 result = 1;
229f615894SAndy Shevchenko 
239f615894SAndy Shevchenko 	while (exp) {
249f615894SAndy Shevchenko 		if (exp & 1)
259f615894SAndy Shevchenko 			result *= base;
269f615894SAndy Shevchenko 		exp >>= 1;
279f615894SAndy Shevchenko 		base *= base;
289f615894SAndy Shevchenko 	}
299f615894SAndy Shevchenko 
309f615894SAndy Shevchenko 	return result;
319f615894SAndy Shevchenko }
329f615894SAndy Shevchenko EXPORT_SYMBOL_GPL(int_pow);
33