Skip to content

Commit 9f61589

Browse files
andy-shevtorvalds
authored andcommitted
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>
1 parent 2c64e9c commit 9f61589

File tree

4 files changed

+34
-16
lines changed

4 files changed

+34
-16
lines changed

drivers/video/backlight/pwm_bl.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -155,21 +155,6 @@ static const struct backlight_ops pwm_backlight_ops = {
155155
#ifdef CONFIG_OF
156156
#define PWM_LUMINANCE_SCALE 10000 /* luminance scale */
157157

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-
173158
/*
174159
* CIE lightness to PWM conversion.
175160
*

include/linux/kernel.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ extern int __kernel_text_address(unsigned long addr);
484484
extern int kernel_text_address(unsigned long addr);
485485
extern int func_ptr_is_kernel_text(void *ptr);
486486

487+
u64 int_pow(u64 base, unsigned int exp);
487488
unsigned long int_sqrt(unsigned long);
488489

489490
#if BITS_PER_LONG < 64

lib/math/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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
22

33
obj-$(CONFIG_CORDIC) += cordic.o
44
obj-$(CONFIG_PRIME_NUMBERS) += prime_numbers.o

lib/math/int_pow.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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);

0 commit comments

Comments
 (0)