ce426f
From 835c3bf23a119a7fcb8c70734d4fdf49461d8195 Mon Sep 17 00:00:00 2001
ce426f
From: Stefan Liebler <stli@linux.vnet.ibm.com>
ce426f
Date: Mon, 7 Nov 2016 16:14:07 +0100
ce426f
Subject: [PATCH 09/17] S390: Optimize utf8-utf32 module.
ce426f
ce426f
Upstream commit 421c5278d83e72740150259960a431706ac343f9
ce426f
ce426f
This patch reworks the s390 specific module to convert between utf8 and utf32.
ce426f
Now ifunc is used to choose either the c or etf3eh (with convert utf
ce426f
instruction) variants at runtime.
ce426f
Furthermore a new vector variant for z13 is introduced which will be build
ce426f
and chosen if vector support is available at build / runtime.
ce426f
The vector variants optimize input of 1byte utf8 characters. The convert utf
ce426f
instruction is used if a multibyte utf8 character is found.
ce426f
ce426f
This patch also fixes some whitespace errors. The c variants are rejecting
ce426f
UTF-16 surrogates and values above 0x10ffff now.
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-utf32-z9.c: Use ifunc to select c, etf3eh
ce426f
	or new vector loop-variant.
ce426f
---
ce426f
 sysdeps/s390/s390-64/utf8-utf32-z9.c | 664 +++++++++++++++++++++++++----------
ce426f
 1 file changed, 480 insertions(+), 184 deletions(-)
ce426f
ce426f
diff --git a/sysdeps/s390/s390-64/utf8-utf32-z9.c b/sysdeps/s390/s390-64/utf8-utf32-z9.c
ce426f
index 721279e..1ce5ac5 100644
ce426f
--- a/sysdeps/s390/s390-64/utf8-utf32-z9.c
ce426f
+++ b/sysdeps/s390/s390-64/utf8-utf32-z9.c
ce426f
@@ -30,35 +30,25 @@
ce426f
 #include <dl-procinfo.h>
ce426f
 #include <gconv.h>
ce426f
 
ce426f
-/* UTF-32 big endian byte order mark.  */
ce426f
-#define BOM	                0x0000feffu
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
-/* These definitions apply to the UTF-8 to UTF-32 direction.  The
ce426f
-   software implementation for UTF-8 still supports multibyte
ce426f
-   characters up to 6 bytes whereas the hardware variant does not.  */
ce426f
 #define MIN_NEEDED_FROM		1
ce426f
 #define MAX_NEEDED_FROM		6
ce426f
 #define MIN_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 Byte Order Mark.  */					\
ce426f
-      if (__glibc_unlikely (outbuf + 4 > outend))			      \
ce426f
-	return __GCONV_FULL_OUTPUT;					\
ce426f
-									\
ce426f
-      put32u (outbuf, BOM);						\
ce426f
-      outbuf += 4;							\
ce426f
-    }
ce426f
+
ce426f
+/* UTF-32 big endian byte order mark.  */
ce426f
+#define BOM			0x0000feffu
ce426f
 
ce426f
 /* Direction of the transformation.  */
ce426f
 enum direction
ce426f
@@ -155,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
@@ -173,49 +163,150 @@ 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 Byte Order Mark.  */					\
ce426f
+      if (__glibc_unlikely (outbuf + 4 > outend))			\
ce426f
+	return __GCONV_FULL_OUTPUT;					\
ce426f
+									\
ce426f
+      put32u (outbuf, BOM);						\
ce426f
+      outbuf += 4;							\
ce426f
+    }
ce426f
+
ce426f
 /* Conversion function from UTF-8 to UTF-32 internal/BE.  */
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 LOOPFCT			FROM_LOOP
ce426f
-/* The software routine is copied from gconv_simple.c.  */
ce426f
-#define BODY								\
ce426f
+#define STORE_REST_COMMON						      \
ce426f
+  {									      \
ce426f
+    /* We store the remaining bytes while converting them into the UCS4	      \
ce426f
+       format.  We can assume that the first byte in the buffer is	      \
ce426f
+       correct and that it requires a larger number of bytes than there	      \
ce426f
+       are in the input buffer.  */					      \
ce426f
+    wint_t ch = **inptrp;						      \
ce426f
+    size_t cnt, r;							      \
ce426f
+									      \
ce426f
+    state->__count = inend - *inptrp;					      \
ce426f
+									      \
ce426f
+    assert (ch != 0xc0 && ch != 0xc1);					      \
ce426f
+    if (ch >= 0xc2 && ch < 0xe0)					      \
ce426f
+      {									      \
ce426f
+	/* We expect two bytes.  The first byte cannot be 0xc0 or	      \
ce426f
+	   0xc1, otherwise the wide character could have been		      \
ce426f
+	   represented using a single byte.  */				      \
ce426f
+	cnt = 2;							      \
ce426f
+	ch &= 0x1f;							      \
ce426f
+      }									      \
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
+      {									      \
ce426f
+	/* We expect four bytes.  */					      \
ce426f
+	cnt = 4;							      \
ce426f
+	ch &= 0x07;							      \
ce426f
+      }									      \
ce426f
+    else if (__glibc_likely ((ch & 0xfc) == 0xf8))			      \
ce426f
+      {									      \
ce426f
+	/* We expect five bytes.  */					      \
ce426f
+	cnt = 5;							      \
ce426f
+	ch &= 0x03;							      \
ce426f
+      }									      \
ce426f
+    else								      \
ce426f
+      {									      \
ce426f
+	/* We expect six bytes.  */					      \
ce426f
+	cnt = 6;							      \
ce426f
+	ch &= 0x01;							      \
ce426f
+      }									      \
ce426f
+									      \
ce426f
+    /* The first byte is already consumed.  */				      \
ce426f
+    r = cnt - 1;							      \
ce426f
+    while (++(*inptrp) < inend)						      \
ce426f
+      {									      \
ce426f
+	ch <<= 6;							      \
ce426f
+	ch |= **inptrp & 0x3f;						      \
ce426f
+	--r;								      \
ce426f
+      }									      \
ce426f
+									      \
ce426f
+    /* Shift for the so far missing bytes.  */				      \
ce426f
+    ch <<= r * 6;							      \
ce426f
+									      \
ce426f
+    /* Store the number of bytes expected for the entire sequence.  */	      \
ce426f
+    state->__count |= cnt << 8;						      \
ce426f
+									      \
ce426f
+    /* Store the value.  */						      \
ce426f
+    state->__value.__wch = ch;						      \
ce426f
+  }
ce426f
+
ce426f
+#define UNPACK_BYTES_COMMON \
ce426f
+  {									      \
ce426f
+    static const unsigned char inmask[5] = { 0xc0, 0xe0, 0xf0, 0xf8, 0xfc };  \
ce426f
+    wint_t wch = state->__value.__wch;					      \
ce426f
+    size_t ntotal = state->__count >> 8;				      \
ce426f
+									      \
ce426f
+    inlen = state->__count & 255;					      \
ce426f
+									      \
ce426f
+    bytebuf[0] = inmask[ntotal - 2];					      \
ce426f
+									      \
ce426f
+    do									      \
ce426f
+      {									      \
ce426f
+	if (--ntotal < inlen)						      \
ce426f
+	  bytebuf[ntotal] = 0x80 | (wch & 0x3f);			      \
ce426f
+	wch >>= 6;							      \
ce426f
+      }									      \
ce426f
+    while (ntotal > 1);							      \
ce426f
+									      \
ce426f
+    bytebuf[0] |= wch;							      \
ce426f
+  }
ce426f
+
ce426f
+#define CLEAR_STATE_COMMON \
ce426f
+  state->__count = 0
ce426f
+
ce426f
+#define BODY_FROM_HW(ASM)						\
ce426f
   {									\
ce426f
-    if (GLRO (dl_hwcap) & HWCAP_S390_ETF3EH)				\
ce426f
-      {									\
ce426f
-	HARDWARE_CONVERT ("cu14 %0, %1, 1");				\
ce426f
+    ASM;								\
ce426f
+    if (__glibc_likely (inptr == inend)					\
ce426f
+	|| result == __GCONV_FULL_OUTPUT)				\
ce426f
+      break;								\
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
+    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
-	      {								\
ce426f
-		result = __GCONV_INCOMPLETE_INPUT;			\
ce426f
-		break;							\
ce426f
-	      }								\
ce426f
-	    STANDARD_FROM_LOOP_ERR_HANDLER (i);				\
ce426f
-	  }								\
ce426f
-	continue;							\
ce426f
+    if (__glibc_likely (inptr + i == inend				\
ce426f
+			&& result == __GCONV_EMPTY_INPUT))		\
ce426f
+      {									\
ce426f
+	result = __GCONV_INCOMPLETE_INPUT;				\
ce426f
+	break;								\
ce426f
       }									\
ce426f
-									\
ce426f
+    STANDARD_FROM_LOOP_ERR_HANDLER (i);					\
ce426f
+  }
ce426f
+
ce426f
+/* This hardware routine uses the Convert UTF8 to UTF32 (cu14) instruction.  */
ce426f
+#define BODY_FROM_ETF3EH BODY_FROM_HW (HARDWARE_CONVERT ("cu14 %0, %1, 1"))
ce426f
+
ce426f
+
ce426f
+/* The software routine is copied from gconv_simple.c.  */
ce426f
+#define BODY_FROM_C							\
ce426f
+  {									\
ce426f
     /* Next input byte.  */						\
ce426f
     uint32_t ch = *inptr;						\
ce426f
 									\
ce426f
-    if (__glibc_likely (ch < 0x80))					      \
ce426f
+    if (__glibc_likely (ch < 0x80))					\
ce426f
       {									\
ce426f
 	/* One byte sequence.  */					\
ce426f
 	++inptr;							\
ce426f
@@ -233,30 +324,18 @@ 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
 	    ch &= 0x07;							\
ce426f
 	  }								\
ce426f
-	else if (__glibc_likely ((ch & 0xfc) == 0xf8))			      \
ce426f
-	  {								\
ce426f
-	    /* We expect five bytes.  */				\
ce426f
-	    cnt = 5;							\
ce426f
-	    ch &= 0x03;							\
ce426f
-	  }								\
ce426f
-	else if (__glibc_likely ((ch & 0xfe) == 0xfc))			      \
ce426f
-	  {								\
ce426f
-	    /* We expect six bytes.  */					\
ce426f
-	    cnt = 6;							\
ce426f
-	    ch &= 0x01;							\
ce426f
-	  }								\
ce426f
 	else								\
ce426f
 	  {								\
ce426f
 	    /* Search the end of this ill-formed UTF-8 character.  This	\
ce426f
@@ -272,7 +351,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
@@ -280,7 +359,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
@@ -305,7 +384,10 @@ gconv_end (struct __gconv_step *data)
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
+	if (i < cnt || (cnt > 2 && (ch >> (5 * cnt - 4)) == 0)		\
ce426f
+	    /* Do not accept UTF-16 surrogates.  */			\
ce426f
+	    || (ch >= 0xd800 && ch <= 0xdfff)				\
ce426f
+	    || (ch > 0x10ffff))						\
ce426f
 	  {								\
ce426f
 	    /* This is an illegal encoding.  */				\
ce426f
 	    goto errout;						\
ce426f
@@ -318,137 +400,212 @@ gconv_end (struct __gconv_step *data)
ce426f
     *((uint32_t *) outptr) = ch;					\
ce426f
     outptr += sizeof (uint32_t);					\
ce426f
   }
ce426f
-#define LOOP_NEED_FLAGS
ce426f
 
ce426f
-#define STORE_REST							\
ce426f
-  {									      \
ce426f
-    /* We store the remaining bytes while converting them into the UCS4	      \
ce426f
-       format.  We can assume that the first byte in the buffer is	      \
ce426f
-       correct and that it requires a larger number of bytes than there	      \
ce426f
-       are in the input buffer.  */					      \
ce426f
-    wint_t ch = **inptrp;						      \
ce426f
-    size_t cnt, r;							      \
ce426f
-									      \
ce426f
-    state->__count = inend - *inptrp;					      \
ce426f
-									      \
ce426f
-    if (ch >= 0xc2 && ch < 0xe0)					      \
ce426f
-      {									      \
ce426f
-	/* We expect two bytes.  The first byte cannot be 0xc0 or	      \
ce426f
-	   0xc1, otherwise the wide character could have been		      \
ce426f
-	   represented using a single byte.  */				      \
ce426f
-	cnt = 2;							      \
ce426f
-	ch &= 0x1f;							      \
ce426f
-      }									      \
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
-      {									      \
ce426f
-	/* We expect four bytes.  */					      \
ce426f
-	cnt = 4;							      \
ce426f
-	ch &= 0x07;							      \
ce426f
-      }									      \
ce426f
-    else if (__glibc_likely ((ch & 0xfc) == 0xf8))			      \
ce426f
-      {									      \
ce426f
-	/* We expect five bytes.  */					      \
ce426f
-	cnt = 5;							      \
ce426f
-	ch &= 0x03;							      \
ce426f
-      }									      \
ce426f
-    else								      \
ce426f
-      {									      \
ce426f
-	/* We expect six bytes.  */					      \
ce426f
-	cnt = 6;							      \
ce426f
-	ch &= 0x01;							      \
ce426f
-      }									      \
ce426f
-									      \
ce426f
-    /* The first byte is already consumed.  */				      \
ce426f
-    r = cnt - 1;							      \
ce426f
-    while (++(*inptrp) < inend)						      \
ce426f
-      {									      \
ce426f
-	ch <<= 6;							      \
ce426f
-	ch |= **inptrp & 0x3f;						      \
ce426f
-	--r;								      \
ce426f
-      }									      \
ce426f
-									      \
ce426f
-    /* Shift for the so far missing bytes.  */				      \
ce426f
-    ch <<= r * 6;							      \
ce426f
-									      \
ce426f
-    /* Store the number of bytes expected for the entire sequence.  */	      \
ce426f
-    state->__count |= cnt << 8;						      \
ce426f
-									      \
ce426f
-    /* Store the value.  */						      \
ce426f
-    state->__value.__wch = ch;						      \
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],64,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 UCS4.  */				\
ce426f
+		  "    vuplhb %%v18,%%v16\n\t"				\
ce426f
+		  "    vupllb %%v19,%%v16\n\t"				\
ce426f
+		  "    la %[R_IN],16(%[R_IN])\n\t"			\
ce426f
+		  "    vuplhh %%v20,%%v18\n\t"				\
ce426f
+		  "    aghi %[R_INLEN],-16\n\t"				\
ce426f
+		  "    vupllh %%v21,%%v18\n\t"				\
ce426f
+		  "    aghi %[R_OUTLEN],-64\n\t"			\
ce426f
+		  "    vuplhh %%v22,%%v19\n\t"				\
ce426f
+		  "    vupllh %%v23,%%v19\n\t"				\
ce426f
+		  /* Store 64 bytes to buf_out.  */			\
ce426f
+		  "    vstm %%v20,%%v23,0(%[R_OUT])\n\t"		\
ce426f
+		  "    la %[R_OUT],64(%[R_OUT])\n\t"			\
ce426f
+		  "    clgijl %[R_INLEN],16,20f\n\t"			\
ce426f
+		  "    clgijl %[R_OUTLEN],64,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],2\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
+		  "    vuplhh %%v20,%%v18\n\t"				\
ce426f
+		  "    vstl %%v20,%[R_TMP2],0(%[R_OUT])\n\t"		\
ce426f
+		  "    ahi %[R_TMP2],-16\n\t"				\
ce426f
+		  "    jl 11f\n\t"					\
ce426f
+		  "    vupllh %%v21,%%v18\n\t"				\
ce426f
+		  "    vstl %%v21,%[R_TMP2],16(%[R_OUT])\n\t"		\
ce426f
+		  "    ahi %[R_TMP2],-16\n\t"				\
ce426f
+		  "    jl 11f\n\t"					\
ce426f
+		  "    vupllb %%v19,%%v16\n\t"				\
ce426f
+		  "    vuplhh %%v22,%%v19\n\t"				\
ce426f
+		  "    vstl %%v22,%[R_TMP2],32(%[R_OUT])\n\t"		\
ce426f
+		  "    ahi %[R_TMP2],-16\n\t"				\
ce426f
+		  "    jl 11f\n\t"					\
ce426f
+		  "    vupllh %%v23,%%v19\n\t"				\
ce426f
+		  "    vstl %%v23,%[R_TMP2],48(%[R_OUT])\n\t"		\
ce426f
+		  "11: \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
+		  /* Handle multibyte utf8-char with convert instruction. */ \
ce426f
+		  "20: cu14 %[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 ("v20") ASM_CLOBBER_VR ("v21")	\
ce426f
+		    ASM_CLOBBER_VR ("v22") ASM_CLOBBER_VR ("v30")	\
ce426f
+		    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
-#define UNPACK_BYTES \
ce426f
-  {									      \
ce426f
-    static const unsigned char inmask[5] = { 0xc0, 0xe0, 0xf0, 0xf8, 0xfc };  \
ce426f
-    wint_t wch = state->__value.__wch;					      \
ce426f
-    size_t ntotal = state->__count >> 8;				      \
ce426f
-									      \
ce426f
-    inlen = state->__count & 255;					      \
ce426f
-									      \
ce426f
-    bytebuf[0] = inmask[ntotal - 2];					      \
ce426f
-									      \
ce426f
-    do									      \
ce426f
-      {									      \
ce426f
-	if (--ntotal < inlen)						      \
ce426f
-	  bytebuf[ntotal] = 0x80 | (wch & 0x3f);			      \
ce426f
-	wch >>= 6;							      \
ce426f
-      }									      \
ce426f
-    while (ntotal > 1);							      \
ce426f
-									      \
ce426f
-    bytebuf[0] |= wch;							      \
ce426f
-  }
ce426f
+/* These definitions apply to the UTF-8 to UTF-32 direction.  The
ce426f
+   software implementation for UTF-8 still supports multibyte
ce426f
+   characters up to 6 bytes whereas the hardware variant does not.  */
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 LOOPFCT			__from_utf8_loop_c
ce426f
 
ce426f
-#define CLEAR_STATE \
ce426f
-  state->__count = 0
ce426f
+#define LOOP_NEED_FLAGS
ce426f
 
ce426f
+#define STORE_REST		STORE_REST_COMMON
ce426f
+#define UNPACK_BYTES		UNPACK_BYTES_COMMON
ce426f
+#define CLEAR_STATE		CLEAR_STATE_COMMON
ce426f
+#define BODY			BODY_FROM_C
ce426f
 #include <iconv/loop.c>
ce426f
 
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 LOOPFCT			__from_utf8_loop_etf3eh
ce426f
+
ce426f
+#define LOOP_NEED_FLAGS
ce426f
+
ce426f
+#define STORE_REST		STORE_REST_COMMON
ce426f
+#define UNPACK_BYTES		UNPACK_BYTES_COMMON
ce426f
+#define CLEAR_STATE		CLEAR_STATE_COMMON
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 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 LOOPFCT		__from_utf8_loop_vx
ce426f
+
ce426f
+# define LOOP_NEED_FLAGS
ce426f
+
ce426f
+# define STORE_REST		STORE_REST_COMMON
ce426f
+# define UNPACK_BYTES		UNPACK_BYTES_COMMON
ce426f
+# define CLEAR_STATE		CLEAR_STATE_COMMON
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
+
ce426f
 /* Conversion from UTF-32 internal/BE to UTF-8.  */
ce426f
+#define BODY_TO_HW(ASM)							\
ce426f
+  {									\
ce426f
+    ASM;								\
ce426f
+    if (__glibc_likely (inptr == inend)					\
ce426f
+	|| result == __GCONV_FULL_OUTPUT)				\
ce426f
+      break;								\
ce426f
+    if (inptr + 4 > inend)						\
ce426f
+      {									\
ce426f
+	result = __GCONV_INCOMPLETE_INPUT;				\
ce426f
+	break;								\
ce426f
+      }									\
ce426f
+    STANDARD_TO_LOOP_ERR_HANDLER (4);					\
ce426f
+  }
ce426f
+
ce426f
+/* The hardware routine uses the S/390 cu41 instruction.  */
ce426f
+#define BODY_TO_ETF3EH BODY_TO_HW (HARDWARE_CONVERT ("cu41 %0, %1"))
ce426f
+
ce426f
+/* The hardware routine uses the S/390 vector and cu41 instructions.  */
ce426f
+#define BODY_TO_VX BODY_TO_HW (HW_TO_VX)
ce426f
 
ce426f
-#define MIN_NEEDED_INPUT	MIN_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 mimics the S/390 cu41 instruction.  */
ce426f
-#define BODY							\
ce426f
+#define BODY_TO_C						\
ce426f
   {								\
ce426f
-    if (GLRO (dl_hwcap) & HWCAP_S390_ETF3EH)			\
ce426f
-      {								\
ce426f
-	HARDWARE_CONVERT ("cu41 %0, %1");			\
ce426f
-								\
ce426f
-	if (inptr != inend)					\
ce426f
-	  {							\
ce426f
-	    result = __GCONV_INCOMPLETE_INPUT;			\
ce426f
-	    break;						\
ce426f
-	  }							\
ce426f
-	continue;						\
ce426f
-      }								\
ce426f
-								\
ce426f
     uint32_t wc = *((const uint32_t *) inptr);			\
ce426f
 								\
ce426f
-    if (__glibc_likely (wc <= 0x7f))					      \
ce426f
+    if (__glibc_likely (wc <= 0x7f))				\
ce426f
       {								\
ce426f
-        /* Single UTF-8 char.  */				\
ce426f
-        *outptr = (uint8_t)wc;					\
ce426f
+	/* Single UTF-8 char.  */				\
ce426f
+	*outptr = (uint8_t)wc;					\
ce426f
 	outptr++;						\
ce426f
       }								\
ce426f
     else if (wc <= 0x7ff)					\
ce426f
       {								\
ce426f
-        /* Two UTF-8 chars.  */					\
ce426f
-        if (__glibc_unlikely (outptr + 2 > outend))			      \
ce426f
+	/* Two UTF-8 chars.  */					\
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] = 0xc0;					\
ce426f
 	outptr[0] |= wc >> 6;					\
ce426f
 								\
ce426f
 	outptr[1] = 0x80;					\
ce426f
@@ -459,12 +616,18 @@ gconv_end (struct __gconv_step *data)
ce426f
     else if (wc <= 0xffff)					\
ce426f
       {								\
ce426f
 	/* Three UTF-8 chars.  */				\
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
 	    break;						\
ce426f
 	  }							\
ce426f
+	if (wc >= 0xd800 && wc < 0xdc00)			\
ce426f
+	  {							\
ce426f
+	    /* Do not accept UTF-16 surrogates.   */		\
ce426f
+	    result = __GCONV_ILLEGAL_INPUT;			\
ce426f
+	    STANDARD_TO_LOOP_ERR_HANDLER (4);			\
ce426f
+	  }							\
ce426f
 	outptr[0] = 0xe0;					\
ce426f
 	outptr[0] |= wc >> 12;					\
ce426f
 								\
ce426f
@@ -479,7 +642,7 @@ gconv_end (struct __gconv_step *data)
ce426f
       else if (wc <= 0x10ffff)					\
ce426f
 	{							\
ce426f
 	  /* Four UTF-8 chars.  */				\
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
@@ -505,7 +668,140 @@ gconv_end (struct __gconv_step *data)
ce426f
 	}							\
ce426f
     inptr += 4;							\
ce426f
   }
ce426f
+
ce426f
+#define HW_TO_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;						\
ce426f
+    asm volatile (".machine push\n\t"					\
ce426f
+		  ".machine \"z13\"\n\t"				\
ce426f
+		  ".machinemode \"zarch_nohighgprs\"\n\t"		\
ce426f
+		  "    vleif %%v20,127,0\n\t"   /* element 0: 127  */	\
ce426f
+		  "    vzero %%v21\n\t"					\
ce426f
+		  "    vleih %%v21,8192,0\n\t"  /* element 0:   >  */	\
ce426f
+		  "    vleih %%v21,-8192,2\n\t" /* element 1: =<>  */	\
ce426f
+		  /* Loop which handles UTF-32 chars <=0x7f.  */	\
ce426f
+		  "0:  clgijl %[R_INLEN],64,20f\n\t"			\
ce426f
+		  "    clgijl %[R_OUTLEN],16,20f\n\t"			\
ce426f
+		  "1:  vlm %%v16,%%v19,0(%[R_IN])\n\t"			\
ce426f
+		  "    lghi %[R_TMP],0\n\t"				\
ce426f
+		  /* Shorten to byte values.  */			\
ce426f
+		  "    vpkf %%v23,%%v16,%%v17\n\t"			\
ce426f
+		  "    vpkf %%v24,%%v18,%%v19\n\t"			\
ce426f
+		  "    vpkh %%v23,%%v23,%%v24\n\t"			\
ce426f
+		  /* Checking for values > 0x7f.  */			\
ce426f
+		  "    vstrcfs %%v22,%%v16,%%v20,%%v21\n\t"		\
ce426f
+		  "    jno 10f\n\t"					\
ce426f
+		  "    vstrcfs %%v22,%%v17,%%v20,%%v21\n\t"		\
ce426f
+		  "    jno 11f\n\t"					\
ce426f
+		  "    vstrcfs %%v22,%%v18,%%v20,%%v21\n\t"		\
ce426f
+		  "    jno 12f\n\t"					\
ce426f
+		  "    vstrcfs %%v22,%%v19,%%v20,%%v21\n\t"		\
ce426f
+		  "    jno 13f\n\t"					\
ce426f
+		  /* Store 16bytes to outptr.  */			\
ce426f
+		  "    vst %%v23,0(%[R_OUT])\n\t"			\
ce426f
+		  "    aghi %[R_INLEN],-64\n\t"				\
ce426f
+		  "    aghi %[R_OUTLEN],-16\n\t"			\
ce426f
+		  "    la %[R_IN],64(%[R_IN])\n\t"			\
ce426f
+		  "    la %[R_OUT],16(%[R_OUT])\n\t"			\
ce426f
+		  "    clgijl %[R_INLEN],64,20f\n\t"			\
ce426f
+		  "    clgijl %[R_OUTLEN],16,20f\n\t"			\
ce426f
+		  "    j 1b\n\t"					\
ce426f
+		  /* Found a value > 0x7f.  */				\
ce426f
+		  "13: ahi %[R_TMP],4\n\t"				\
ce426f
+		  "12: ahi %[R_TMP],4\n\t"				\
ce426f
+		  "11: ahi %[R_TMP],4\n\t"				\
ce426f
+		  "10: vlgvb %[R_I],%%v22,7\n\t"			\
ce426f
+		  "    srlg %[R_I],%[R_I],2\n\t"			\
ce426f
+		  "    agr %[R_I],%[R_TMP]\n\t"				\
ce426f
+		  "    je 20f\n\t"					\
ce426f
+		  /* Store characters before invalid one...  */		\
ce426f
+		  "    slgr %[R_OUTLEN],%[R_I]\n\t"			\
ce426f
+		  "15: aghi %[R_I],-1\n\t"				\
ce426f
+		  "    vstl %%v23,%[R_I],0(%[R_OUT])\n\t"		\
ce426f
+		  /* ... and update pointers.  */			\
ce426f
+		  "    aghi %[R_I],1\n\t"				\
ce426f
+		  "    la %[R_OUT],0(%[R_I],%[R_OUT])\n\t"		\
ce426f
+		  "    sllg %[R_I],%[R_I],2\n\t"			\
ce426f
+		  "    la %[R_IN],0(%[R_I],%[R_IN])\n\t"		\
ce426f
+		  "    slgr %[R_INLEN],%[R_I]\n\t"			\
ce426f
+		  /* Handle multibyte utf8-char with convert instruction. */ \
ce426f
+		  "20: cu41 %[R_OUT],%[R_IN]\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] "=d" (tmp)	\
ce426f
+		    , [R_I] "=a" (tmp2)					\
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 ("v20") ASM_CLOBBER_VR ("v21")	\
ce426f
+		    ASM_CLOBBER_VR ("v22") ASM_CLOBBER_VR ("v23")	\
ce426f
+		    ASM_CLOBBER_VR ("v24")				\
ce426f
+		  );							\
ce426f
+    inptr = pInput;							\
ce426f
+    outptr = pOutput;							\
ce426f
+  }
ce426f
+
ce426f
+/* Generate loop-function with software routing.  */
ce426f
+#define MIN_NEEDED_INPUT	MIN_NEEDED_TO
ce426f
+#define MIN_NEEDED_OUTPUT	MIN_NEEDED_FROM
ce426f
+#define MAX_NEEDED_OUTPUT	MAX_NEEDED_FROM
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 hardware utf-convert instruction.  */
ce426f
+#define MIN_NEEDED_INPUT	MIN_NEEDED_TO
ce426f
+#define MIN_NEEDED_OUTPUT	MIN_NEEDED_FROM
ce426f
+#define MAX_NEEDED_OUTPUT	MAX_NEEDED_FROM
ce426f
+#define LOOPFCT			__to_utf8_loop_etf3eh
ce426f
 #define LOOP_NEED_FLAGS
ce426f
+#define BODY			BODY_TO_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_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
+#endif
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 defined HAVE_S390_VX_ASM_SUPPORT
ce426f
+  if (dl_hwcap & HWCAP_S390_VX)
ce426f
+    return __to_utf8_loop_vx;
ce426f
+  else
ce426f
+#endif
ce426f
+  if (dl_hwcap & HWCAP_S390_ETF3EH)
ce426f
+    return __to_utf8_loop_etf3eh;
ce426f
+  else
ce426f
+    return __to_utf8_loop_c;
ce426f
+}
ce426f
+
ce426f
+strong_alias (__to_utf8_loop_c_single, __to_utf8_loop_single)
ce426f
+
ce426f
+
ce426f
 #include <iconv/skeleton.c>
ce426f
-- 
ce426f
1.8.3.1
ce426f