This adds the optimized ffs(3) versions on aarch64 and powerpc to
libc, for completeness' sake.

Also add a brief regression test.

OK?

Index: lib/libc/arch/aarch64/string/ffs.c
===================================================================
RCS file: lib/libc/arch/aarch64/string/ffs.c
diff -N lib/libc/arch/aarch64/string/ffs.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ lib/libc/arch/aarch64/string/ffs.c  9 Jun 2020 19:54:21 -0000
@@ -0,0 +1,15 @@
+/*     $OpenBSD$ */
+/*
+ * Written by Christian Weisgerber <na...@openbsd.org>.
+ * Public domain.
+ */
+
+#include <string.h>
+
+int
+ffs(int x)
+{
+       x = x & -x;
+       __asm volatile("clz %w0, %w0" : "+r" (x));
+       return (32 - x);
+}
Index: lib/libc/arch/powerpc/string/ffs.c
===================================================================
RCS file: lib/libc/arch/powerpc/string/ffs.c
diff -N lib/libc/arch/powerpc/string/ffs.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ lib/libc/arch/powerpc/string/ffs.c  9 Jun 2020 19:54:41 -0000
@@ -0,0 +1,15 @@
+/*     $OpenBSD$ */
+/*
+ * Written by Christian Weisgerber <na...@openbsd.org>.
+ * Public domain.
+ */
+
+#include <string.h>
+
+int
+ffs(int x)
+{
+       x = x & -x;
+       __asm volatile("cntlzw %0, %0" : "+r" (x));
+       return (32 - x);
+}
Index: regress/lib/libc/ffs/Makefile
===================================================================
RCS file: regress/lib/libc/ffs/Makefile
diff -N regress/lib/libc/ffs/Makefile
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ regress/lib/libc/ffs/Makefile       9 Jun 2020 19:45:02 -0000
@@ -0,0 +1,8 @@
+# $OpenBSD$
+
+PROG=          ffs_test
+
+# prevent inlining of __builtin_ffs()
+CFLAGS+=       -ffreestanding
+
+.include <bsd.regress.mk>
Index: regress/lib/libc/ffs/ffs_test.c
===================================================================
RCS file: regress/lib/libc/ffs/ffs_test.c
diff -N regress/lib/libc/ffs/ffs_test.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ regress/lib/libc/ffs/ffs_test.c     9 Jun 2020 19:40:33 -0000
@@ -0,0 +1,18 @@
+/*     $OpenBSD$ */
+/*
+ * Written by Christian Weisgerber <na...@openbsd.org>.
+ * Public domain.
+ */
+
+#include <assert.h>
+#include <stdint.h>
+#include <string.h>
+
+int
+main(void)
+{
+       assert(ffs(0) == 0);
+       assert(ffs(0x8080) == 8);
+       assert(ffs(INT32_MIN) == 32);
+       return (0);
+}
-- 
Christian "naddy" Weisgerber                          na...@mips.inka.de

Reply via email to