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