Skip to contentSkip to conversation

lib: Move mathematic helpers to separate folder

PATCH New Andy Shevchenko 1 participant 2 messages Mar 23, 2019

Subsystems (4)
lore
Share & tools
lore
#1 lore raw

PATCH 2/2 lib/math: Move int_pow() from pwm_bl.c for wider use

In reply to Andy Shevchenko

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

---
 drivers/video/backlight/pwm_bl.c | 15 ---------------
 include/linux/kernel.h           |  1 +
 lib/math/Makefile                |  2 +-
 lib/math/int_pow.c               | 32 ++++++++++++++++++++++++++++++++
 4 files changed, 34 insertions(+), 16 deletions(-)
 create mode 100644 lib/math/int_pow.c
drivers/video/backlight/pwm_bl.c +0 -15
index 53b8ceea9bde..fb45f866b923 100644
@@ -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.
  *
include/linux/kernel.h +1 -0
index bb8958690b69..d327fe835f2c 100644
@@ -500,6 +500,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
lib/math/Makefile +1 -1
index b75878420da6..583bbfebfc09 100644
@@ -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
lib/math/int_pow.c new file +32 -0
new file mode 100644
index 000000000000..622fc1ab3c74
@@ -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);
Signature · 1 line
2.20.1
#0 lore raw

PATCH 1/2 lib: Move mathematic helpers to separate folder

For better maintenance and expansion move the mathematic helpers to the separate folder.

No functional change intended.

Note, the int_sqrt() is not used as a part of lib, so, moved to regular obj.

---
 lib/Kconfig                     | 14 ++------------
 lib/Makefile                    | 15 ++++++---------
 lib/math/Kconfig                | 11 +++++++++++
 lib/math/Makefile               |  5 +++++
 lib/{ => math}/cordic.c         |  0
 lib/{ => math}/div64.c          |  0
 lib/{ => math}/gcd.c            |  0
 lib/{ => math}/int_sqrt.c       |  0
 lib/{ => math}/lcm.c            |  0
 lib/{ => math}/prime_numbers.c  |  0
 lib/{ => math}/rational.c       |  0
 lib/{ => math}/reciprocal_div.c |  0
 12 files changed, 24 insertions(+), 21 deletions(-)
 create mode 100644 lib/math/Kconfig
 create mode 100644 lib/math/Makefile
 rename lib/{ => math}/cordic.c (100%)
 rename lib/{ => math}/div64.c (100%)
 rename lib/{ => math}/gcd.c (100%)
 rename lib/{ => math}/int_sqrt.c (100%)
 rename lib/{ => math}/lcm.c (100%)
 rename lib/{ => math}/prime_numbers.c (100%)
 rename lib/{ => math}/rational.c (100%)
 rename lib/{ => math}/reciprocal_div.c (100%)
lib/Kconfig +2 -12
index a9e56539bd11..fb453afff32e 100644
@@ -29,9 +29,6 @@ config HAVE_ARCH_BITREVERSE
 	  This option enables the use of hardware bit-reversal instructions on
 	  architectures which support such operations.
 
-config RATIONAL
-	bool
-
 config GENERIC_STRNCPY_FROM_USER
 	bool
 
@@ -44,6 +41,8 @@ config GENERIC_NET_UTILS
 config GENERIC_FIND_FIRST_BIT
 	bool
 
+source "lib/math/Kconfig"
+
 config NO_GENERIC_PCI_IOPORT_MAP
 	bool
 
@@ -514,12 +513,6 @@ config LRU_CACHE
 config CLZ_TAB
 	bool
 
-config CORDIC
-	tristate "CORDIC algorithm"
-	help
-	  This option provides an implementation of the CORDIC algorithm;
-	  calculations are in fixed point. Module will be called cordic.
-
 config DDR
 	bool "JEDEC DDR data"
 	help
@@ -607,9 +600,6 @@ config SBITMAP
 config PARMAN
 	tristate "parman" if COMPILE_TEST
 
-config PRIME_NUMBERS
-	tristate
-
 config STRING_SELFTEST
 	tristate "Test string functions"
 
lib/Makefile +6 -9
index 3b08673e8881..4eeb814eee2e 100644
@@ -19,7 +19,7 @@ KCOV_INSTRUMENT_dynamic_debug.o := n
 
 lib-y := ctype.o string.o vsprintf.o cmdline.o \
 	 rbtree.o radix-tree.o timerqueue.o xarray.o \
-	 idr.o int_sqrt.o extable.o \
+	 idr.o extable.o \
 	 sha1.o chacha.o irq_regs.o argv_split.o \
 	 flex_proportions.o ratelimit.o show_mem.o \
 	 is_single_threaded.o plist.o decompress.o kobject_uevent.o \
@@ -33,11 +33,11 @@ lib-$(CONFIG_SMP) += cpumask.o
 lib-y	+= kobject.o klist.o
 obj-y	+= lockref.o
 
-obj-y += bcd.o div64.o sort.o parser.o debug_locks.o random32.o \
+obj-y += bcd.o sort.o parser.o debug_locks.o random32.o \
 	 bust_spinlocks.o kasprintf.o bitmap.o scatterlist.o \
-	 gcd.o lcm.o list_sort.o uuid.o iov_iter.o clz_ctz.o \
+	 list_sort.o uuid.o iov_iter.o clz_ctz.o \
 	 bsearch.o find_bit.o llist.o memweight.o kfifo.o \
-	 percpu-refcount.o rhashtable.o reciprocal_div.o \
+	 percpu-refcount.o rhashtable.o \
 	 once.o refcount.o usercopy.o errseq.o bucket_locks.o \
 	 generic-radix-tree.o
 obj-$(CONFIG_STRING_SELFTEST) += test_string.o
@@ -90,6 +90,8 @@ endif
 obj-$(CONFIG_DEBUG_INFO_REDUCED) += debug_info.o
 CFLAGS_debug_info.o += $(call cc-option, -femit-struct-debug-detailed=any)
 
+obj-y += math/
+
 obj-$(CONFIG_GENERIC_IOMAP) += iomap.o
 obj-$(CONFIG_GENERIC_PCI_IOMAP) += pci_iomap.o
 obj-$(CONFIG_HAS_IOMEM) += iomap_copy.o devres.o
@@ -108,7 +110,6 @@ obj-$(CONFIG_DEBUG_LIST) += list_debug.o
 obj-$(CONFIG_DEBUG_OBJECTS) += debugobjects.o
 
 obj-$(CONFIG_BITREVERSE) += bitrev.o
-obj-$(CONFIG_RATIONAL)	+= rational.o
 obj-$(CONFIG_CRC_CCITT)	+= crc-ccitt.o
 obj-$(CONFIG_CRC16)	+= crc16.o
 obj-$(CONFIG_CRC_T10DIF)+= crc-t10dif.o
@@ -182,8 +183,6 @@ obj-$(CONFIG_ATOMIC64_SELFTEST) += atomic64_test.o
 
 obj-$(CONFIG_CPU_RMAP) += cpu_rmap.o
 
-obj-$(CONFIG_CORDIC) += cordic.o
-
 obj-$(CONFIG_DQL) += dynamic_queue_limits.o
 
 obj-$(CONFIG_GLOB) += glob.o
@@ -225,8 +224,6 @@ obj-$(CONFIG_ASN1) += asn1_decoder.o
 
 obj-$(CONFIG_FONT_SUPPORT) += fonts/
 
-obj-$(CONFIG_PRIME_NUMBERS) += prime_numbers.o
-
 hostprogs-y	:= gen_crc32table
 hostprogs-y	+= gen_crc64table
 clean-files	:= crc32table.h
lib/math/Kconfig new file +11 -0
new file mode 100644
index 000000000000..73bdf37178d1
@@ -0,0 +1,11 @@
+config CORDIC
+	tristate "CORDIC algorithm"
+	help
+	  This option provides an implementation of the CORDIC algorithm;
+	  calculations are in fixed point. Module will be called cordic.
+
+config PRIME_NUMBERS
+	tristate
+
+config RATIONAL
+	bool
lib/math/Makefile new file +5 -0
new file mode 100644
index 000000000000..b75878420da6
@@ -0,0 +1,5 @@
+obj-y += div64.o gcd.o lcm.o int_sqrt.o reciprocal_div.o
+
+obj-$(CONFIG_CORDIC)		+= cordic.o
+obj-$(CONFIG_PRIME_NUMBERS)	+= prime_numbers.o
+obj-$(CONFIG_RATIONAL)		+= rational.o
lib/math/cordic.c renamed from lib/cordic.c +0 -0
similarity index 100%
rename from lib/cordic.c
rename to lib/math/cordic.c
lib/math/div64.c renamed from lib/div64.c +0 -0
similarity index 100%
rename from lib/div64.c
rename to lib/math/div64.c
lib/math/gcd.c renamed from lib/gcd.c +0 -0
similarity index 100%
rename from lib/gcd.c
rename to lib/math/gcd.c
lib/math/int_sqrt.c renamed from lib/int_sqrt.c +0 -0
similarity index 100%
rename from lib/int_sqrt.c
rename to lib/math/int_sqrt.c
lib/math/lcm.c renamed from lib/lcm.c +0 -0
similarity index 100%
rename from lib/lcm.c
rename to lib/math/lcm.c
lib/math/prime_numbers.c renamed from lib/prime_numbers.c +0 -0
similarity index 100%
rename from lib/prime_numbers.c
rename to lib/math/prime_numbers.c
lib/math/rational.c renamed from lib/rational.c +0 -0
similarity index 100%
rename from lib/rational.c
rename to lib/math/rational.c
lib/math/reciprocal_div.c renamed from lib/reciprocal_div.c +0 -0
similarity index 100%
rename from lib/reciprocal_div.c
rename to lib/math/reciprocal_div.c
Signature · 1 line
2.20.1