fa3bfd
From 5855d3b7d0fc85b2827755bbb3b4dacf6a08dae7 Mon Sep 17 00:00:00 2001
fa3bfd
From: Stefan Liebler <stli@linux.vnet.ibm.com>
fa3bfd
Date: Thu, 27 Jul 2017 10:53:59 +0200
fa3bfd
Subject: [PATCH 10/10] S390: Use cu21 instruction for converting from utf16 to
fa3bfd
 utf8.
fa3bfd
fa3bfd
upstream-commit a37b5daa6bc7fbcbbc229b2549a161fa15023f41
fa3bfd
fa3bfd
This patch adds an ifunc variant to use the cu instruction on arch12 CPUs.
fa3bfd
This new ifunc variant can be built if binutils support z13 vector
fa3bfd
instructions.  At runtime, HWCAP_S390_VXE decides if we can use the
fa3bfd
cu21 instruction.
fa3bfd
fa3bfd
ChangeLog:
fa3bfd
fa3bfd
	* sysdeps/s390/utf8-utf16-z9.c (__to_utf8_loop_vx_cu):
fa3bfd
	Use vector and cu21 instruction.
fa3bfd
	* sysdeps/s390/multiarch/utf8-utf16-z9.c:
fa3bfd
	Add __to_utf8_loop_vx_cu in ifunc resolver.
fa3bfd
---
fa3bfd
 sysdeps/s390/multiarch/utf8-utf16-z9.c |   8 ++-
fa3bfd
 sysdeps/s390/utf8-utf16-z9.c           | 117 +++++++++++++++++++++++++++++++++
fa3bfd
 2 files changed, 122 insertions(+), 3 deletions(-)
fa3bfd
fa3bfd
diff --git a/sysdeps/s390/multiarch/utf8-utf16-z9.c b/sysdeps/s390/multiarch/utf8-utf16-z9.c
fa3bfd
index b55ef1a..1252281 100644
fa3bfd
--- a/sysdeps/s390/multiarch/utf8-utf16-z9.c
fa3bfd
+++ b/sysdeps/s390/multiarch/utf8-utf16-z9.c
fa3bfd
@@ -41,8 +41,10 @@ s390_libc_ifunc_expr (FROM_LOOP_DEFAULT, FROM_LOOP,
fa3bfd
 			: FROM_LOOP_DEFAULT);
fa3bfd
 
fa3bfd
 s390_libc_ifunc_expr (TO_LOOP_DEFAULT, TO_LOOP,
fa3bfd
-		      (HAVE_TO_VX && (hwcap & HWCAP_S390_VX))
fa3bfd
-		      ? TO_LOOP_VX
fa3bfd
-		      : TO_LOOP_DEFAULT);
fa3bfd
+		      (HAVE_TO_VX_CU && (hwcap & HWCAP_S390_VXE))
fa3bfd
+		      ? TO_LOOP_VX_CU
fa3bfd
+		      : (HAVE_TO_VX && (hwcap & HWCAP_S390_VX))
fa3bfd
+			? TO_LOOP_VX
fa3bfd
+			: TO_LOOP_DEFAULT);
fa3bfd
 
fa3bfd
 #include <iconv/skeleton.c>
fa3bfd
diff --git a/sysdeps/s390/utf8-utf16-z9.c b/sysdeps/s390/utf8-utf16-z9.c
fa3bfd
index d870a29..76e463a 100644
fa3bfd
--- a/sysdeps/s390/utf8-utf16-z9.c
fa3bfd
+++ b/sysdeps/s390/utf8-utf16-z9.c
fa3bfd
@@ -52,9 +52,11 @@
fa3bfd
 #if defined HAVE_S390_VX_ASM_SUPPORT && defined USE_MULTIARCH
fa3bfd
 # define HAVE_FROM_VX		1
fa3bfd
 # define HAVE_TO_VX		1
fa3bfd
+# define HAVE_TO_VX_CU		1
fa3bfd
 #else
fa3bfd
 # define HAVE_FROM_VX		0
fa3bfd
 # define HAVE_TO_VX		0
fa3bfd
+# define HAVE_TO_VX_CU		0
fa3bfd
 #endif
fa3bfd
 
fa3bfd
 #if defined HAVE_S390_VX_GCC_SUPPORT
fa3bfd
@@ -817,6 +819,121 @@ gconv_end (struct __gconv_step *data)
fa3bfd
 # define TO_LOOP_VX		NULL
fa3bfd
 #endif /* HAVE_TO_VX != 1  */
fa3bfd
 
fa3bfd
+#if HAVE_TO_VX_CU == 1
fa3bfd
+#define BODY_TO_VX_CU							\
fa3bfd
+  {									\
fa3bfd
+    register const unsigned char* pInput asm ("8") = inptr;		\
fa3bfd
+    register size_t inlen asm ("9") = inend - inptr;			\
fa3bfd
+    register unsigned char* pOutput asm ("10") = outptr;		\
fa3bfd
+    register size_t outlen asm ("11") = outend - outptr;		\
fa3bfd
+    unsigned long tmp, tmp2, tmp3;					\
fa3bfd
+    asm volatile (".machine push\n\t"					\
fa3bfd
+		  ".machine \"z13\"\n\t"				\
fa3bfd
+		  ".machinemode \"zarch_nohighgprs\"\n\t"		\
fa3bfd
+		  /* Setup to check for values <= 0x7f.  */		\
fa3bfd
+		  "    larl %[R_TMP],9f\n\t"				\
fa3bfd
+		  "    vlm %%v30,%%v31,0(%[R_TMP])\n\t"			\
fa3bfd
+		  CONVERT_32BIT_SIZE_T ([R_INLEN])			\
fa3bfd
+		  CONVERT_32BIT_SIZE_T ([R_OUTLEN])			\
fa3bfd
+		  /* Loop which handles UTF-16 chars <=0x7f.  */	\
fa3bfd
+		  "0:  clgijl %[R_INLEN],32,20f\n\t"			\
fa3bfd
+		  "    clgijl %[R_OUTLEN],16,20f\n\t"			\
fa3bfd
+		  "1:  vlm %%v16,%%v17,0(%[R_IN])\n\t"			\
fa3bfd
+		  "    lghi %[R_TMP2],0\n\t"				\
fa3bfd
+		  /* Check for > 1byte UTF-8 chars.  */			\
fa3bfd
+		  "    vstrchs %%v19,%%v16,%%v30,%%v31\n\t"		\
fa3bfd
+		  "    jno 10f\n\t" /* Jump away if not all bytes are 1byte \
fa3bfd
+				       UTF8 chars.  */			\
fa3bfd
+		  "    vstrchs %%v19,%%v17,%%v30,%%v31\n\t"		\
fa3bfd
+		  "    jno 11f\n\t" /* Jump away if not all bytes are 1byte \
fa3bfd
+				       UTF8 chars.  */			\
fa3bfd
+		  /* Shorten to UTF-8.  */				\
fa3bfd
+		  "    vpkh %%v18,%%v16,%%v17\n\t"			\
fa3bfd
+		  "    la %[R_IN],32(%[R_IN])\n\t"			\
fa3bfd
+		  "    aghi %[R_INLEN],-32\n\t"				\
fa3bfd
+		  /* Store 16 bytes to buf_out.  */			\
fa3bfd
+		  "    vst %%v18,0(%[R_OUT])\n\t"			\
fa3bfd
+		  "    aghi %[R_OUTLEN],-16\n\t"			\
fa3bfd
+		  "    la %[R_OUT],16(%[R_OUT])\n\t"			\
fa3bfd
+		  "    clgijl %[R_INLEN],32,20f\n\t"			\
fa3bfd
+		  "    clgijl %[R_OUTLEN],16,20f\n\t"			\
fa3bfd
+		  "    j 1b\n\t"					\
fa3bfd
+		  /* Setup to check for ch > 0x7f. (v30, v31)  */	\
fa3bfd
+		  "9:  .short 0x7f,0x7f,0x0,0x0,0x0,0x0,0x0,0x0\n\t"	\
fa3bfd
+		  "    .short 0x2000,0x2000,0x0,0x0,0x0,0x0,0x0,0x0\n\t" \
fa3bfd
+		  /* At least one byte is > 0x7f.			\
fa3bfd
+		     Store the preceding 1-byte chars.  */		\
fa3bfd
+		  "11: lghi %[R_TMP2],16\n\t" /* match was found in v17.  */ \
fa3bfd
+		  "10: vlgvb %[R_TMP],%%v19,7\n\t"			\
fa3bfd
+		  /* Shorten to UTF-8.  */				\
fa3bfd
+		  "    vpkh %%v18,%%v16,%%v17\n\t"			\
fa3bfd
+		  "    ar %[R_TMP],%[R_TMP2]\n\t" /* Number of in bytes.  */ \
fa3bfd
+		  "    srlg %[R_TMP3],%[R_TMP],1\n\t" /* Number of out bytes.  */ \
fa3bfd
+		  "    ahik %[R_TMP2],%[R_TMP3],-1\n\t" /* Highest index to store.  */ \
fa3bfd
+		  "    jl 20f\n\t"					\
fa3bfd
+		  "    vstl %%v18,%[R_TMP2],0(%[R_OUT])\n\t"		\
fa3bfd
+		  /* Update pointers.  */				\
fa3bfd
+		  "    la %[R_IN],0(%[R_TMP],%[R_IN])\n\t"		\
fa3bfd
+		  "    slgr %[R_INLEN],%[R_TMP]\n\t"			\
fa3bfd
+		  "    la %[R_OUT],0(%[R_TMP3],%[R_OUT])\n\t"		\
fa3bfd
+		  "    slgr %[R_OUTLEN],%[R_TMP3]\n\t"			\
fa3bfd
+		  /* Handles UTF16 surrogates with convert instruction.  */ \
fa3bfd
+		  "20: cu21 %[R_OUT],%[R_IN],1\n\t"			\
fa3bfd
+		  "    jo 0b\n\t" /* Try vector implemenation again.  */ \
fa3bfd
+		  "    lochil %[R_RES],%[RES_OUT_FULL]\n\t" /* cc == 1.  */ \
fa3bfd
+		  "    lochih %[R_RES],%[RES_IN_ILL]\n\t" /* cc == 2.  */ \
fa3bfd
+		  ".machine pop"					\
fa3bfd
+		  : /* outputs */ [R_IN] "+a" (pInput)			\
fa3bfd
+		    , [R_INLEN] "+d" (inlen), [R_OUT] "+a" (pOutput)	\
fa3bfd
+		    , [R_OUTLEN] "+d" (outlen), [R_TMP] "=a" (tmp)	\
fa3bfd
+		    , [R_TMP2] "=d" (tmp2), [R_TMP3] "=a" (tmp3)	\
fa3bfd
+		    , [R_RES] "+d" (result)				\
fa3bfd
+		  : /* inputs */					\
fa3bfd
+		    [RES_OUT_FULL] "i" (__GCONV_FULL_OUTPUT)		\
fa3bfd
+		    , [RES_IN_ILL] "i" (__GCONV_ILLEGAL_INPUT)		\
fa3bfd
+		  : /* clobber list */ "memory", "cc"			\
fa3bfd
+		    ASM_CLOBBER_VR ("v16") ASM_CLOBBER_VR ("v17")	\
fa3bfd
+		    ASM_CLOBBER_VR ("v18") ASM_CLOBBER_VR ("v19")	\
fa3bfd
+		    ASM_CLOBBER_VR ("v30") ASM_CLOBBER_VR ("v31")	\
fa3bfd
+		  );							\
fa3bfd
+    inptr = pInput;							\
fa3bfd
+    outptr = pOutput;							\
fa3bfd
+									\
fa3bfd
+    if (__glibc_likely (inlen == 0)					\
fa3bfd
+	|| result == __GCONV_FULL_OUTPUT)				\
fa3bfd
+      break;								\
fa3bfd
+    if (inlen == 1)							\
fa3bfd
+      {									\
fa3bfd
+	/* Input does not contain a complete utf16 character.  */	\
fa3bfd
+	result = __GCONV_INCOMPLETE_INPUT;				\
fa3bfd
+	break;								\
fa3bfd
+      }									\
fa3bfd
+    else if (result != __GCONV_ILLEGAL_INPUT)				\
fa3bfd
+      {									\
fa3bfd
+	/* Input is >= 2 and < 4 bytes (as cu21 would have processed	\
fa3bfd
+	   a possible next utf16 character) and not illegal.		\
fa3bfd
+	   => we have a single high surrogate at end of input.  */	\
fa3bfd
+	result = __GCONV_INCOMPLETE_INPUT;				\
fa3bfd
+	break;								\
fa3bfd
+      }									\
fa3bfd
+									\
fa3bfd
+    STANDARD_TO_LOOP_ERR_HANDLER (2);					\
fa3bfd
+  }
fa3bfd
+
fa3bfd
+/* Generate loop-function with vector and utf-convert instructions.  */
fa3bfd
+# define MIN_NEEDED_INPUT	MIN_NEEDED_TO
fa3bfd
+# define MAX_NEEDED_INPUT	MAX_NEEDED_TO
fa3bfd
+# define MIN_NEEDED_OUTPUT	MIN_NEEDED_FROM
fa3bfd
+# define MAX_NEEDED_OUTPUT	MAX_NEEDED_FROM
fa3bfd
+# define TO_LOOP_VX_CU		__to_utf8_loop_vx_cu
fa3bfd
+# define LOOPFCT		TO_LOOP_VX_CU
fa3bfd
+# define BODY                   BODY_TO_VX_CU
fa3bfd
+# define LOOP_NEED_FLAGS
fa3bfd
+# include <iconv/loop.c>
fa3bfd
+#else
fa3bfd
+# define TO_LOOP_VX_CU		NULL
fa3bfd
+#endif /* HAVE_TO_VX_CU != 1  */
fa3bfd
+
fa3bfd
 /* This file also exists in sysdeps/s390/multiarch/ which
fa3bfd
    generates ifunc resolvers for FROM/TO_LOOP functions
fa3bfd
    and includes iconv/skeleton.c afterwards.  */
fa3bfd
-- 
fa3bfd
1.8.3.1
fa3bfd