ffs(3): libc arch versions, regress

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

ffs(3): libc arch versions, regress

Christian Weisgerber
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 <[hidden email]>.
+ * 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 <[hidden email]>.
+ * 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 <[hidden email]>.
+ * 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                          [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: ffs(3): libc arch versions, regress

Christian Weisgerber
Christian Weisgerber:

> 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/Makefile.inc
===================================================================
RCS file: /cvs/src/lib/libc/arch/aarch64/string/Makefile.inc,v
retrieving revision 1.1
diff -u -p -r1.1 Makefile.inc
--- lib/libc/arch/aarch64/string/Makefile.inc 11 Jan 2017 18:09:24 -0000 1.1
+++ lib/libc/arch/aarch64/string/Makefile.inc 11 Jun 2020 20:30:34 -0000
@@ -2,7 +2,7 @@
 
 SRCS+= bcopy.c memcpy.c memmove.c \
  strchr.c strrchr.c \
- bcmp.c bzero.c ffs.c memchr.c memcmp.c memset.c \
+ bcmp.c bzero.c ffs.S memchr.c memcmp.c memset.c \
  strcmp.c strncmp.c \
  strcat.c strcpy.c strcspn.c strlen.c strlcat.c strlcpy.c \
  strncat.c  strncpy.c strpbrk.c strsep.c \
Index: lib/libc/arch/aarch64/string/ffs.S
===================================================================
RCS file: lib/libc/arch/aarch64/string/ffs.S
diff -N lib/libc/arch/aarch64/string/ffs.S
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/libc/arch/aarch64/string/ffs.S 11 Jun 2020 20:31:19 -0000
@@ -0,0 +1,18 @@
+/* $OpenBSD$ */
+/*
+ * Written by Christian Weisgerber <[hidden email]>.
+ * Public domain.
+ */
+
+#include "DEFS.h"
+
+ENTRY(ffs)
+ RETGUARD_SETUP(ffs, x15)
+ rbit w1, w0
+ clz w1, w1
+ cmp w0, wzr
+ csinc w0, wzr, w1, eq
+ RETGUARD_CHECK(ffs, x15)
+ ret
+END(ffs)
+.protected
Index: lib/libc/arch/powerpc/string/Makefile.inc
===================================================================
RCS file: /cvs/src/lib/libc/arch/powerpc/string/Makefile.inc,v
retrieving revision 1.6
diff -u -p -r1.6 Makefile.inc
--- lib/libc/arch/powerpc/string/Makefile.inc 15 May 2015 22:29:37 -0000 1.6
+++ lib/libc/arch/powerpc/string/Makefile.inc 11 Jun 2020 20:33:04 -0000
@@ -2,7 +2,7 @@
 
 SRCS+= memcpy.c memmove.S \
  strchr.c strrchr.c \
- bcmp.c bzero.c ffs.c memchr.c memcmp.c memset.c strcat.c \
+ bcmp.c bzero.c ffs.S memchr.c memcmp.c memset.c strcat.c \
  strcmp.c strcpy.c strcspn.c strlen.c strlcat.c strlcpy.c \
  strncat.c strncmp.c strncpy.c strpbrk.c strsep.c \
  strspn.c strstr.c swab.c
Index: lib/libc/arch/powerpc/string/ffs.S
===================================================================
RCS file: lib/libc/arch/powerpc/string/ffs.S
diff -N lib/libc/arch/powerpc/string/ffs.S
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/libc/arch/powerpc/string/ffs.S 11 Jun 2020 20:33:19 -0000
@@ -0,0 +1,16 @@
+/* $OpenBSD$ */
+/*
+ * Written by Christian Weisgerber <[hidden email]>.
+ * Public domain.
+ */
+
+#include "SYS.h"
+
+ENTRY(ffs)
+ neg %r4, %r3
+ and %r3, %r3, %r4
+ cntlzw %r3, %r3
+ subfic %r3, %r3, 32
+ blr
+END(ffs)
+.protected
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 12 Jun 2020 08:43:43 -0000
@@ -0,0 +1,6 @@
+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 12 Jun 2020 08:43:19 -0000
@@ -0,0 +1,18 @@
+/* $OpenBSD$ */
+/*
+ * Written by Christian Weisgerber <[hidden email]>.
+ * 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                          [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: ffs(3): libc arch versions, regress

Christian Weisgerber
Trying again, this time with powerpc64 added:

This adds the optimized ffs(3) versions on aarch64, powerpc, and
powerpc64 to libc.  Also add a brief regression test.

Index: lib/libc/arch/aarch64/string/Makefile.inc
===================================================================
RCS file: /cvs/src/lib/libc/arch/aarch64/string/Makefile.inc,v
retrieving revision 1.1
diff -u -p -r1.1 Makefile.inc
--- lib/libc/arch/aarch64/string/Makefile.inc 11 Jan 2017 18:09:24 -0000 1.1
+++ lib/libc/arch/aarch64/string/Makefile.inc 11 Jun 2020 20:30:34 -0000
@@ -2,7 +2,7 @@
 
 SRCS+= bcopy.c memcpy.c memmove.c \
  strchr.c strrchr.c \
- bcmp.c bzero.c ffs.c memchr.c memcmp.c memset.c \
+ bcmp.c bzero.c ffs.S memchr.c memcmp.c memset.c \
  strcmp.c strncmp.c \
  strcat.c strcpy.c strcspn.c strlen.c strlcat.c strlcpy.c \
  strncat.c  strncpy.c strpbrk.c strsep.c \
Index: lib/libc/arch/aarch64/string/ffs.S
===================================================================
RCS file: lib/libc/arch/aarch64/string/ffs.S
diff -N lib/libc/arch/aarch64/string/ffs.S
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/libc/arch/aarch64/string/ffs.S 11 Jun 2020 20:31:19 -0000
@@ -0,0 +1,18 @@
+/* $OpenBSD$ */
+/*
+ * Written by Christian Weisgerber <[hidden email]>.
+ * Public domain.
+ */
+
+#include "DEFS.h"
+
+ENTRY(ffs)
+ RETGUARD_SETUP(ffs, x15)
+ rbit w1, w0
+ clz w1, w1
+ cmp w0, wzr
+ csinc w0, wzr, w1, eq
+ RETGUARD_CHECK(ffs, x15)
+ ret
+END(ffs)
+.protected
Index: lib/libc/arch/powerpc/string/Makefile.inc
===================================================================
RCS file: /cvs/src/lib/libc/arch/powerpc/string/Makefile.inc,v
retrieving revision 1.6
diff -u -p -r1.6 Makefile.inc
--- lib/libc/arch/powerpc/string/Makefile.inc 15 May 2015 22:29:37 -0000 1.6
+++ lib/libc/arch/powerpc/string/Makefile.inc 11 Jun 2020 20:33:04 -0000
@@ -2,7 +2,7 @@
 
 SRCS+= memcpy.c memmove.S \
  strchr.c strrchr.c \
- bcmp.c bzero.c ffs.c memchr.c memcmp.c memset.c strcat.c \
+ bcmp.c bzero.c ffs.S memchr.c memcmp.c memset.c strcat.c \
  strcmp.c strcpy.c strcspn.c strlen.c strlcat.c strlcpy.c \
  strncat.c strncmp.c strncpy.c strpbrk.c strsep.c \
  strspn.c strstr.c swab.c
Index: lib/libc/arch/powerpc/string/ffs.S
===================================================================
RCS file: lib/libc/arch/powerpc/string/ffs.S
diff -N lib/libc/arch/powerpc/string/ffs.S
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/libc/arch/powerpc/string/ffs.S 11 Jun 2020 20:33:19 -0000
@@ -0,0 +1,16 @@
+/* $OpenBSD$ */
+/*
+ * Written by Christian Weisgerber <[hidden email]>.
+ * Public domain.
+ */
+
+#include "SYS.h"
+
+ENTRY(ffs)
+ neg %r4, %r3
+ and %r3, %r3, %r4
+ cntlzw %r3, %r3
+ subfic %r3, %r3, 32
+ blr
+END(ffs)
+.protected
Index: lib/libc/arch/powerpc64/string/Makefile.inc
===================================================================
RCS file: /cvs/src/lib/libc/arch/powerpc64/string/Makefile.inc,v
retrieving revision 1.1
diff -u -p -r1.1 Makefile.inc
--- lib/libc/arch/powerpc64/string/Makefile.inc 25 Jun 2020 02:34:22 -0000 1.1
+++ lib/libc/arch/powerpc64/string/Makefile.inc 25 Jun 2020 20:53:42 -0000
@@ -2,7 +2,7 @@
 
 SRCS+= memcpy.c memmove.S \
  strchr.c strrchr.c \
- bcmp.c bzero.c ffs.c memchr.c memcmp.c memset.c strcat.c \
+ bcmp.c bzero.c ffs.S memchr.c memcmp.c memset.c strcat.c \
  strcmp.c strcpy.c strcspn.c strlen.c strlcat.c strlcpy.c \
  strncat.c strncmp.c strncpy.c strpbrk.c strsep.c \
  strspn.c strstr.c swab.c
Index: lib/libc/arch/powerpc64/string/ffs.S
===================================================================
RCS file: lib/libc/arch/powerpc64/string/ffs.S
diff -N lib/libc/arch/powerpc64/string/ffs.S
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/libc/arch/powerpc64/string/ffs.S 25 Jun 2020 20:57:16 -0000
@@ -0,0 +1,15 @@
+/* $OpenBSD$ */
+/*
+ * Written by Christian Weisgerber <[hidden email]>.
+ * Public domain.
+ */
+
+#include "DEFS.h"
+
+ENTRY(ffs)
+ neg %r4, %r3
+ and %r3, %r3, %r4
+ cntlzw %r3, %r3
+ subfic %r3, %r3, 32
+ blr
+END_BUILTIN(ffs)
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 20 Jun 2020 15:26:51 -0000
@@ -0,0 +1,6 @@
+PROG= ffs_test
+
+# prevent constant folding and 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 12 Jun 2020 08:43:19 -0000
@@ -0,0 +1,18 @@
+/* $OpenBSD$ */
+/*
+ * Written by Christian Weisgerber <[hidden email]>.
+ * 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                          [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: ffs(3): libc arch versions, regress

Mark Kettenis
> Date: Thu, 25 Jun 2020 23:05:36 +0200
> From: Christian Weisgerber <[hidden email]>
>
> Trying again, this time with powerpc64 added:
>
> This adds the optimized ffs(3) versions on aarch64, powerpc, and
> powerpc64 to libc.  Also add a brief regression test.

Not yet in the position to test the powerpc64 implementation.  But it
should work.

ok kettenis@

> Index: lib/libc/arch/aarch64/string/Makefile.inc
> ===================================================================
> RCS file: /cvs/src/lib/libc/arch/aarch64/string/Makefile.inc,v
> retrieving revision 1.1
> diff -u -p -r1.1 Makefile.inc
> --- lib/libc/arch/aarch64/string/Makefile.inc 11 Jan 2017 18:09:24 -0000 1.1
> +++ lib/libc/arch/aarch64/string/Makefile.inc 11 Jun 2020 20:30:34 -0000
> @@ -2,7 +2,7 @@
>  
>  SRCS+= bcopy.c memcpy.c memmove.c \
>   strchr.c strrchr.c \
> - bcmp.c bzero.c ffs.c memchr.c memcmp.c memset.c \
> + bcmp.c bzero.c ffs.S memchr.c memcmp.c memset.c \
>   strcmp.c strncmp.c \
>   strcat.c strcpy.c strcspn.c strlen.c strlcat.c strlcpy.c \
>   strncat.c  strncpy.c strpbrk.c strsep.c \
> Index: lib/libc/arch/aarch64/string/ffs.S
> ===================================================================
> RCS file: lib/libc/arch/aarch64/string/ffs.S
> diff -N lib/libc/arch/aarch64/string/ffs.S
> --- /dev/null 1 Jan 1970 00:00:00 -0000
> +++ lib/libc/arch/aarch64/string/ffs.S 11 Jun 2020 20:31:19 -0000
> @@ -0,0 +1,18 @@
> +/* $OpenBSD$ */
> +/*
> + * Written by Christian Weisgerber <[hidden email]>.
> + * Public domain.
> + */
> +
> +#include "DEFS.h"
> +
> +ENTRY(ffs)
> + RETGUARD_SETUP(ffs, x15)
> + rbit w1, w0
> + clz w1, w1
> + cmp w0, wzr
> + csinc w0, wzr, w1, eq
> + RETGUARD_CHECK(ffs, x15)
> + ret
> +END(ffs)
> +.protected
> Index: lib/libc/arch/powerpc/string/Makefile.inc
> ===================================================================
> RCS file: /cvs/src/lib/libc/arch/powerpc/string/Makefile.inc,v
> retrieving revision 1.6
> diff -u -p -r1.6 Makefile.inc
> --- lib/libc/arch/powerpc/string/Makefile.inc 15 May 2015 22:29:37 -0000 1.6
> +++ lib/libc/arch/powerpc/string/Makefile.inc 11 Jun 2020 20:33:04 -0000
> @@ -2,7 +2,7 @@
>  
>  SRCS+= memcpy.c memmove.S \
>   strchr.c strrchr.c \
> - bcmp.c bzero.c ffs.c memchr.c memcmp.c memset.c strcat.c \
> + bcmp.c bzero.c ffs.S memchr.c memcmp.c memset.c strcat.c \
>   strcmp.c strcpy.c strcspn.c strlen.c strlcat.c strlcpy.c \
>   strncat.c strncmp.c strncpy.c strpbrk.c strsep.c \
>   strspn.c strstr.c swab.c
> Index: lib/libc/arch/powerpc/string/ffs.S
> ===================================================================
> RCS file: lib/libc/arch/powerpc/string/ffs.S
> diff -N lib/libc/arch/powerpc/string/ffs.S
> --- /dev/null 1 Jan 1970 00:00:00 -0000
> +++ lib/libc/arch/powerpc/string/ffs.S 11 Jun 2020 20:33:19 -0000
> @@ -0,0 +1,16 @@
> +/* $OpenBSD$ */
> +/*
> + * Written by Christian Weisgerber <[hidden email]>.
> + * Public domain.
> + */
> +
> +#include "SYS.h"
> +
> +ENTRY(ffs)
> + neg %r4, %r3
> + and %r3, %r3, %r4
> + cntlzw %r3, %r3
> + subfic %r3, %r3, 32
> + blr
> +END(ffs)
> +.protected
> Index: lib/libc/arch/powerpc64/string/Makefile.inc
> ===================================================================
> RCS file: /cvs/src/lib/libc/arch/powerpc64/string/Makefile.inc,v
> retrieving revision 1.1
> diff -u -p -r1.1 Makefile.inc
> --- lib/libc/arch/powerpc64/string/Makefile.inc 25 Jun 2020 02:34:22 -0000 1.1
> +++ lib/libc/arch/powerpc64/string/Makefile.inc 25 Jun 2020 20:53:42 -0000
> @@ -2,7 +2,7 @@
>  
>  SRCS+= memcpy.c memmove.S \
>   strchr.c strrchr.c \
> - bcmp.c bzero.c ffs.c memchr.c memcmp.c memset.c strcat.c \
> + bcmp.c bzero.c ffs.S memchr.c memcmp.c memset.c strcat.c \
>   strcmp.c strcpy.c strcspn.c strlen.c strlcat.c strlcpy.c \
>   strncat.c strncmp.c strncpy.c strpbrk.c strsep.c \
>   strspn.c strstr.c swab.c
> Index: lib/libc/arch/powerpc64/string/ffs.S
> ===================================================================
> RCS file: lib/libc/arch/powerpc64/string/ffs.S
> diff -N lib/libc/arch/powerpc64/string/ffs.S
> --- /dev/null 1 Jan 1970 00:00:00 -0000
> +++ lib/libc/arch/powerpc64/string/ffs.S 25 Jun 2020 20:57:16 -0000
> @@ -0,0 +1,15 @@
> +/* $OpenBSD$ */
> +/*
> + * Written by Christian Weisgerber <[hidden email]>.
> + * Public domain.
> + */
> +
> +#include "DEFS.h"
> +
> +ENTRY(ffs)
> + neg %r4, %r3
> + and %r3, %r3, %r4
> + cntlzw %r3, %r3
> + subfic %r3, %r3, 32
> + blr
> +END_BUILTIN(ffs)
> 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 20 Jun 2020 15:26:51 -0000
> @@ -0,0 +1,6 @@
> +PROG= ffs_test
> +
> +# prevent constant folding and 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 12 Jun 2020 08:43:19 -0000
> @@ -0,0 +1,18 @@
> +/* $OpenBSD$ */
> +/*
> + * Written by Christian Weisgerber <[hidden email]>.
> + * 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                          [hidden email]
>
>