bca718
From 130a0ae7cad6bc7a2d102624027a1d8de19cc3b3 Mon Sep 17 00:00:00 2001
bca718
From: Stefan Liebler <stli@linux.vnet.ibm.com>
bca718
Date: Thu, 8 Oct 2015 13:21:11 +0200
bca718
Subject: [PATCH 27/30] S390: Optimize memccpy.
bca718
bca718
upstream-commit-id: 9b593dc3055d44a4179c03050be58a437ae385a1
bca718
https://www.sourceware.org/ml/libc-alpha/2015-07/msg00100.html
bca718
bca718
This patch provides optimized versions of memccpy with the z13 vector
bca718
instructions.
bca718
bca718
ChangeLog:
bca718
bca718
	* sysdeps/s390/multiarch/memccpy-c.c: New File.
bca718
	* sysdeps/s390/multiarch/memccpy-vx.S: Likewise.
bca718
	* sysdeps/s390/multiarch/memccpy.c: Likewise.
bca718
	* sysdeps/s390/multiarch/Makefile
bca718
	(sysdep_routines): Add memccpy functions.
bca718
	* sysdeps/s390/multiarch/ifunc-impl-list-common.c
bca718
	(__libc_ifunc_impl_list_common): Add ifunc test for memccpy.
bca718
	* string/memccpy.c: Use MEMCCPY if defined.
bca718
---
bca718
 string/memccpy.c                         |   4 +
bca718
 sysdeps/s390/multiarch/Makefile          |   3 +-
bca718
 sysdeps/s390/multiarch/ifunc-impl-list.c |   2 +
bca718
 sysdeps/s390/multiarch/memccpy-c.c       |  27 ++++++
bca718
 sysdeps/s390/multiarch/memccpy-vx.S      | 156 +++++++++++++++++++++++++++++++
bca718
 sysdeps/s390/multiarch/memccpy.c         |  28 ++++++
bca718
 6 files changed, 219 insertions(+), 1 deletion(-)
bca718
 create mode 100644 sysdeps/s390/multiarch/memccpy-c.c
bca718
 create mode 100644 sysdeps/s390/multiarch/memccpy-vx.S
bca718
 create mode 100644 sysdeps/s390/multiarch/memccpy.c
bca718
bca718
diff --git a/string/memccpy.c b/string/memccpy.c
bca718
index a8649ff..78c13b6 100644
bca718
--- a/string/memccpy.c
bca718
+++ b/string/memccpy.c
bca718
@@ -20,6 +20,10 @@
bca718
 #undef __memccpy
bca718
 #undef memccpy
bca718
 
bca718
+#ifdef MEMCCPY
bca718
+# define __memccpy MEMCCPY
bca718
+#endif
bca718
+
bca718
 /* Copy no more than N bytes of SRC to DEST, stopping when C is found.
bca718
    Return the position in DEST one byte past where C was copied, or
bca718
    NULL if C was not found in the first N bytes of SRC.  */
bca718
diff --git a/sysdeps/s390/multiarch/Makefile b/sysdeps/s390/multiarch/Makefile
bca718
index 4a04c34..87dff0f 100644
bca718
--- a/sysdeps/s390/multiarch/Makefile
bca718
+++ b/sysdeps/s390/multiarch/Makefile
bca718
@@ -16,7 +16,8 @@ sysdep_routines += strlen strlen-vx strlen-c \
bca718
 		   strpbrk strpbrk-vx strpbrk-c \
bca718
 		   strcspn strcspn-vx strcspn-c \
bca718
 		   memchr memchr-vx \
bca718
-		   rawmemchr rawmemchr-vx rawmemchr-c
bca718
+		   rawmemchr rawmemchr-vx rawmemchr-c \
bca718
+		   memccpy memccpy-vx memccpy-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 d4c7c0d..c90fb6b 100644
bca718
--- a/sysdeps/s390/multiarch/ifunc-impl-list.c
bca718
+++ b/sysdeps/s390/multiarch/ifunc-impl-list.c
bca718
@@ -131,6 +131,8 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
bca718
   IFUNC_VX_IMPL (wmemchr);
bca718
   IFUNC_VX_IMPL (rawmemchr);
bca718
 
bca718
+  IFUNC_VX_IMPL (memccpy);
bca718
+
bca718
 #endif /* HAVE_S390_VX_ASM_SUPPORT */
bca718
 
bca718
   return i;
bca718
diff --git a/sysdeps/s390/multiarch/memccpy-c.c b/sysdeps/s390/multiarch/memccpy-c.c
bca718
new file mode 100644
bca718
index 0000000..4d07b63
bca718
--- /dev/null
bca718
+++ b/sysdeps/s390/multiarch/memccpy-c.c
bca718
@@ -0,0 +1,27 @@
bca718
+/* Default memccpy 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 MEMCCPY  __memccpy_c
bca718
+# undef weak_alias
bca718
+# define weak_alias(a, b)
bca718
+
bca718
+# include <string.h>
bca718
+extern __typeof (__memccpy) __memccpy_c;
bca718
+# include <string/memccpy.c>
bca718
+#endif
bca718
diff --git a/sysdeps/s390/multiarch/memccpy-vx.S b/sysdeps/s390/multiarch/memccpy-vx.S
bca718
new file mode 100644
bca718
index 0000000..b1dc69c
bca718
--- /dev/null
bca718
+++ b/sysdeps/s390/multiarch/memccpy-vx.S
bca718
@@ -0,0 +1,156 @@
bca718
+/* Vector optimized 32/64 bit S/390 version of memccpy.
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 *memccpy (void * dest, const void *src, int c, size_t n)
bca718
+   Copies no more than n bytes from src to dest,
bca718
+   stopping when the character c is found
bca718
+   and returns pointer next to c in dest or null if c not found.
bca718
+
bca718
+   Register usage:
bca718
+   -r0=tmp
bca718
+   -r1=tmp
bca718
+   -r2=dest
bca718
+   -r3=src
bca718
+   -r4=c
bca718
+   -r5=n
bca718
+   -r6=current_len
bca718
+   -v16=part of s
bca718
+   -v17=index of found c
bca718
+   -v18=c replicated
bca718
+   -v19=part #2 of s
bca718
+   -v31=save area for r6
bca718
+*/
bca718
+ENTRY(__memccpy_vx)
bca718
+	.machine "z13"
bca718
+	.machinemode "zarch_nohighgprs"
bca718
+
bca718
+# if !defined __s390x__
bca718
+	llgfr	%r5,%r5
bca718
+# endif /* !defined __s390x__ */
bca718
+
bca718
+	vlvgp	%v31,%r6,%r7	/* Save registers.  */
bca718
+	clgije	%r5,0,.Lnf_end	/* If len == 0 then exit.  */
bca718
+
bca718
+	vlbb	%v16,0(%r3),6	/* Load s until next 4k-byte boundary.  */
bca718
+	lcbb	%r0,0(%r3),6	/* Get bytes to 4k-byte boundary or 16.  */
bca718
+	llgfr	%r0,%r0		/* Convert 32bit to 64bit.  */
bca718
+
bca718
+	vlvgb	%v18,%r4,0	/* Generate vector which elements are all c.
bca718
+				   if c > 255, c will be truncated.  */
bca718
+	vrepb	%v18,%v18,0
bca718
+	lghi	%r6,0		/* current_len = 0.  */
bca718
+
bca718
+	clgrjle	%r5,%r0,.Lremaining_v16 /* If maxlen <= loaded-bytes
bca718
+					   -> Process remaining.  */
bca718
+
bca718
+	vfeebs	%v17,%v16,%v18	/* Find c.  */
bca718
+	vlgvb	%r1,%v17,7	/* Load byte index of c.  */
bca718
+	clgrjl	%r1,%r0,.Lfound_v16 /* Found c is within loaded bytes.  */
bca718
+
bca718
+	/* Align s to 16 byte.  */
bca718
+	risbgn	%r1,%r3,60,128+63,0 /* %r3 = bits 60-63 of %r2 'and' 15.  */
bca718
+	lghi	%r6,15		/* current_len = 15.  */
bca718
+	slr	%r6,%r1		/* Compute highest index to 16byte boundary.  */
bca718
+
bca718
+	vstl	%v16,%r6,0(%r2)	/* Store prcessed bytes */
bca718
+	ahi	%r6,1
bca718
+
bca718
+.Lpreloop1:
bca718
+	/* Now we are 16byte aligned, so we can load
bca718
+	   a full vreg without page fault.  */
bca718
+	vl	%v16,0(%r6,%r3)	/* Load s.  */
bca718
+	clgijl	%r5,17,.Lremaining_v16	/* If n <= 16,
bca718
+					   process remaining bytes.  */
bca718
+	lgr	%r7,%r5
bca718
+	slgfi	%r7,16		/* border_len = n - 16.  */
bca718
+	j	.Lloop1
bca718
+
bca718
+.Lloop2:
bca718
+	vl	%v16,16(%r6,%r3)
bca718
+	vst	%v19,0(%r6,%r2)
bca718
+	aghi	%r6,16
bca718
+
bca718
+.Lloop1:
bca718
+	clgrjhe	%r6,%r7,.Lremaining_v16 /* If current_len >= border
bca718
+					   then process remaining bytes.  */
bca718
+	vfeebs	%v17,%v16,%v18	/* Find c.  */
bca718
+	jl	.Lfound_v16	/* Jump away if c was found.  */
bca718
+	vl	%v19,16(%r6,%r3) /* Load next s part.  */
bca718
+	vst	%v16,0(%r6,%r2)	/* Store previous part without c.  */
bca718
+	aghi	%r6,16
bca718
+
bca718
+	clgrjhe	%r6,%r7,.Lremaining_v19
bca718
+	vfeebs	%v17,%v19,%v18
bca718
+	jl	.Lfound_v19
bca718
+	vl	%v16,16(%r6,%r3)
bca718
+	vst	%v19,0(%r6,%r2)
bca718
+	aghi	%r6,16
bca718
+
bca718
+	clgrjhe	%r6,%r7,.Lremaining_v16
bca718
+	vfeebs	%v17,%v16,%v18
bca718
+	jl	.Lfound_v16
bca718
+	vl	%v19,16(%r6,%r3)
bca718
+	vst	%v16,0(%r6,%r2)
bca718
+	aghi	%r6,16
bca718
+
bca718
+	clgrjhe	%r6,%r7,.Lremaining_v19
bca718
+	vfeebs	%v17,%v19,%v18
bca718
+	jo	.Lloop2
bca718
+
bca718
+.Lfound_v19:
bca718
+	vlr	%v16,%v19
bca718
+.Lfound_v16:
bca718
+	/* v16 contains c. Store remaining bytes to c. currlen hasnĀ“t
bca718
+	   reached border, thus checking for maxlen is not needed! */
bca718
+	vlgvb	%r1,%v17,7	/* Load byte index of c.  */
bca718
+	la	%r2,0(%r6,%r2)	/* vstl has no support for index-register.  */
bca718
+.Lfound_v16_store:
bca718
+	vstl	%v16,%r1,0(%r2)	/* Copy bytes including c.  */
bca718
+	la	%r2,1(%r1,%r2)	/* Return pointer next to c in dest.  */
bca718
+	vlgvg	%r6,%v31,0
bca718
+	vlgvg	%r7,%v31,1
bca718
+	br	%r14
bca718
+
bca718
+.Lremaining_v19:
bca718
+	vlr	%v16,%v19
bca718
+.Lremaining_v16:
bca718
+	/* v16 contains the remaining bytes [1...16].
bca718
+	   Check and store remaining bytes.  */
bca718
+	vfeebs	%v17,%v16,%v18
bca718
+	slgrk	%r7,%r5,%r6	/* Remaining bytes = maxlen - current_len.  */
bca718
+	aghi	%r7,-1		/* vstl needs highest index.  */
bca718
+	la	%r2,0(%r6,%r2)	/* vstl has no index register.  */
bca718
+	vlgvb	%r1,%v17,7	/* Load index of c or 16 if not found.  */
bca718
+	/* c in remaining bytes? -> Jump away (c-index <= max-index)  */
bca718
+	clrjle	%r1,%r7,.Lfound_v16_store
bca718
+	vstl	%v16,%r7,0(%r2)	/* Store remaining bytes.  */
bca718
+
bca718
+.Lnf_end:
bca718
+	vlgvg	%r6,%v31,0
bca718
+	vlgvg	%r7,%v31,1
bca718
+	lghi	%r2,0		/* Return null.  */
bca718
+	br	%r14
bca718
+END(__memccpy_vx)
bca718
+#endif /* HAVE_S390_VX_ASM_SUPPORT && !defined NOT_IN_libc */
bca718
diff --git a/sysdeps/s390/multiarch/memccpy.c b/sysdeps/s390/multiarch/memccpy.c
bca718
new file mode 100644
bca718
index 0000000..746bc25
bca718
--- /dev/null
bca718
+++ b/sysdeps/s390/multiarch/memccpy.c
bca718
@@ -0,0 +1,28 @@
bca718
+/* Multiple versions of memccpy.
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 (__memccpy)
bca718
+weak_alias (__memccpy, memccpy)
bca718
+
bca718
+#else
bca718
+# include <string/memccpy.c>
bca718
+#endif /* !(defined HAVE_S390_VX_ASM_SUPPORT && !defined NOT_IN_libc) */
bca718
-- 
bca718
2.3.0
bca718