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