From 0f164cb73d243a909a07b3c6ed09629b0b6349d0 Mon Sep 17 00:00:00 2001
From: naddy <>
Date: Fri, 26 Jun 2020 20:16:22 +0000
Subject: Provide an optimized implementation of ffs(3) in libc on
 aarch64/powerpc/powerpc64, making use of the count leading zeros instruction.
  Also add a brief regression test. ok deraadt@ kettenis@

---
 src/regress/lib/libc/Makefile       |  4 ++--
 src/regress/lib/libc/ffs/Makefile   |  6 ++++++
 src/regress/lib/libc/ffs/ffs_test.c | 18 ++++++++++++++++++
 3 files changed, 26 insertions(+), 2 deletions(-)
 create mode 100644 src/regress/lib/libc/ffs/Makefile
 create mode 100644 src/regress/lib/libc/ffs/ffs_test.c

(limited to 'src')

diff --git a/src/regress/lib/libc/Makefile b/src/regress/lib/libc/Makefile
index 3d0712f951..9ad3120961 100644
--- a/src/regress/lib/libc/Makefile
+++ b/src/regress/lib/libc/Makefile
@@ -1,4 +1,4 @@
-#	$OpenBSD: Makefile,v 1.55 2020/03/23 03:01:21 schwarze Exp $
+#	$OpenBSD: Makefile,v 1.56 2020/06/26 20:16:21 naddy Exp $
 
 SUBDIR+= _setjmp
 SUBDIR+= alloca arc4random-fork atexit
@@ -6,7 +6,7 @@ SUBDIR+= basename
 SUBDIR+= cephes cxa-atexit
 SUBDIR+= db dirname
 SUBDIR+= env explicit_bzero
-SUBDIR+= fmemopen fnmatch fpclassify fread
+SUBDIR+= ffs fmemopen fnmatch fpclassify fread
 SUBDIR+= gcvt getaddrinfo getcap getopt getopt_long glob
 SUBDIR+= hsearch
 SUBDIR+= ieeefp ifnameindex
diff --git a/src/regress/lib/libc/ffs/Makefile b/src/regress/lib/libc/ffs/Makefile
new file mode 100644
index 0000000000..1ad004394f
--- /dev/null
+++ b/src/regress/lib/libc/ffs/Makefile
@@ -0,0 +1,6 @@
+PROG=		ffs_test
+
+# prevent constant folding and inlining of __builtin_ffs()
+CFLAGS+=	-ffreestanding
+
+.include <bsd.regress.mk>
diff --git a/src/regress/lib/libc/ffs/ffs_test.c b/src/regress/lib/libc/ffs/ffs_test.c
new file mode 100644
index 0000000000..bc1e5c53c2
--- /dev/null
+++ b/src/regress/lib/libc/ffs/ffs_test.c
@@ -0,0 +1,18 @@
+/*	$OpenBSD: ffs_test.c,v 1.1 2020/06/26 20:16:22 naddy Exp $ */
+/*
+ * Written by Christian Weisgerber <naddy@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);
+}
-- 
cgit v1.2.3-55-g6feb