bdc76f
commit 6f47401bd5fc71209219779a0426170a9a7395b0
bdc76f
Author: Stefan Liebler <stli@linux.ibm.com>
bdc76f
Date:   Fri Mar 22 11:14:08 2019 +0100
bdc76f
bdc76f
    S390: Add arch13 strstr ifunc variant.
bdc76f
    
bdc76f
    This patch introduces the new arch13 ifunc variant for strstr.
bdc76f
    For needles longer than 9 charachters it is relying on the common-code
bdc76f
    implementation.  For shorter needles it is using the new vstrs instruction
bdc76f
    which is able to search a substring within a vector register.
bdc76f
    
bdc76f
    ChangeLog:
bdc76f
    
bdc76f
            * sysdeps/s390/Makefile (sysdep_routines): Add strstr-arch13.
bdc76f
            * sysdeps/s390/ifunc-strstr.h (HAVE_STRSTR_ARCH13, STRSTR_ARCH13,
bdc76f
            STRSTR_Z13_ONLY_USED_AS_FALLBACK, HAVE_STRSTR_IFUNC_AND_ARCH13_SUPPORT):
bdc76f
            New defines.
bdc76f
            * sysdeps/s390/multiarch/ifunc-impl-list.c
bdc76f
            (__libc_ifunc_impl_list): Add ifunc variant for arch13 strstr.
bdc76f
            * sysdeps/s390/strstr-arch13.S: New file.
bdc76f
            * sysdeps/s390/strstr-vx.c: Omit GI symbol for z13 strstr ifunc variant
bdc76f
            if it is only used as fallback.
bdc76f
            * sysdeps/s390/strstr.c (strstr): Add arch13 variant in ifunc selector.
bdc76f
bdc76f
diff --git a/sysdeps/s390/Makefile b/sysdeps/s390/Makefile
bdc76f
index 3f7de6613c343819..7287b1833da9500f 100644
bdc76f
--- a/sysdeps/s390/Makefile
bdc76f
+++ b/sysdeps/s390/Makefile
bdc76f
@@ -35,7 +35,7 @@ sysdep_routines += bzero memset memset-z900 \
bdc76f
 		   memcmp memcmp-z900 \
bdc76f
 		   mempcpy memcpy memcpy-z900 \
bdc76f
 		   memmove memmove-c \
bdc76f
-		   strstr strstr-vx strstr-c \
bdc76f
+		   strstr strstr-arch13 strstr-vx strstr-c \
bdc76f
 		   memmem memmem-vx memmem-c \
bdc76f
 		   strlen strlen-vx strlen-c \
bdc76f
 		   strnlen strnlen-vx strnlen-c \
bdc76f
diff --git a/sysdeps/s390/ifunc-strstr.h b/sysdeps/s390/ifunc-strstr.h
bdc76f
index e6ccfd4e44a1a790..809184d425ad06b0 100644
bdc76f
--- a/sysdeps/s390/ifunc-strstr.h
bdc76f
+++ b/sysdeps/s390/ifunc-strstr.h
bdc76f
@@ -17,7 +17,7 @@
bdc76f
    <http://www.gnu.org/licenses/>.  */
bdc76f
 
bdc76f
 #if defined USE_MULTIARCH && IS_IN (libc)		\
bdc76f
-  && ! defined HAVE_S390_MIN_Z13_ZARCH_ASM_SUPPORT
bdc76f
+  && ! defined HAVE_S390_MIN_ARCH13_ZARCH_ASM_SUPPORT
bdc76f
 # define HAVE_STRSTR_IFUNC	1
bdc76f
 #else
bdc76f
 # define HAVE_STRSTR_IFUNC	0
bdc76f
@@ -29,14 +29,32 @@
bdc76f
 # define HAVE_STRSTR_IFUNC_AND_VX_SUPPORT 0
bdc76f
 #endif
bdc76f
 
bdc76f
-#if defined HAVE_S390_MIN_Z13_ZARCH_ASM_SUPPORT
bdc76f
+#ifdef HAVE_S390_ARCH13_ASM_SUPPORT
bdc76f
+# define HAVE_STRSTR_IFUNC_AND_ARCH13_SUPPORT HAVE_STRSTR_IFUNC
bdc76f
+#else
bdc76f
+# define HAVE_STRSTR_IFUNC_AND_ARCH13_SUPPORT 0
bdc76f
+#endif
bdc76f
+
bdc76f
+#if defined HAVE_S390_MIN_ARCH13_ZARCH_ASM_SUPPORT
bdc76f
+# define STRSTR_DEFAULT		STRSTR_ARCH13
bdc76f
+# define HAVE_STRSTR_C		0
bdc76f
+# define HAVE_STRSTR_Z13	1
bdc76f
+# define STRSTR_Z13_ONLY_USED_AS_FALLBACK 1
bdc76f
+# define HAVE_STRSTR_ARCH13	1
bdc76f
+#elif defined HAVE_S390_MIN_Z13_ZARCH_ASM_SUPPORT
bdc76f
 # define STRSTR_DEFAULT		STRSTR_Z13
bdc76f
 # define HAVE_STRSTR_C		0
bdc76f
 # define HAVE_STRSTR_Z13	1
bdc76f
+# define HAVE_STRSTR_ARCH13	HAVE_STRSTR_IFUNC_AND_ARCH13_SUPPORT
bdc76f
 #else
bdc76f
 # define STRSTR_DEFAULT		STRSTR_C
bdc76f
 # define HAVE_STRSTR_C		1
bdc76f
 # define HAVE_STRSTR_Z13	HAVE_STRSTR_IFUNC_AND_VX_SUPPORT
bdc76f
+# define HAVE_STRSTR_ARCH13	HAVE_STRSTR_IFUNC_AND_ARCH13_SUPPORT
bdc76f
+#endif
bdc76f
+
bdc76f
+#ifndef STRSTR_Z13_ONLY_USED_AS_FALLBACK
bdc76f
+# define STRSTR_Z13_ONLY_USED_AS_FALLBACK 0
bdc76f
 #endif
bdc76f
 
bdc76f
 #if HAVE_STRSTR_C
bdc76f
@@ -50,3 +68,9 @@
bdc76f
 #else
bdc76f
 # define STRSTR_Z13		NULL
bdc76f
 #endif
bdc76f
+
bdc76f
+#if HAVE_STRSTR_ARCH13
bdc76f
+# define STRSTR_ARCH13		__strstr_arch13
bdc76f
+#else
bdc76f
+# define STRSTR_ARCH13		NULL
bdc76f
+#endif
bdc76f
diff --git a/sysdeps/s390/multiarch/ifunc-impl-list.c b/sysdeps/s390/multiarch/ifunc-impl-list.c
bdc76f
index c24bfc95f2d7a22d..67a6a9c94afccd48 100644
bdc76f
--- a/sysdeps/s390/multiarch/ifunc-impl-list.c
bdc76f
+++ b/sysdeps/s390/multiarch/ifunc-impl-list.c
bdc76f
@@ -186,6 +186,10 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
bdc76f
 
bdc76f
 #if HAVE_STRSTR_IFUNC
bdc76f
     IFUNC_IMPL (i, name, strstr,
bdc76f
+# if HAVE_STRSTR_ARCH13
bdc76f
+		IFUNC_IMPL_ADD (array, i, strstr,
bdc76f
+				dl_hwcap & HWCAP_S390_VXRS_EXT2, STRSTR_ARCH13)
bdc76f
+# endif
bdc76f
 # if HAVE_STRSTR_Z13
bdc76f
 		IFUNC_IMPL_ADD (array, i, strstr,
bdc76f
 				dl_hwcap & HWCAP_S390_VX, STRSTR_Z13)
bdc76f
diff --git a/sysdeps/s390/strstr-arch13.S b/sysdeps/s390/strstr-arch13.S
bdc76f
new file mode 100644
bdc76f
index 0000000000000000..929b026adfeba740
bdc76f
--- /dev/null
bdc76f
+++ b/sysdeps/s390/strstr-arch13.S
bdc76f
@@ -0,0 +1,179 @@
bdc76f
+/* Vector optimized 32/64 bit S/390 version of strstr.
bdc76f
+   Copyright (C) 2019 Free Software Foundation, Inc.
bdc76f
+   This file is part of the GNU C Library.
bdc76f
+
bdc76f
+   The GNU C Library is free software; you can redistribute it and/or
bdc76f
+   modify it under the terms of the GNU Lesser General Public
bdc76f
+   License as published by the Free Software Foundation; either
bdc76f
+   version 2.1 of the License, or (at your option) any later version.
bdc76f
+
bdc76f
+   The GNU C Library is distributed in the hope that it will be useful,
bdc76f
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
bdc76f
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
bdc76f
+   Lesser General Public License for more details.
bdc76f
+
bdc76f
+   You should have received a copy of the GNU Lesser General Public
bdc76f
+   License along with the GNU C Library; if not, see
bdc76f
+   <http://www.gnu.org/licenses/>.  */
bdc76f
+
bdc76f
+#include <ifunc-strstr.h>
bdc76f
+#if HAVE_STRSTR_ARCH13
bdc76f
+# include "sysdep.h"
bdc76f
+# include "asm-syntax.h"
bdc76f
+	.text
bdc76f
+
bdc76f
+/* char *strstr (const char *haystack=r2, const char *needle=r3)
bdc76f
+   Locate a substring.  */
bdc76f
+ENTRY(STRSTR_ARCH13)
bdc76f
+	.machine "arch13"
bdc76f
+	.machinemode "zarch_nohighgprs"
bdc76f
+	lcbb	%r1,0(%r3),6
bdc76f
+	jo	.Lneedle_on_bb	/* Needle on block-boundary?  */
bdc76f
+	vl	%v18,0(%r3),6	/* Load needle.  */
bdc76f
+	vfenezb %v19,%v18,%v18	/* v19[7] contains the length of needle.  */
bdc76f
+.Lneedle_loaded:
bdc76f
+	vlgvb	%r4,%v19,7	/* Get index of zero or 16 if not found.  */
bdc76f
+	lghi	%r5,17		/* See below: min-skip-partial-match-index.  */
bdc76f
+	cgibe	%r4,0,0(%r14)	/* Test if needle is zero and return.  */
bdc76f
+
bdc76f
+	/* The vstrs instruction is able to handle needles up to a length of 16,
bdc76f
+	   but then we may have to load the next part of haystack with a
bdc76f
+	   small offset.  This will be slow - see examples:
bdc76f
+	   haystack =mmmmmmmmmmmmmmmm mmmmmmmmmmmmmmmmmm...mmmmmmmmmmmmmmmmmmma
bdc76f
+	   needle   =  mmmmmmmmmmmmmma0
bdc76f
+	   => needle_len=15; vstrs reports a partial match; haystack+=2
bdc76f
+	   haystack =mmmmmmmmmmmmmmmm mmmmmmmmmmmmmmmmmm...mmmmmmmmmmmmmmmmmmma
bdc76f
+	   needle   =        mmmmmmmma0000000
bdc76f
+	   => needle_len=9; vstrs reports a partial match; haystack+=8  */
bdc76f
+# if ! HAVE_STRSTR_Z13
bdc76f
+#  error The arch13 variant of strstr needs the z13 variant of strstr!
bdc76f
+# endif
bdc76f
+	clgfi	%r4,9
bdc76f
+	jh	STRSTR_Z13
bdc76f
+
bdc76f
+	/* In case of a partial match, the vstrs instruction returns the index
bdc76f
+	   of the partial match in a vector-register.  Then we have to
bdc76f
+	   reload the string at the "current-position plus this index" and run
bdc76f
+	   vstrs again in order to determine if it was a full match or no match.
bdc76f
+	   Transferring this index from vr to gr, compute the haystack-address
bdc76f
+	   and loading with vl is quite slow as all instructions have data
bdc76f
+	   dependencies.  Thus we assume, that a partial match is always at the
bdc76f
+	   first possible index and just load the next part of haystack from
bdc76f
+	   there instead of waiting until the correct index is computed:
bdc76f
+	   min-skip-partial-match-index = (16 - n_len) + 1  */
bdc76f
+	sgr	%r5,%r4
bdc76f
+
bdc76f
+.Lloop:
bdc76f
+	lcbb	%r1,0(%r2),6
bdc76f
+	jo	.Lloop_haystack_on_bb	/* Haystack on block-boundary?  */
bdc76f
+	vl	%v16,0(%r2)		/* Load next part of haystack.  */
bdc76f
+.Lloop_haystack_loaded:
bdc76f
+	/* Vector string search with zero search (cc=0 => no match).  */
bdc76f
+	vstrs	%v20,%v16,%v18,%v19,0,2
bdc76f
+	jne	.Lloop_vstrs_nonzero_cc
bdc76f
+	lcbb	%r1,16(%r2),6		/* Next part of haystack.  */
bdc76f
+	jo	.Lloop_haystack_on_bb16
bdc76f
+	vl	%v16,16(%r2)
bdc76f
+	vstrs	%v20,%v16,%v18,%v19,0,2
bdc76f
+	jne	.Lloop_vstrs_nonzero_cc16
bdc76f
+	lcbb	%r1,32(%r2),6		/* Next part of haystack.  */
bdc76f
+	jo	.Lloop_haystack_on_bb32
bdc76f
+	vl	%v16,32(%r2)
bdc76f
+	vstrs	%v20,%v16,%v18,%v19,0,2
bdc76f
+	jne	.Lloop_vstrs_nonzero_cc32
bdc76f
+	lcbb	%r1,48(%r2),6		/* Next part of haystack.  */
bdc76f
+	jo	.Lloop_haystack_on_bb48
bdc76f
+	vl	%v16,48(%r2)
bdc76f
+	vstrs	%v20,%v16,%v18,%v19,0,2
bdc76f
+	jne	.Lloop_vstrs_nonzero_cc48
bdc76f
+	la	%r2,64(%r2)
bdc76f
+	j	.Lloop
bdc76f
+
bdc76f
+.Lloop_vstrs_nonzero_cc48:
bdc76f
+	la	%r2,16(%r2)
bdc76f
+.Lloop_vstrs_nonzero_cc32:
bdc76f
+	la	%r2,16(%r2)
bdc76f
+.Lloop_vstrs_nonzero_cc16:
bdc76f
+	la	%r2,16(%r2)
bdc76f
+.Lloop_vstrs_nonzero_cc:
bdc76f
+	jh	.Lend_match_found /* cc == 2 (full match)  */
bdc76f
+	jl	.Lend_no_match	/* cc == 1 (no match, end of string)  */
bdc76f
+	/* cc == 3 (partial match) See above: min-skip-partial-match-index!  */
bdc76f
+	lcbb	%r1,0(%r5,%r2),6
bdc76f
+	la	%r2,0(%r5,%r2)
bdc76f
+	jo	.Lloop_haystack_on_bb
bdc76f
+	vl	%v16,0(%r2)
bdc76f
+	vstrs	%v20,%v16,%v18,%v19,0,2
bdc76f
+.Lloop_vstrs_nonzero_cc_loop:
bdc76f
+	jh	.Lend_match_found
bdc76f
+	jl	.Lend_no_match
bdc76f
+	la	%r2,0(%r5,%r2)
bdc76f
+	je	.Lloop
bdc76f
+	lcbb	%r1,0(%r2),6		/* Next part of haystack.  */
bdc76f
+	jo	.Lloop_haystack_on_bb
bdc76f
+	vl	%v16,0(%r2)
bdc76f
+	vstrs	%v20,%v16,%v18,%v19,0,2
bdc76f
+	jh	.Lend_match_found
bdc76f
+	jl	.Lend_no_match
bdc76f
+	la	%r2,0(%r5,%r2)
bdc76f
+	je	.Lloop
bdc76f
+	lcbb	%r1,0(%r2),6		/* Next part of haystack.  */
bdc76f
+	jo	.Lloop_haystack_on_bb
bdc76f
+	vl	%v16,0(%r2)
bdc76f
+	vstrs	%v20,%v16,%v18,%v19,0,2
bdc76f
+	jh	.Lend_match_found
bdc76f
+	jl	.Lend_no_match
bdc76f
+	la	%r2,0(%r5,%r2)
bdc76f
+	je	.Lloop
bdc76f
+	lcbb	%r1,0(%r2),6		/* Next part of haystack.  */
bdc76f
+	jo	.Lloop_haystack_on_bb
bdc76f
+	vl	%v16,0(%r2)
bdc76f
+	vstrs	%v20,%v16,%v18,%v19,0,2
bdc76f
+	j	.Lloop_vstrs_nonzero_cc_loop
bdc76f
+
bdc76f
+.Lend_no_match:
bdc76f
+	lghi	%r2,0
bdc76f
+	br	%r14
bdc76f
+.Lend_match_found:
bdc76f
+	vlgvb	%r4,%v20,7
bdc76f
+	la	%r2,0(%r4,%r2)
bdc76f
+	br	%r14
bdc76f
+
bdc76f
+.Lloop_haystack_on_bb48:
bdc76f
+	la	%r2,16(%r2)
bdc76f
+.Lloop_haystack_on_bb32:
bdc76f
+	la	%r2,16(%r2)
bdc76f
+.Lloop_haystack_on_bb16:
bdc76f
+	la	%r2,16(%r2)
bdc76f
+.Lloop_haystack_on_bb:
bdc76f
+	/* Haystack located on page-boundary.  */
bdc76f
+	ahi	%r1,-1		/* vll needs highest index instead of count.  */
bdc76f
+	vll	%v16,%r1,0(%r2)
bdc76f
+	vlvgb	%v21,%r1,7
bdc76f
+	vfenezb	%v17,%v16,%v16	/* Search zero in loaded haystack bytes.  */
bdc76f
+	veclb	%v17,%v21		/* Zero index <= loaded byte index?  */
bdc76f
+	jle	.Lloop_haystack_loaded	/* -> v16 contains full haystack.  */
bdc76f
+	vl	%v16,0(%r2)	/* Load haystack beyond page boundary.  */
bdc76f
+	j	.Lloop_haystack_loaded
bdc76f
+
bdc76f
+.Lneedle_on_bb:
bdc76f
+	/* Needle located on page-boundary.  */
bdc76f
+	ahi	%r1,-1		/* vll needs highest index instead of count.  */
bdc76f
+	vll	%v18,%r1,0(%r3)
bdc76f
+	vlvgb	%v21,%r1,7
bdc76f
+	vfenezb	%v19,%v18,%v18	/* Search zero in loaded needle bytes.  */
bdc76f
+	veclb	%v19,%v21	/* Zero index <= max loaded byte index?  */
bdc76f
+	jle	.Lneedle_loaded	/* -> v18 contains full needle.  */
bdc76f
+	vl	%v16,0(%r3)	/* Load needle beyond page boundary.  */
bdc76f
+	vfenezb	%v19,%v18,%v18
bdc76f
+	j	.Lneedle_loaded
bdc76f
+END(STRSTR_ARCH13)
bdc76f
+
bdc76f
+# if ! HAVE_STRSTR_IFUNC
bdc76f
+strong_alias (STRSTR_ARCH13, strstr)
bdc76f
+# endif
bdc76f
+
bdc76f
+# if STRSTR_Z13_ONLY_USED_AS_FALLBACK && defined SHARED && IS_IN (libc)
bdc76f
+strong_alias (STRSTR_ARCH13, __GI_strstr)
bdc76f
+# endif
bdc76f
+#endif
bdc76f
diff --git a/sysdeps/s390/strstr-vx.c b/sysdeps/s390/strstr-vx.c
bdc76f
index effae9d5eb7d2fb1..f69159ffc198b10b 100644
bdc76f
--- a/sysdeps/s390/strstr-vx.c
bdc76f
+++ b/sysdeps/s390/strstr-vx.c
bdc76f
@@ -19,11 +19,11 @@
bdc76f
 #include <ifunc-strstr.h>
bdc76f
 
bdc76f
 #if HAVE_STRSTR_Z13
bdc76f
-# if HAVE_STRSTR_IFUNC
bdc76f
+# if HAVE_STRSTR_IFUNC || STRSTR_Z13_ONLY_USED_AS_FALLBACK
bdc76f
 #  define STRSTR STRSTR_Z13
bdc76f
 #  if defined SHARED && IS_IN (libc)
bdc76f
 #   undef libc_hidden_builtin_def
bdc76f
-#   if HAVE_STRSTR_C
bdc76f
+#   if HAVE_STRSTR_C || STRSTR_Z13_ONLY_USED_AS_FALLBACK
bdc76f
 #    define libc_hidden_builtin_def(name)
bdc76f
 #   else
bdc76f
 #    define libc_hidden_builtin_def(name)		\
bdc76f
diff --git a/sysdeps/s390/strstr.c b/sysdeps/s390/strstr.c
bdc76f
index f8432349a7254cc6..d2969fd0a68fadbf 100644
bdc76f
--- a/sysdeps/s390/strstr.c
bdc76f
+++ b/sysdeps/s390/strstr.c
bdc76f
@@ -32,8 +32,14 @@ extern __typeof (__redirect_strstr) STRSTR_C attribute_hidden;
bdc76f
 extern __typeof (__redirect_strstr) STRSTR_Z13 attribute_hidden;
bdc76f
 # endif
bdc76f
 
bdc76f
+# if HAVE_STRSTR_ARCH13
bdc76f
+extern __typeof (__redirect_strstr) STRSTR_ARCH13 attribute_hidden;
bdc76f
+# endif
bdc76f
+
bdc76f
 s390_libc_ifunc_expr (__redirect_strstr, strstr,
bdc76f
-		      (HAVE_STRSTR_Z13 && (hwcap & HWCAP_S390_VX))
bdc76f
+		      (HAVE_STRSTR_ARCH13 && (hwcap & HWCAP_S390_VXRS_EXT2))
bdc76f
+		      ? STRSTR_ARCH13
bdc76f
+		      : (HAVE_STRSTR_Z13 && (hwcap & HWCAP_S390_VX))
bdc76f
 		      ? STRSTR_Z13
bdc76f
 		      : STRSTR_DEFAULT
bdc76f
 		      )