Skip to content

Commit

Permalink
lib/math: move int_pow() from pwm_bl.c for wider use
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
andy-shev authored and torvalds committed May 15, 2019
1 parent 2c64e9c commit 9f61589
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
15 changes: 0 additions & 15 deletions drivers/video/backlight/pwm_bl.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,6 @@ static const struct backlight_ops pwm_backlight_ops = {
#ifdef CONFIG_OF
#define PWM_LUMINANCE_SCALE 10000 /* luminance scale */

/* An integer based power function */
static u64 int_pow(u64 base, int exp)
{
u64 result = 1;

while (exp) {
if (exp & 1)
result *= base;
exp >>= 1;
base *= base;
}

return result;
}

/*
* CIE lightness to PWM conversion.
*
Expand Down
1 change: 1 addition & 0 deletions include/linux/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ extern int __kernel_text_address(unsigned long addr);
extern int kernel_text_address(unsigned long addr);
extern int func_ptr_is_kernel_text(void *ptr);

u64 int_pow(u64 base, unsigned int exp);
unsigned long int_sqrt(unsigned long);

#if BITS_PER_LONG < 64
Expand Down
2 changes: 1 addition & 1 deletion lib/math/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
obj-y += div64.o gcd.o lcm.o int_sqrt.o reciprocal_div.o
obj-y += div64.o gcd.o lcm.o int_pow.o int_sqrt.o reciprocal_div.o

obj-$(CONFIG_CORDIC) += cordic.o
obj-$(CONFIG_PRIME_NUMBERS) += prime_numbers.o
Expand Down
32 changes: 32 additions & 0 deletions lib/math/int_pow.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: GPL-2.0
/*
* An integer based power function
*
* Derived from drivers/video/backlight/pwm_bl.c
*/

#include <linux/export.h>
#include <linux/kernel.h>
#include <linux/types.h>

/**
* int_pow - computes the exponentiation of the given base and exponent
* @base: base which will be raised to the given power
* @exp: power to be raised to
*
* Computes: pow(base, exp), i.e. @base raised to the @exp power
*/
u64 int_pow(u64 base, unsigned int exp)
{
u64 result = 1;

while (exp) {
if (exp & 1)
result *= base;
exp >>= 1;
base *= base;
}

return result;
}
EXPORT_SYMBOL_GPL(int_pow);

0 comments on commit 9f61589

Please sign in to comment.