e354a5
commit 765de945efc5d5602999b2999fe8abdf04881370
e354a5
Author: Anton Blanchard <anton@ozlabs.org>
e354a5
Date:   Thu May 14 21:49:16 2020 +1000
e354a5
e354a5
    powerpc: Optimized rawmemchr for POWER9
e354a5
    
e354a5
    This version uses vector instructions and is up to 60% faster on medium
e354a5
    matches and up to 90% faster on long matches, compared to the POWER7
e354a5
    version. A few examples:
e354a5
    
e354a5
                                __rawmemchr_power9  __rawmemchr_power7
e354a5
    Length   32, alignment  0:   2.27566             3.77765
e354a5
    Length   64, alignment  2:   2.46231             3.51064
e354a5
    Length 1024, alignment  0:  17.3059             32.6678
e354a5
e354a5
diff --git a/sysdeps/powerpc/powerpc64/le/power9/rawmemchr.S b/sysdeps/powerpc/powerpc64/le/power9/rawmemchr.S
e354a5
new file mode 100644
e354a5
index 0000000000000000..9d0276c9315af5c8
e354a5
--- /dev/null
e354a5
+++ b/sysdeps/powerpc/powerpc64/le/power9/rawmemchr.S
e354a5
@@ -0,0 +1,107 @@
e354a5
+/* Optimized rawmemchr implementation for PowerPC64/POWER9.
e354a5
+   Copyright (C) 2020 Free Software Foundation, Inc.
e354a5
+   This file is part of the GNU C Library.
e354a5
+
e354a5
+   The GNU C Library is free software; you can redistribute it and/or
e354a5
+   modify it under the terms of the GNU Lesser General Public
e354a5
+   License as published by the Free Software Foundation; either
e354a5
+   version 2.1 of the License, or (at your option) any later version.
e354a5
+
e354a5
+   The GNU C Library is distributed in the hope that it will be useful,
e354a5
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
e354a5
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
e354a5
+   Lesser General Public License for more details.
e354a5
+
e354a5
+   You should have received a copy of the GNU Lesser General Public
e354a5
+   License along with the GNU C Library; if not, see
e354a5
+   <https://www.gnu.org/licenses/>.  */
e354a5
+
e354a5
+#include <sysdep.h>
e354a5
+
e354a5
+#ifndef RAWMEMCHR
e354a5
+# define RAWMEMCHR __rawmemchr
e354a5
+#endif
e354a5
+
e354a5
+/* Implements the function
e354a5
+
e354a5
+   int [r3] rawmemchr (void *s [r3], int c [r4])
e354a5
+
e354a5
+   The implementation can load bytes past a matching byte, but only
e354a5
+   up to the next 16B boundary, so it never crosses a page.  */
e354a5
+
e354a5
+.machine power9
e354a5
+ENTRY_TOCLESS (RAWMEMCHR, 4)
e354a5
+	CALL_MCOUNT 2
e354a5
+
e354a5
+	xori	r5,r4,0xff
e354a5
+
e354a5
+	mtvsrd	v18+32,r4	/* matching char in v18  */
e354a5
+	mtvsrd	v19+32,r5	/* non matching char in v19  */
e354a5
+
e354a5
+	vspltb	v18,v18,7	/* replicate  */
e354a5
+	vspltb	v19,v19,7	/* replicate  */
e354a5
+
e354a5
+	neg	r5,r3
e354a5
+	rldicl	r9,r5,0,60	/* How many bytes to get source 16B aligned?  */
e354a5
+
e354a5
+	/* Align data and fill bytes not loaded with non matching char  */
e354a5
+	lvx	v0,0,r3
e354a5
+	lvsr	v1,0,r3
e354a5
+	vperm	v0,v19,v0,v1
e354a5
+
e354a5
+	vcmpequb. v6,v0,v18	/* 0xff if byte matches, 0x00 otherwise  */
e354a5
+	beq	cr6,L(aligned)
e354a5
+
e354a5
+	vctzlsbb r0,v6
e354a5
+	add	r3,r3,r0
e354a5
+	blr
e354a5
+
e354a5
+L(aligned):
e354a5
+	add	r3,r3,r9
e354a5
+
e354a5
+L(loop):
e354a5
+	lxv	v0+32,0(r3)
e354a5
+	vcmpequb. v6,v0,v18	/* 0xff if byte matches, 0x00 otherwise  */
e354a5
+	bne	cr6,L(tail1)
e354a5
+
e354a5
+	lxv	v0+32,16(r3)
e354a5
+	vcmpequb. v6,v0,v18	/* 0xff if byte matches, 0x00 otherwise  */
e354a5
+	bne	cr6,L(tail2)
e354a5
+
e354a5
+	lxv	v0+32,32(r3)
e354a5
+	vcmpequb. v6,v0,v18	/* 0xff if byte matches, 0x00 otherwise  */
e354a5
+	bne	cr6,L(tail3)
e354a5
+
e354a5
+	lxv	v0+32,48(r3)
e354a5
+	vcmpequb. v6,v0,v18	/* 0xff if byte matches, 0x00 otherwise  */
e354a5
+	bne	cr6,L(tail4)
e354a5
+
e354a5
+	addi	r3,r3,64
e354a5
+	b	L(loop)
e354a5
+
e354a5
+L(tail1):
e354a5
+	vctzlsbb r0,v6
e354a5
+	add	r3,r3,r0
e354a5
+	blr
e354a5
+
e354a5
+L(tail2):
e354a5
+	vctzlsbb r0,v6
e354a5
+	add	r3,r3,r0
e354a5
+	addi	r3,r3,16
e354a5
+	blr
e354a5
+
e354a5
+L(tail3):
e354a5
+	vctzlsbb r0,v6
e354a5
+	add	r3,r3,r0
e354a5
+	addi	r3,r3,32
e354a5
+	blr
e354a5
+
e354a5
+L(tail4):
e354a5
+	vctzlsbb r0,v6
e354a5
+	add	r3,r3,r0
e354a5
+	addi	r3,r3,48
e354a5
+	blr
e354a5
+
e354a5
+END (RAWMEMCHR)
e354a5
+weak_alias (__rawmemchr,rawmemchr)
e354a5
+libc_hidden_builtin_def (__rawmemchr)
e354a5
diff --git a/sysdeps/powerpc/powerpc64/multiarch/Makefile b/sysdeps/powerpc/powerpc64/multiarch/Makefile
e354a5
index cada6b19bf3c8fab..1a8ef5fb73c3b0db 100644
e354a5
--- a/sysdeps/powerpc/powerpc64/multiarch/Makefile
e354a5
+++ b/sysdeps/powerpc/powerpc64/multiarch/Makefile
e354a5
@@ -32,7 +32,8 @@ sysdep_routines += memcpy-power8-cached memcpy-power7 memcpy-a2 memcpy-power6 \
e354a5
 		   strncase-power8
e354a5
 
e354a5
 ifneq (,$(filter %le,$(config-machine)))
e354a5
-sysdep_routines += strcmp-power9 strncmp-power9 strcpy-power9 stpcpy-power9
e354a5
+sysdep_routines += strcmp-power9 strncmp-power9 strcpy-power9 stpcpy-power9 \
e354a5
+		   rawmemchr-power9
e354a5
 endif
e354a5
 CFLAGS-strncase-power7.c += -mcpu=power7 -funroll-loops
e354a5
 CFLAGS-strncase_l-power7.c += -mcpu=power7 -funroll-loops
e354a5
diff --git a/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c b/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
e354a5
index b0abc6b61dc15f19..297935863e44c0e1 100644
e354a5
--- a/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
e354a5
+++ b/sysdeps/powerpc/powerpc64/multiarch/ifunc-impl-list.c
e354a5
@@ -216,6 +216,11 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
e354a5
 
e354a5
   /* Support sysdeps/powerpc/powerpc64/multiarch/rawmemchr.c.  */
e354a5
   IFUNC_IMPL (i, name, rawmemchr,
e354a5
+#ifdef __LITTLE_ENDIAN__
e354a5
+	      IFUNC_IMPL_ADD (array, i, rawmemchr,
e354a5
+			      hwcap2 & PPC_FEATURE2_ARCH_3_00,
e354a5
+			      __rawmemchr_power9)
e354a5
+#endif
e354a5
 	      IFUNC_IMPL_ADD (array, i, rawmemchr,
e354a5
 			      hwcap & PPC_FEATURE_HAS_VSX,
e354a5
 			      __rawmemchr_power7)
e354a5
diff --git a/sysdeps/powerpc/powerpc64/multiarch/rawmemchr-power9.S b/sysdeps/powerpc/powerpc64/multiarch/rawmemchr-power9.S
e354a5
new file mode 100644
e354a5
index 0000000000000000..bac0a9090e7a07f8
e354a5
--- /dev/null
e354a5
+++ b/sysdeps/powerpc/powerpc64/multiarch/rawmemchr-power9.S
e354a5
@@ -0,0 +1,21 @@
e354a5
+/* Optimized rawmemchr implementation for PowerPC64/POWER9.
e354a5
+   Copyright (C) 2020 Free Software Foundation, Inc.
e354a5
+   This file is part of the GNU C Library.
e354a5
+
e354a5
+   The GNU C Library is free software; you can redistribute it and/or
e354a5
+   modify it under the terms of the GNU Lesser General Public
e354a5
+   License as published by the Free Software Foundation; either
e354a5
+   version 2.1 of the License, or (at your option) any later version.
e354a5
+
e354a5
+   The GNU C Library is distributed in the hope that it will be useful,
e354a5
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
e354a5
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
e354a5
+   Lesser General Public License for more details.
e354a5
+
e354a5
+   You should have received a copy of the GNU Lesser General Public
e354a5
+   License along with the GNU C Library; if not, see
e354a5
+   <https://www.gnu.org/licenses/>.  */
e354a5
+
e354a5
+#define RAWMEMCHR __rawmemchr_power9
e354a5
+
e354a5
+#include <sysdeps/powerpc/powerpc64/le/power9/rawmemchr.S>
e354a5
diff --git a/sysdeps/powerpc/powerpc64/multiarch/rawmemchr.c b/sysdeps/powerpc/powerpc64/multiarch/rawmemchr.c
e354a5
index 02bac49b53d52411..2a7ae5a1ed02e556 100644
e354a5
--- a/sysdeps/powerpc/powerpc64/multiarch/rawmemchr.c
e354a5
+++ b/sysdeps/powerpc/powerpc64/multiarch/rawmemchr.c
e354a5
@@ -24,13 +24,21 @@
e354a5
 
e354a5
 extern __typeof (__rawmemchr) __rawmemchr_ppc attribute_hidden;
e354a5
 extern __typeof (__rawmemchr) __rawmemchr_power7 attribute_hidden;
e354a5
+# ifdef __LITTLE_ENDIAN__
e354a5
+extern __typeof (__rawmemchr) __rawmemchr_power9 attribute_hidden;
e354a5
+# endif
e354a5
+
e354a5
 # undef __rawmemchr
e354a5
 
e354a5
 /* Avoid DWARF definition DIE on ifunc symbol so that GDB can handle
e354a5
    ifunc symbol properly.  */
e354a5
 libc_ifunc_redirected (__redirect___rawmemchr, __rawmemchr,
e354a5
-		       (hwcap & PPC_FEATURE_HAS_VSX)
e354a5
-		       ? __rawmemchr_power7
e354a5
+# ifdef __LITTLE_ENDIAN__
e354a5
+		       (hwcap2 & PPC_FEATURE2_ARCH_3_00)
e354a5
+		       ? __rawmemchr_power9 :
e354a5
+# endif
e354a5
+		         (hwcap & PPC_FEATURE_HAS_VSX)
e354a5
+		         ? __rawmemchr_power7
e354a5
 		       : __rawmemchr_ppc);
e354a5
 
e354a5
 weak_alias (__rawmemchr, rawmemchr)