00db10
From 250962053fadd0911accad515802d9b957c9d019 Mon Sep 17 00:00:00 2001
00db10
From: Stefan Liebler <stli@linux.vnet.ibm.com>
00db10
Date: Thu, 8 Oct 2015 13:24:58 +0200
00db10
Subject: [PATCH 30/30] S390: Optimize memrchr.
00db10
00db10
upstream-commit-id: 798f5b4b5d24e5687dd55acc535954ed1d59c10c
00db10
https://www.sourceware.org/ml/libc-alpha/2015-07/msg00092.html
00db10
00db10
This patch provides optimized version of memrchr with the z13 vector
00db10
instructions.
00db10
00db10
ChangeLog:
00db10
00db10
	* sysdeps/s390/multiarch/memrchr-c.c: New File.
00db10
	* sysdeps/s390/multiarch/memrchr-vx.S: Likewise.
00db10
	* sysdeps/s390/multiarch/memrchr.c: Likewise.
00db10
	* sysdeps/s390/multiarch/Makefile
00db10
	(sysdep_routines): Add memrchr functions.
00db10
	* sysdeps/s390/multiarch/ifunc-impl-list-common.c
00db10
	(__libc_ifunc_impl_list_common): Add ifunc test for memrchr.
00db10
---
00db10
 sysdeps/s390/multiarch/Makefile          |   3 +-
00db10
 sysdeps/s390/multiarch/ifunc-impl-list.c |   2 +
00db10
 sysdeps/s390/multiarch/memrchr-c.c       |  25 +++++
00db10
 sysdeps/s390/multiarch/memrchr-vx.S      | 160 +++++++++++++++++++++++++++++++
00db10
 sysdeps/s390/multiarch/memrchr.c         |  28 ++++++
00db10
 5 files changed, 217 insertions(+), 1 deletion(-)
00db10
 create mode 100644 sysdeps/s390/multiarch/memrchr-c.c
00db10
 create mode 100644 sysdeps/s390/multiarch/memrchr-vx.S
00db10
 create mode 100644 sysdeps/s390/multiarch/memrchr.c
00db10
00db10
diff --git a/sysdeps/s390/multiarch/Makefile b/sysdeps/s390/multiarch/Makefile
00db10
index 929a545..0805b07 100644
00db10
--- a/sysdeps/s390/multiarch/Makefile
00db10
+++ b/sysdeps/s390/multiarch/Makefile
00db10
@@ -17,7 +17,8 @@ sysdep_routines += strlen strlen-vx strlen-c \
00db10
 		   strcspn strcspn-vx strcspn-c \
00db10
 		   memchr memchr-vx \
00db10
 		   rawmemchr rawmemchr-vx rawmemchr-c \
00db10
-		   memccpy memccpy-vx memccpy-c
00db10
+		   memccpy memccpy-vx memccpy-c \
00db10
+		   memrchr memrchr-vx memrchr-c
00db10
 endif
00db10
 
00db10
 ifeq ($(subdir),wcsmbs)
00db10
diff --git a/sysdeps/s390/multiarch/ifunc-impl-list.c b/sysdeps/s390/multiarch/ifunc-impl-list.c
00db10
index 5ea258b..c235bdc 100644
00db10
--- a/sysdeps/s390/multiarch/ifunc-impl-list.c
00db10
+++ b/sysdeps/s390/multiarch/ifunc-impl-list.c
00db10
@@ -137,6 +137,8 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
00db10
 
00db10
   IFUNC_VX_IMPL (wmemcmp);
00db10
 
00db10
+  IFUNC_VX_IMPL (memrchr);
00db10
+
00db10
 #endif /* HAVE_S390_VX_ASM_SUPPORT */
00db10
 
00db10
   return i;
00db10
diff --git a/sysdeps/s390/multiarch/memrchr-c.c b/sysdeps/s390/multiarch/memrchr-c.c
00db10
new file mode 100644
00db10
index 0000000..6a32861
00db10
--- /dev/null
00db10
+++ b/sysdeps/s390/multiarch/memrchr-c.c
00db10
@@ -0,0 +1,25 @@
00db10
+/* Default memrchr implementation for S/390.
00db10
+   Copyright (C) 2015 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#if defined HAVE_S390_VX_ASM_SUPPORT && !defined NOT_IN_libc
00db10
+# define MEMRCHR  __memrchr_c
00db10
+
00db10
+# include <string.h>
00db10
+extern __typeof (__memrchr) __memrchr_c;
00db10
+# include <string/memrchr.c>
00db10
+#endif
00db10
diff --git a/sysdeps/s390/multiarch/memrchr-vx.S b/sysdeps/s390/multiarch/memrchr-vx.S
00db10
new file mode 100644
00db10
index 0000000..da6dbdf
00db10
--- /dev/null
00db10
+++ b/sysdeps/s390/multiarch/memrchr-vx.S
00db10
@@ -0,0 +1,160 @@
00db10
+/* Vector optimized 32/64 bit S/390 version of memrchr.
00db10
+   Copyright (C) 2015 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#if defined HAVE_S390_VX_ASM_SUPPORT && !defined NOT_IN_libc
00db10
+
00db10
+# include "sysdep.h"
00db10
+# include "asm-syntax.h"
00db10
+
00db10
+	.text
00db10
+
00db10
+/* void *memrchr (const void *s, int c, size_t n)
00db10
+   Scans memory for character c backwards
00db10
+   and returns pointer to first c.
00db10
+
00db10
+   Register usage:
00db10
+   -r0=tmp
00db10
+   -r1=tmp
00db10
+   -r2=s
00db10
+   -r3=c
00db10
+   -r4=n
00db10
+   -r5=s in loop
00db10
+
00db10
+   -v16=part of s
00db10
+   -v17=index of found c
00db10
+   -v18=c replicated
00db10
+   -v20=permute pattern
00db10
+*/
00db10
+ENTRY(__memrchr_vx)
00db10
+	.machine "z13"
00db10
+	.machinemode "zarch_nohighgprs"
00db10
+
00db10
+# if !defined __s390x__
00db10
+	llgfr	%r4,%r4
00db10
+# endif /* !defined __s390x__ */
00db10
+	clgije	%r4,0,.Lnot_found
00db10
+
00db10
+	vlvgb	%v18,%r3,0	/* Generate vector which elements are all c.
00db10
+				   If c > 255, c will be truncated.  */
00db10
+	vrepb	%v18,%v18,0
00db10
+
00db10
+	llcr	%r3,%r3		/* char c_char = (char) c.  */
00db10
+
00db10
+	/* check byte n - 1.  */
00db10
+	llc	%r0,-1(%r4,%r2)
00db10
+	slgfi	%r4,1
00db10
+	clrje	%r0,%r3,.Lfound_end
00db10
+	jh	.Lnot_found	/* Return NULL if n is now 0.  */
00db10
+
00db10
+	larl	%r1,.Lpermute_mask /* Load permute mask.  */
00db10
+	vl	%v20,0(%r1)
00db10
+
00db10
+	/* check byte n - 2.  */
00db10
+	llc	%r0,-1(%r4,%r2)
00db10
+	slgfi	%r4,1
00db10
+	clrje	%r0,%r3,.Lfound_end
00db10
+	jh	.Lnot_found	/* Return NULL if n is now 0.  */
00db10
+
00db10
+	clgijhe	%r4,64,.Lloop64	/* If n >= 64 -> loop64.  */
00db10
+
00db10
+.Llt64:
00db10
+	/* Process n < 64 bytes.  */
00db10
+	clgijl	%r4,16,.Llt16	/* Jump away if n < 16.  */
00db10
+	aghi	%r4,-16
00db10
+	vl	%v16,0(%r4,%r2)
00db10
+	vfeebs	%v17,%v16,%v18
00db10
+	jno	.Lfound0
00db10
+	clgijl	%r4,16,.Llt16
00db10
+	aghi	%r4,-16
00db10
+	vl	%v16,0(%r4,%r2)
00db10
+	vfeebs	%v17,%v16,%v18
00db10
+	jno	.Lfound0
00db10
+	clgijl	%r4,16,.Llt16
00db10
+	aghi	%r4,-16
00db10
+	vl	%v16,0(%r4,%r2)
00db10
+	vfeebs	%v17,%v16,%v18
00db10
+	jno	.Lfound0
00db10
+.Llt16:
00db10
+	clgfi	%r4,0		/* if remaining bytes == 0, return NULL.  */
00db10
+	locghie	%r2,0
00db10
+	ber	%r14
00db10
+
00db10
+	aghi	%r4,-1		/* vll needs highest index.  */
00db10
+	vll	%v16,%r4,0(%r2)	/* Load remaining bytes.  */
00db10
+
00db10
+	/* Right-shift of v16 to mask bytes after highest index.  */
00db10
+	lhi	%r0,15
00db10
+	slr	%r0,%r4		/* Compute byte count for vector shift right.  */
00db10
+	sll	%r0,3		/* Convert to bit count.  */
00db10
+	vlvgb	%v17,%r0,7
00db10
+	vsrlb	%v16,%v16,%v17	/* Vector shift right by byte by number of bytes
00db10
+				   specified in bits 1-4 of byte 7 in v17.   */
00db10
+	j	.Lfound_permute
00db10
+
00db10
+.Lfound48:
00db10
+	aghi	%r4,16
00db10
+.Lfound32:
00db10
+	aghi	%r4,16
00db10
+.Lfound16:
00db10
+	aghi	%r4,16
00db10
+.Lfound0:
00db10
+	la	%r2,0(%r4,%r2)	/* Set pointer to start of v16.  */
00db10
+	lghi	%r4,15		/* Set highest index in v16 to last index.  */
00db10
+.Lfound_permute:
00db10
+	/* Search for a c in v16 in reversed byte order. v16 contains %r4 + 1
00db10
+	   bytes. If v16 was not fully loaded, the bytes are already
00db10
+	   right shifted, so that the bytes in v16 can simply be reversed.  */
00db10
+	vperm	%v16,%v16,%v16,%v20 /* Permute v16 to reversed order.  */
00db10
+	vfeeb	%v16,%v16,%v18	/* Find c in reversed v16.  */
00db10
+	vlgvb	%r1,%v16,7	/* Index of c or 16 if not found.  */
00db10
+
00db10
+	/* Return NULL if there is no c in loaded bytes. */
00db10
+	clrjh	%r1,%r4,.Lnot_found
00db10
+
00db10
+	slgr	%r4,%r1
00db10
+.Lfound_end:
00db10
+	la	%r2,0(%r4,%r2)	/* Return pointer to c.  */
00db10
+	br	%r14
00db10
+
00db10
+.Lnot_found:
00db10
+	lghi	%r2,0
00db10
+	br	%r14
00db10
+
00db10
+.Lpermute_mask:
00db10
+	.byte	0x0F,0x0E,0x0D,0x0C,0x0B,0x0A,0x09,0x08
00db10
+	.byte	0x07,0x06,0x05,0x04,0x03,0x02,0x01,0x00
00db10
+
00db10
+.Lloop64:
00db10
+	aghi	%r4,-64
00db10
+	vl	%v16,48(%r4,%r2) /* Load 16bytes of memory area.  */
00db10
+	vfeebs	%v17,%v16,%v18	/* Find c.  */
00db10
+	jno	.Lfound48	/* Jump away if c was found.  */
00db10
+	vl	%v16,32(%r4,%r2)
00db10
+	vfeebs	%v17,%v16,%v18
00db10
+	jno	.Lfound32
00db10
+	vl	%v16,16(%r4,%r2)
00db10
+	vfeebs	%v17,%v16,%v18
00db10
+	jno	.Lfound16
00db10
+	vl	%v16,0(%r4,%r2)
00db10
+	vfeebs	%v17,%v16,%v18
00db10
+	jno	.Lfound0
00db10
+
00db10
+	clgijhe	%r4,64,.Lloop64	/* If n >= 64 -> loop64.  */
00db10
+	j	.Llt64
00db10
+END(__memrchr_vx)
00db10
+#endif /* HAVE_S390_VX_ASM_SUPPORT && !defined NOT_IN_libc */
00db10
diff --git a/sysdeps/s390/multiarch/memrchr.c b/sysdeps/s390/multiarch/memrchr.c
00db10
new file mode 100644
00db10
index 0000000..6596067
00db10
--- /dev/null
00db10
+++ b/sysdeps/s390/multiarch/memrchr.c
00db10
@@ -0,0 +1,28 @@
00db10
+/* Multiple versions of memrchr.
00db10
+   Copyright (C) 2015 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#if defined HAVE_S390_VX_ASM_SUPPORT && !defined NOT_IN_libc
00db10
+# include <string.h>
00db10
+# include <ifunc-resolve.h>
00db10
+
00db10
+s390_vx_libc_ifunc (__memrchr)
00db10
+weak_alias (__memrchr, memrchr)
00db10
+
00db10
+#else
00db10
+# include <string/memrchr.c>
00db10
+#endif /* !(defined HAVE_S390_VX_ASM_SUPPORT && !defined NOT_IN_libc) */
00db10
-- 
00db10
2.3.0
00db10