bca718
From 250962053fadd0911accad515802d9b957c9d019 Mon Sep 17 00:00:00 2001
bca718
From: Stefan Liebler <stli@linux.vnet.ibm.com>
bca718
Date: Thu, 8 Oct 2015 13:24:58 +0200
bca718
Subject: [PATCH 30/30] S390: Optimize memrchr.
bca718
bca718
upstream-commit-id: 798f5b4b5d24e5687dd55acc535954ed1d59c10c
bca718
https://www.sourceware.org/ml/libc-alpha/2015-07/msg00092.html
bca718
bca718
This patch provides optimized version of memrchr with the z13 vector
bca718
instructions.
bca718
bca718
ChangeLog:
bca718
bca718
	* sysdeps/s390/multiarch/memrchr-c.c: New File.
bca718
	* sysdeps/s390/multiarch/memrchr-vx.S: Likewise.
bca718
	* sysdeps/s390/multiarch/memrchr.c: Likewise.
bca718
	* sysdeps/s390/multiarch/Makefile
bca718
	(sysdep_routines): Add memrchr functions.
bca718
	* sysdeps/s390/multiarch/ifunc-impl-list-common.c
bca718
	(__libc_ifunc_impl_list_common): Add ifunc test for memrchr.
bca718
---
bca718
 sysdeps/s390/multiarch/Makefile          |   3 +-
bca718
 sysdeps/s390/multiarch/ifunc-impl-list.c |   2 +
bca718
 sysdeps/s390/multiarch/memrchr-c.c       |  25 +++++
bca718
 sysdeps/s390/multiarch/memrchr-vx.S      | 160 +++++++++++++++++++++++++++++++
bca718
 sysdeps/s390/multiarch/memrchr.c         |  28 ++++++
bca718
 5 files changed, 217 insertions(+), 1 deletion(-)
bca718
 create mode 100644 sysdeps/s390/multiarch/memrchr-c.c
bca718
 create mode 100644 sysdeps/s390/multiarch/memrchr-vx.S
bca718
 create mode 100644 sysdeps/s390/multiarch/memrchr.c
bca718
bca718
diff --git a/sysdeps/s390/multiarch/Makefile b/sysdeps/s390/multiarch/Makefile
bca718
index 929a545..0805b07 100644
bca718
--- a/sysdeps/s390/multiarch/Makefile
bca718
+++ b/sysdeps/s390/multiarch/Makefile
bca718
@@ -17,7 +17,8 @@ sysdep_routines += strlen strlen-vx strlen-c \
bca718
 		   strcspn strcspn-vx strcspn-c \
bca718
 		   memchr memchr-vx \
bca718
 		   rawmemchr rawmemchr-vx rawmemchr-c \
bca718
-		   memccpy memccpy-vx memccpy-c
bca718
+		   memccpy memccpy-vx memccpy-c \
bca718
+		   memrchr memrchr-vx memrchr-c
bca718
 endif
bca718
 
bca718
 ifeq ($(subdir),wcsmbs)
bca718
diff --git a/sysdeps/s390/multiarch/ifunc-impl-list.c b/sysdeps/s390/multiarch/ifunc-impl-list.c
bca718
index 5ea258b..c235bdc 100644
bca718
--- a/sysdeps/s390/multiarch/ifunc-impl-list.c
bca718
+++ b/sysdeps/s390/multiarch/ifunc-impl-list.c
bca718
@@ -137,6 +137,8 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
bca718
 
bca718
   IFUNC_VX_IMPL (wmemcmp);
bca718
 
bca718
+  IFUNC_VX_IMPL (memrchr);
bca718
+
bca718
 #endif /* HAVE_S390_VX_ASM_SUPPORT */
bca718
 
bca718
   return i;
bca718
diff --git a/sysdeps/s390/multiarch/memrchr-c.c b/sysdeps/s390/multiarch/memrchr-c.c
bca718
new file mode 100644
bca718
index 0000000..6a32861
bca718
--- /dev/null
bca718
+++ b/sysdeps/s390/multiarch/memrchr-c.c
bca718
@@ -0,0 +1,25 @@
bca718
+/* Default memrchr 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 MEMRCHR  __memrchr_c
bca718
+
bca718
+# include <string.h>
bca718
+extern __typeof (__memrchr) __memrchr_c;
bca718
+# include <string/memrchr.c>
bca718
+#endif
bca718
diff --git a/sysdeps/s390/multiarch/memrchr-vx.S b/sysdeps/s390/multiarch/memrchr-vx.S
bca718
new file mode 100644
bca718
index 0000000..da6dbdf
bca718
--- /dev/null
bca718
+++ b/sysdeps/s390/multiarch/memrchr-vx.S
bca718
@@ -0,0 +1,160 @@
bca718
+/* Vector optimized 32/64 bit S/390 version of memrchr.
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
+/* void *memrchr (const void *s, int c, size_t n)
bca718
+   Scans memory for character c backwards
bca718
+   and returns pointer to first c.
bca718
+
bca718
+   Register usage:
bca718
+   -r0=tmp
bca718
+   -r1=tmp
bca718
+   -r2=s
bca718
+   -r3=c
bca718
+   -r4=n
bca718
+   -r5=s in loop
bca718
+
bca718
+   -v16=part of s
bca718
+   -v17=index of found c
bca718
+   -v18=c replicated
bca718
+   -v20=permute pattern
bca718
+*/
bca718
+ENTRY(__memrchr_vx)
bca718
+	.machine "z13"
bca718
+	.machinemode "zarch_nohighgprs"
bca718
+
bca718
+# if !defined __s390x__
bca718
+	llgfr	%r4,%r4
bca718
+# endif /* !defined __s390x__ */
bca718
+	clgije	%r4,0,.Lnot_found
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
+	llcr	%r3,%r3		/* char c_char = (char) c.  */
bca718
+
bca718
+	/* check byte n - 1.  */
bca718
+	llc	%r0,-1(%r4,%r2)
bca718
+	slgfi	%r4,1
bca718
+	clrje	%r0,%r3,.Lfound_end
bca718
+	jh	.Lnot_found	/* Return NULL if n is now 0.  */
bca718
+
bca718
+	larl	%r1,.Lpermute_mask /* Load permute mask.  */
bca718
+	vl	%v20,0(%r1)
bca718
+
bca718
+	/* check byte n - 2.  */
bca718
+	llc	%r0,-1(%r4,%r2)
bca718
+	slgfi	%r4,1
bca718
+	clrje	%r0,%r3,.Lfound_end
bca718
+	jh	.Lnot_found	/* Return NULL if n is now 0.  */
bca718
+
bca718
+	clgijhe	%r4,64,.Lloop64	/* If n >= 64 -> loop64.  */
bca718
+
bca718
+.Llt64:
bca718
+	/* Process n < 64 bytes.  */
bca718
+	clgijl	%r4,16,.Llt16	/* Jump away if n < 16.  */
bca718
+	aghi	%r4,-16
bca718
+	vl	%v16,0(%r4,%r2)
bca718
+	vfeebs	%v17,%v16,%v18
bca718
+	jno	.Lfound0
bca718
+	clgijl	%r4,16,.Llt16
bca718
+	aghi	%r4,-16
bca718
+	vl	%v16,0(%r4,%r2)
bca718
+	vfeebs	%v17,%v16,%v18
bca718
+	jno	.Lfound0
bca718
+	clgijl	%r4,16,.Llt16
bca718
+	aghi	%r4,-16
bca718
+	vl	%v16,0(%r4,%r2)
bca718
+	vfeebs	%v17,%v16,%v18
bca718
+	jno	.Lfound0
bca718
+.Llt16:
bca718
+	clgfi	%r4,0		/* if remaining bytes == 0, return NULL.  */
bca718
+	locghie	%r2,0
bca718
+	ber	%r14
bca718
+
bca718
+	aghi	%r4,-1		/* vll needs highest index.  */
bca718
+	vll	%v16,%r4,0(%r2)	/* Load remaining bytes.  */
bca718
+
bca718
+	/* Right-shift of v16 to mask bytes after highest index.  */
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	%v16,%v16,%v17	/* Vector shift right by byte by number of bytes
bca718
+				   specified in bits 1-4 of byte 7 in v17.   */
bca718
+	j	.Lfound_permute
bca718
+
bca718
+.Lfound48:
bca718
+	aghi	%r4,16
bca718
+.Lfound32:
bca718
+	aghi	%r4,16
bca718
+.Lfound16:
bca718
+	aghi	%r4,16
bca718
+.Lfound0:
bca718
+	la	%r2,0(%r4,%r2)	/* Set pointer to start of v16.  */
bca718
+	lghi	%r4,15		/* Set highest index in v16 to last index.  */
bca718
+.Lfound_permute:
bca718
+	/* Search for a c in v16 in reversed byte order. v16 contains %r4 + 1
bca718
+	   bytes. If v16 was not fully loaded, the bytes are already
bca718
+	   right shifted, so that the bytes in v16 can simply be reversed.  */
bca718
+	vperm	%v16,%v16,%v16,%v20 /* Permute v16 to reversed order.  */
bca718
+	vfeeb	%v16,%v16,%v18	/* Find c in reversed v16.  */
bca718
+	vlgvb	%r1,%v16,7	/* Index of c or 16 if not found.  */
bca718
+
bca718
+	/* Return NULL if there is no c in loaded bytes. */
bca718
+	clrjh	%r1,%r4,.Lnot_found
bca718
+
bca718
+	slgr	%r4,%r1
bca718
+.Lfound_end:
bca718
+	la	%r2,0(%r4,%r2)	/* Return pointer to c.  */
bca718
+	br	%r14
bca718
+
bca718
+.Lnot_found:
bca718
+	lghi	%r2,0
bca718
+	br	%r14
bca718
+
bca718
+.Lpermute_mask:
bca718
+	.byte	0x0F,0x0E,0x0D,0x0C,0x0B,0x0A,0x09,0x08
bca718
+	.byte	0x07,0x06,0x05,0x04,0x03,0x02,0x01,0x00
bca718
+
bca718
+.Lloop64:
bca718
+	aghi	%r4,-64
bca718
+	vl	%v16,48(%r4,%r2) /* Load 16bytes of memory area.  */
bca718
+	vfeebs	%v17,%v16,%v18	/* Find c.  */
bca718
+	jno	.Lfound48	/* Jump away if c was found.  */
bca718
+	vl	%v16,32(%r4,%r2)
bca718
+	vfeebs	%v17,%v16,%v18
bca718
+	jno	.Lfound32
bca718
+	vl	%v16,16(%r4,%r2)
bca718
+	vfeebs	%v17,%v16,%v18
bca718
+	jno	.Lfound16
bca718
+	vl	%v16,0(%r4,%r2)
bca718
+	vfeebs	%v17,%v16,%v18
bca718
+	jno	.Lfound0
bca718
+
bca718
+	clgijhe	%r4,64,.Lloop64	/* If n >= 64 -> loop64.  */
bca718
+	j	.Llt64
bca718
+END(__memrchr_vx)
bca718
+#endif /* HAVE_S390_VX_ASM_SUPPORT && !defined NOT_IN_libc */
bca718
diff --git a/sysdeps/s390/multiarch/memrchr.c b/sysdeps/s390/multiarch/memrchr.c
bca718
new file mode 100644
bca718
index 0000000..6596067
bca718
--- /dev/null
bca718
+++ b/sysdeps/s390/multiarch/memrchr.c
bca718
@@ -0,0 +1,28 @@
bca718
+/* Multiple versions of memrchr.
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_ifunc (__memrchr)
bca718
+weak_alias (__memrchr, memrchr)
bca718
+
bca718
+#else
bca718
+# include <string/memrchr.c>
bca718
+#endif /* !(defined HAVE_S390_VX_ASM_SUPPORT && !defined NOT_IN_libc) */
bca718
-- 
bca718
2.3.0
bca718