ce426f
From ec79c95f95869a30d3f13c6229ebef1ad4931281 Mon Sep 17 00:00:00 2001
ce426f
From: Stefan Liebler <stli@linux.vnet.ibm.com>
ce426f
Date: Mon, 7 Nov 2016 16:20:45 +0100
ce426f
Subject: [PATCH 11/17] S390: Optimize utf8-utf16 module.
ce426f
ce426f
Upstream commit 5bd11b19099b3f22d821515f9c93f1ecc1a7e15e
ce426f
ce426f
This patch reworks the s390 specific module to convert between utf8 and utf16.
ce426f
Now ifunc is used to choose either the c or etf3eh (with convert utf instruction)
ce426f
variants at runtime. Furthermore a new vector variant for z13 is introduced
ce426f
which will be build and chosen if vector support is available at build / runtime.
ce426f
ce426f
In case of converting utf 8 to utf16, the vector variant optimizes input of
ce426f
1byte utf8 characters. The convert utf instruction is used if a multibyte utf8
ce426f
character is found.
ce426f
ce426f
For the other direction utf16 to utf8, the cu21 instruction can't be re-enabled,
ce426f
because it does not report an error, if the input-stream consists of a single
ce426f
low surrogate utf16 char (e.g. 0xdc00). This applies to the newest z13, too.
ce426f
Thus there is only the c or the new vector variant, which can handle 1..4 byte
ce426f
utf8 characters.
ce426f
ce426f
The c variant from utf16 to utf8 has beed fixed. If a high surrogate was at the
ce426f
end of the input-buffer, then errno was set to EINVAL and the input-pointer
ce426f
pointed just after the high surrogate. Now it points to the beginning of the
ce426f
high surrogate.
ce426f
ce426f
This patch also fixes some whitespace errors. The c variant from utf8 to utf16
ce426f
is now checking that tail-bytes starts with 0b10... and the value is not in
ce426f
range of an utf16 surrogate.
ce426f
ce426f
Furthermore, the etf3eh variants are handling the "UTF-xx//IGNORE" case now.
ce426f
Before they ignored the ignore-case and always stopped at an error.
ce426f
ce426f
ChangeLog:
ce426f
ce426f
	* sysdeps/s390/s390-64/utf8-utf16-z9.c: Use ifunc to select c,
ce426f
	etf3eh or new vector loop-variant.
ce426f
---
ce426f
 sysdeps/s390/s390-64/utf8-utf16-z9.c | 547 ++++++++++++++++++++++++++++-------
ce426f
 1 file changed, 441 insertions(+), 106 deletions(-)
ce426f
ce426f
diff --git a/sysdeps/s390/s390-64/utf8-utf16-z9.c b/sysdeps/s390/s390-64/utf8-utf16-z9.c
ce426f
index 590a149..b36ee9e 100644
ce426f
--- a/sysdeps/s390/s390-64/utf8-utf16-z9.c
ce426f
+++ b/sysdeps/s390/s390-64/utf8-utf16-z9.c
ce426f
@@ -30,33 +30,27 @@
ce426f
 #include <dl-procinfo.h>
ce426f
 #include <gconv.h>
ce426f
 
ce426f
-/* UTF-16 big endian byte order mark.  */
ce426f
-#define BOM_UTF16	0xfeff
ce426f
+#if defined HAVE_S390_VX_GCC_SUPPORT
ce426f
+# define ASM_CLOBBER_VR(NR) , NR
ce426f
+#else
ce426f
+# define ASM_CLOBBER_VR(NR)
ce426f
+#endif
ce426f
 
ce426f
+/* Defines for skeleton.c.  */
ce426f
 #define DEFINE_INIT		0
ce426f
 #define DEFINE_FINI		0
ce426f
 #define MIN_NEEDED_FROM		1
ce426f
 #define MAX_NEEDED_FROM		4
ce426f
 #define MIN_NEEDED_TO		2
ce426f
 #define MAX_NEEDED_TO		4
ce426f
-#define FROM_LOOP		from_utf8_loop
ce426f
-#define TO_LOOP			to_utf8_loop
ce426f
+#define FROM_LOOP		__from_utf8_loop
ce426f
+#define TO_LOOP			__to_utf8_loop
ce426f
 #define FROM_DIRECTION		(dir == from_utf8)
ce426f
 #define ONE_DIRECTION           0
ce426f
-#define PREPARE_LOOP							\
ce426f
-  enum direction dir = ((struct utf8_data *) step->__data)->dir;	\
ce426f
-  int emit_bom = ((struct utf8_data *) step->__data)->emit_bom;		\
ce426f
-									\
ce426f
-  if (emit_bom && !data->__internal_use					\
ce426f
-      && data->__invocation_counter == 0)				\
ce426f
-    {									\
ce426f
-      /* Emit the UTF-16 Byte Order Mark.  */				\
ce426f
-      if (__glibc_unlikely (outbuf + 2 > outend))			      \
ce426f
-	return __GCONV_FULL_OUTPUT;					\
ce426f
-									\
ce426f
-      put16u (outbuf, BOM_UTF16);					\
ce426f
-      outbuf += 2;							\
ce426f
-    }
ce426f
+
ce426f
+
ce426f
+/* UTF-16 big endian byte order mark.  */
ce426f
+#define BOM_UTF16	0xfeff
ce426f
 
ce426f
 /* Direction of the transformation.  */
ce426f
 enum direction
ce426f
@@ -151,16 +145,16 @@ gconv_end (struct __gconv_step *data)
ce426f
     register unsigned long long outlen __asm__("11") = outend - outptr;	\
ce426f
     uint64_t cc = 0;							\
ce426f
 									\
ce426f
-    __asm__ volatile (".machine push       \n\t"			\
ce426f
-		      ".machine \"z9-109\" \n\t"			\
ce426f
-		      "0: " INSTRUCTION "  \n\t"			\
ce426f
-		      ".machine pop        \n\t"			\
ce426f
-		      "   jo     0b        \n\t"			\
ce426f
-		      "   ipm    %2        \n"				\
ce426f
-		      : "+a" (pOutput), "+a" (pInput), "+d" (cc),	\
ce426f
-			"+d" (outlen), "+d" (inlen)			\
ce426f
-		      :							\
ce426f
-		      : "cc", "memory");				\
ce426f
+    __asm__ __volatile__ (".machine push       \n\t"			\
ce426f
+			  ".machine \"z9-109\" \n\t"			\
ce426f
+			  "0: " INSTRUCTION "  \n\t"			\
ce426f
+			  ".machine pop        \n\t"			\
ce426f
+			  "   jo     0b        \n\t"			\
ce426f
+			  "   ipm    %2        \n"			\
ce426f
+			  : "+a" (pOutput), "+a" (pInput), "+d" (cc),	\
ce426f
+			    "+d" (outlen), "+d" (inlen)			\
ce426f
+			  :						\
ce426f
+			  : "cc", "memory");				\
ce426f
 									\
ce426f
     inptr = pInput;							\
ce426f
     outptr = pOutput;							\
ce426f
@@ -169,50 +163,135 @@ gconv_end (struct __gconv_step *data)
ce426f
     if (cc == 1)							\
ce426f
       {									\
ce426f
 	result = __GCONV_FULL_OUTPUT;					\
ce426f
-	break;								\
ce426f
       }									\
ce426f
     else if (cc == 2)							\
ce426f
       {									\
ce426f
 	result = __GCONV_ILLEGAL_INPUT;					\
ce426f
-	break;								\
ce426f
       }									\
ce426f
   }
ce426f
 
ce426f
+#define PREPARE_LOOP							\
ce426f
+  enum direction dir = ((struct utf8_data *) step->__data)->dir;	\
ce426f
+  int emit_bom = ((struct utf8_data *) step->__data)->emit_bom;		\
ce426f
+									\
ce426f
+  if (emit_bom && !data->__internal_use					\
ce426f
+      && data->__invocation_counter == 0)				\
ce426f
+    {									\
ce426f
+      /* Emit the UTF-16 Byte Order Mark.  */				\
ce426f
+      if (__glibc_unlikely (outbuf + 2 > outend))			\
ce426f
+	return __GCONV_FULL_OUTPUT;					\
ce426f
+									\
ce426f
+      put16u (outbuf, BOM_UTF16);					\
ce426f
+      outbuf += 2;							\
ce426f
+    }
ce426f
+
ce426f
 /* Conversion function from UTF-8 to UTF-16.  */
ce426f
+#define BODY_FROM_HW(ASM)						\
ce426f
+  {									\
ce426f
+    ASM;								\
ce426f
+    if (__glibc_likely (inptr == inend)					\
ce426f
+	|| result == __GCONV_FULL_OUTPUT)				\
ce426f
+      break;								\
ce426f
+									\
ce426f
+    int i;								\
ce426f
+    for (i = 1; inptr + i < inend && i < 5; ++i)			\
ce426f
+      if ((inptr[i] & 0xc0) != 0x80)					\
ce426f
+	break;								\
ce426f
+									\
ce426f
+    if (__glibc_likely (inptr + i == inend				\
ce426f
+			&& result == __GCONV_EMPTY_INPUT))		\
ce426f
+      {									\
ce426f
+	result = __GCONV_INCOMPLETE_INPUT;				\
ce426f
+	break;								\
ce426f
+      }									\
ce426f
+    STANDARD_FROM_LOOP_ERR_HANDLER (i);					\
ce426f
+  }
ce426f
+
ce426f
+#define BODY_FROM_ETF3EH BODY_FROM_HW (HARDWARE_CONVERT ("cu12 %0, %1, 1"))
ce426f
+
ce426f
+#define HW_FROM_VX							\
ce426f
+  {									\
ce426f
+    register const unsigned char* pInput asm ("8") = inptr;		\
ce426f
+    register size_t inlen asm ("9") = inend - inptr;			\
ce426f
+    register unsigned char* pOutput asm ("10") = outptr;		\
ce426f
+    register size_t outlen asm("11") = outend - outptr;			\
ce426f
+    unsigned long tmp, tmp2, tmp3;					\
ce426f
+    asm volatile (".machine push\n\t"					\
ce426f
+		  ".machine \"z13\"\n\t"				\
ce426f
+		  ".machinemode \"zarch_nohighgprs\"\n\t"		\
ce426f
+		  "    vrepib %%v30,0x7f\n\t" /* For compare > 0x7f.  */ \
ce426f
+		  "    vrepib %%v31,0x20\n\t"				\
ce426f
+		  /* Loop which handles UTF-8 chars <=0x7f.  */		\
ce426f
+		  "0:  clgijl %[R_INLEN],16,20f\n\t"			\
ce426f
+		  "    clgijl %[R_OUTLEN],32,20f\n\t"			\
ce426f
+		  "1:  vl %%v16,0(%[R_IN])\n\t"				\
ce426f
+		  "    vstrcbs %%v17,%%v16,%%v30,%%v31\n\t"		\
ce426f
+		  "    jno 10f\n\t" /* Jump away if not all bytes are 1byte \
ce426f
+				       UTF8 chars.  */			\
ce426f
+		  /* Enlarge to UTF-16.  */				\
ce426f
+		  "    vuplhb %%v18,%%v16\n\t"				\
ce426f
+		  "    la %[R_IN],16(%[R_IN])\n\t"			\
ce426f
+		  "    vupllb %%v19,%%v16\n\t"				\
ce426f
+		  "    aghi %[R_INLEN],-16\n\t"				\
ce426f
+		  /* Store 32 bytes to buf_out.  */			\
ce426f
+		  "    vstm %%v18,%%v19,0(%[R_OUT])\n\t"		\
ce426f
+		  "    aghi %[R_OUTLEN],-32\n\t"			\
ce426f
+		  "    la %[R_OUT],32(%[R_OUT])\n\t"			\
ce426f
+		  "    clgijl %[R_INLEN],16,20f\n\t"			\
ce426f
+		  "    clgijl %[R_OUTLEN],32,20f\n\t"			\
ce426f
+		  "    j 1b\n\t"					\
ce426f
+		  "10:\n\t"						\
ce426f
+		  /* At least one byte is > 0x7f.			\
ce426f
+		     Store the preceding 1-byte chars.  */		\
ce426f
+		  "    vlgvb %[R_TMP],%%v17,7\n\t"			\
ce426f
+		  "    sllk %[R_TMP2],%[R_TMP],1\n\t" /* Compute highest \
ce426f
+							 index to store. */ \
ce426f
+		  "    llgfr %[R_TMP3],%[R_TMP2]\n\t"			\
ce426f
+		  "    ahi %[R_TMP2],-1\n\t"				\
ce426f
+		  "    jl 20f\n\t"					\
ce426f
+		  "    vuplhb %%v18,%%v16\n\t"				\
ce426f
+		  "    vstl %%v18,%[R_TMP2],0(%[R_OUT])\n\t"		\
ce426f
+		  "    ahi %[R_TMP2],-16\n\t"				\
ce426f
+		  "    jl 11f\n\t"					\
ce426f
+		  "    vupllb %%v19,%%v16\n\t"				\
ce426f
+		  "    vstl %%v19,%[R_TMP2],16(%[R_OUT])\n\t"		\
ce426f
+		  "11: \n\t" /* Update pointers.  */			\
ce426f
+		  "    la %[R_IN],0(%[R_TMP],%[R_IN])\n\t"		\
ce426f
+		  "    slgr %[R_INLEN],%[R_TMP]\n\t"			\
ce426f
+		  "    la %[R_OUT],0(%[R_TMP3],%[R_OUT])\n\t"		\
ce426f
+		  "    slgr %[R_OUTLEN],%[R_TMP3]\n\t"			\
ce426f
+		  /* Handle multibyte utf8-char with convert instruction. */ \
ce426f
+		  "20: cu12 %[R_OUT],%[R_IN],1\n\t"			\
ce426f
+		  "    jo 0b\n\t" /* Try vector implemenation again.  */ \
ce426f
+		  "    lochil %[R_RES],%[RES_OUT_FULL]\n\t" /* cc == 1.  */ \
ce426f
+		  "    lochih %[R_RES],%[RES_IN_ILL]\n\t" /* cc == 2.  */ \
ce426f
+		  ".machine pop"					\
ce426f
+		  : /* outputs */ [R_IN] "+a" (pInput)			\
ce426f
+		    , [R_INLEN] "+d" (inlen), [R_OUT] "+a" (pOutput)	\
ce426f
+		    , [R_OUTLEN] "+d" (outlen), [R_TMP] "=a" (tmp)	\
ce426f
+		    , [R_TMP2] "=d" (tmp2), [R_TMP3] "=a" (tmp3)	\
ce426f
+		    , [R_RES] "+d" (result)				\
ce426f
+		  : /* inputs */					\
ce426f
+		    [RES_OUT_FULL] "i" (__GCONV_FULL_OUTPUT)		\
ce426f
+		    , [RES_IN_ILL] "i" (__GCONV_ILLEGAL_INPUT)		\
ce426f
+		  : /* clobber list */ "memory", "cc"			\
ce426f
+		    ASM_CLOBBER_VR ("v16") ASM_CLOBBER_VR ("v17")	\
ce426f
+		    ASM_CLOBBER_VR ("v18") ASM_CLOBBER_VR ("v19")	\
ce426f
+		    ASM_CLOBBER_VR ("v30") ASM_CLOBBER_VR ("v31")	\
ce426f
+		  );							\
ce426f
+    inptr = pInput;							\
ce426f
+    outptr = pOutput;							\
ce426f
+  }
ce426f
+#define BODY_FROM_VX BODY_FROM_HW (HW_FROM_VX)
ce426f
+
ce426f
 
ce426f
-#define MIN_NEEDED_INPUT	MIN_NEEDED_FROM
ce426f
-#define MAX_NEEDED_INPUT	MAX_NEEDED_FROM
ce426f
-#define MIN_NEEDED_OUTPUT	MIN_NEEDED_TO
ce426f
-#define MAX_NEEDED_OUTPUT	MAX_NEEDED_TO
ce426f
-#define LOOPFCT			FROM_LOOP
ce426f
 /* The software implementation is based on the code in gconv_simple.c.  */
ce426f
-#define BODY								\
ce426f
+#define BODY_FROM_C							\
ce426f
   {									\
ce426f
-    if (GLRO (dl_hwcap) & HWCAP_S390_ETF3EH)				\
ce426f
-      {									\
ce426f
-	HARDWARE_CONVERT ("cu12 %0, %1, 1");				\
ce426f
-									\
ce426f
-	if (inptr != inend)						\
ce426f
-	  {								\
ce426f
-	    int i;							\
ce426f
-	    for (i = 1; inptr + i < inend; ++i)				\
ce426f
-	      if ((inptr[i] & 0xc0) != 0x80)				\
ce426f
-		break;							\
ce426f
-								\
ce426f
-	    if (__glibc_likely (inptr + i == inend))			      \
ce426f
-	      {								\
ce426f
-		result = __GCONV_INCOMPLETE_INPUT;			\
ce426f
-		break;							\
ce426f
-	      }								\
ce426f
-	    STANDARD_FROM_LOOP_ERR_HANDLER (i);				\
ce426f
-	  }								\
ce426f
-	continue;							\
ce426f
-    }									\
ce426f
-									\
ce426f
     /* Next input byte.  */						\
ce426f
     uint16_t ch = *inptr;						\
ce426f
 									\
ce426f
-    if (__glibc_likely (ch < 0x80))					      \
ce426f
+    if (__glibc_likely (ch < 0x80))					\
ce426f
       {									\
ce426f
 	/* One byte sequence.  */					\
ce426f
 	++inptr;							\
ce426f
@@ -230,13 +309,13 @@ gconv_end (struct __gconv_step *data)
ce426f
 	    cnt = 2;							\
ce426f
 	    ch &= 0x1f;							\
ce426f
 	  }								\
ce426f
-        else if (__glibc_likely ((ch & 0xf0) == 0xe0))			      \
ce426f
+	else if (__glibc_likely ((ch & 0xf0) == 0xe0))			\
ce426f
 	  {								\
ce426f
 	    /* We expect three bytes.  */				\
ce426f
 	    cnt = 3;							\
ce426f
 	    ch &= 0x0f;							\
ce426f
 	  }								\
ce426f
-	else if (__glibc_likely ((ch & 0xf8) == 0xf0))			      \
ce426f
+	else if (__glibc_likely ((ch & 0xf8) == 0xf0))			\
ce426f
 	  {								\
ce426f
 	    /* We expect four bytes.  */				\
ce426f
 	    cnt = 4;							\
ce426f
@@ -257,7 +336,7 @@ gconv_end (struct __gconv_step *data)
ce426f
 	    STANDARD_FROM_LOOP_ERR_HANDLER (i);				\
ce426f
 	  }								\
ce426f
 									\
ce426f
-	if (__glibc_unlikely (inptr + cnt > inend))			      \
ce426f
+	if (__glibc_unlikely (inptr + cnt > inend))			\
ce426f
 	  {								\
ce426f
 	    /* We don't have enough input.  But before we report	\
ce426f
 	       that check that all the bytes are correct.  */		\
ce426f
@@ -265,7 +344,7 @@ gconv_end (struct __gconv_step *data)
ce426f
 	      if ((inptr[i] & 0xc0) != 0x80)				\
ce426f
 		break;							\
ce426f
 									\
ce426f
-	    if (__glibc_likely (inptr + i == inend))			      \
ce426f
+	    if (__glibc_likely (inptr + i == inend))			\
ce426f
 	      {								\
ce426f
 		result = __GCONV_INCOMPLETE_INPUT;			\
ce426f
 		break;							\
ce426f
@@ -280,23 +359,31 @@ gconv_end (struct __gconv_step *data)
ce426f
 	       low) are needed.  */					\
ce426f
 	    uint16_t zabcd, high, low;					\
ce426f
 									\
ce426f
-	    if (__glibc_unlikely (outptr + 4 > outend))			      \
ce426f
+	    if (__glibc_unlikely (outptr + 4 > outend))			\
ce426f
 	      {								\
ce426f
 		/* Overflow in the output buffer.  */			\
ce426f
 		result = __GCONV_FULL_OUTPUT;				\
ce426f
 		break;							\
ce426f
 	      }								\
ce426f
 									\
ce426f
+	    /* Check if tail-bytes >= 0x80, < 0xc0.  */			\
ce426f
+	    for (i = 1; i < cnt; ++i)					\
ce426f
+	      {								\
ce426f
+		if ((inptr[i] & 0xc0) != 0x80)				\
ce426f
+		  /* This is an illegal encoding.  */			\
ce426f
+		  goto errout;						\
ce426f
+	      }								\
ce426f
+									\
ce426f
 	    /* See Principles of Operations cu12.  */			\
ce426f
 	    zabcd = (((inptr[0] & 0x7) << 2) |				\
ce426f
-                     ((inptr[1] & 0x30) >> 4)) - 1;			\
ce426f
+		     ((inptr[1] & 0x30) >> 4)) - 1;			\
ce426f
 									\
ce426f
 	    /* z-bit must be zero after subtracting 1.  */		\
ce426f
 	    if (zabcd & 0x10)						\
ce426f
 	      STANDARD_FROM_LOOP_ERR_HANDLER (4)			\
ce426f
 									\
ce426f
 	    high = (uint16_t)(0xd8 << 8);       /* high surrogate id */ \
ce426f
-	    high |= zabcd << 6;	                        /* abcd bits */	\
ce426f
+	    high |= zabcd << 6;                         /* abcd bits */	\
ce426f
 	    high |= (inptr[1] & 0xf) << 2;              /* efgh bits */	\
ce426f
 	    high |= (inptr[2] & 0x30) >> 4;               /* ij bits */	\
ce426f
 									\
ce426f
@@ -326,8 +413,19 @@ gconv_end (struct __gconv_step *data)
ce426f
 		ch <<= 6;						\
ce426f
 		ch |= byte & 0x3f;					\
ce426f
 	      }								\
ce426f
-	    inptr += cnt;						\
ce426f
 									\
ce426f
+	    /* If i < cnt, some trail byte was not >= 0x80, < 0xc0.	\
ce426f
+	       If cnt > 2 and ch < 2^(5*cnt-4), the wide character ch could \
ce426f
+	       have been represented with fewer than cnt bytes.  */	\
ce426f
+	    if (i < cnt || (cnt > 2 && (ch >> (5 * cnt - 4)) == 0)	\
ce426f
+		/* Do not accept UTF-16 surrogates.  */			\
ce426f
+		|| (ch >= 0xd800 && ch <= 0xdfff))			\
ce426f
+	      {								\
ce426f
+		/* This is an illegal encoding.  */			\
ce426f
+		goto errout;						\
ce426f
+	      }								\
ce426f
+									\
ce426f
+	    inptr += cnt;						\
ce426f
 	  }								\
ce426f
       }									\
ce426f
     /* Now adjust the pointers and store the result.  */		\
ce426f
@@ -335,43 +433,70 @@ gconv_end (struct __gconv_step *data)
ce426f
     outptr += sizeof (uint16_t);					\
ce426f
   }
ce426f
 
ce426f
+/* Generate loop-function with software implementation.  */
ce426f
+#define MIN_NEEDED_INPUT	MIN_NEEDED_FROM
ce426f
+#define MAX_NEEDED_INPUT	MAX_NEEDED_FROM
ce426f
+#define MIN_NEEDED_OUTPUT	MIN_NEEDED_TO
ce426f
+#define MAX_NEEDED_OUTPUT	MAX_NEEDED_TO
ce426f
+#define LOOPFCT			__from_utf8_loop_c
ce426f
+#define LOOP_NEED_FLAGS
ce426f
+#define BODY			BODY_FROM_C
ce426f
+#include <iconv/loop.c>
ce426f
+
ce426f
+/* Generate loop-function with hardware utf-convert instruction.  */
ce426f
+#define MIN_NEEDED_INPUT	MIN_NEEDED_FROM
ce426f
+#define MAX_NEEDED_INPUT	MAX_NEEDED_FROM
ce426f
+#define MIN_NEEDED_OUTPUT	MIN_NEEDED_TO
ce426f
+#define MAX_NEEDED_OUTPUT	MAX_NEEDED_TO
ce426f
+#define LOOPFCT			__from_utf8_loop_etf3eh
ce426f
 #define LOOP_NEED_FLAGS
ce426f
+#define BODY			BODY_FROM_ETF3EH
ce426f
 #include <iconv/loop.c>
ce426f
 
ce426f
+#if defined HAVE_S390_VX_ASM_SUPPORT
ce426f
+/* Generate loop-function with hardware vector and utf-convert instructions.  */
ce426f
+# define MIN_NEEDED_INPUT	MIN_NEEDED_FROM
ce426f
+# define MAX_NEEDED_INPUT	MAX_NEEDED_FROM
ce426f
+# define MIN_NEEDED_OUTPUT	MIN_NEEDED_TO
ce426f
+# define MAX_NEEDED_OUTPUT	MAX_NEEDED_TO
ce426f
+# define LOOPFCT		__from_utf8_loop_vx
ce426f
+# define LOOP_NEED_FLAGS
ce426f
+# define BODY			BODY_FROM_VX
ce426f
+# include <iconv/loop.c>
ce426f
+#endif
ce426f
+
ce426f
+
ce426f
+/* Generate ifunc'ed loop function.  */
ce426f
+__typeof(__from_utf8_loop_c)
ce426f
+__attribute__ ((ifunc ("__from_utf8_loop_resolver")))
ce426f
+__from_utf8_loop;
ce426f
+
ce426f
+static void *
ce426f
+__from_utf8_loop_resolver (unsigned long int dl_hwcap)
ce426f
+{
ce426f
+#if defined HAVE_S390_VX_ASM_SUPPORT
ce426f
+  if (dl_hwcap & HWCAP_S390_VX)
ce426f
+    return __from_utf8_loop_vx;
ce426f
+  else
ce426f
+#endif
ce426f
+  if (dl_hwcap & HWCAP_S390_ETF3EH)
ce426f
+    return __from_utf8_loop_etf3eh;
ce426f
+  else
ce426f
+    return __from_utf8_loop_c;
ce426f
+}
ce426f
+
ce426f
+strong_alias (__from_utf8_loop_c_single, __from_utf8_loop_single)
ce426f
+
ce426f
 /* Conversion from UTF-16 to UTF-8.  */
ce426f
 
ce426f
-#define MIN_NEEDED_INPUT	MIN_NEEDED_TO
ce426f
-#define MAX_NEEDED_INPUT	MAX_NEEDED_TO
ce426f
-#define MIN_NEEDED_OUTPUT	MIN_NEEDED_FROM
ce426f
-#define MAX_NEEDED_OUTPUT	MAX_NEEDED_FROM
ce426f
-#define LOOPFCT			TO_LOOP
ce426f
 /* The software routine is based on the functionality of the S/390
ce426f
    hardware instruction (cu21) as described in the Principles of
ce426f
    Operation.  */
ce426f
-#define BODY								\
ce426f
+#define BODY_TO_C							\
ce426f
   {									\
ce426f
-    /* The hardware instruction currently fails to report an error for	\
ce426f
-       isolated low surrogates so we have to disable the instruction	\
ce426f
-       until this gets resolved.  */					\
ce426f
-    if (0) /* (GLRO (dl_hwcap) & HWCAP_S390_ETF3EH) */			\
ce426f
-      {									\
ce426f
-	HARDWARE_CONVERT ("cu21 %0, %1, 1");				\
ce426f
-	if (inptr != inend)						\
ce426f
-	  {								\
ce426f
-	    /* Check if the third byte is				\
ce426f
-	       a valid start of a UTF-16 surrogate.  */			\
ce426f
-	    if (inend - inptr == 3 && (inptr[3] & 0xfc) != 0xdc)	\
ce426f
-	      STANDARD_TO_LOOP_ERR_HANDLER (3);				\
ce426f
-									\
ce426f
-	    result = __GCONV_INCOMPLETE_INPUT;				\
ce426f
-	    break;							\
ce426f
-	  }								\
ce426f
-	continue;							\
ce426f
-      }									\
ce426f
-									\
ce426f
     uint16_t c = get16 (inptr);						\
ce426f
 									\
ce426f
-    if (__glibc_likely (c <= 0x007f))					      \
ce426f
+    if (__glibc_likely (c <= 0x007f))					\
ce426f
       {									\
ce426f
 	/* Single byte UTF-8 char.  */					\
ce426f
 	*outptr = c & 0xff;						\
ce426f
@@ -379,20 +504,20 @@ gconv_end (struct __gconv_step *data)
ce426f
       }									\
ce426f
     else if (c >= 0x0080 && c <= 0x07ff)				\
ce426f
       {									\
ce426f
-        /* Two byte UTF-8 char.  */					\
ce426f
+	/* Two byte UTF-8 char.  */					\
ce426f
 									\
ce426f
-	if (__glibc_unlikely (outptr + 2 > outend))			      \
ce426f
+	if (__glibc_unlikely (outptr + 2 > outend))			\
ce426f
 	  {								\
ce426f
 	    /* Overflow in the output buffer.  */			\
ce426f
 	    result = __GCONV_FULL_OUTPUT;				\
ce426f
 	    break;							\
ce426f
 	  }								\
ce426f
 									\
ce426f
-        outptr[0] = 0xc0;						\
ce426f
-        outptr[0] |= c >> 6;						\
ce426f
+	outptr[0] = 0xc0;						\
ce426f
+	outptr[0] |= c >> 6;						\
ce426f
 									\
ce426f
-        outptr[1] = 0x80;						\
ce426f
-        outptr[1] |= c & 0x3f;						\
ce426f
+	outptr[1] = 0x80;						\
ce426f
+	outptr[1] |= c & 0x3f;						\
ce426f
 									\
ce426f
 	outptr += 2;							\
ce426f
       }									\
ce426f
@@ -400,7 +525,7 @@ gconv_end (struct __gconv_step *data)
ce426f
       {									\
ce426f
 	/* Three byte UTF-8 char.  */					\
ce426f
 									\
ce426f
-	if (__glibc_unlikely (outptr + 3 > outend))			      \
ce426f
+	if (__glibc_unlikely (outptr + 3 > outend))			\
ce426f
 	  {								\
ce426f
 	    /* Overflow in the output buffer.  */			\
ce426f
 	    result = __GCONV_FULL_OUTPUT;				\
ce426f
@@ -419,22 +544,22 @@ gconv_end (struct __gconv_step *data)
ce426f
       }									\
ce426f
     else if (c >= 0xd800 && c <= 0xdbff)				\
ce426f
       {									\
ce426f
-        /* Four byte UTF-8 char.  */					\
ce426f
+	/* Four byte UTF-8 char.  */					\
ce426f
 	uint16_t low, uvwxy;						\
ce426f
 									\
ce426f
-	if (__glibc_unlikely (outptr + 4 > outend))			      \
ce426f
+	if (__glibc_unlikely (outptr + 4 > outend))			\
ce426f
 	  {								\
ce426f
 	    /* Overflow in the output buffer.  */			\
ce426f
 	    result = __GCONV_FULL_OUTPUT;				\
ce426f
 	    break;							\
ce426f
 	  }								\
ce426f
-	inptr += 2;							\
ce426f
-	if (__glibc_unlikely (inptr + 2 > inend))			      \
ce426f
+	if (__glibc_unlikely (inptr + 4 > inend))			\
ce426f
 	  {								\
ce426f
 	    result = __GCONV_INCOMPLETE_INPUT;				\
ce426f
 	    break;							\
ce426f
 	  }								\
ce426f
 									\
ce426f
+	inptr += 2;							\
ce426f
 	low = get16 (inptr);						\
ce426f
 									\
ce426f
 	if ((low & 0xfc00) != 0xdc00)					\
ce426f
@@ -461,11 +586,221 @@ gconv_end (struct __gconv_step *data)
ce426f
       }									\
ce426f
     else								\
ce426f
       {									\
ce426f
-        STANDARD_TO_LOOP_ERR_HANDLER (2);				\
ce426f
+	STANDARD_TO_LOOP_ERR_HANDLER (2);				\
ce426f
       }									\
ce426f
     inptr += 2;								\
ce426f
   }
ce426f
-#define LOOP_NEED_FLAGS
ce426f
-#include <iconv/loop.c>
ce426f
+
ce426f
+#define BODY_TO_VX							\
ce426f
+  {									\
ce426f
+    size_t inlen  = inend - inptr;					\
ce426f
+    size_t outlen  = outend - outptr;					\
ce426f
+    unsigned long tmp, tmp2, tmp3;					\
ce426f
+    asm volatile (".machine push\n\t"					\
ce426f
+		  ".machine \"z13\"\n\t"				\
ce426f
+		  ".machinemode \"zarch_nohighgprs\"\n\t"		\
ce426f
+		  /* Setup to check for values <= 0x7f.  */		\
ce426f
+		  "    larl %[R_TMP],9f\n\t"				\
ce426f
+		  "    vlm %%v30,%%v31,0(%[R_TMP])\n\t"			\
ce426f
+		  /* Loop which handles UTF-16 chars <=0x7f.  */	\
ce426f
+		  "0:  clgijl %[R_INLEN],32,2f\n\t"			\
ce426f
+		  "    clgijl %[R_OUTLEN],16,2f\n\t"			\
ce426f
+		  "1:  vlm %%v16,%%v17,0(%[R_IN])\n\t"			\
ce426f
+		  "    lghi %[R_TMP2],0\n\t"				\
ce426f
+		  /* Check for > 1byte UTF-8 chars.  */			\
ce426f
+		  "    vstrchs %%v19,%%v16,%%v30,%%v31\n\t"		\
ce426f
+		  "    jno 10f\n\t" /* Jump away if not all bytes are 1byte \
ce426f
+				       UTF8 chars.  */			\
ce426f
+		  "    vstrchs %%v19,%%v17,%%v30,%%v31\n\t"		\
ce426f
+		  "    jno 11f\n\t" /* Jump away if not all bytes are 1byte \
ce426f
+				       UTF8 chars.  */			\
ce426f
+		  /* Shorten to UTF-8.  */				\
ce426f
+		  "    vpkh %%v18,%%v16,%%v17\n\t"			\
ce426f
+		  "    la %[R_IN],32(%[R_IN])\n\t"			\
ce426f
+		  "    aghi %[R_INLEN],-32\n\t"				\
ce426f
+		  /* Store 16 bytes to buf_out.  */			\
ce426f
+		  "    vst %%v18,0(%[R_OUT])\n\t"			\
ce426f
+		  "    aghi %[R_OUTLEN],-16\n\t"			\
ce426f
+		  "    la %[R_OUT],16(%[R_OUT])\n\t"			\
ce426f
+		  "    clgijl %[R_INLEN],32,2f\n\t"			\
ce426f
+		  "    clgijl %[R_OUTLEN],16,2f\n\t"			\
ce426f
+		  "    j 1b\n\t"					\
ce426f
+		  /* Setup to check for ch > 0x7f. (v30, v31)  */	\
ce426f
+		  "9:  .short 0x7f,0x7f,0x0,0x0,0x0,0x0,0x0,0x0\n\t"	\
ce426f
+		  "    .short 0x2000,0x2000,0x0,0x0,0x0,0x0,0x0,0x0\n\t" \
ce426f
+		  /* At least one byte is > 0x7f.			\
ce426f
+		     Store the preceding 1-byte chars.  */		\
ce426f
+		  "11: lghi %[R_TMP2],16\n\t" /* match was found in v17.  */ \
ce426f
+		  "10:\n\t"						\
ce426f
+		  "    vlgvb %[R_TMP],%%v19,7\n\t"			\
ce426f
+		  /* Shorten to UTF-8.  */				\
ce426f
+		  "    vpkh %%v18,%%v16,%%v17\n\t"			\
ce426f
+		  "    ar %[R_TMP],%[R_TMP2]\n\t" /* Number of in bytes.  */ \
ce426f
+		  "    srlg %[R_TMP3],%[R_TMP],1\n\t" /* Number of out bytes.  */ \
ce426f
+		  "    ahik %[R_TMP2],%[R_TMP3],-1\n\t" /* Highest index to store.  */ \
ce426f
+		  "    jl 13f\n\t"					\
ce426f
+		  "    vstl %%v18,%[R_TMP2],0(%[R_OUT])\n\t"		\
ce426f
+		  /* Update pointers.  */				\
ce426f
+		  "    la %[R_IN],0(%[R_TMP],%[R_IN])\n\t"		\
ce426f
+		  "    slgr %[R_INLEN],%[R_TMP]\n\t"			\
ce426f
+		  "    la %[R_OUT],0(%[R_TMP3],%[R_OUT])\n\t"		\
ce426f
+		  "    slgr %[R_OUTLEN],%[R_TMP3]\n\t"			\
ce426f
+		  "13: \n\t"						\
ce426f
+		  /* Calculate remaining uint16_t values in loaded vrs.  */ \
ce426f
+		  "    lghi %[R_TMP2],16\n\t"				\
ce426f
+		  "    slgr %[R_TMP2],%[R_TMP3]\n\t"			\
ce426f
+		  "    llh %[R_TMP],0(%[R_IN])\n\t"			\
ce426f
+		  "    aghi %[R_INLEN],-2\n\t"				\
ce426f
+		  "    j 22f\n\t"					\
ce426f
+		  /* Handle remaining bytes.  */			\
ce426f
+		  "2:  \n\t"						\
ce426f
+		  /* Zero, one or more bytes available?  */		\
ce426f
+		  "    clgfi %[R_INLEN],1\n\t"				\
ce426f
+		  "    locghie %[R_RES],%[RES_IN_FULL]\n\t" /* Only one byte.  */ \
ce426f
+		  "    jle 99f\n\t" /* End if less than two bytes.  */	\
ce426f
+		  /* Calculate remaining uint16_t values in inptr.  */	\
ce426f
+		  "    srlg %[R_TMP2],%[R_INLEN],1\n\t"			\
ce426f
+		  /* Handle multibyte utf8-char. */			\
ce426f
+		  "20: llh %[R_TMP],0(%[R_IN])\n\t"			\
ce426f
+		  "    aghi %[R_INLEN],-2\n\t"				\
ce426f
+		  /* Test if ch is 1-byte UTF-8 char.  */		\
ce426f
+		  "21: clijh %[R_TMP],0x7f,22f\n\t"			\
ce426f
+		  /* Handle 1-byte UTF-8 char.  */			\
ce426f
+		  "31: slgfi %[R_OUTLEN],1\n\t"				\
ce426f
+		  "    jl 90f \n\t"					\
ce426f
+		  "    stc %[R_TMP],0(%[R_OUT])\n\t"			\
ce426f
+		  "    la %[R_IN],2(%[R_IN])\n\t"			\
ce426f
+		  "    la %[R_OUT],1(%[R_OUT])\n\t"			\
ce426f
+		  "    brctg %[R_TMP2],20b\n\t"				\
ce426f
+		  "    j 0b\n\t" /* Switch to vx-loop.  */		\
ce426f
+		  /* Test if ch is 2-byte UTF-8 char.  */		\
ce426f
+		  "22: clfi %[R_TMP],0x7ff\n\t"				\
ce426f
+		  "    jh 23f\n\t"					\
ce426f
+		  /* Handle 2-byte UTF-8 char.  */			\
ce426f
+		  "32: slgfi %[R_OUTLEN],2\n\t"				\
ce426f
+		  "    jl 90f \n\t"					\
ce426f
+		  "    llill %[R_TMP3],0xc080\n\t"			\
ce426f
+		  "    la %[R_IN],2(%[R_IN])\n\t"			\
ce426f
+		  "    risbgn %[R_TMP3],%[R_TMP],51,55,2\n\t" /* 1. byte.   */ \
ce426f
+		  "    risbgn %[R_TMP3],%[R_TMP],58,63,0\n\t" /* 2. byte.   */ \
ce426f
+		  "    sth %[R_TMP3],0(%[R_OUT])\n\t"			\
ce426f
+		  "    la %[R_OUT],2(%[R_OUT])\n\t"			\
ce426f
+		  "    brctg %[R_TMP2],20b\n\t"				\
ce426f
+		  "    j 0b\n\t" /* Switch to vx-loop.  */		\
ce426f
+		  /* Test if ch is 3-byte UTF-8 char.  */		\
ce426f
+		  "23: clfi %[R_TMP],0xd7ff\n\t"			\
ce426f
+		  "    jh 24f\n\t"					\
ce426f
+		  /* Handle 3-byte UTF-8 char.  */			\
ce426f
+		  "33: slgfi %[R_OUTLEN],3\n\t"				\
ce426f
+		  "    jl 90f \n\t"					\
ce426f
+		  "    llilf %[R_TMP3],0xe08080\n\t"			\
ce426f
+		  "    la %[R_IN],2(%[R_IN])\n\t"			\
ce426f
+		  "    risbgn %[R_TMP3],%[R_TMP],44,47,4\n\t" /* 1. byte.  */ \
ce426f
+		  "    risbgn %[R_TMP3],%[R_TMP],50,55,2\n\t" /* 2. byte.  */ \
ce426f
+		  "    risbgn %[R_TMP3],%[R_TMP],58,63,0\n\t" /* 3. byte.  */ \
ce426f
+		  "    stcm %[R_TMP3],7,0(%[R_OUT])\n\t"		\
ce426f
+		  "    la %[R_OUT],3(%[R_OUT])\n\t"			\
ce426f
+		  "    brctg %[R_TMP2],20b\n\t"				\
ce426f
+		  "    j 0b\n\t" /* Switch to vx-loop.  */		\
ce426f
+		  /* Test if ch is 4-byte UTF-8 char.  */		\
ce426f
+		  "24: clfi %[R_TMP],0xdfff\n\t"			\
ce426f
+		  "    jh 33b\n\t" /* Handle this 3-byte UTF-8 char.  */ \
ce426f
+		  "    clfi %[R_TMP],0xdbff\n\t"			\
ce426f
+		  "    locghih %[R_RES],%[RES_IN_ILL]\n\t"		\
ce426f
+		  "    jh 99f\n\t" /* Jump away if this is a low surrogate \
ce426f
+				      without a preceding high surrogate.  */ \
ce426f
+		  /* Handle 4-byte UTF-8 char.  */			\
ce426f
+		  "34: slgfi %[R_OUTLEN],4\n\t"				\
ce426f
+		  "    jl 90f \n\t"					\
ce426f
+		  "    slgfi %[R_INLEN],2\n\t"				\
ce426f
+		  "    locghil %[R_RES],%[RES_IN_FULL]\n\t"		\
ce426f
+		  "    jl 99f\n\t" /* Jump away if low surrogate is missing.  */ \
ce426f
+		  "    llilf %[R_TMP3],0xf0808080\n\t"			\
ce426f
+		  "    aghi %[R_TMP],0x40\n\t"				\
ce426f
+		  "    risbgn %[R_TMP3],%[R_TMP],37,39,16\n\t" /* 1. byte: uvw  */ \
ce426f
+		  "    risbgn %[R_TMP3],%[R_TMP],42,43,14\n\t" /* 2. byte: xy  */ \
ce426f
+		  "    risbgn %[R_TMP3],%[R_TMP],44,47,14\n\t" /* 2. byte: efgh  */ \
ce426f
+		  "    risbgn %[R_TMP3],%[R_TMP],50,51,12\n\t" /* 3. byte: ij */ \
ce426f
+		  "    llh %[R_TMP],2(%[R_IN])\n\t" /* Load low surrogate.  */ \
ce426f
+		  "    risbgn %[R_TMP3],%[R_TMP],52,55,2\n\t" /* 3. byte: klmn  */ \
ce426f
+		  "    risbgn %[R_TMP3],%[R_TMP],58,63,0\n\t" /* 4. byte: opqrst  */ \
ce426f
+		  "    nilf %[R_TMP],0xfc00\n\t"			\
ce426f
+		  "    clfi %[R_TMP],0xdc00\n\t" /* Check if it starts with 0xdc00.  */ \
ce426f
+		  "    locghine %[R_RES],%[RES_IN_ILL]\n\t"		\
ce426f
+		  "    jne 99f\n\t" /* Jump away if low surrogate is invalid.  */ \
ce426f
+		  "    st %[R_TMP3],0(%[R_OUT])\n\t"			\
ce426f
+		  "    la %[R_IN],4(%[R_IN])\n\t"			\
ce426f
+		  "    la %[R_OUT],4(%[R_OUT])\n\t"			\
ce426f
+		  "    aghi %[R_TMP2],-2\n\t"				\
ce426f
+		  "    jh 20b\n\t"					\
ce426f
+		  "    j 0b\n\t" /* Switch to vx-loop.  */		\
ce426f
+		  /* Exit with __GCONV_FULL_OUTPUT.  */			\
ce426f
+		  "90: lghi %[R_RES],%[RES_OUT_FULL]\n\t"		\
ce426f
+		  "99: \n\t"						\
ce426f
+		  ".machine pop"					\
ce426f
+		  : /* outputs */ [R_IN] "+a" (inptr)			\
ce426f
+		    , [R_INLEN] "+d" (inlen), [R_OUT] "+a" (outptr)	\
ce426f
+		    , [R_OUTLEN] "+d" (outlen), [R_TMP] "=a" (tmp)	\
ce426f
+		    , [R_TMP2] "=d" (tmp2), [R_TMP3] "=a" (tmp3)	\
ce426f
+		    , [R_RES] "+d" (result)				\
ce426f
+		  : /* inputs */					\
ce426f
+		    [RES_OUT_FULL] "i" (__GCONV_FULL_OUTPUT)		\
ce426f
+		    , [RES_IN_ILL] "i" (__GCONV_ILLEGAL_INPUT)		\
ce426f
+		    , [RES_IN_FULL] "i" (__GCONV_INCOMPLETE_INPUT)	\
ce426f
+		  : /* clobber list */ "memory", "cc"			\
ce426f
+		    ASM_CLOBBER_VR ("v16") ASM_CLOBBER_VR ("v17")	\
ce426f
+		    ASM_CLOBBER_VR ("v18") ASM_CLOBBER_VR ("v19")	\
ce426f
+		    ASM_CLOBBER_VR ("v30") ASM_CLOBBER_VR ("v31")	\
ce426f
+		  );							\
ce426f
+    if (__glibc_likely (inptr == inend)					\
ce426f
+	|| result != __GCONV_ILLEGAL_INPUT)				\
ce426f
+      break;								\
ce426f
+									\
ce426f
+    STANDARD_TO_LOOP_ERR_HANDLER (2);					\
ce426f
+  }
ce426f
+
ce426f
+/* Generate loop-function with software implementation.  */
ce426f
+#define MIN_NEEDED_INPUT	MIN_NEEDED_TO
ce426f
+#define MAX_NEEDED_INPUT	MAX_NEEDED_TO
ce426f
+#define MIN_NEEDED_OUTPUT	MIN_NEEDED_FROM
ce426f
+#define MAX_NEEDED_OUTPUT	MAX_NEEDED_FROM
ce426f
+#if defined HAVE_S390_VX_ASM_SUPPORT
ce426f
+# define LOOPFCT		__to_utf8_loop_c
ce426f
+# define BODY                   BODY_TO_C
ce426f
+# define LOOP_NEED_FLAGS
ce426f
+# include <iconv/loop.c>
ce426f
+
ce426f
+/* Generate loop-function with software implementation.  */
ce426f
+# define MIN_NEEDED_INPUT	MIN_NEEDED_TO
ce426f
+# define MAX_NEEDED_INPUT	MAX_NEEDED_TO
ce426f
+# define MIN_NEEDED_OUTPUT	MIN_NEEDED_FROM
ce426f
+# define MAX_NEEDED_OUTPUT	MAX_NEEDED_FROM
ce426f
+# define LOOPFCT		__to_utf8_loop_vx
ce426f
+# define BODY                   BODY_TO_VX
ce426f
+# define LOOP_NEED_FLAGS
ce426f
+# include <iconv/loop.c>
ce426f
+
ce426f
+/* Generate ifunc'ed loop function.  */
ce426f
+__typeof(__to_utf8_loop_c)
ce426f
+__attribute__ ((ifunc ("__to_utf8_loop_resolver")))
ce426f
+__to_utf8_loop;
ce426f
+
ce426f
+static void *
ce426f
+__to_utf8_loop_resolver (unsigned long int dl_hwcap)
ce426f
+{
ce426f
+  if (dl_hwcap & HWCAP_S390_VX)
ce426f
+    return __to_utf8_loop_vx;
ce426f
+  else
ce426f
+    return __to_utf8_loop_c;
ce426f
+}
ce426f
+
ce426f
+strong_alias (__to_utf8_loop_c_single, __to_utf8_loop_single)
ce426f
+
ce426f
+#else
ce426f
+# define LOOPFCT		TO_LOOP
ce426f
+# define BODY                   BODY_TO_C
ce426f
+# define LOOP_NEED_FLAGS
ce426f
+# include <iconv/loop.c>
ce426f
+#endif /* !HAVE_S390_VX_ASM_SUPPORT  */
ce426f
 
ce426f
 #include <iconv/skeleton.c>
ce426f
-- 
ce426f
1.8.3.1
ce426f