[PATCH 01/18] lib: Add gcd() function.

Andrey Panov rockford at yandex.ru
Mon Mar 2 12:21:45 PST 2015


    It calculates greatest common divisor and is
    compiled in only if CONFIG_GCD is selected.

Signed-off-by: Andrey Panov <rockford at yandex.ru>
---
 include/linux/gcd.h | 12 ++++++++++++
 lib/Kconfig         |  3 +++
 lib/Makefile        |  1 +
 lib/gcd.c           | 19 +++++++++++++++++++
 4 files changed, 35 insertions(+)
 create mode 100644 include/linux/gcd.h
 create mode 100644 lib/gcd.c

diff --git a/include/linux/gcd.h b/include/linux/gcd.h
new file mode 100644
index 0000000..6405b27
--- /dev/null
+++ b/include/linux/gcd.h
@@ -0,0 +1,12 @@
+#ifndef _GCD_H
+#define _GCD_H
+
+#include <linux/compiler.h>
+
+#if !defined(swap)
+#define swap(x, y) do { typeof(x) z = x; x = y; y = z; } while (0)
+#endif
+
+unsigned long gcd(unsigned long a, unsigned long b) __attribute_const__;
+
+#endif /* _GCD_H */
diff --git a/lib/Kconfig b/lib/Kconfig
index 62695f1..4b9b472 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -55,6 +55,9 @@ config LIBMTD
 config STMP_DEVICE
 	bool
 
+config GCD
+	bool
+
 source lib/gui/Kconfig
 
 source lib/bootstrap/Kconfig
diff --git a/lib/Makefile b/lib/Makefile
index b97e52d..6f70f01 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -52,3 +52,4 @@ obj-$(CONFIG_STMP_DEVICE) += stmp-device.o
 obj-y			+= wchar.o
 obj-y			+= libfile.o
 obj-y			+= bitmap.o
+obj-$(CONFIG_GCD)	+= gcd.o
diff --git a/lib/gcd.c b/lib/gcd.c
new file mode 100644
index 0000000..dde8c9e
--- /dev/null
+++ b/lib/gcd.c
@@ -0,0 +1,19 @@
+#include <linux/kernel.h>
+#include <linux/gcd.h>
+
+/* Greatest common divisor */
+unsigned long gcd(unsigned long a, unsigned long b)
+{
+	unsigned long r;
+
+	if (a < b)
+		swap(a, b);
+
+	if (!b)
+		return a;
+	while ((r = a % b) != 0) {
+		a = b;
+		b = r;
+	}
+	return b;
+}
-- 
2.1.4




More information about the barebox mailing list