summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorLucas Stach <l.stach@pengutronix.de>2018-02-21 10:53:33 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2018-02-21 20:03:47 +0100
commit76e439f6c4ca2e17f34d6be888f839299f6df1da (patch)
tree8613ec6f6035606985e2813c0e613805f06a8a42 /lib
parent7ecd9ebe70e5421666a0adc77b280d7eec27939a (diff)
downloadbarebox-76e439f6c4ca2e17f34d6be888f839299f6df1da.tar.gz
barebox-76e439f6c4ca2e17f34d6be888f839299f6df1da.tar.xz
lib: add weak clz/ctz functions
This is a copy of the Linux kernel implementation. This adds weak functions for the clz and ctz gcc builtins. Normally GCC will map those builtins to CPU instructions directly, but for CPUs where those are unavailable, we need to provide a fallback implementation. Fixes build of the zlib decompressor on ARMv4 CPUs. Signed-off-by: Lucas Stach <l.stach@pengutronix.de> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'lib')
-rw-r--r--lib/Makefile1
-rw-r--r--lib/clz_ctz.c59
2 files changed, 60 insertions, 0 deletions
diff --git a/lib/Makefile b/lib/Makefile
index e5cf32c162..878c2fbe03 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -63,3 +63,4 @@ obj-$(CONFIG_RATP) += ratp.o
obj-y += list_sort.o
obj-y += int_sqrt.o
obj-y += parseopt.o
+obj-y += clz_ctz.o
diff --git a/lib/clz_ctz.c b/lib/clz_ctz.c
new file mode 100644
index 0000000000..b5ac7f447a
--- /dev/null
+++ b/lib/clz_ctz.c
@@ -0,0 +1,59 @@
+/*
+ * lib/clz_ctz.c
+ *
+ * Copyright (C) 2013 Chanho Min <chanho.min@lge.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ * The functions in this file aren't called directly, but are required by
+ * GCC builtins such as __builtin_ctz, and therefore they can't be removed
+ * despite appearing unreferenced in kernel source.
+ *
+ * __c[lt]z[sd]i2 can be overridden by linking arch-specific versions.
+ */
+
+#include <linux/bitops.h>
+#include <linux/kernel.h>
+
+int __weak __ctzsi2(int val);
+int __weak __ctzsi2(int val)
+{
+ return __ffs(val);
+}
+
+int __weak __clzsi2(int val);
+int __weak __clzsi2(int val)
+{
+ return 32 - fls(val);
+}
+
+int __weak __clzdi2(long val);
+int __weak __ctzdi2(long val);
+#if BITS_PER_LONG == 32
+
+int __weak __clzdi2(long val)
+{
+ return 32 - fls((int)val);
+}
+
+int __weak __ctzdi2(long val)
+{
+ return __ffs((u32)val);
+}
+
+#elif BITS_PER_LONG == 64
+
+int __weak __clzdi2(long val)
+{
+ return 64 - fls64((u64)val);
+}
+
+int __weak __ctzdi2(long val)
+{
+ return __ffs64((u64)val);
+}
+
+#else
+#error BITS_PER_LONG not 32 or 64
+#endif