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