077c9d
commit 421749d693c4147017bc55f5ac3227c3a6e4bf31
077c9d
Author: Stefan Liebler <stli@linux.ibm.com>
077c9d
Date:   Fri Mar 22 11:14:09 2019 +0100
077c9d
077c9d
    S390: Add arch13 memmem ifunc variant.
077c9d
    
077c9d
    This patch introduces the new arch13 ifunc variant for memmem.
077c9d
    For needles longer than 9 bytes it is relying on the common-code
077c9d
    implementation.  For shorter needles it is using the new vstrs instruction
077c9d
    which is able to search a substring within a vector register.
077c9d
    
077c9d
    ChangeLog:
077c9d
    
077c9d
            * sysdeps/s390/Makefile (sysdep_routines): Add memmem-arch13.
077c9d
            * sysdeps/s390/ifunc-memmem.h (HAVE_MEMMEM_ARCH13, MEMMEM_ARCH13,
077c9d
            MEMMEM_Z13_ONLY_USED_AS_FALLBACK, HAVE_MEMMEM_IFUNC_AND_ARCH13_SUPPORT):
077c9d
            New defines.
077c9d
            * sysdeps/s390/memmem-arch13.S: New file.
077c9d
            * sysdeps/s390/memmem-vx.c: Omit GI symbol for z13 memmem ifunc variant
077c9d
            if it is only used as fallback.
077c9d
            * sysdeps/s390/memmem.c (memmem): Add arch13 variant in ifunc selector.
077c9d
            * sysdeps/s390/multiarch/ifunc-impl-list.c
077c9d
            (__libc_ifunc_impl_list): Add ifunc variant for arch13 memmem.
077c9d
077c9d
diff --git a/sysdeps/s390/Makefile b/sysdeps/s390/Makefile
077c9d
index 7287b1833da9500f..8bc82e523f9049db 100644
077c9d
--- a/sysdeps/s390/Makefile
077c9d
+++ b/sysdeps/s390/Makefile
077c9d
@@ -36,7 +36,7 @@ sysdep_routines += bzero memset memset-z900 \
077c9d
 		   mempcpy memcpy memcpy-z900 \
077c9d
 		   memmove memmove-c \
077c9d
 		   strstr strstr-arch13 strstr-vx strstr-c \
077c9d
-		   memmem memmem-vx memmem-c \
077c9d
+		   memmem memmem-arch13 memmem-vx memmem-c \
077c9d
 		   strlen strlen-vx strlen-c \
077c9d
 		   strnlen strnlen-vx strnlen-c \
077c9d
 		   strcpy strcpy-vx strcpy-z900 \
077c9d
diff --git a/sysdeps/s390/ifunc-memmem.h b/sysdeps/s390/ifunc-memmem.h
077c9d
index 0f860d8d40080acf..48079b22b070f381 100644
077c9d
--- a/sysdeps/s390/ifunc-memmem.h
077c9d
+++ b/sysdeps/s390/ifunc-memmem.h
077c9d
@@ -17,7 +17,7 @@
077c9d
    <http://www.gnu.org/licenses/>.  */
077c9d
 
077c9d
 #if defined USE_MULTIARCH && IS_IN (libc)		\
077c9d
-  && ! defined HAVE_S390_MIN_Z13_ZARCH_ASM_SUPPORT
077c9d
+  && ! defined HAVE_S390_MIN_ARCH13_ZARCH_ASM_SUPPORT
077c9d
 # define HAVE_MEMMEM_IFUNC	1
077c9d
 #else
077c9d
 # define HAVE_MEMMEM_IFUNC	0
077c9d
@@ -29,14 +29,32 @@
077c9d
 # define HAVE_MEMMEM_IFUNC_AND_VX_SUPPORT 0
077c9d
 #endif
077c9d
 
077c9d
-#if defined HAVE_S390_MIN_Z13_ZARCH_ASM_SUPPORT
077c9d
+#ifdef HAVE_S390_ARCH13_ASM_SUPPORT
077c9d
+# define HAVE_MEMMEM_IFUNC_AND_ARCH13_SUPPORT HAVE_MEMMEM_IFUNC
077c9d
+#else
077c9d
+# define HAVE_MEMMEM_IFUNC_AND_ARCH13_SUPPORT 0
077c9d
+#endif
077c9d
+
077c9d
+#if defined HAVE_S390_MIN_ARCH13_ZARCH_ASM_SUPPORT
077c9d
+# define MEMMEM_DEFAULT		MEMMEM_ARCH13
077c9d
+# define HAVE_MEMMEM_C		0
077c9d
+# define HAVE_MEMMEM_Z13	1
077c9d
+# define MEMMEM_Z13_ONLY_USED_AS_FALLBACK 1
077c9d
+# define HAVE_MEMMEM_ARCH13	1
077c9d
+#elif defined HAVE_S390_MIN_Z13_ZARCH_ASM_SUPPORT
077c9d
 # define MEMMEM_DEFAULT		MEMMEM_Z13
077c9d
 # define HAVE_MEMMEM_C		0
077c9d
 # define HAVE_MEMMEM_Z13	1
077c9d
+# define HAVE_MEMMEM_ARCH13	HAVE_MEMMEM_IFUNC_AND_ARCH13_SUPPORT
077c9d
 #else
077c9d
 # define MEMMEM_DEFAULT		MEMMEM_C
077c9d
 # define HAVE_MEMMEM_C		1
077c9d
 # define HAVE_MEMMEM_Z13	HAVE_MEMMEM_IFUNC_AND_VX_SUPPORT
077c9d
+# define HAVE_MEMMEM_ARCH13	HAVE_MEMMEM_IFUNC_AND_ARCH13_SUPPORT
077c9d
+#endif
077c9d
+
077c9d
+#ifndef MEMMEM_Z13_ONLY_USED_AS_FALLBACK
077c9d
+# define MEMMEM_Z13_ONLY_USED_AS_FALLBACK 0
077c9d
 #endif
077c9d
 
077c9d
 #if HAVE_MEMMEM_C
077c9d
@@ -50,3 +68,9 @@
077c9d
 #else
077c9d
 # define MEMMEM_Z13		NULL
077c9d
 #endif
077c9d
+
077c9d
+#if HAVE_MEMMEM_ARCH13
077c9d
+# define MEMMEM_ARCH13		__memmem_arch13
077c9d
+#else
077c9d
+# define MEMMEM_ARCH13		NULL
077c9d
+#endif
077c9d
diff --git a/sysdeps/s390/memmem-arch13.S b/sysdeps/s390/memmem-arch13.S
077c9d
new file mode 100644
077c9d
index 0000000000000000..b59d60acf0f6aaa0
077c9d
--- /dev/null
077c9d
+++ b/sysdeps/s390/memmem-arch13.S
077c9d
@@ -0,0 +1,161 @@
077c9d
+/* Vector optimized 32/64 bit S/390 version of memmem.
077c9d
+   Copyright (C) 2019 Free Software Foundation, Inc.
077c9d
+   This file is part of the GNU C Library.
077c9d
+
077c9d
+   The GNU C Library is free software; you can redistribute it and/or
077c9d
+   modify it under the terms of the GNU Lesser General Public
077c9d
+   License as published by the Free Software Foundation; either
077c9d
+   version 2.1 of the License, or (at your option) any later version.
077c9d
+
077c9d
+   The GNU C Library is distributed in the hope that it will be useful,
077c9d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
077c9d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
077c9d
+   Lesser General Public License for more details.
077c9d
+
077c9d
+   You should have received a copy of the GNU Lesser General Public
077c9d
+   License along with the GNU C Library; if not, see
077c9d
+   <http://www.gnu.org/licenses/>.  */
077c9d
+
077c9d
+#include <ifunc-memmem.h>
077c9d
+#if HAVE_MEMMEM_ARCH13
077c9d
+# include "sysdep.h"
077c9d
+# include "asm-syntax.h"
077c9d
+	.text
077c9d
+
077c9d
+/* void *memmem(const void *haystack=r2, size_t haystacklen=r3,
077c9d
+		const void *needle=r4, size_t needlelen=r5);
077c9d
+   Locate a substring.  */
077c9d
+ENTRY(MEMMEM_ARCH13)
077c9d
+	.machine "arch13"
077c9d
+	.machinemode "zarch_nohighgprs"
077c9d
+# if ! defined __s390x__
077c9d
+	llgfr	%r3,%r3
077c9d
+	llgfr	%r5,%r5
077c9d
+	llgfr	%r4,%r4
077c9d
+	llgfr	%r2,%r2
077c9d
+# endif /* ! defined __s390x__ */
077c9d
+	clgrjl	%r3,%r5,.Lend_no_match	/* Haystack < needle?  */
077c9d
+
077c9d
+	/* Jump to fallback if needle > 9.  See also strstr-arch13.S.  */
077c9d
+# if ! HAVE_MEMMEM_Z13
077c9d
+#  error The arch13 variant of memmem needs the z13 variant of memmem!
077c9d
+# endif
077c9d
+	clgfi	%r5,9
077c9d
+	jh	MEMMEM_Z13
077c9d
+
077c9d
+	aghik	%r0,%r5,-1		/* vll needs highest index.  */
077c9d
+	bc	4,0(%r14)		/* cc==1: return if needle-len == 0.  */
077c9d
+	vll	%v18,%r0,0(%r4)		/* Load needle.  */
077c9d
+	vlvgb	%v19,%r5,7		/* v19[7] contains length of needle.  */
077c9d
+
077c9d
+	clgijh	%r3,16,.Lhaystack_larger_16
077c9d
+.Lhaystack_smaller_16_on_bb:
077c9d
+	aghik	%r0,%r3,-1		/* vll needs highest index.  */
077c9d
+	vll	%v16,%r0,0(%r2)		/* Load haystack.  */
077c9d
+.Lhaystack_smaller_16:
077c9d
+	sgr	%r3,%r5			/* r3 = largest valid match-index.  */
077c9d
+	jl	.Lend_no_match		/* Haystack-len < needle-len?  */
077c9d
+	vstrs	%v20,%v16,%v18,%v19,0,0
077c9d
+	/* Vector string search without zero search where v20 will contain
077c9d
+	   the index of a partial/full match or 16 (index is named k).
077c9d
+	   cc=0 (no match; k=16): .Lend_no_match
077c9d
+	   cc=1 (only available with zero-search): Ignore
077c9d
+	   cc=2 (full match; k<16): Needle found, but could be beyond haystack!
077c9d
+	   cc=3 (partial match; k<16): Always at end of v16 and thus beyond!  */
077c9d
+	brc	9,.Lend_no_match	/* Jump away if cc == 0 || cc == 3.  */
077c9d
+	vlgvb	%r1,%v20,7
077c9d
+	/* Verify that the full-match (cc=2) is valid!  */
077c9d
+	clgrjh	%r1,%r3,.Lend_no_match	/* Jump away if match is beyond.  */
077c9d
+	la	%r2,0(%r1,%r2)
077c9d
+	br	%r14
077c9d
+.Lend_no_match:
077c9d
+	lghi	%r2,0
077c9d
+	br	%r14
077c9d
+
077c9d
+.Lhaystack_larger_16:
077c9d
+	vl	%v16,0(%r2)
077c9d
+	lghi	%r1,17
077c9d
+	lay	%r4,-16(%r3,%r2)	/* Boundary for loading with vl.  */
077c9d
+	lay	%r0,-64(%r3,%r2)	/* Boundary for loading with 4xvl.  */
077c9d
+	/* See also strstr-arch13.S:
077c9d
+	   min-skip-partial-match-index = (16 - n_len) + 1  */
077c9d
+	sgr	%r1,%r5
077c9d
+	clgfi	%r3,64			/* Set Boundary to zero ...  */
077c9d
+	la	%r3,0(%r3,%r2)
077c9d
+	locghil	%r0,0			/* ... if haystack < 64bytes.  */
077c9d
+	jh	.Lloop64
077c9d
+.Lloop:
077c9d
+	la	%r2,16(%r2)
077c9d
+	/* Vector string search with zero search.  cc=0 => no match.  */
077c9d
+	vstrs	%v20,%v16,%v18,%v19,0,0
077c9d
+	jne	.Lloop_vstrs_nonzero_cc
077c9d
+	clgrjh	%r2,%r4,.Lhaystack_too_small
077c9d
+.Lloop16:
077c9d
+	vl	%v16,0(%r2)
077c9d
+	la	%r2,16(%r2)
077c9d
+	vstrs	%v20,%v16,%v18,%v19,0,0
077c9d
+	jne	.Lloop_vstrs_nonzero_cc
077c9d
+	clgrjle	%r2,%r4,.Lloop16
077c9d
+.Lhaystack_too_small:
077c9d
+	sgr	%r3,%r2			/* r3 = (haystack + len) - curr_pos  */
077c9d
+	je	.Lend_no_match		/* Remaining haystack is empty.  */
077c9d
+	lcbb	%r0,0(%r2),6
077c9d
+	jo	.Lhaystack_smaller_16_on_bb
077c9d
+	vl	%v16,0(%r2)		/* Load haystack.  */
077c9d
+	j	.Lhaystack_smaller_16
077c9d
+
077c9d
+.Lend_match_found:
077c9d
+	vlgvb	%r4,%v20,7
077c9d
+	sgr	%r2,%r1
077c9d
+	la	%r2,0(%r4,%r2)
077c9d
+	br	%r14
077c9d
+
077c9d
+.Lloop_vstrs_nonzero_cc32:
077c9d
+	la	%r2,16(%r2)
077c9d
+.Lloop_vstrs_nonzero_cc16:
077c9d
+	la	%r2,16(%r2)
077c9d
+.Lloop_vstrs_nonzero_cc0:
077c9d
+	la	%r2,16(%r2)
077c9d
+.Lloop_vstrs_nonzero_cc:
077c9d
+	lay	%r2,-16(%r1,%r2)	/* Compute next load address.  */
077c9d
+	jh	.Lend_match_found	/* cc == 2 (full match)  */
077c9d
+	clgrjh	%r2,%r4,.Lhaystack_too_small
077c9d
+	vl	%v16,0(%r2)
077c9d
+.Lloop_vstrs_nonzero_cc_loop:
077c9d
+	la	%r2,0(%r1,%r2)
077c9d
+	vstrs	%v20,%v16,%v18,%v19,0,0
077c9d
+	jh	.Lend_match_found
077c9d
+	clgrjh	%r2,%r4,.Lhaystack_too_small
077c9d
+	vl	%v16,0(%r2)		/* Next part of haystack.  */
077c9d
+	jo	.Lloop_vstrs_nonzero_cc_loop
077c9d
+	/* Case: no-match.  */
077c9d
+	clgrjh	%r2,%r0,.Lloop	/* Jump away if haystack has less than 64b.  */
077c9d
+.Lloop64:
077c9d
+	vstrs	%v20,%v16,%v18,%v19,0,0
077c9d
+	jne	.Lloop_vstrs_nonzero_cc0
077c9d
+	vl	%v16,16(%r2)		/* Next part of haystack.  */
077c9d
+	vstrs	%v20,%v16,%v18,%v19,0,0
077c9d
+	jne	.Lloop_vstrs_nonzero_cc16
077c9d
+	vl	%v16,32(%r2)		/* Next part of haystack.  */
077c9d
+	vstrs	%v20,%v16,%v18,%v19,0,0
077c9d
+	jne	.Lloop_vstrs_nonzero_cc32
077c9d
+	vl	%v16,48(%r2)		/* Next part of haystack.  */
077c9d
+	la	%r2,64(%r2)
077c9d
+	vstrs	%v20,%v16,%v18,%v19,0,0
077c9d
+	jne	.Lloop_vstrs_nonzero_cc
077c9d
+	clgrjh	%r2,%r4,.Lhaystack_too_small
077c9d
+	vl	%v16,0(%r2)		/* Next part of haystack.  */
077c9d
+	clgrjle	%r2,%r0,.Lloop64
077c9d
+	j	.Lloop
077c9d
+END(MEMMEM_ARCH13)
077c9d
+
077c9d
+# if ! HAVE_MEMMEM_IFUNC
077c9d
+strong_alias (MEMMEM_ARCH13, __memmem)
077c9d
+weak_alias (__memmem, memmem)
077c9d
+# endif
077c9d
+
077c9d
+# if MEMMEM_Z13_ONLY_USED_AS_FALLBACK && defined SHARED && IS_IN (libc)
077c9d
+weak_alias (MEMMEM_ARCH13, __GI_memmem)
077c9d
+strong_alias (MEMMEM_ARCH13, __GI___memmem)
077c9d
+# endif
077c9d
+#endif
077c9d
diff --git a/sysdeps/s390/memmem-vx.c b/sysdeps/s390/memmem-vx.c
077c9d
index af6e200e4e0af1a5..e5608be77f05f2a9 100644
077c9d
--- a/sysdeps/s390/memmem-vx.c
077c9d
+++ b/sysdeps/s390/memmem-vx.c
077c9d
@@ -20,7 +20,7 @@
077c9d
 
077c9d
 #if HAVE_MEMMEM_Z13
077c9d
 # include <string.h>
077c9d
-# if HAVE_MEMMEM_IFUNC
077c9d
+# if HAVE_MEMMEM_IFUNC || MEMMEM_Z13_ONLY_USED_AS_FALLBACK
077c9d
 
077c9d
 #  ifndef _LIBC
077c9d
 #   define memmem MEMMEM_Z13
077c9d
@@ -32,7 +32,7 @@
077c9d
 #   undef libc_hidden_def
077c9d
 #   undef libc_hidden_weak
077c9d
 
077c9d
-#   if HAVE_MEMMEM_C
077c9d
+#   if HAVE_MEMMEM_C || MEMMEM_Z13_ONLY_USED_AS_FALLBACK
077c9d
 #    define libc_hidden_def(name)
077c9d
 #    define libc_hidden_weak(name)
077c9d
 #   else
077c9d
diff --git a/sysdeps/s390/memmem.c b/sysdeps/s390/memmem.c
077c9d
index 8c50b3f403eb8d1f..28871cd4b24868cc 100644
077c9d
--- a/sysdeps/s390/memmem.c
077c9d
+++ b/sysdeps/s390/memmem.c
077c9d
@@ -34,8 +34,14 @@ extern __typeof (__redirect_memmem) MEMMEM_C attribute_hidden;
077c9d
 extern __typeof (__redirect_memmem) MEMMEM_Z13 attribute_hidden;
077c9d
 # endif
077c9d
 
077c9d
+# if HAVE_MEMMEM_ARCH13
077c9d
+extern __typeof (__redirect_memmem) MEMMEM_ARCH13 attribute_hidden;
077c9d
+# endif
077c9d
+
077c9d
 s390_libc_ifunc_expr (__redirect_memmem, __memmem,
077c9d
-		      (HAVE_MEMMEM_Z13 && (hwcap & HWCAP_S390_VX))
077c9d
+		      (HAVE_MEMMEM_ARCH13 && (hwcap & HWCAP_S390_VXRS_EXT2))
077c9d
+		      ? MEMMEM_ARCH13
077c9d
+		      : (HAVE_MEMMEM_Z13 && (hwcap & HWCAP_S390_VX))
077c9d
 		      ? MEMMEM_Z13
077c9d
 		      : MEMMEM_DEFAULT
077c9d
 		      )
077c9d
diff --git a/sysdeps/s390/multiarch/ifunc-impl-list.c b/sysdeps/s390/multiarch/ifunc-impl-list.c
077c9d
index 67a6a9c94afccd48..787d40688f4110ff 100644
077c9d
--- a/sysdeps/s390/multiarch/ifunc-impl-list.c
077c9d
+++ b/sysdeps/s390/multiarch/ifunc-impl-list.c
077c9d
@@ -202,6 +202,10 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
077c9d
 
077c9d
 #if HAVE_MEMMEM_IFUNC
077c9d
     IFUNC_IMPL (i, name, memmem,
077c9d
+# if HAVE_MEMMEM_ARCH13
077c9d
+	      IFUNC_IMPL_ADD (array, i, memmem,
077c9d
+			      dl_hwcap & HWCAP_S390_VXRS_EXT2, MEMMEM_ARCH13)
077c9d
+# endif
077c9d
 # if HAVE_MEMMEM_Z13
077c9d
 		IFUNC_IMPL_ADD (array, i, memmem,
077c9d
 				dl_hwcap & HWCAP_S390_VX, MEMMEM_Z13)