ce426f
From f34499b3a506359f84fcb63a125e44d4a8e4ee68 Mon Sep 17 00:00:00 2001
ce426f
From: Stefan Liebler <stli@linux.vnet.ibm.com>
ce426f
Date: Thu, 8 Oct 2015 13:23:50 +0200
ce426f
Subject: [PATCH 29/30] S390: Optimize wmemcmp.
ce426f
ce426f
upstream-commit-id: f21216015b7395c535abc01b9585a1ae3ceaa132
ce426f
https://www.sourceware.org/ml/libc-alpha/2015-07/msg00086.html
ce426f
ce426f
This patch provides optimized version of wmemcmp with the z13 vector
ce426f
instructions.
ce426f
ce426f
ChangeLog:
ce426f
ce426f
	* sysdeps/s390/multiarch/wmemcmp-c.c: New File.
ce426f
	* sysdeps/s390/multiarch/wmemcmp-vx.S: Likewise.
ce426f
	* sysdeps/s390/multiarch/wmemcmp.c: Likewise.
ce426f
	* sysdeps/s390/multiarch/Makefile
ce426f
	(sysdep_routines): Add wmemcmp functions.
ce426f
	* sysdeps/s390/multiarch/ifunc-impl-list-common.c
ce426f
	(__libc_ifunc_impl_list_common): Add ifunc test for wmemcmp.
ce426f
	* benchtests/bench-wmemcmp.c: New File.
ce426f
	* benchtests/Makefile (wcsmbs-bench): Add wmemcmp.
ce426f
---
ce426f
 benchtests/Makefile                      |   2 +-
ce426f
 benchtests/bench-wmemcmp.c               |  20 +++++
ce426f
 sysdeps/s390/multiarch/Makefile          |   3 +-
ce426f
 sysdeps/s390/multiarch/ifunc-impl-list.c |   2 +
ce426f
 sysdeps/s390/multiarch/wmemcmp-c.c       |  26 ++++++
ce426f
 sysdeps/s390/multiarch/wmemcmp-vx.S      | 149 +++++++++++++++++++++++++++++++
ce426f
 sysdeps/s390/multiarch/wmemcmp.c         |  27 ++++++
ce426f
 7 files changed, 227 insertions(+), 2 deletions(-)
ce426f
 create mode 100644 benchtests/bench-wmemcmp.c
ce426f
 create mode 100644 sysdeps/s390/multiarch/wmemcmp-c.c
ce426f
 create mode 100644 sysdeps/s390/multiarch/wmemcmp-vx.S
ce426f
 create mode 100644 sysdeps/s390/multiarch/wmemcmp.c
ce426f
ce426f
diff --git a/benchtests/Makefile b/benchtests/Makefile
ce426f
index b5edfdd..911b6df 100644
ce426f
--- a/benchtests/Makefile
ce426f
+++ b/benchtests/Makefile
ce426f
@@ -40,7 +40,7 @@ string-bench := bcopy bzero memccpy memchr memcmp memcpy memmem memmove \
ce426f
 		strspn strstr strcpy_chk stpcpy_chk memrchr strsep strtok
ce426f
 wcsmbs-bench := wcslen wcsnlen wcscpy wcpcpy wcsncpy wcpncpy wcscat wcsncat \
ce426f
 		wcsncmp wcsncmp wcschr wcschrnul wcsrchr wcsspn wcspbrk wcscspn \
ce426f
-		wmemchr wmemset
ce426f
+		wmemchr wmemset wmemcmp
ce426f
 string-bench-all := $(string-bench) ${wcsmbs-bench}
ce426f
 
ce426f
 stdlib-bench := strtod
ce426f
diff --git a/benchtests/bench-wmemcmp.c b/benchtests/bench-wmemcmp.c
ce426f
new file mode 100644
ce426f
index 0000000..8b33f89
ce426f
--- /dev/null
ce426f
+++ b/benchtests/bench-wmemcmp.c
ce426f
@@ -0,0 +1,20 @@
ce426f
+/* Measure wmemcmp functions.
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
+#define WIDE 1
ce426f
+#include "bench-memcmp.c"
ce426f
diff --git a/sysdeps/s390/multiarch/Makefile b/sysdeps/s390/multiarch/Makefile
ce426f
index eac88e0..929a545 100644
ce426f
--- a/sysdeps/s390/multiarch/Makefile
ce426f
+++ b/sysdeps/s390/multiarch/Makefile
ce426f
@@ -38,5 +38,6 @@ sysdep_routines += wcslen wcslen-vx wcslen-c \
ce426f
 		   wcspbrk wcspbrk-vx wcspbrk-c \
ce426f
 		   wcscspn wcscspn-vx wcscspn-c \
ce426f
 		   wmemchr wmemchr-vx wmemchr-c \
ce426f
-		   wmemset wmemset-vx wmemset-c
ce426f
+		   wmemset wmemset-vx wmemset-c \
ce426f
+		   wmemcmp wmemcmp-vx wmemcmp-c
ce426f
 endif
ce426f
diff --git a/sysdeps/s390/multiarch/ifunc-impl-list.c b/sysdeps/s390/multiarch/ifunc-impl-list.c
ce426f
index 44d534b..5ea258b 100644
ce426f
--- a/sysdeps/s390/multiarch/ifunc-impl-list.c
ce426f
+++ b/sysdeps/s390/multiarch/ifunc-impl-list.c
ce426f
@@ -135,6 +135,8 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
ce426f
 
ce426f
   IFUNC_VX_IMPL (wmemset);
ce426f
 
ce426f
+  IFUNC_VX_IMPL (wmemcmp);
ce426f
+
ce426f
 #endif /* HAVE_S390_VX_ASM_SUPPORT */
ce426f
 
ce426f
   return i;
ce426f
diff --git a/sysdeps/s390/multiarch/wmemcmp-c.c b/sysdeps/s390/multiarch/wmemcmp-c.c
ce426f
new file mode 100644
ce426f
index 0000000..b43e5d4
ce426f
--- /dev/null
ce426f
+++ b/sysdeps/s390/multiarch/wmemcmp-c.c
ce426f
@@ -0,0 +1,26 @@
ce426f
+/* Default wmemcmp 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 WMEMCMP  __wmemcmp_c
ce426f
+
ce426f
+# include <wchar.h>
ce426f
+extern __typeof (wmemcmp) __wmemcmp_c;
ce426f
+
ce426f
+# include <wcsmbs/wmemcmp.c>
ce426f
+#endif
ce426f
diff --git a/sysdeps/s390/multiarch/wmemcmp-vx.S b/sysdeps/s390/multiarch/wmemcmp-vx.S
ce426f
new file mode 100644
ce426f
index 0000000..b509bef
ce426f
--- /dev/null
ce426f
+++ b/sysdeps/s390/multiarch/wmemcmp-vx.S
ce426f
@@ -0,0 +1,149 @@
ce426f
+/* Vector Optimized 32/64 bit S/390 version of wmemcmp.
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
+/* int wmemcmp (const wchar_t *s1, const wchar_t *s2, size_t n)
ce426f
+   Compare at most n characters of two wchar_t-arrays.
ce426f
+
ce426f
+   Register usage:
ce426f
+   -r0=tmp
ce426f
+   -r1=number of blocks
ce426f
+   -r2=s1
ce426f
+   -r3=s2
ce426f
+   -r4=n
ce426f
+   -r5=current_len
ce426f
+   -v16=part of s1
ce426f
+   -v17=part of s2
ce426f
+   -v18=index of unequal
ce426f
+*/
ce426f
+ENTRY(__wmemcmp_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,.Lend_equal /* Nothing to do if n == 0.  */
ce426f
+
ce426f
+	/* Check range of maxlen and convert to byte-count.  */
ce426f
+# ifdef __s390x__
ce426f
+	tmhh	%r4,49152	/* Test bit 0 or 1 of maxlen.  */
ce426f
+	lghi	%r1,-4		/* Max byte-count is 18446744073709551612.  */
ce426f
+# else
ce426f
+	tmlh	%r4,49152	/* Test bit 0 or 1 of maxlen.  */
ce426f
+	llilf	%r1,4294967292	/* Max byte-count is 4294967292.  */
ce426f
+# endif /* !__s390x__ */
ce426f
+	sllg	%r4,%r4,2	/* Convert character-count to byte-count.  */
ce426f
+	locgrne	%r4,%r1		/* Use max byte-count, if bit 0/1 was one.  */
ce426f
+
ce426f
+	lghi	%r5,0		/* current_len = 0.  */
ce426f
+
ce426f
+	clgijh	%r4,16,.Lgt16
ce426f
+
ce426f
+.Lremaining:
ce426f
+	aghi	%r4,-1		/* vstl needs highest index.  */
ce426f
+	vll	%v16,%r4,0(%r2)
ce426f
+	vll	%v17,%r4,0(%r3)
ce426f
+	vfenef	%v18,%v16,%v17	/* Compare not equal.  */
ce426f
+	vlgvb	%r1,%v18,7	/* Load unequal index or 16 if not found.  */
ce426f
+	clrj	%r1,%r4,12,.Lfound2 /* r1 <= r4 -> unequal within loaded
ce426f
+					bytes.  */
ce426f
+
ce426f
+.Lend_equal:
ce426f
+	lghi	%r2,0
ce426f
+	br	%r14
ce426f
+
ce426f
+.Lfound:
ce426f
+	/* vfenezf found an unequal element or zero.
ce426f
+	   This instruction compares unsigned words, but wchar_t is signed.
ce426f
+	   Thus we have to compare the found element again.  */
ce426f
+	vlgvb	%r1,%v18,7	/* Extract not equal byte-index.  */
ce426f
+.Lfound2:
ce426f
+	srl	%r1,2		/* And convert it to character-index.  */
ce426f
+	vlgvf	%r0,%v16,0(%r1)	/* Load character-values.  */
ce426f
+	vlgvf	%r1,%v17,0(%r1)
ce426f
+	cr	%r0,%r1
ce426f
+	je	.Lend_equal
ce426f
+	lghi	%r2,1
ce426f
+	lghi	%r1,-1
ce426f
+	locgrl	%r2,%r1
ce426f
+	br	%r14
ce426f
+
ce426f
+.Lgt16:
ce426f
+	clgijh	%r4,64,.Lpreloop64
ce426f
+
ce426f
+.Lpreloop16:
ce426f
+	srlg	%r1,%r4,4	/* Split into 16byte blocks */
ce426f
+.Lloop16:
ce426f
+	vl	%v16,0(%r5,%r2)
ce426f
+	vl	%v17,0(%r5,%r3)
ce426f
+	aghi	%r5,16
ce426f
+	vfenefs	%v18,%v16,%v17	/* Compare not equal.  */
ce426f
+	jno	.Lfound
ce426f
+	brctg	%r1,.Lloop16	/* Loop until all blocks are processed.  */
ce426f
+
ce426f
+	llgfr	%r4,%r4
ce426f
+	nilf	%r4,15		/* Get remaining bytes */
ce426f
+	locgre	%r2,%r4
ce426f
+	ber	%r14
ce426f
+	la	%r2,0(%r5,%r2)
ce426f
+	la	%r3,0(%r5,%r3)
ce426f
+	j	.Lremaining
ce426f
+
ce426f
+.Lpreloop64:
ce426f
+	srlg	%r1,%r4,6	/* Split into 64byte blocks */
ce426f
+.Lloop64:
ce426f
+	vl	%v16,0(%r5,%r2)
ce426f
+	vl	%v17,0(%r5,%r3)
ce426f
+	vfenefs	%v18,%v16,%v17	/* Compare not equal.  */
ce426f
+	jno	.Lfound
ce426f
+
ce426f
+	vl	%v16,16(%r5,%r2)
ce426f
+	vl	%v17,16(%r5,%r3)
ce426f
+	vfenefs	%v18,%v16,%v17
ce426f
+	jno	.Lfound
ce426f
+
ce426f
+	vl	%v16,32(%r5,%r2)
ce426f
+	vl	%v17,32(%r5,%r3)
ce426f
+	vfenefs	%v18,%v16,%v17
ce426f
+	jno	.Lfound
ce426f
+
ce426f
+	vl	%v16,48(%r5,%r2)
ce426f
+	vl	%v17,48(%r5,%r3)
ce426f
+	aghi	%r5,64
ce426f
+	vfenefs	%v18,%v16,%v17
ce426f
+	jno	.Lfound
ce426f
+
ce426f
+	brctg	%r1,.Lloop64	/* Loop until all blocks are processed.  */
ce426f
+
ce426f
+	llgfr	%r4,%r4
ce426f
+	nilf	%r4,63		/* Get remaining bytes */
ce426f
+	locgre	%r2,%r4
ce426f
+	ber	%r14
ce426f
+	clgijh	%r4,16,.Lpreloop16
ce426f
+	la	%r2,0(%r5,%r2)
ce426f
+	la	%r3,0(%r5,%r3)
ce426f
+	j	.Lremaining
ce426f
+END(__wmemcmp_vx)
ce426f
+#endif /* HAVE_S390_VX_ASM_SUPPORT && !defined NOT_IN_libc */
ce426f
diff --git a/sysdeps/s390/multiarch/wmemcmp.c b/sysdeps/s390/multiarch/wmemcmp.c
ce426f
new file mode 100644
ce426f
index 0000000..24a57e9
ce426f
--- /dev/null
ce426f
+++ b/sysdeps/s390/multiarch/wmemcmp.c
ce426f
@@ -0,0 +1,27 @@
ce426f
+/* Multiple versions of wmemcmp.
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 <wchar.h>
ce426f
+# include <ifunc-resolve.h>
ce426f
+
ce426f
+s390_vx_libc_ifunc2 (__wmemcmp, wmemcmp)
ce426f
+
ce426f
+#else
ce426f
+# include <wcsmbs/wmemcmp.c>
ce426f
+#endif /* !(defined HAVE_S390_VX_ASM_SUPPORT && !defined NOT_IN_libc) */
ce426f
-- 
ce426f
2.3.0
ce426f