bca718
From cb5e8d0ec1a4c4eb09da45015755e3d18b3a086b Mon Sep 17 00:00:00 2001
bca718
From: Stefan Liebler <stli@linux.vnet.ibm.com>
bca718
Date: Thu, 8 Oct 2015 12:43:56 +0200
bca718
Subject: [PATCH 22/30] S390: Optimize strrchr and wcsrchr.
bca718
bca718
upstream-commit-id: f40132d4bda984479bac89dfcd6968e9ff56e088
bca718
https://www.sourceware.org/ml/libc-alpha/2015-07/msg00101.html
bca718
bca718
This patch provides optimized versions of strrchr and wcsrchr with the z13
bca718
vector instructions.
bca718
bca718
ChangeLog:
bca718
bca718
	* sysdeps/s390/multiarch/strrchr-c.c: New File.
bca718
	* sysdeps/s390/multiarch/strrchr-vx.S: Likewise.
bca718
	* sysdeps/s390/multiarch/strrchr.c: Likewise.
bca718
	* sysdeps/s390/multiarch/wcsrchr-c.c: Likewise.
bca718
	* sysdeps/s390/multiarch/wcsrchr-vx.S: Likewise.
bca718
	* sysdeps/s390/multiarch/wcsrchr.c: Likewise.
bca718
	* sysdeps/s390/multiarch/Makefile (sysdep_routines): Add strrchr and
bca718
	wcsrchr functions.
bca718
	* sysdeps/s390/multiarch/ifunc-impl-list.c
bca718
	(__libc_ifunc_impl_list): Add ifunc test for strrchr, wcsrchr.
bca718
	* benchtests/bench-wcsrchr.c: New File.
bca718
	* benchtests/Makefile (wcsmbs-bench): Add wcsrchr.
bca718
---
bca718
 benchtests/Makefile                      |   2 +-
bca718
 benchtests/bench-wcsrchr.c               |  20 ++++
bca718
 string/strrchr.c                         |   6 +-
bca718
 sysdeps/s390/multiarch/Makefile          |   6 +-
bca718
 sysdeps/s390/multiarch/ifunc-impl-list.c |   3 +
bca718
 sysdeps/s390/multiarch/strrchr-c.c       |  29 +++++
bca718
 sysdeps/s390/multiarch/strrchr-vx.S      | 180 +++++++++++++++++++++++++++++
bca718
 sysdeps/s390/multiarch/strrchr.c         |  28 +++++
bca718
 sysdeps/s390/multiarch/wcsrchr-c.c       |  25 ++++
bca718
 sysdeps/s390/multiarch/wcsrchr-vx.S      | 190 +++++++++++++++++++++++++++++++
bca718
 sysdeps/s390/multiarch/wcsrchr.c         |  27 +++++
bca718
 11 files changed, 512 insertions(+), 4 deletions(-)
bca718
 create mode 100644 benchtests/bench-wcsrchr.c
bca718
 create mode 100644 sysdeps/s390/multiarch/strrchr-c.c
bca718
 create mode 100644 sysdeps/s390/multiarch/strrchr-vx.S
bca718
 create mode 100644 sysdeps/s390/multiarch/strrchr.c
bca718
 create mode 100644 sysdeps/s390/multiarch/wcsrchr-c.c
bca718
 create mode 100644 sysdeps/s390/multiarch/wcsrchr-vx.S
bca718
 create mode 100644 sysdeps/s390/multiarch/wcsrchr.c
bca718
bca718
diff --git a/benchtests/Makefile b/benchtests/Makefile
bca718
index d34bb3d..6444394 100644
bca718
--- a/benchtests/Makefile
bca718
+++ b/benchtests/Makefile
bca718
@@ -39,7 +39,7 @@ string-bench := bcopy bzero memccpy memchr memcmp memcpy memmem memmove \
bca718
 		strncasecmp strncat strncmp strncpy strnlen strpbrk strrchr \
bca718
 		strspn strstr strcpy_chk stpcpy_chk memrchr strsep strtok
bca718
 wcsmbs-bench := wcslen wcsnlen wcscpy wcpcpy wcsncpy wcpncpy wcscat wcsncat \
bca718
-		wcsncmp wcsncmp wcschr wcschrnul
bca718
+		wcsncmp wcsncmp wcschr wcschrnul wcsrchr
bca718
 string-bench-all := $(string-bench) ${wcsmbs-bench}
bca718
 
bca718
 stdlib-bench := strtod
bca718
diff --git a/benchtests/bench-wcsrchr.c b/benchtests/bench-wcsrchr.c
bca718
new file mode 100644
bca718
index 0000000..0d3923f
bca718
--- /dev/null
bca718
+++ b/benchtests/bench-wcsrchr.c
bca718
@@ -0,0 +1,20 @@
bca718
+/* Measure wcsrchr functions.
bca718
+   Copyright (C) 2015 Free Software Foundation, Inc.
bca718
+   This file is part of the GNU C Library.
bca718
+
bca718
+   The GNU C Library is free software; you can redistribute it and/or
bca718
+   modify it under the terms of the GNU Lesser General Public
bca718
+   License as published by the Free Software Foundation; either
bca718
+   version 2.1 of the License, or (at your option) any later version.
bca718
+
bca718
+   The GNU C Library is distributed in the hope that it will be useful,
bca718
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
bca718
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
bca718
+   Lesser General Public License for more details.
bca718
+
bca718
+   You should have received a copy of the GNU Lesser General Public
bca718
+   License along with the GNU C Library; if not, see
bca718
+   <http://www.gnu.org/licenses/>.  */
bca718
+
bca718
+#define WIDE 1
bca718
+#include "bench-strrchr.c"
bca718
diff --git a/string/strrchr.c b/string/strrchr.c
bca718
index a986ff9..03f3957 100644
bca718
--- a/string/strrchr.c
bca718
+++ b/string/strrchr.c
bca718
@@ -19,9 +19,13 @@
bca718
 
bca718
 #undef strrchr
bca718
 
bca718
+#ifndef STRRCHR
bca718
+# define STRRCHR strrchr
bca718
+#endif
bca718
+
bca718
 /* Find the last occurrence of C in S.  */
bca718
 char *
bca718
-strrchr (const char *s, int c)
bca718
+STRRCHR (const char *s, int c)
bca718
 {
bca718
   register const char *found, *p;
bca718
 
bca718
diff --git a/sysdeps/s390/multiarch/Makefile b/sysdeps/s390/multiarch/Makefile
bca718
index b6b64c9..b8b141e 100644
bca718
--- a/sysdeps/s390/multiarch/Makefile
bca718
+++ b/sysdeps/s390/multiarch/Makefile
bca718
@@ -10,7 +10,8 @@ sysdep_routines += strlen strlen-vx strlen-c \
bca718
 		   strcmp strcmp-vx \
bca718
 		   strncmp strncmp-vx strncmp-c \
bca718
 		   strchr strchr-vx strchr-c \
bca718
-		   strchrnul strchrnul-vx strchrnul-c
bca718
+		   strchrnul strchrnul-vx strchrnul-c \
bca718
+		   strrchr strrchr-vx strrchr-c
bca718
 endif
bca718
 
bca718
 ifeq ($(subdir),wcsmbs)
bca718
@@ -25,5 +26,6 @@ sysdep_routines += wcslen wcslen-vx wcslen-c \
bca718
 		   wcscmp wcscmp-vx wcscmp-c \
bca718
 		   wcsncmp wcsncmp-vx wcsncmp-c \
bca718
 		   wcschr wcschr-vx wcschr-c \
bca718
-		   wcschrnul wcschrnul-vx wcschrnul-c
bca718
+		   wcschrnul wcschrnul-vx wcschrnul-c \
bca718
+		   wcsrchr wcsrchr-vx wcsrchr-c
bca718
 endif
bca718
diff --git a/sysdeps/s390/multiarch/ifunc-impl-list.c b/sysdeps/s390/multiarch/ifunc-impl-list.c
bca718
index ea474a8..ee84d80 100644
bca718
--- a/sysdeps/s390/multiarch/ifunc-impl-list.c
bca718
+++ b/sysdeps/s390/multiarch/ifunc-impl-list.c
bca718
@@ -115,6 +115,9 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
bca718
   IFUNC_VX_IMPL (strchrnul);
bca718
   IFUNC_VX_IMPL (wcschrnul);
bca718
 
bca718
+  IFUNC_VX_IMPL (strrchr);
bca718
+  IFUNC_VX_IMPL (wcsrchr);
bca718
+
bca718
 #endif /* HAVE_S390_VX_ASM_SUPPORT */
bca718
 
bca718
   return i;
bca718
diff --git a/sysdeps/s390/multiarch/strrchr-c.c b/sysdeps/s390/multiarch/strrchr-c.c
bca718
new file mode 100644
bca718
index 0000000..b035cdc
bca718
--- /dev/null
bca718
+++ b/sysdeps/s390/multiarch/strrchr-c.c
bca718
@@ -0,0 +1,29 @@
bca718
+/* Default strrchr implementation for S/390.
bca718
+   Copyright (C) 2015 Free Software Foundation, Inc.
bca718
+   This file is part of the GNU C Library.
bca718
+
bca718
+   The GNU C Library is free software; you can redistribute it and/or
bca718
+   modify it under the terms of the GNU Lesser General Public
bca718
+   License as published by the Free Software Foundation; either
bca718
+   version 2.1 of the License, or (at your option) any later version.
bca718
+
bca718
+   The GNU C Library is distributed in the hope that it will be useful,
bca718
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
bca718
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
bca718
+   Lesser General Public License for more details.
bca718
+
bca718
+   You should have received a copy of the GNU Lesser General Public
bca718
+   License along with the GNU C Library; if not, see
bca718
+   <http://www.gnu.org/licenses/>.  */
bca718
+
bca718
+#if defined HAVE_S390_VX_ASM_SUPPORT && !defined NOT_IN_libc
bca718
+# define STRRCHR  __strrchr_c
bca718
+# undef weak_alias
bca718
+# ifdef SHARED
bca718
+#  undef libc_hidden_builtin_def
bca718
+#  define libc_hidden_builtin_def(name)				\
bca718
+     __hidden_ver1 (__strrchr_c, __GI_strrchr, __strrchr_c);
bca718
+# endif /* SHARED */
bca718
+
bca718
+# include <string/strrchr.c>
bca718
+#endif /* HAVE_S390_VX_ASM_SUPPORT && !defined NOT_IN_libc */
bca718
diff --git a/sysdeps/s390/multiarch/strrchr-vx.S b/sysdeps/s390/multiarch/strrchr-vx.S
bca718
new file mode 100644
bca718
index 0000000..dbc183b
bca718
--- /dev/null
bca718
+++ b/sysdeps/s390/multiarch/strrchr-vx.S
bca718
@@ -0,0 +1,180 @@
bca718
+/* Vector optimized 32/64 bit S/390 version of strrchr.
bca718
+   Copyright (C) 2015 Free Software Foundation, Inc.
bca718
+   This file is part of the GNU C Library.
bca718
+
bca718
+   The GNU C Library is free software; you can redistribute it and/or
bca718
+   modify it under the terms of the GNU Lesser General Public
bca718
+   License as published by the Free Software Foundation; either
bca718
+   version 2.1 of the License, or (at your option) any later version.
bca718
+
bca718
+   The GNU C Library is distributed in the hope that it will be useful,
bca718
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
bca718
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
bca718
+   Lesser General Public License for more details.
bca718
+
bca718
+   You should have received a copy of the GNU Lesser General Public
bca718
+   License along with the GNU C Library; if not, see
bca718
+   <http://www.gnu.org/licenses/>.  */
bca718
+
bca718
+#if defined HAVE_S390_VX_ASM_SUPPORT && !defined NOT_IN_libc
bca718
+
bca718
+# include "sysdep.h"
bca718
+# include "asm-syntax.h"
bca718
+
bca718
+	.text
bca718
+
bca718
+/* char *strrchr (const char *s, int c)
bca718
+   Locate the last character c in string.
bca718
+
bca718
+   Register usage:
bca718
+   -r0=loaded bytes in first part of s.
bca718
+   -r1=pointer to last occurence of c or NULL if not found.
bca718
+   -r2=s
bca718
+   -r3=c
bca718
+   -r4=tmp
bca718
+   -r5=current_len
bca718
+   -v16=part of s
bca718
+   -v17=index of found element
bca718
+   -v18=replicated c
bca718
+   -v19=part of s with last occurence of c.
bca718
+   -v20=permute pattern
bca718
+*/
bca718
+ENTRY(__strrchr_vx)
bca718
+	.machine "z13"
bca718
+	.machinemode "zarch_nohighgprs"
bca718
+
bca718
+	vlbb	%v16,0(%r2),6	/* Load s until next 4k-byte boundary.  */
bca718
+	lcbb	%r0,0(%r2),6	/* Get bytes to 4k-byte boundary or 16.  */
bca718
+
bca718
+	vlvgb	%v18,%r3,0	/* Generate vector which elements are all c.
bca718
+				   if c > 255, c will be truncated.  */
bca718
+	vrepb	%v18,%v18,0
bca718
+
bca718
+	lghi	%r1,-1		/* Currently no c found.  */
bca718
+	lghi	%r5,0		/* current_len = 0.  */
bca718
+
bca718
+	vfeezbs	%v17,%v16,%v18	/* Find element equal or zero.  */
bca718
+	vlgvb	%r4,%v17,7	/* Load byte index of c/zero or 16.  */
bca718
+	clrjl	%r4,%r0,.Lfound_first_part /* Found c/zero in loaded bytes.  */
bca718
+.Lalign:
bca718
+	/* Align s to 16 byte.  */
bca718
+	risbgn	%r4,%r2,60,128+63,0 /* %r3 = bits 60-63 of %r2 'and' 15.  */
bca718
+	lghi	%r5,16		/* current_len = 16.  */
bca718
+	slr	%r5,%r4		/* Compute bytes to 16bytes boundary.  */
bca718
+
bca718
+.Lloop:
bca718
+	vl	%v16,0(%r5,%r2) /* Load s.  */
bca718
+	vfeezbs	%v17,%v16,%v18	/* Find element equal with zero search.  */
bca718
+	jno	.Lfound		/* Found c/zero (cc=0|1|2).  */
bca718
+	vl	%v16,16(%r5,%r2)
bca718
+	vfeezbs	%v17,%v16,%v18
bca718
+	jno	.Lfound16
bca718
+	vl	%v16,32(%r5,%r2)
bca718
+	vfeezbs	%v17,%v16,%v18
bca718
+	jno	.Lfound32
bca718
+	vl	%v16,48(%r5,%r2)
bca718
+	vfeezbs	%v17,%v16,%v18
bca718
+	jno	.Lfound48
bca718
+
bca718
+	aghi	%r5,64
bca718
+	j	.Lloop		/* No character and no zero -> loop.  */
bca718
+
bca718
+.Lfound48:
bca718
+	la	%r5,16(%r5)	/* Use la since aghi would clobber cc.  */
bca718
+.Lfound32:
bca718
+	la	%r5,16(%r5)
bca718
+.Lfound16:
bca718
+	la	%r5,16(%r5)
bca718
+.Lfound:
bca718
+	je	.Lzero		/* Found zero, but no c before that zero.  */
bca718
+	/* Save this part of s to check for further matches after reaching
bca718
+	   the end of the complete string.  */
bca718
+	vlr	%v19,%v16
bca718
+	lgr	%r1,%r5
bca718
+
bca718
+	jh	.Lzero		/* Found a zero after the found c.  */
bca718
+	aghi	%r5,16		/* Start search of next part of s.  */
bca718
+	j	.Lloop
bca718
+
bca718
+.Lfound_first_part:
bca718
+	/* This code is only executed if the found c/zero is whithin loaded
bca718
+	   bytes. If no c/zero was found (cc==3) the found index = 16, thus
bca718
+	   this code is not called.
bca718
+	   Resulting condition code of vector find element equal:
bca718
+	   cc==0: no c, found zero
bca718
+	   cc==1: c found, no zero
bca718
+	   cc==2: c found, found zero after c
bca718
+	   cc==3: no c, no zero (this case can be ignored).  */
bca718
+	je	.Lzero		/* Found zero, but no c before that zero.  */
bca718
+
bca718
+	locgrne	%r1,%r5		/* Mark c as found in first part of s.  */
bca718
+	vlr	%v19,%v16
bca718
+
bca718
+	jl	.Lalign		/* No zero (e.g. if vr was fully loaded)
bca718
+				   -> Align and loop afterwards.  */
bca718
+
bca718
+	/* Found a zero in vr. If vr was not fully loaded due to block
bca718
+	   boundary, the remaining bytes are filled with zero and we can't
bca718
+	   rely on zero indication of condition code here!  */
bca718
+
bca718
+	vfenezb	%v17,%v16,%v16	/* Find zero.  */
bca718
+	vlgvb	%r4,%v17,7	/* Load byte index of zero or 16.  */
bca718
+	clrjl	%r4,%r0,.Lzero	/* Zero within loaded bytes -> end.  */
bca718
+	j	.Lalign		/* Align and loop afterwards.  */
bca718
+
bca718
+.Lend_searched_zero:
bca718
+	vlgvb	%r4,%v17,7	/* Load byte index of zero.  */
bca718
+	algr	%r5,%r4
bca718
+	la	%r2,0(%r5,%r2)	/* Return pointer to zero.  */
bca718
+	br	%r14
bca718
+
bca718
+.Lzero:
bca718
+	/* Reached end of string. Check if one c was found before.  */
bca718
+	clije	%r3,0,.Lend_searched_zero /* Found zero and c is zero.  */
bca718
+
bca718
+	cgfi	%r1,-1		/* No c found -> return NULL.  */
bca718
+	locghie	%r2,0
bca718
+	ber	%r14
bca718
+
bca718
+	larl	%r3,.Lpermute_mask /* Load permute mask.  */
bca718
+	vl	%v20,0(%r3)
bca718
+
bca718
+	/* c was found and is part of v19.  */
bca718
+	vfenezb	%v17,%v19,%v19	/* Find zero.  */
bca718
+	vlgvb	%r4,%v17,7	/* Load byte index of zero or 16.  */
bca718
+
bca718
+	clgfi	%r5,0		/* Loaded byte count in v19 is 16, ...  */
bca718
+	lochine	%r0,16		/* ... if v19 is not the first part of s.  */
bca718
+	ahi	%r0,-1		/* Convert byte count to highest index.  */
bca718
+
bca718
+	clr	%r0,%r4
bca718
+	locrl	%r4,%r0		/* r4 = min (zero-index, highest-index).  */
bca718
+
bca718
+	/* Right-shift of v19 to mask bytes after zero.  */
bca718
+	clije	%r4,15,.Lzero_permute /* No shift is needed if highest index
bca718
+					 in vr is 15.  */
bca718
+	lhi	%r0,15
bca718
+	slr	%r0,%r4		/* Compute byte count for vector shift right.  */
bca718
+	sll	%r0,3		/* Convert to bit count.  */
bca718
+	vlvgb	%v17,%r0,7
bca718
+	vsrlb	%v19,%v19,%v17	/* Vector shift right by byte by number of bytes
bca718
+				   specified in bits 1-4 of byte 7 in v17.   */
bca718
+
bca718
+	/* Reverse bytes in v19.  */
bca718
+.Lzero_permute:
bca718
+	vperm	%v19,%v19,%v19,%v20 /* Permute v19 to reversed order.  */
bca718
+
bca718
+	/* Find c in reversed v19.  */
bca718
+	vfeeb	%v19,%v19,%v18	/* Find c.  */
bca718
+	la	%r2,0(%r1,%r2)
bca718
+	vlgvb	%r3,%v19,7	/* Load byte index of c.  */
bca718
+
bca718
+	/* Compute index in real s and return.  */
bca718
+	slgr	%r4,%r3
bca718
+	la	%r2,0(%r4,%r2)	/* Return pointer to zero.  */
bca718
+	br	%r14
bca718
+.Lpermute_mask:
bca718
+	.byte	0x0F,0x0E,0x0D,0x0C,0x0B,0x0A,0x09,0x08
bca718
+	.byte	0x07,0x06,0x05,0x04,0x03,0x02,0x01,0x00
bca718
+END(__strrchr_vx)
bca718
+#endif /* HAVE_S390_VX_ASM_SUPPORT && !defined NOT_IN_libc */
bca718
diff --git a/sysdeps/s390/multiarch/strrchr.c b/sysdeps/s390/multiarch/strrchr.c
bca718
new file mode 100644
bca718
index 0000000..dc4efd3
bca718
--- /dev/null
bca718
+++ b/sysdeps/s390/multiarch/strrchr.c
bca718
@@ -0,0 +1,28 @@
bca718
+/* Multiple versions of strrchr.
bca718
+   Copyright (C) 2015 Free Software Foundation, Inc.
bca718
+   This file is part of the GNU C Library.
bca718
+
bca718
+   The GNU C Library is free software; you can redistribute it and/or
bca718
+   modify it under the terms of the GNU Lesser General Public
bca718
+   License as published by the Free Software Foundation; either
bca718
+   version 2.1 of the License, or (at your option) any later version.
bca718
+
bca718
+   The GNU C Library is distributed in the hope that it will be useful,
bca718
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
bca718
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
bca718
+   Lesser General Public License for more details.
bca718
+
bca718
+   You should have received a copy of the GNU Lesser General Public
bca718
+   License along with the GNU C Library; if not, see
bca718
+   <http://www.gnu.org/licenses/>.  */
bca718
+
bca718
+#if defined HAVE_S390_VX_ASM_SUPPORT && !defined NOT_IN_libc
bca718
+# include <string.h>
bca718
+# include <ifunc-resolve.h>
bca718
+
bca718
+s390_vx_libc_ifunc2 (__strrchr, strrchr)
bca718
+weak_alias (strrchr, rindex)
bca718
+
bca718
+#else
bca718
+# include <string/strrchr.c>
bca718
+#endif /* !(defined HAVE_S390_VX_ASM_SUPPORT && !defined NOT_IN_libc) */
bca718
diff --git a/sysdeps/s390/multiarch/wcsrchr-c.c b/sysdeps/s390/multiarch/wcsrchr-c.c
bca718
new file mode 100644
bca718
index 0000000..8f66600
bca718
--- /dev/null
bca718
+++ b/sysdeps/s390/multiarch/wcsrchr-c.c
bca718
@@ -0,0 +1,25 @@
bca718
+/* Default wcsrchr implementation for S/390.
bca718
+   Copyright (C) 2015 Free Software Foundation, Inc.
bca718
+   This file is part of the GNU C Library.
bca718
+
bca718
+   The GNU C Library is free software; you can redistribute it and/or
bca718
+   modify it under the terms of the GNU Lesser General Public
bca718
+   License as published by the Free Software Foundation; either
bca718
+   version 2.1 of the License, or (at your option) any later version.
bca718
+
bca718
+   The GNU C Library is distributed in the hope that it will be useful,
bca718
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
bca718
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
bca718
+   Lesser General Public License for more details.
bca718
+
bca718
+   You should have received a copy of the GNU Lesser General Public
bca718
+   License along with the GNU C Library; if not, see
bca718
+   <http://www.gnu.org/licenses/>.  */
bca718
+
bca718
+#if defined HAVE_S390_VX_ASM_SUPPORT && !defined NOT_IN_libc
bca718
+# define WCSRCHR  __wcsrchr_c
bca718
+
bca718
+# include <wchar.h>
bca718
+extern __typeof (wcsrchr) __wcsrchr_c;
bca718
+# include <wcsmbs/wcsrchr.c>
bca718
+#endif
bca718
diff --git a/sysdeps/s390/multiarch/wcsrchr-vx.S b/sysdeps/s390/multiarch/wcsrchr-vx.S
bca718
new file mode 100644
bca718
index 0000000..efb7701
bca718
--- /dev/null
bca718
+++ b/sysdeps/s390/multiarch/wcsrchr-vx.S
bca718
@@ -0,0 +1,190 @@
bca718
+/* Vector optimized 32/64 bit S/390 version of wcsrchr.
bca718
+   Copyright (C) 2015 Free Software Foundation, Inc.
bca718
+   This file is part of the GNU C Library.
bca718
+
bca718
+   The GNU C Library is free software; you can redistribute it and/or
bca718
+   modify it under the terms of the GNU Lesser General Public
bca718
+   License as published by the Free Software Foundation; either
bca718
+   version 2.1 of the License, or (at your option) any later version.
bca718
+
bca718
+   The GNU C Library is distributed in the hope that it will be useful,
bca718
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
bca718
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
bca718
+   Lesser General Public License for more details.
bca718
+
bca718
+   You should have received a copy of the GNU Lesser General Public
bca718
+   License along with the GNU C Library; if not, see
bca718
+   <http://www.gnu.org/licenses/>.  */
bca718
+
bca718
+#if defined HAVE_S390_VX_ASM_SUPPORT && !defined NOT_IN_libc
bca718
+
bca718
+# include "sysdep.h"
bca718
+# include "asm-syntax.h"
bca718
+
bca718
+	.text
bca718
+
bca718
+/* wchar_t *wcsrchr (const wchar_t *s, wchar_t c)
bca718
+   Locate the last character c in string.
bca718
+
bca718
+   Register usage:
bca718
+   -r0=loaded bytes in first part of s.
bca718
+   -r1=pointer to last occurence of c or NULL if not found.
bca718
+   -r2=s
bca718
+   -r3=c
bca718
+   -r4=tmp
bca718
+   -r5=current_len
bca718
+   -v16=part of s
bca718
+   -v17=index of found element
bca718
+   -v18=replicated c
bca718
+   -v19=part of s with last occurence of c.
bca718
+   -v20=permute pattern
bca718
+*/
bca718
+ENTRY(__wcsrchr_vx)
bca718
+	.machine "z13"
bca718
+	.machinemode "zarch_nohighgprs"
bca718
+
bca718
+	vlbb	%v16,0(%r2),6	/* Load s until next 4k-byte boundary.  */
bca718
+	lcbb	%r0,0(%r2),6	/* Get bytes to 4k-byte boundary or 16.  */
bca718
+
bca718
+	tmll	%r2,3		/* Test if s is 4-byte aligned?   */
bca718
+	jne	.Lfallback	/* And use common-code variant if not.  */
bca718
+
bca718
+	vlvgf	%v18,%r3,0	/* Generate vector which elements are all c.  */
bca718
+	vrepf	%v18,%v18,0
bca718
+
bca718
+	lghi	%r1,-1		/* Currently no c found.  */
bca718
+	lghi	%r5,0		/* current_len = 0.  */
bca718
+
bca718
+	vfeezfs	%v17,%v16,%v18	/* Find element equal or zero.  */
bca718
+	vlgvb	%r4,%v17,7	/* Load byte index of c/zero or 16.  */
bca718
+	clrjl	%r4,%r0,.Lfound_first_part /* Found c/zero in loaded bytes.  */
bca718
+.Lalign:
bca718
+	/* Align s to 16 byte.  */
bca718
+	risbgn	%r4,%r2,60,128+63,0 /* %r3 = bits 60-63 of %r2 'and' 15.  */
bca718
+	lghi	%r5,16		/* current_len = 16.  */
bca718
+	slr	%r5,%r4		/* Compute bytes to 16bytes boundary.  */
bca718
+
bca718
+.Lloop:
bca718
+	vl	%v16,0(%r5,%r2) /* Load s.  */
bca718
+	vfeezfs	%v17,%v16,%v18	/* Find element equal with zero search.  */
bca718
+	jno	.Lfound		/* Found c/zero (cc=0|1|2).  */
bca718
+	vl	%v16,16(%r5,%r2)
bca718
+	vfeezfs	%v17,%v16,%v18
bca718
+	jno	.Lfound16
bca718
+	vl	%v16,32(%r5,%r2)
bca718
+	vfeezfs	%v17,%v16,%v18
bca718
+	jno	.Lfound32
bca718
+	vl	%v16,48(%r5,%r2)
bca718
+	vfeezfs	%v17,%v16,%v18
bca718
+	jno	.Lfound48
bca718
+
bca718
+	aghi	%r5,64
bca718
+	j	.Lloop		/* No character and no zero -> loop.  */
bca718
+
bca718
+.Lfound48:
bca718
+	la	%r5,16(%r5)	/* Use la since aghi would clobber cc.  */
bca718
+.Lfound32:
bca718
+	la	%r5,16(%r5)
bca718
+.Lfound16:
bca718
+	la	%r5,16(%r5)
bca718
+.Lfound:
bca718
+	je	.Lzero		/* Found zero, but no c before that zero.  */
bca718
+	/* Save this part of s to check for further matches after reaching
bca718
+	   the end of the complete string.  */
bca718
+	vlr	%v19,%v16
bca718
+	lgr	%r1,%r5
bca718
+
bca718
+	jh	.Lzero		/* Found a zero after the found c.  */
bca718
+	aghi	%r5,16		/* Start search of next part of s.  */
bca718
+	j	.Lloop
bca718
+
bca718
+.Lfound_first_part:
bca718
+	/* This code is only executed if the found c/zero is whithin loaded
bca718
+	   bytes. If no c/zero was found (cc==3) the found index = 16, thus
bca718
+	   this code is not called.
bca718
+	   Resulting condition code of vector find element equal:
bca718
+	   cc==0: no c, found zero
bca718
+	   cc==1: c found, no zero
bca718
+	   cc==2: c found, found zero after c
bca718
+	   cc==3: no c, no zero (this case can be ignored).  */
bca718
+	je	.Lzero		/* Found zero, but no c before that zero.  */
bca718
+
bca718
+	locgrne	%r1,%r5		/* Mark c as found in first part of s.  */
bca718
+	vlr	%v19,%v16
bca718
+
bca718
+	jl	.Lalign		/* No zero (e.g. if vr was fully loaded)
bca718
+				   -> Align and loop afterwards.  */
bca718
+
bca718
+	/* Found a zero in vr. If vr was not fully loaded due to block
bca718
+	   boundary, the remaining bytes are filled with zero and we can't
bca718
+	   rely on zero indication of condition code here!  */
bca718
+
bca718
+	vfenezf	%v17,%v16,%v16
bca718
+	vlgvb	%r4,%v17,7	/* Load byte index of zero or 16.  */
bca718
+	clrjl	%r4,%r0,.Lzero	/* Zero within loaded bytes -> end.  */
bca718
+	j	.Lalign		/* Align and loop afterwards.  */
bca718
+
bca718
+.Lend_searched_zero:
bca718
+	vlgvb	%r4,%v17,7	/* Load byte index of zero.  */
bca718
+	algr	%r5,%r4
bca718
+	la	%r2,0(%r5,%r2)	/* Return pointer to zero.  */
bca718
+	br	%r14
bca718
+
bca718
+.Lzero:
bca718
+	/* Reached end of string. Check if one c was found before.  */
bca718
+	clije	%r3,0,.Lend_searched_zero /* Found zero and c is zero.  */
bca718
+
bca718
+	cgfi	%r1,-1		/* No c found -> return NULL.  */
bca718
+	locghie	%r2,0
bca718
+	ber	%r14
bca718
+
bca718
+	larl	%r3,.Lpermute_mask /* Load permute mask.  */
bca718
+	vl	%v20,0(%r3)
bca718
+
bca718
+	/* c was found and is part of v19.  */
bca718
+	vfenezf	%v17,%v19,%v19	/* Find zero.  */
bca718
+	vlgvb	%r4,%v17,7	/* Load byte index of zero or 16.  */
bca718
+	ahi	%r4,3		/* Found zero index is first byte,
bca718
+				   thus highest byte index is last byte of
bca718
+				   wchar_t zero.  */
bca718
+
bca718
+	clgfi	%r5,0		/* Loaded byte count in v19 is 16, ...  */
bca718
+	lochine	%r0,16		/* ... if v19 is not the first part of s.  */
bca718
+	ahi	%r0,-1		/* Convert byte count to highest index.  */
bca718
+
bca718
+	clr	%r0,%r4
bca718
+	locrl	%r4,%r0		/* r4 = min (zero-index, highest-index).  */
bca718
+
bca718
+	/* Right-shift of v19 to mask bytes after zero.  */
bca718
+	clije	%r4,15,.Lzero_permute /* No shift is needed if highest index
bca718
+					 in vr is 15.  */
bca718
+	lhi	%r0,15
bca718
+	slr	%r0,%r4		/* Compute byte count for vector shift left.  */
bca718
+	sll	%r0,3		/* Convert to bit count.  */
bca718
+	vlvgb	%v17,%r0,7
bca718
+	vsrlb	%v19,%v19,%v17	/* Vector shift right by byte by number of bytes
bca718
+				   specified in bits 1-4 of byte 7 in v17.   */
bca718
+
bca718
+	/* Reverse bytes in v19.  */
bca718
+.Lzero_permute:
bca718
+	vperm	%v19,%v19,%v19,%v20 /* Permute v19 to reversed order.  */
bca718
+
bca718
+	/* Find c in reversed v19.  */
bca718
+	vfeef	%v19,%v19,%v18	/* Find c.  */
bca718
+	la	%r2,0(%r1,%r2)
bca718
+	vlgvb	%r3,%v19,7	/* Load byte index of c.  */
bca718
+
bca718
+	/* Compute index in real s and return.  */
bca718
+	slgr	%r4,%r3
bca718
+	lay	%r2,-3(%r4,%r2)	/* Return pointer to zero. -3 is needed,
bca718
+				   because the found byte index is reversed in
bca718
+				   vector-register. Thus point to first byte of
bca718
+				   wchar_t.  */
bca718
+	br	%r14
bca718
+.Lpermute_mask:
bca718
+	.byte	0x0C,0x0D,0x0E,0x0F,0x08,0x09,0x0A,0x0B
bca718
+	.byte	0x04,0x05,0x06,0x07,0x00,0x01,0x02,0x03
bca718
+.Lfallback:
bca718
+	jg	__wcsrchr_c
bca718
+END(__wcsrchr_vx)
bca718
+#endif /* HAVE_S390_VX_ASM_SUPPORT && !defined NOT_IN_libc */
bca718
diff --git a/sysdeps/s390/multiarch/wcsrchr.c b/sysdeps/s390/multiarch/wcsrchr.c
bca718
new file mode 100644
bca718
index 0000000..db3f467
bca718
--- /dev/null
bca718
+++ b/sysdeps/s390/multiarch/wcsrchr.c
bca718
@@ -0,0 +1,27 @@
bca718
+/* Multiple versions of wcsrchr.
bca718
+   Copyright (C) 2015 Free Software Foundation, Inc.
bca718
+   This file is part of the GNU C Library.
bca718
+
bca718
+   The GNU C Library is free software; you can redistribute it and/or
bca718
+   modify it under the terms of the GNU Lesser General Public
bca718
+   License as published by the Free Software Foundation; either
bca718
+   version 2.1 of the License, or (at your option) any later version.
bca718
+
bca718
+   The GNU C Library is distributed in the hope that it will be useful,
bca718
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
bca718
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
bca718
+   Lesser General Public License for more details.
bca718
+
bca718
+   You should have received a copy of the GNU Lesser General Public
bca718
+   License along with the GNU C Library; if not, see
bca718
+   <http://www.gnu.org/licenses/>.  */
bca718
+
bca718
+#if defined HAVE_S390_VX_ASM_SUPPORT && !defined NOT_IN_libc
bca718
+# include <wchar.h>
bca718
+# include <ifunc-resolve.h>
bca718
+
bca718
+s390_vx_libc_ifunc2 (__wcsrchr, wcsrchr)
bca718
+
bca718
+#else
bca718
+# include <wcsmbs/wcsrchr.c>
bca718
+#endif /* !(defined HAVE_S390_VX_ASM_SUPPORT && !defined NOT_IN_libc) */
bca718
-- 
bca718
2.3.0
bca718