Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

lib/math: move int_pow() from pwm_bl.c for wider use

The integer exponentiation is used in few places and might be used in
the future by other call sites. Move it to wider use.

Link: http://lkml.kernel.org/r/20190323172531.80025-2-andriy.shevchenko@linux.intel.com
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Daniel Thompson <daniel.thompson@linaro.org>
Cc: Lee Jones <lee.jones@linaro.org>
Cc: Ray Jui <rjui@broadcom.com>
Cc: Thierry Reding <thierry.reding@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Andy Shevchenko and committed by
Linus Torvalds
9f615894 2c64e9cb

+34 -16
-15
drivers/video/backlight/pwm_bl.c
··· 155 155 #ifdef CONFIG_OF 156 156 #define PWM_LUMINANCE_SCALE 10000 /* luminance scale */ 157 157 158 - /* An integer based power function */ 159 - static u64 int_pow(u64 base, int exp) 160 - { 161 - u64 result = 1; 162 - 163 - while (exp) { 164 - if (exp & 1) 165 - result *= base; 166 - exp >>= 1; 167 - base *= base; 168 - } 169 - 170 - return result; 171 - } 172 - 173 158 /* 174 159 * CIE lightness to PWM conversion. 175 160 *
+1
include/linux/kernel.h
··· 484 484 extern int kernel_text_address(unsigned long addr); 485 485 extern int func_ptr_is_kernel_text(void *ptr); 486 486 487 + u64 int_pow(u64 base, unsigned int exp); 487 488 unsigned long int_sqrt(unsigned long); 488 489 489 490 #if BITS_PER_LONG < 64
+1 -1
lib/math/Makefile
··· 1 - obj-y += div64.o gcd.o lcm.o int_sqrt.o reciprocal_div.o 1 + obj-y += div64.o gcd.o lcm.o int_pow.o int_sqrt.o reciprocal_div.o 2 2 3 3 obj-$(CONFIG_CORDIC) += cordic.o 4 4 obj-$(CONFIG_PRIME_NUMBERS) += prime_numbers.o
+32
lib/math/int_pow.c
··· 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 + 8 + #include <linux/export.h> 9 + #include <linux/kernel.h> 10 + #include <linux/types.h> 11 + 12 + /** 13 + * int_pow - computes the exponentiation of the given base and exponent 14 + * @base: base which will be raised to the given power 15 + * @exp: power to be raised to 16 + * 17 + * Computes: pow(base, exp), i.e. @base raised to the @exp power 18 + */ 19 + u64 int_pow(u64 base, unsigned int exp) 20 + { 21 + u64 result = 1; 22 + 23 + while (exp) { 24 + if (exp & 1) 25 + result *= base; 26 + exp >>= 1; 27 + base *= base; 28 + } 29 + 30 + return result; 31 + } 32 + EXPORT_SYMBOL_GPL(int_pow);