Blame SOURCES/glibc-rh1268008-14.patch

147e83
From 74fa8494fa732a0bd63b14b60ea0f1e542e0cee6 Mon Sep 17 00:00:00 2001
147e83
From: Stefan Liebler <stli@linux.vnet.ibm.com>
147e83
Date: Thu, 8 Oct 2015 11:09:04 +0200
147e83
Subject: [PATCH 14/30] S390: Optimize strncpy and wcsncpy.
147e83
147e83
upstream-commit-id: d183b96ee6dc694e95f212c9272a178163351b19
147e83
https://www.sourceware.org/ml/libc-alpha/2015-07/msg00160.html
147e83
147e83
This patch provides optimized versions of strncpy and wcsncpy with the z13
147e83
vector instructions.
147e83
147e83
ChangeLog:
147e83
147e83
	* sysdeps/s390/multiarch/strncpy-vx.S: New File.
147e83
	* sysdeps/s390/multiarch/strncpy.c: Likewise.
147e83
	* sysdeps/s390/multiarch/wcsncpy-c.c: Likewise.
147e83
	* sysdeps/s390/multiarch/wcsncpy-vx.S: Likewise.
147e83
	* sysdeps/s390/multiarch/wcsncpy.c: Likewise.
147e83
	* sysdeps/s390/s390-32/multiarch/strncpy.c: Likewise.
147e83
	* sysdeps/s390/s390-64/multiarch/strncpy.c: Likewise.
147e83
	* sysdeps/s390/multiarch/Makefile (sysdep_routines): Add strncpy and
147e83
	wcsncpy functions.
147e83
	* wcsmbs/wcsncpy.c: Use WCSNCPY if defined.
147e83
	* sysdeps/s390/multiarch/ifunc-impl-list.c
147e83
	(__libc_ifunc_impl_list): Add ifunc test for strncpy, wcsncpy.
147e83
	* string/test-strncpy.c: Add wcsncpy support.
147e83
	* wcsmbs/test-wcsncpy.c: New File.
147e83
	* wcsmbs/Makefile (strop-tests): Add wcsncpy.
147e83
	* benchtests/bench-strncpy.c: Add wcsncpy support.
147e83
	* benchtests/bench-wcsncpy.c: New File.
147e83
	* benchtests/Makefile (wcsmbs-bench): Add wcsncpy
147e83
---
147e83
 benchtests/Makefile                      |   2 +-
147e83
 benchtests/bench-strncpy.c               | 108 ++++++++++-----
147e83
 benchtests/bench-wcsncpy.c               |  20 +++
147e83
 string/test-strncpy.c                    | 129 +++++++++++-------
147e83
 sysdeps/s390/multiarch/Makefile          |   6 +-
147e83
 sysdeps/s390/multiarch/ifunc-impl-list.c |   3 +
147e83
 sysdeps/s390/multiarch/strncpy-vx.S      | 207 ++++++++++++++++++++++++++++
147e83
 sysdeps/s390/multiarch/strncpy.c         |  24 ++++
147e83
 sysdeps/s390/multiarch/wcsncpy-c.c       |  25 ++++
147e83
 sysdeps/s390/multiarch/wcsncpy-vx.S      | 223 +++++++++++++++++++++++++++++++
147e83
 sysdeps/s390/multiarch/wcsncpy.c         |  28 ++++
147e83
 sysdeps/s390/s390-32/multiarch/strncpy.c |  21 +++
147e83
 sysdeps/s390/s390-64/multiarch/strncpy.c |  21 +++
147e83
 wcsmbs/Makefile                          |   2 +-
147e83
 wcsmbs/test-wcsncpy-ifunc.c              |  20 +++
147e83
 wcsmbs/test-wcsncpy.c                    |  20 +++
147e83
 wcsmbs/wcsncpy.c                         |   5 +
147e83
 17 files changed, 778 insertions(+), 86 deletions(-)
147e83
 create mode 100644 benchtests/bench-wcsncpy.c
147e83
 create mode 100644 sysdeps/s390/multiarch/strncpy-vx.S
147e83
 create mode 100644 sysdeps/s390/multiarch/strncpy.c
147e83
 create mode 100644 sysdeps/s390/multiarch/wcsncpy-c.c
147e83
 create mode 100644 sysdeps/s390/multiarch/wcsncpy-vx.S
147e83
 create mode 100644 sysdeps/s390/multiarch/wcsncpy.c
147e83
 create mode 100644 sysdeps/s390/s390-32/multiarch/strncpy.c
147e83
 create mode 100644 sysdeps/s390/s390-64/multiarch/strncpy.c
147e83
 create mode 100644 wcsmbs/test-wcsncpy-ifunc.c
147e83
 create mode 100644 wcsmbs/test-wcsncpy.c
147e83
147e83
diff --git a/benchtests/Makefile b/benchtests/Makefile
147e83
index 1b491c6..f898258 100644
147e83
--- a/benchtests/Makefile
147e83
+++ b/benchtests/Makefile
147e83
@@ -38,7 +38,7 @@ string-bench := bcopy bzero memccpy memchr memcmp memcpy memmem memmove \
147e83
 		strcat strchr strchrnul strcmp strcpy strcspn strlen \
147e83
 		strncasecmp strncat strncmp strncpy strnlen strpbrk strrchr \
147e83
 		strspn strstr strcpy_chk stpcpy_chk memrchr strsep strtok
147e83
-wcsmbs-bench := wcslen wcsnlen wcscpy wcpcpy
147e83
+wcsmbs-bench := wcslen wcsnlen wcscpy wcpcpy wcsncpy
147e83
 string-bench-all := $(string-bench) ${wcsmbs-bench}
147e83
 
147e83
 stdlib-bench := strtod
147e83
diff --git a/benchtests/bench-strncpy.c b/benchtests/bench-strncpy.c
147e83
index 645925b..2cfd56a 100644
147e83
--- a/benchtests/bench-strncpy.c
147e83
+++ b/benchtests/bench-strncpy.c
147e83
@@ -1,5 +1,5 @@
147e83
 /* Measure strncpy functions.
147e83
-   Copyright (C) 2013 Free Software Foundation, Inc.
147e83
+   Copyright (C) 2013-2015 Free Software Foundation, Inc.
147e83
    This file is part of the GNU C Library.
147e83
 
147e83
    The GNU C Library is free software; you can redistribute it and/or
147e83
@@ -16,23 +16,56 @@
147e83
    License along with the GNU C Library; if not, see
147e83
    <http://www.gnu.org/licenses/>.  */
147e83
 
147e83
+#ifdef WIDE
147e83
+# include <wchar.h>
147e83
+# define CHAR wchar_t
147e83
+# define UCHAR wchar_t
147e83
+# define BIG_CHAR WCHAR_MAX
147e83
+# define SMALL_CHAR 1273
147e83
+# define MEMCMP wmemcmp
147e83
+# define MEMSET wmemset
147e83
+# define STRNLEN wcsnlen
147e83
+#else
147e83
+# define CHAR char
147e83
+# define UCHAR unsigned char
147e83
+# define BIG_CHAR CHAR_MAX
147e83
+# define SMALL_CHAR 127
147e83
+# define MEMCMP memcmp
147e83
+# define MEMSET memset
147e83
+# define STRNLEN strnlen
147e83
+#endif /* !WIDE */
147e83
+
147e83
 #ifndef STRNCPY_RESULT
147e83
 # define STRNCPY_RESULT(dst, len, n) dst
147e83
 # define TEST_MAIN
147e83
-# define TEST_NAME "strncpy"
147e83
+# ifndef WIDE
147e83
+#  define TEST_NAME "strncpy"
147e83
+# else
147e83
+#  define TEST_NAME "wcsncpy"
147e83
+# endif /* WIDE */
147e83
 # include "bench-string.h"
147e83
 
147e83
-char *simple_strncpy (char *, const char *, size_t);
147e83
-char *stupid_strncpy (char *, const char *, size_t);
147e83
-
147e83
-IMPL (stupid_strncpy, 0)
147e83
-IMPL (simple_strncpy, 0)
147e83
-IMPL (strncpy, 1)
147e83
-
147e83
-char *
147e83
-simple_strncpy (char *dst, const char *src, size_t n)
147e83
+# ifndef WIDE
147e83
+#  define SIMPLE_STRNCPY simple_strncpy
147e83
+#  define STUPID_STRNCPY stupid_strncpy
147e83
+#  define STRNCPY strncpy
147e83
+# else
147e83
+#  define SIMPLE_STRNCPY simple_wcsncpy
147e83
+#  define STUPID_STRNCPY stupid_wcsncpy
147e83
+#  define STRNCPY wcsncpy
147e83
+# endif /* WIDE */
147e83
+
147e83
+CHAR *SIMPLE_STRNCPY (CHAR *, const CHAR *, size_t);
147e83
+CHAR *STUPID_STRNCPY (CHAR *, const CHAR *, size_t);
147e83
+
147e83
+IMPL (STUPID_STRNCPY, 0)
147e83
+IMPL (SIMPLE_STRNCPY, 0)
147e83
+IMPL (STRNCPY, 1)
147e83
+
147e83
+CHAR *
147e83
+SIMPLE_STRNCPY (CHAR *dst, const CHAR *src, size_t n)
147e83
 {
147e83
-  char *ret = dst;
147e83
+  CHAR *ret = dst;
147e83
   while (n--)
147e83
     if ((*dst++ = *src++) == '\0')
147e83
       {
147e83
@@ -43,10 +76,10 @@ simple_strncpy (char *dst, const char *src, size_t n)
147e83
   return ret;
147e83
 }
147e83
 
147e83
-char *
147e83
-stupid_strncpy (char *dst, const char *src, size_t n)
147e83
+CHAR *
147e83
+STUPID_STRNCPY (CHAR *dst, const CHAR *src, size_t n)
147e83
 {
147e83
-  size_t nc = strnlen (src, n);
147e83
+  size_t nc = STRNLEN (src, n);
147e83
   size_t i;
147e83
 
147e83
   for (i = 0; i < nc; ++i)
147e83
@@ -55,12 +88,12 @@ stupid_strncpy (char *dst, const char *src, size_t n)
147e83
     dst[i] = '\0';
147e83
   return dst;
147e83
 }
147e83
-#endif
147e83
+#endif /* !STRNCPY_RESULT */
147e83
 
147e83
-typedef char *(*proto_t) (char *, const char *, size_t);
147e83
+typedef CHAR *(*proto_t) (CHAR *, const CHAR *, size_t);
147e83
 
147e83
 static void
147e83
-do_one_test (impl_t *impl, char *dst, const char *src, size_t len, size_t n)
147e83
+do_one_test (impl_t *impl, CHAR *dst, const CHAR *src, size_t len, size_t n)
147e83
 {
147e83
   size_t i, iters = INNER_LOOP_ITERS;
147e83
   timing_t start, stop, cur;
147e83
@@ -73,7 +106,7 @@ do_one_test (impl_t *impl, char *dst, const char *src, size_t len, size_t n)
147e83
       return;
147e83
     }
147e83
 
147e83
-  if (memcmp (dst, src, len > n ? n : len) != 0)
147e83
+  if (memcmp (dst, src, (len > n ? n : len) * sizeof (CHAR)) != 0)
147e83
     {
147e83
       error (0, 0, "Wrong result in function %s", impl->name);
147e83
       ret = 1;
147e83
@@ -109,23 +142,24 @@ static void
147e83
 do_test (size_t align1, size_t align2, size_t len, size_t n, int max_char)
147e83
 {
147e83
   size_t i;
147e83
-  char *s1, *s2;
147e83
+  CHAR *s1, *s2;
147e83
 
147e83
   align1 &= 7;
147e83
-  if (align1 + len >= page_size)
147e83
+  if ((align1 + len) * sizeof (CHAR) >= page_size)
147e83
     return;
147e83
 
147e83
   align2 &= 7;
147e83
-  if (align2 + len >= page_size)
147e83
+  if ((align2 + len) * sizeof (CHAR) >= page_size)
147e83
     return;
147e83
 
147e83
-  s1 = (char *) (buf1 + align1);
147e83
-  s2 = (char *) (buf2 + align2);
147e83
+  s1 = (CHAR *) (buf1 + align1);
147e83
+  s2 = (CHAR *) (buf2 + align2);
147e83
 
147e83
   for (i = 0; i < len; ++i)
147e83
     s1[i] = 32 + 23 * i % (max_char - 32);
147e83
   s1[len] = 0;
147e83
-  for (i = len + 1; i + align1 < page_size && i < len + 64; ++i)
147e83
+  for (i = len + 1; (i + align1) * sizeof (CHAR) < page_size && i < len + 64;
147e83
+       ++i)
147e83
     s1[i] = 32 + 32 * i % (max_char - 32);
147e83
 
147e83
   printf ("Length %4zd, n %4zd, alignment %2zd/%2zd:", len, n, align1, align2);
147e83
@@ -150,22 +184,22 @@ test_main (void)
147e83
 
147e83
   for (i = 1; i < 8; ++i)
147e83
     {
147e83
-      do_test (i, i, 16, 16, 127);
147e83
-      do_test (i, i, 16, 16, 255);
147e83
-      do_test (i, 2 * i, 16, 16, 127);
147e83
-      do_test (2 * i, i, 16, 16, 255);
147e83
-      do_test (8 - i, 2 * i, 1 << i, 2 << i, 127);
147e83
-      do_test (2 * i, 8 - i, 2 << i, 1 << i, 127);
147e83
-      do_test (8 - i, 2 * i, 1 << i, 2 << i, 255);
147e83
-      do_test (2 * i, 8 - i, 2 << i, 1 << i, 255);
147e83
+      do_test (i, i, 16, 16, SMALL_CHAR);
147e83
+      do_test (i, i, 16, 16, BIG_CHAR);
147e83
+      do_test (i, 2 * i, 16, 16, SMALL_CHAR);
147e83
+      do_test (2 * i, i, 16, 16, BIG_CHAR);
147e83
+      do_test (8 - i, 2 * i, 1 << i, 2 << i, SMALL_CHAR);
147e83
+      do_test (2 * i, 8 - i, 2 << i, 1 << i, SMALL_CHAR);
147e83
+      do_test (8 - i, 2 * i, 1 << i, 2 << i, BIG_CHAR);
147e83
+      do_test (2 * i, 8 - i, 2 << i, 1 << i, BIG_CHAR);
147e83
     }
147e83
 
147e83
   for (i = 1; i < 8; ++i)
147e83
     {
147e83
-      do_test (0, 0, 4 << i, 8 << i, 127);
147e83
-      do_test (0, 0, 16 << i, 8 << i, 127);
147e83
-      do_test (8 - i, 2 * i, 4 << i, 8 << i, 127);
147e83
-      do_test (8 - i, 2 * i, 16 << i, 8 << i, 127);
147e83
+      do_test (0, 0, 4 << i, 8 << i, SMALL_CHAR);
147e83
+      do_test (0, 0, 16 << i, 8 << i, SMALL_CHAR);
147e83
+      do_test (8 - i, 2 * i, 4 << i, 8 << i, SMALL_CHAR);
147e83
+      do_test (8 - i, 2 * i, 16 << i, 8 << i, SMALL_CHAR);
147e83
     }
147e83
 
147e83
   return ret;
147e83
diff --git a/benchtests/bench-wcsncpy.c b/benchtests/bench-wcsncpy.c
147e83
new file mode 100644
147e83
index 0000000..d6f63c9
147e83
--- /dev/null
147e83
+++ b/benchtests/bench-wcsncpy.c
147e83
@@ -0,0 +1,20 @@
147e83
+/* Measure wcsncpy functions.
147e83
+   Copyright (C) 2015 Free Software Foundation, Inc.
147e83
+   This file is part of the GNU C Library.
147e83
+
147e83
+   The GNU C Library is free software; you can redistribute it and/or
147e83
+   modify it under the terms of the GNU Lesser General Public
147e83
+   License as published by the Free Software Foundation; either
147e83
+   version 2.1 of the License, or (at your option) any later version.
147e83
+
147e83
+   The GNU C Library is distributed in the hope that it will be useful,
147e83
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
147e83
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
147e83
+   Lesser General Public License for more details.
147e83
+
147e83
+   You should have received a copy of the GNU Lesser General Public
147e83
+   License along with the GNU C Library; if not, see
147e83
+   <http://www.gnu.org/licenses/>.  */
147e83
+
147e83
+#define WIDE 1
147e83
+#include "bench-strncpy.c"
147e83
diff --git a/string/test-strncpy.c b/string/test-strncpy.c
147e83
index a623093..bdccc98 100644
147e83
--- a/string/test-strncpy.c
147e83
+++ b/string/test-strncpy.c
147e83
@@ -17,23 +17,56 @@
147e83
    License along with the GNU C Library; if not, see
147e83
    <http://www.gnu.org/licenses/>.  */
147e83
 
147e83
+#ifdef WIDE
147e83
+# include <wchar.h>
147e83
+# define CHAR wchar_t
147e83
+# define UCHAR wchar_t
147e83
+# define BIG_CHAR WCHAR_MAX
147e83
+# define SMALL_CHAR 1273
147e83
+# define MEMCMP wmemcmp
147e83
+# define MEMSET wmemset
147e83
+# define STRNLEN wcsnlen
147e83
+#else
147e83
+# define CHAR char
147e83
+# define UCHAR unsigned char
147e83
+# define BIG_CHAR CHAR_MAX
147e83
+# define SMALL_CHAR 127
147e83
+# define MEMCMP memcmp
147e83
+# define MEMSET memset
147e83
+# define STRNLEN strnlen
147e83
+#endif /* !WIDE */
147e83
+
147e83
+
147e83
 #ifndef STRNCPY_RESULT
147e83
 # define STRNCPY_RESULT(dst, len, n) dst
147e83
 # define TEST_MAIN
147e83
-# define TEST_NAME "strncpy"
147e83
+# ifndef WIDE
147e83
+#  define TEST_NAME "strncpy"
147e83
+# else
147e83
+#  define TEST_NAME "wcsncpy"
147e83
+# endif /* WIDE */
147e83
 # include "test-string.h"
147e83
+# ifndef WIDE
147e83
+#  define SIMPLE_STRNCPY simple_strncpy
147e83
+#  define STUPID_STRNCPY stupid_strncpy
147e83
+#  define STRNCPY strncpy
147e83
+# else
147e83
+#  define SIMPLE_STRNCPY simple_wcsncpy
147e83
+#  define STUPID_STRNCPY stupid_wcsncpy
147e83
+#  define STRNCPY wcsncpy
147e83
+# endif /* WIDE */
147e83
 
147e83
-char *simple_strncpy (char *, const char *, size_t);
147e83
-char *stupid_strncpy (char *, const char *, size_t);
147e83
+CHAR *SIMPLE_STRNCPY (CHAR *, const CHAR *, size_t);
147e83
+CHAR *STUPID_STRNCPY (CHAR *, const CHAR *, size_t);
147e83
 
147e83
-IMPL (stupid_strncpy, 0)
147e83
-IMPL (simple_strncpy, 0)
147e83
-IMPL (strncpy, 1)
147e83
+IMPL (STUPID_STRNCPY, 0)
147e83
+IMPL (SIMPLE_STRNCPY, 0)
147e83
+IMPL (STRNCPY, 1)
147e83
 
147e83
-char *
147e83
-simple_strncpy (char *dst, const char *src, size_t n)
147e83
+CHAR *
147e83
+SIMPLE_STRNCPY (CHAR *dst, const CHAR *src, size_t n)
147e83
 {
147e83
-  char *ret = dst;
147e83
+  CHAR *ret = dst;
147e83
   while (n--)
147e83
     if ((*dst++ = *src++) == '\0')
147e83
       {
147e83
@@ -44,10 +77,10 @@ simple_strncpy (char *dst, const char *src, size_t n)
147e83
   return ret;
147e83
 }
147e83
 
147e83
-char *
147e83
-stupid_strncpy (char *dst, const char *src, size_t n)
147e83
+CHAR *
147e83
+STUPID_STRNCPY (CHAR *dst, const CHAR *src, size_t n)
147e83
 {
147e83
-  size_t nc = strnlen (src, n);
147e83
+  size_t nc = STRNLEN (src, n);
147e83
   size_t i;
147e83
 
147e83
   for (i = 0; i < nc; ++i)
147e83
@@ -56,12 +89,12 @@ stupid_strncpy (char *dst, const char *src, size_t n)
147e83
     dst[i] = '\0';
147e83
   return dst;
147e83
 }
147e83
-#endif
147e83
+#endif /* !STRNCPY_RESULT */
147e83
 
147e83
-typedef char *(*proto_t) (char *, const char *, size_t);
147e83
+typedef CHAR *(*proto_t) (CHAR *, const CHAR *, size_t);
147e83
 
147e83
 static void
147e83
-do_one_test (impl_t *impl, char *dst, const char *src, size_t len, size_t n)
147e83
+do_one_test (impl_t *impl, CHAR *dst, const char *src, size_t len, size_t n)
147e83
 {
147e83
   if (CALL (impl, dst, src, n) != STRNCPY_RESULT (dst, len, n))
147e83
     {
147e83
@@ -71,7 +104,7 @@ do_one_test (impl_t *impl, char *dst, const char *src, size_t len, size_t n)
147e83
       return;
147e83
     }
147e83
 
147e83
-  if (memcmp (dst, src, len > n ? n : len) != 0)
147e83
+  if (memcmp (dst, src, (len > n ? n : len) * sizeof (CHAR)) != 0)
147e83
     {
147e83
       error (0, 0, "Wrong result in function %s", impl->name);
147e83
       ret = 1;
147e83
@@ -96,23 +129,26 @@ static void
147e83
 do_test (size_t align1, size_t align2, size_t len, size_t n, int max_char)
147e83
 {
147e83
   size_t i;
147e83
-  char *s1, *s2;
147e83
+  CHAR *s1, *s2;
147e83
 
147e83
+/* For wcsncpy: align1 and align2 here mean alignment not in bytes,
147e83
+   but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t)).  */
147e83
   align1 &= 7;
147e83
-  if (align1 + len >= page_size)
147e83
+  if ((align1 + len) * sizeof (CHAR) >= page_size)
147e83
     return;
147e83
 
147e83
   align2 &= 7;
147e83
-  if (align2 + len >= page_size)
147e83
+  if ((align2 + len) * sizeof (CHAR) >= page_size)
147e83
     return;
147e83
 
147e83
-  s1 = (char *) (buf1 + align1);
147e83
-  s2 = (char *) (buf2 + align2);
147e83
+  s1 = (CHAR *) (buf1) + align1;
147e83
+  s2 = (CHAR *) (buf2) + align2;
147e83
 
147e83
   for (i = 0; i < len; ++i)
147e83
     s1[i] = 32 + 23 * i % (max_char - 32);
147e83
   s1[len] = 0;
147e83
-  for (i = len + 1; i + align1 < page_size && i < len + 64; ++i)
147e83
+  for (i = len + 1; (i + align1) * sizeof (CHAR) < page_size && i < len + 64;
147e83
+       ++i)
147e83
     s1[i] = 32 + 32 * i % (max_char - 32);
147e83
 
147e83
   FOR_EACH_IMPL (impl, 0)
147e83
@@ -123,12 +159,16 @@ static void
147e83
 do_random_tests (void)
147e83
 {
147e83
   size_t i, j, n, align1, align2, len, size, mode;
147e83
-  unsigned char *p1 = buf1 + page_size - 512;
147e83
-  unsigned char *p2 = buf2 + page_size - 512;
147e83
-  unsigned char *res;
147e83
+  UCHAR *p1 = (UCHAR *) (buf1 + page_size) - 512;
147e83
+  UCHAR *p2 = (UCHAR *) (buf2 + page_size) - 512;
147e83
+  UCHAR *res;
147e83
 
147e83
   for (n = 0; n < ITERATIONS; n++)
147e83
     {
147e83
+      /* For wcsncpy: align1 and align2 here mean align not in bytes,
147e83
+	 but in wchar_ts, in bytes it will equal to align * (sizeof
147e83
+	 (wchar_t)).  */
147e83
+
147e83
       mode = random ();
147e83
       if (mode & 1)
147e83
 	{
147e83
@@ -166,7 +206,7 @@ do_random_tests (void)
147e83
 	    {
147e83
 	      size = random () & 511;
147e83
 	      if (size + j > 512)
147e83
-		size = 512 - j - (random() & 31);
147e83
+		size = 512 - j - (random () & 31);
147e83
 	    }
147e83
 	  else
147e83
 	    size = 512 - j;
147e83
@@ -182,18 +222,17 @@ do_random_tests (void)
147e83
 	    p1[i] = 0;
147e83
 	  else
147e83
 	    {
147e83
-	      p1[i] = random () & 255;
147e83
+	      p1[i] = random () & BIG_CHAR;
147e83
 	      if (i >= align1 && i < len + align1 && !p1[i])
147e83
-		p1[i] = (random () & 127) + 3;
147e83
+		p1[i] = (random () & SMALL_CHAR) + 3;
147e83
 	    }
147e83
 	}
147e83
 
147e83
       FOR_EACH_IMPL (impl, 1)
147e83
 	{
147e83
-	  memset (p2 - 64, '\1', 512 + 64);
147e83
-	  res = (unsigned char *) CALL (impl,
147e83
-					(char *) (p2 + align2),
147e83
-					(char *) (p1 + align1), size);
147e83
+	  MEMSET (p2 - 64, '\1', 512 + 64);
147e83
+	  res = (UCHAR *) CALL (impl, (CHAR *) (p2 + align2),
147e83
+				(CHAR *) (p1 + align1), size);
147e83
 	  if (res != STRNCPY_RESULT (p2 + align2, len, size))
147e83
 	    {
147e83
 	      error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p",
147e83
@@ -235,7 +274,7 @@ do_random_tests (void)
147e83
 	  j = len + 1;
147e83
 	  if (size < j)
147e83
 	    j = size;
147e83
-	  if (memcmp (p1 + align1, p2 + align2, j))
147e83
+	  if (MEMCMP (p1 + align1, p2 + align2, j))
147e83
 	    {
147e83
 	      error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
147e83
 		     n, impl->name, align1, align2, len);
147e83
@@ -259,22 +298,22 @@ test_main (void)
147e83
 
147e83
   for (i = 1; i < 8; ++i)
147e83
     {
147e83
-      do_test (i, i, 16, 16, 127);
147e83
-      do_test (i, i, 16, 16, 255);
147e83
-      do_test (i, 2 * i, 16, 16, 127);
147e83
-      do_test (2 * i, i, 16, 16, 255);
147e83
-      do_test (8 - i, 2 * i, 1 << i, 2 << i, 127);
147e83
-      do_test (2 * i, 8 - i, 2 << i, 1 << i, 127);
147e83
-      do_test (8 - i, 2 * i, 1 << i, 2 << i, 255);
147e83
-      do_test (2 * i, 8 - i, 2 << i, 1 << i, 255);
147e83
+      do_test (i, i, 16, 16, SMALL_CHAR);
147e83
+      do_test (i, i, 16, 16, BIG_CHAR);
147e83
+      do_test (i, 2 * i, 16, 16, SMALL_CHAR);
147e83
+      do_test (2 * i, i, 16, 16, BIG_CHAR);
147e83
+      do_test (8 - i, 2 * i, 1 << i, 2 << i, SMALL_CHAR);
147e83
+      do_test (2 * i, 8 - i, 2 << i, 1 << i, SMALL_CHAR);
147e83
+      do_test (8 - i, 2 * i, 1 << i, 2 << i, BIG_CHAR);
147e83
+      do_test (2 * i, 8 - i, 2 << i, 1 << i, BIG_CHAR);
147e83
     }
147e83
 
147e83
   for (i = 1; i < 8; ++i)
147e83
     {
147e83
-      do_test (0, 0, 4 << i, 8 << i, 127);
147e83
-      do_test (0, 0, 16 << i, 8 << i, 127);
147e83
-      do_test (8 - i, 2 * i, 4 << i, 8 << i, 127);
147e83
-      do_test (8 - i, 2 * i, 16 << i, 8 << i, 127);
147e83
+      do_test (0, 0, 4 << i, 8 << i, SMALL_CHAR);
147e83
+      do_test (0, 0, 16 << i, 8 << i, SMALL_CHAR);
147e83
+      do_test (8 - i, 2 * i, 4 << i, 8 << i, SMALL_CHAR);
147e83
+      do_test (8 - i, 2 * i, 16 << i, 8 << i, SMALL_CHAR);
147e83
     }
147e83
 
147e83
   do_random_tests ();
147e83
diff --git a/sysdeps/s390/multiarch/Makefile b/sysdeps/s390/multiarch/Makefile
147e83
index 5b57342..0dff2dc 100644
147e83
--- a/sysdeps/s390/multiarch/Makefile
147e83
+++ b/sysdeps/s390/multiarch/Makefile
147e83
@@ -2,12 +2,14 @@ ifeq ($(subdir),string)
147e83
 sysdep_routines += strlen strlen-vx strlen-c \
147e83
 		   strnlen strnlen-vx strnlen-c \
147e83
 		   strcpy strcpy-vx \
147e83
-		   stpcpy stpcpy-vx stpcpy-c
147e83
+		   stpcpy stpcpy-vx stpcpy-c \
147e83
+		   strncpy strncpy-vx
147e83
 endif
147e83
 
147e83
 ifeq ($(subdir),wcsmbs)
147e83
 sysdep_routines += wcslen wcslen-vx wcslen-c \
147e83
 		   wcsnlen wcsnlen-vx wcsnlen-c \
147e83
 		   wcscpy wcscpy-vx wcscpy-c \
147e83
-		   wcpcpy wcpcpy-vx wcpcpy-c
147e83
+		   wcpcpy wcpcpy-vx wcpcpy-c \
147e83
+		   wcsncpy wcsncpy-vx wcsncpy-c
147e83
 endif
147e83
diff --git a/sysdeps/s390/multiarch/ifunc-impl-list.c b/sysdeps/s390/multiarch/ifunc-impl-list.c
147e83
index a402301..940421d 100644
147e83
--- a/sysdeps/s390/multiarch/ifunc-impl-list.c
147e83
+++ b/sysdeps/s390/multiarch/ifunc-impl-list.c
147e83
@@ -91,6 +91,9 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
147e83
   IFUNC_VX_IMPL (stpcpy);
147e83
   IFUNC_VX_IMPL (wcpcpy);
147e83
 
147e83
+  IFUNC_VX_IMPL (strncpy);
147e83
+  IFUNC_VX_IMPL (wcsncpy);
147e83
+
147e83
 #endif /* HAVE_S390_VX_ASM_SUPPORT */
147e83
 
147e83
   return i;
147e83
diff --git a/sysdeps/s390/multiarch/strncpy-vx.S b/sysdeps/s390/multiarch/strncpy-vx.S
147e83
new file mode 100644
147e83
index 0000000..c18b9b7
147e83
--- /dev/null
147e83
+++ b/sysdeps/s390/multiarch/strncpy-vx.S
147e83
@@ -0,0 +1,207 @@
147e83
+/* Vector optimized 32/64 bit S/390 version of strncpy.
147e83
+   Copyright (C) 2015 Free Software Foundation, Inc.
147e83
+   This file is part of the GNU C Library.
147e83
+
147e83
+   The GNU C Library is free software; you can redistribute it and/or
147e83
+   modify it under the terms of the GNU Lesser General Public
147e83
+   License as published by the Free Software Foundation; either
147e83
+   version 2.1 of the License, or (at your option) any later version.
147e83
+
147e83
+   The GNU C Library is distributed in the hope that it will be useful,
147e83
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
147e83
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
147e83
+   Lesser General Public License for more details.
147e83
+
147e83
+   You should have received a copy of the GNU Lesser General Public
147e83
+   License along with the GNU C Library; if not, see
147e83
+   <http://www.gnu.org/licenses/>.  */
147e83
+
147e83
+#if defined HAVE_S390_VX_ASM_SUPPORT && !defined NOT_IN_libc
147e83
+
147e83
+# include "sysdep.h"
147e83
+# include "asm-syntax.h"
147e83
+
147e83
+	.text
147e83
+
147e83
+/* char * strncpy (const char *dest, const char *src, size_t n)
147e83
+   Copy at most n characters of string  src to dest.
147e83
+
147e83
+   Register usage:
147e83
+   -r0=dest pointer for return
147e83
+   -r1=tmp, zero byte index
147e83
+   -r2=dest
147e83
+   -r3=src
147e83
+   -r4=n
147e83
+   -r5=current_len
147e83
+   -r6=tmp, loaded bytes
147e83
+   -r7=tmp, border
147e83
+   -v16=part of src
147e83
+   -v17=index of zero
147e83
+   -v18=part of src
147e83
+   -v31=register save area for r6, r7
147e83
+*/
147e83
+ENTRY(__strncpy_vx)
147e83
+	.machine "z13"
147e83
+	.machinemode "zarch_nohighgprs"
147e83
+
147e83
+# if !defined __s390x__
147e83
+	llgfr	%r4,%r4
147e83
+# endif /* !defined __s390x__ */
147e83
+
147e83
+	clgfi	%r4,0
147e83
+	ber	%r14		/* Nothing to do, if n == 0.  */
147e83
+	lgr	%r0,%r2		/* Save destination pointer for return.  */
147e83
+	vlvgp	%v31,%r6,%r7	/* Save registers.  */
147e83
+
147e83
+	vlbb	%v16,0(%r3),6	/* Load s until next 4k-byte boundary.  */
147e83
+	lcbb	%r6,0(%r3),6	/* Get bytes to 4k-byte boundary or 16.  */
147e83
+	llgfr	%r6,%r6		/* Convert 32bit to 64bit.  */
147e83
+
147e83
+	lghi	%r5,0		/* current_len = 0.  */
147e83
+
147e83
+	clgrjle	%r4,%r6,.Lremaining_v16 /* If n <= loaded-bytes
147e83
+					   -> process remaining.  */
147e83
+
147e83
+	/* n > loaded-byte-count.  */
147e83
+	vfenezb	%v17,%v16,%v16	/* Find element not equal with zero search.  */
147e83
+	vlgvb	%r1,%v17,7	/* Load zero index or 16 if not found.  */
147e83
+	clrjl	%r1,%r6,.Lfound_v16_store /* Found zero within loaded bytes,
147e83
+					     copy and return.  */
147e83
+
147e83
+	/* Align s to 16 byte.  */
147e83
+	risbgn	%r7,%r3,60,128+63,0 /* %r3 = bits 60-63 of %r2 'and' 15.  */
147e83
+	lghi	%r5,15		/* current_len = 15.  */
147e83
+	slr	%r5,%r7		/* Compute highest index to 16byte boundary.  */
147e83
+
147e83
+	/* Zero not found and n > loaded-byte-count.  */
147e83
+	vstl	%v16,%r5,0(%r2)	/* Copy loaded characters - no zero.  */
147e83
+	ahi	%r5,1		/* Start loop at next character.  */
147e83
+
147e83
+	/* Now we are 16byte aligned, so we can load
147e83
+	   a full vreg without page fault.  */
147e83
+	lgr	%r1,%r5		/* If %r5 + 64 < maxlen? -> loop64.  */
147e83
+	aghi	%r1,64
147e83
+	clgrjl	%r1,%r4,.Lloop64
147e83
+
147e83
+	vl	%v16,0(%r5,%r3)	/* Load s.  */
147e83
+	clgijl	%r4,17,.Lremaining_v16	/* If n <= 16, process remaining
147e83
+					   bytes.  */
147e83
+.Llt64:
147e83
+	lgr	%r7,%r4
147e83
+	slgfi	%r7,16		/* border_len = n - 16.  */
147e83
+
147e83
+	clgrjhe	%r5,%r7,.Lremaining_v16 /* If current_len >= border
147e83
+					   then process remaining bytes.  */
147e83
+	vfenezbs %v17,%v16,%v16	/* Find element not equal with zero search.  */
147e83
+	je	.Lfound_v16	/* Jump away if zero was found.  */
147e83
+	vl	%v18,16(%r5,%r3) /* Load next part of s.  */
147e83
+	vst	%v16,0(%r5,%r2)	/* Store previous part without zero to dst.  */
147e83
+	aghi	%r5,16
147e83
+
147e83
+	clgrjhe	%r5,%r7,.Lremaining_v18
147e83
+	vfenezbs %v17,%v18,%v18
147e83
+	je	.Lfound_v18
147e83
+	vl	%v16,16(%r5,%r3)
147e83
+	vst	%v18,0(%r5,%r2)
147e83
+	aghi	%r5,16
147e83
+
147e83
+	clgrjhe	%r5,%r7,.Lremaining_v16
147e83
+	vfenezbs %v17,%v16,%v16
147e83
+	je	.Lfound_v16
147e83
+	vl	%v18,16(%r5,%r3)
147e83
+	vst	%v16,0(%r5,%r2)
147e83
+	aghi	%r5,16
147e83
+
147e83
+.Lremaining_v18:
147e83
+	vlr	%v16,%v18
147e83
+.Lremaining_v16:
147e83
+	/* v16 contains the remaining bytes [1...16].
147e83
+	   Store remaining bytes and append string-termination.  */
147e83
+	vfenezb	%v17,%v16,%v16	/* Find element not equal with zero search.  */
147e83
+	slgrk	%r7,%r4,%r5	/* Remaining bytes = maxlen - current_len.  */
147e83
+	aghi	%r7,-1		/* vstl needs highest index.  */
147e83
+	la	%r2,0(%r5,%r2)	/* vstl has no index register.  */
147e83
+	vlgvb	%r1,%v17,7	/* Load zero index or 16 if not found.  */
147e83
+	/* Zero in remaining bytes? -> jump away (zero-index < max-index)
147e83
+	   Do not jump away if zero-index == max-index,
147e83
+	   but simply copy zero with vstl below.  */
147e83
+	clrjl	%r1,%r7,.Lfound_v16_store
147e83
+	vstl	%v16,%r7,0(%r2)	/* Store remaining bytes without null
147e83
+				   termination!.  */
147e83
+.Lend:
147e83
+	/* Restore saved registers.  */
147e83
+	vlgvg	%r6,%v31,0
147e83
+	vlgvg	%r7,%v31,1
147e83
+	lgr	%r2,%r0		/* Load saved dest-ptr.  */
147e83
+	br	%r14
147e83
+
147e83
+
147e83
+.Lfound_v16_32:
147e83
+	aghi	%r5,32
147e83
+	j	.Lfound_v16
147e83
+.Lfound_v18_48:
147e83
+	aghi	%r5,32
147e83
+.Lfound_v18_16:
147e83
+	aghi	%r5,16
147e83
+.Lfound_v18:
147e83
+	vlr	%v16,%v18
147e83
+.Lfound_v16:
147e83
+	/* v16 contains a zero. Store remaining bytes to zero. current_len
147e83
+	   has not reached border, thus checking for n is not needed! */
147e83
+	vlgvb	%r1,%v17,7	/* Load byte index of zero.  */
147e83
+	la	%r2,0(%r5,%r2)	/* vstl has no support for index-register.  */
147e83
+.Lfound_v16_store:
147e83
+	vstl	%v16,%r1,0(%r2)	/* Copy characters including zero.  */
147e83
+	/* Fill remaining bytes with zero - remaining count always > 0.  */
147e83
+	algr	%r5,%r1		/* Remaining bytes (=%r4) = ...  */
147e83
+	slgr	%r4,%r5		/* = n - (current_len + zero_index + 1).  */
147e83
+	la	%r2,0(%r1,%r2)	/* Pointer to zero. start filling beyond.  */
147e83
+	aghi	%r4,-2		/* mvc with exrl needs count - 1.
147e83
+				   (additional -1, see remaining bytes above) */
147e83
+	srlg	%r6,%r4,8	/* Split into 256 byte blocks.  */
147e83
+	ltgr	%r6,%r6
147e83
+	je	.Lzero_lt256
147e83
+.Lzero_loop256:
147e83
+	mvc	1(256,%r2),0(%r2) /* Fill 256 zeros at once.  */
147e83
+	la	%r2,256(%r2)
147e83
+	brctg	%r6,.Lzero_loop256 /* Loop until all blocks are processed.  */
147e83
+.Lzero_lt256:
147e83
+	exrl	%r4,.Lmvc_lt256
147e83
+	j	.Lend
147e83
+.Lmvc_lt256:
147e83
+	mvc	1(1,%r2),0(%r2)
147e83
+
147e83
+.Lloop64:
147e83
+	vl	%v16,0(%r5,%r3)	/* Load s.  */
147e83
+	vfenezbs %v17,%v16,%v16	/* Find element not equal with zero search.  */
147e83
+	je	.Lfound_v16	/* Jump away if zero was found.  */
147e83
+	vl	%v18,16(%r5,%r3) /* Load next part of s.  */
147e83
+	vst	%v16,0(%r5,%r2)	/* Store previous part without zero to dst.  */
147e83
+	vfenezbs %v17,%v18,%v18
147e83
+	je	.Lfound_v18_16
147e83
+	vl	%v16,32(%r5,%r3)
147e83
+	vst	%v18,16(%r5,%r2)
147e83
+	vfenezbs %v17,%v16,%v16
147e83
+	je	.Lfound_v16_32
147e83
+	vl	%v18,48(%r5,%r3)
147e83
+	vst	%v16,32(%r5,%r2)
147e83
+	vfenezbs %v17,%v18,%v18
147e83
+	je	.Lfound_v18_48
147e83
+	vst	%v18,48(%r5,%r2)
147e83
+
147e83
+	aghi	%r5,64
147e83
+	lgr	%r1,%r5		/* If %r5 + 64 < maxlen? -> loop64.  */
147e83
+	aghi	%r1,64
147e83
+	clgrjl	%r1,%r4,.Lloop64
147e83
+
147e83
+	vl	%v16,0(%r5,%r3)	/* Load s.  */
147e83
+	j	.Llt64
147e83
+END(__strncpy_vx)
147e83
+
147e83
+# define strncpy __strncpy_c
147e83
+# undef libc_hidden_builtin_def
147e83
+# define libc_hidden_builtin_def(name) strong_alias(__strncpy_c, __GI_strncpy)
147e83
+#endif /* HAVE_S390_VX_ASM_SUPPORT && !defined NOT_IN_libc */
147e83
+
147e83
+/* Include strncpy-implementation in s390-32/s390-64 subdirectory.  */
147e83
+#include <strncpy.S>
147e83
diff --git a/sysdeps/s390/multiarch/strncpy.c b/sysdeps/s390/multiarch/strncpy.c
147e83
new file mode 100644
147e83
index 0000000..0b0c308
147e83
--- /dev/null
147e83
+++ b/sysdeps/s390/multiarch/strncpy.c
147e83
@@ -0,0 +1,24 @@
147e83
+/* Multiple versions of strncpy.
147e83
+   Copyright (C) 2015 Free Software Foundation, Inc.
147e83
+   This file is part of the GNU C Library.
147e83
+
147e83
+   The GNU C Library is free software; you can redistribute it and/or
147e83
+   modify it under the terms of the GNU Lesser General Public
147e83
+   License as published by the Free Software Foundation; either
147e83
+   version 2.1 of the License, or (at your option) any later version.
147e83
+
147e83
+   The GNU C Library is distributed in the hope that it will be useful,
147e83
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
147e83
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
147e83
+   Lesser General Public License for more details.
147e83
+
147e83
+   You should have received a copy of the GNU Lesser General Public
147e83
+   License along with the GNU C Library; if not, see
147e83
+   <http://www.gnu.org/licenses/>.  */
147e83
+
147e83
+#if defined HAVE_S390_VX_ASM_SUPPORT && !defined NOT_IN_libc
147e83
+# include <string.h>
147e83
+# include <ifunc-resolve.h>
147e83
+
147e83
+s390_vx_libc_ifunc2 (__strncpy, strncpy)
147e83
+#endif
147e83
diff --git a/sysdeps/s390/multiarch/wcsncpy-c.c b/sysdeps/s390/multiarch/wcsncpy-c.c
147e83
new file mode 100644
147e83
index 0000000..a46e4ac
147e83
--- /dev/null
147e83
+++ b/sysdeps/s390/multiarch/wcsncpy-c.c
147e83
@@ -0,0 +1,25 @@
147e83
+/* Default wcsncpy implementation for S/390.
147e83
+   Copyright (C) 2015 Free Software Foundation, Inc.
147e83
+   This file is part of the GNU C Library.
147e83
+
147e83
+   The GNU C Library is free software; you can redistribute it and/or
147e83
+   modify it under the terms of the GNU Lesser General Public
147e83
+   License as published by the Free Software Foundation; either
147e83
+   version 2.1 of the License, or (at your option) any later version.
147e83
+
147e83
+   The GNU C Library is distributed in the hope that it will be useful,
147e83
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
147e83
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
147e83
+   Lesser General Public License for more details.
147e83
+
147e83
+   You should have received a copy of the GNU Lesser General Public
147e83
+   License along with the GNU C Library; if not, see
147e83
+   <http://www.gnu.org/licenses/>.  */
147e83
+
147e83
+#if defined HAVE_S390_VX_ASM_SUPPORT && !defined NOT_IN_libc
147e83
+# define WCSNCPY  __wcsncpy_c
147e83
+
147e83
+# include <wchar.h>
147e83
+extern __typeof (__wcsncpy) __wcsncpy_c;
147e83
+# include <wcsmbs/wcsncpy.c>
147e83
+#endif
147e83
diff --git a/sysdeps/s390/multiarch/wcsncpy-vx.S b/sysdeps/s390/multiarch/wcsncpy-vx.S
147e83
new file mode 100644
147e83
index 0000000..072a2d4
147e83
--- /dev/null
147e83
+++ b/sysdeps/s390/multiarch/wcsncpy-vx.S
147e83
@@ -0,0 +1,223 @@
147e83
+/* Vector optimized 32/64 bit S/390 version of wcsncpy.
147e83
+   Copyright (C) 2015 Free Software Foundation, Inc.
147e83
+   This file is part of the GNU C Library.
147e83
+
147e83
+   The GNU C Library is free software; you can redistribute it and/or
147e83
+   modify it under the terms of the GNU Lesser General Public
147e83
+   License as published by the Free Software Foundation; either
147e83
+   version 2.1 of the License, or (at your option) any later version.
147e83
+
147e83
+   The GNU C Library is distributed in the hope that it will be useful,
147e83
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
147e83
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
147e83
+   Lesser General Public License for more details.
147e83
+
147e83
+   You should have received a copy of the GNU Lesser General Public
147e83
+   License along with the GNU C Library; if not, see
147e83
+   <http://www.gnu.org/licenses/>.  */
147e83
+
147e83
+#if defined HAVE_S390_VX_ASM_SUPPORT && !defined NOT_IN_libc
147e83
+
147e83
+# include "sysdep.h"
147e83
+# include "asm-syntax.h"
147e83
+
147e83
+	.text
147e83
+
147e83
+/* wchar_t *wcsncpy (const wchar_t *dest, const wchar_t *src, size_t n)
147e83
+   Copy at most n characters of string  src to dest.
147e83
+
147e83
+   Register usage:
147e83
+   -r0=dest pointer for return
147e83
+   -r1=tmp, zero byte index
147e83
+   -r2=dest
147e83
+   -r3=src
147e83
+   -r4=n
147e83
+   -r5=current_len
147e83
+   -r6=tmp, loaded bytes
147e83
+   -r7=tmp, border
147e83
+   -v16=part of src
147e83
+   -v17=index of zero
147e83
+   -v18=part of src
147e83
+   -v31=register save area for r6, r7
147e83
+*/
147e83
+ENTRY(__wcsncpy_vx)
147e83
+	.machine "z13"
147e83
+	.machinemode "zarch_nohighgprs"
147e83
+
147e83
+# if !defined __s390x__
147e83
+	llgfr	%r4,%r4
147e83
+# endif /* !defined __s390x__ */
147e83
+
147e83
+	clgfi	%r4,0
147e83
+	ber	%r14		/* Nothing to do, if n == 0.  */
147e83
+
147e83
+	vlbb	%v16,0(%r3),6	/* Load s until next 4k-byte boundary.  */
147e83
+
147e83
+	tmll	%r3,3		/* Test if s is 4-byte aligned?  */
147e83
+	jne	.Lfallback	/* And use common-code variant if not.  */
147e83
+
147e83
+	vlvgp	%v31,%r6,%r7	/* Save registers.  */
147e83
+	lgr	%r0,%r2		/* Save destination pointer for return.  */
147e83
+
147e83
+	lcbb	%r6,0(%r3),6	/* Get bytes to 4k-byte boundary or 16.  */
147e83
+	llgfr	%r6,%r6		/* Convert 32bit to 64bit.  */
147e83
+
147e83
+	lghi	%r5,0		/* current_len = 0.  */
147e83
+
147e83
+	/* Check range of maxlen and convert to byte-count.  */
147e83
+# ifdef __s390x__
147e83
+	tmhh	%r4,49152	/* Test bit 0 or 1 of n.  */
147e83
+	lghi	%r1,-4		/* Max byte-count is 18446744073709551612.  */
147e83
+# else
147e83
+	tmlh	%r4,49152	/* Test bit 0 or 1 of n.  */
147e83
+	llilf	%r1,4294967292	/* Max byte-count is 4294967292.  */
147e83
+# endif /* !__s390x__ */
147e83
+	sllg	%r4,%r4,2	/* Convert character-count to byte-count.  */
147e83
+	locgrne	%r4,%r1		/* Use max byte-count, if bit 0/1 was one.  */
147e83
+
147e83
+	clgrjle	%r4,%r6,.Lremaining_v16 /* If n <= loaded-bytes
147e83
+					   -> process remaining.  */
147e83
+
147e83
+	/* n > loaded-byte-count.  */
147e83
+	vfenezf	%v17,%v16,%v16	/* Find element not equal with zero search.  */
147e83
+	vlgvb	%r1,%v17,7	/* Load zero index or 16 if not found.  */
147e83
+	aghi	%r1,3		/* Also copy remaining bytes of zero.  */
147e83
+	clrjl	%r1,%r6,.Lfound_v16_store /* Found zero within loaded bytes,
147e83
+					     copy and return.  */
147e83
+
147e83
+	/* Align s to 16 byte.  */
147e83
+	risbgn	%r7,%r3,60,128+63,0 /* %r3 = bits 60-63 of %r2 'and' 15.  */
147e83
+	lghi	%r5,15		/* current_len = 15.  */
147e83
+	slr	%r5,%r7		/* Compute highest index to 16byte boundary.  */
147e83
+
147e83
+	/* Zero not found and n > loaded-byte-count.  */
147e83
+	vstl	%v16,%r5,0(%r2)	/* Copy loaded characters - no zero.  */
147e83
+	ahi	%r5,1		/* Start loop at next character.  */
147e83
+
147e83
+	/* Now we are 16byte aligned, so we can load
147e83
+	   a full vreg without page fault.  */
147e83
+	lgr	%r1,%r5		/* If %r5 + 64 < maxlen? -> loop64.  */
147e83
+	aghi	%r1,64
147e83
+	clgrjl	%r1,%r4,.Lloop64
147e83
+
147e83
+	vl	%v16,0(%r5,%r3)	/* Load s.  */
147e83
+	clgijl	%r4,17,.Lremaining_v16	/* If n <=16, process remaining
147e83
+					   bytes.  */
147e83
+.Llt64:
147e83
+	lgr	%r7,%r4
147e83
+	slgfi	%r7,16		/* border_len = maxlen - 16.  */
147e83
+
147e83
+	clgrjhe	%r5,%r7,.Lremaining_v16 /* If current_len >= border
147e83
+					       then process remaining bytes.  */
147e83
+	vfenezfs %v17,%v16,%v16	/* Find element not equal with zero search.  */
147e83
+	je	.Lfound_v16	/* Jump away if zero was found.  */
147e83
+	vl	%v18,16(%r5,%r3) /* Load next part of s.  */
147e83
+	vst	%v16,0(%r5,%r2)	/* Store previous part without zero to dst.  */
147e83
+	aghi	%r5,16
147e83
+
147e83
+	clgrjhe	%r5,%r7,.Lremaining_v18
147e83
+	vfenezfs %v17,%v18,%v18
147e83
+	je	.Lfound_v18
147e83
+	vl	%v16,16(%r5,%r3)
147e83
+	vst	%v18,0(%r5,%r2)
147e83
+	aghi	%r5,16
147e83
+
147e83
+	clgrjhe	%r5,%r7,.Lremaining_v16
147e83
+	vfenezfs %v17,%v16,%v16
147e83
+	je	.Lfound_v16
147e83
+	vl	%v18,16(%r5,%r3)
147e83
+	vst	%v16,0(%r5,%r2)
147e83
+	aghi	%r5,16
147e83
+
147e83
+.Lremaining_v18:
147e83
+	vlr	%v16,%v18
147e83
+.Lremaining_v16:
147e83
+	/* v16 contains the remaining bytes [1...16].
147e83
+	   Store remaining bytes and append string-termination.  */
147e83
+	vfenezf	%v17,%v16,%v16	/* Find element not equal with zero search.  */
147e83
+	slgrk	%r7,%r4,%r5	/* Remaining bytes = maxlen - current_len.  */
147e83
+	aghi	%r7,-1		/* vstl needs highest index.  */
147e83
+	la	%r2,0(%r5,%r2)	/* vstl has no index register.  */
147e83
+	vlgvb	%r1,%v17,7	/* Load zero index or 16 if not found.  */
147e83
+	aghi	%r1,3		/* Also copy remaining bytes of zero.  */
147e83
+	/* Zero in remaining bytes? -> jump away (zero-index < max-index)
147e83
+	   Do not jump away if zero-index == max-index,
147e83
+	   but simply copy zero with vstl below.  */
147e83
+	clrjl	%r1,%r7,.Lfound_v16_store
147e83
+	vstl	%v16,%r7,0(%r2)	/* Store remaining bytes without null
147e83
+				   termination!.  */
147e83
+.Lend:
147e83
+	/* Restore saved registers.  */
147e83
+	vlgvg	%r6,%v31,0
147e83
+	vlgvg	%r7,%v31,1
147e83
+	lgr	%r2,%r0		/* Load saved dest-ptr.  */
147e83
+	br	%r14
147e83
+
147e83
+.Lfound_v16_32:
147e83
+	aghi	%r5,32
147e83
+	j	.Lfound_v16
147e83
+.Lfound_v18_48:
147e83
+	aghi	%r5,32
147e83
+.Lfound_v18_16:
147e83
+	aghi	%r5,16
147e83
+.Lfound_v18:
147e83
+	vlr	%v16,%v18
147e83
+.Lfound_v16:
147e83
+	/* v16 contains a zero. Store remaining bytes to zero. current_len
147e83
+	   has not reached border, thus checking for n is not needed! */
147e83
+	vlgvb	%r1,%v17,7	/* Load byte index of zero.  */
147e83
+	la	%r2,0(%r5,%r2)	/* vstl has no support for index-register.  */
147e83
+	aghi	%r1,3		/* Also copy remaining bytes of zero.  */
147e83
+.Lfound_v16_store:
147e83
+	vstl	%v16,%r1,0(%r2)	/* Copy characters including zero.  */
147e83
+	/* Fill remaining bytes with zero - remaining count always > 0.  */
147e83
+	algr	%r5,%r1		/* Remaining bytes (=%r4) = ...  */
147e83
+	slgr	%r4,%r5		/* = maxlen - (currlen + zero_index + 1).  */
147e83
+	la	%r2,0(%r1,%r2)	/* Pointer to zero. start filling beyond.  */
147e83
+	aghi	%r4,-2		/* mvc with exrl needs count - 1.
147e83
+				   (additional -1, see remaining bytes above) */
147e83
+	srlg	%r6,%r4,8	/* Split into 256 byte blocks.  */
147e83
+	ltgr	%r6,%r6
147e83
+	je	.Lzero_lt256
147e83
+.Lzero_loop256:
147e83
+	mvc	1(256,%r2),0(%r2) /* Fill 256 zeros at once.  */
147e83
+	la	%r2,256(%r2)
147e83
+	brctg	%r6,.Lzero_loop256 /* Loop until all blocks are processed.  */
147e83
+.Lzero_lt256:
147e83
+	exrl	%r4,.Lmvc_lt256
147e83
+	j	.Lend
147e83
+.Lmvc_lt256:
147e83
+	mvc	1(1,%r2),0(%r2)
147e83
+
147e83
+	/* Find zero in 16byte aligned loop.  */
147e83
+.Lloop64:
147e83
+	vl	%v16,0(%r5,%r3) /* Load s.  */
147e83
+	vfenezfs %v17,%v16,%v16	/* Find element not equal with zero search.  */
147e83
+	je	.Lfound_v16	/* Jump away if zero was found.  */
147e83
+	vl	%v18,16(%r5,%r3) /* Load next part of s.  */
147e83
+	vst	%v16,0(%r5,%r2)	/* Store previous part without zero to dst.  */
147e83
+	vfenezfs %v17,%v18,%v18
147e83
+	je	.Lfound_v18_16
147e83
+	vl	%v16,32(%r5,%r3)
147e83
+	vst	%v18,16(%r5,%r2)
147e83
+	vfenezfs %v17,%v16,%v16
147e83
+	je	.Lfound_v16_32
147e83
+	vl	%v18,48(%r5,%r3)
147e83
+	vst	%v16,32(%r5,%r2)
147e83
+	vfenezfs %v17,%v18,%v18
147e83
+	je	.Lfound_v18_48
147e83
+	vst	%v18,48(%r5,%r2)
147e83
+
147e83
+	aghi	%r5,64
147e83
+	lgr	%r1,%r5		/* If %r5 + 64 < maxlen? -> loop64.  */
147e83
+	aghi	%r1,64
147e83
+	clgrjl	%r1,%r4,.Lloop64
147e83
+
147e83
+	vl	%v16,0(%r5,%r3)	/* Load s.  */
147e83
+	j	.Llt64
147e83
+
147e83
+.Lfallback:
147e83
+	jg	__wcsncpy_c
147e83
+END(__wcsncpy_vx)
147e83
+
147e83
+#endif /* HAVE_S390_VX_ASM_SUPPORT && !defined NOT_IN_libc */
147e83
diff --git a/sysdeps/s390/multiarch/wcsncpy.c b/sysdeps/s390/multiarch/wcsncpy.c
147e83
new file mode 100644
147e83
index 0000000..99aa344
147e83
--- /dev/null
147e83
+++ b/sysdeps/s390/multiarch/wcsncpy.c
147e83
@@ -0,0 +1,28 @@
147e83
+/* Multiple versions of wcsncpy.
147e83
+   Copyright (C) 2015 Free Software Foundation, Inc.
147e83
+   This file is part of the GNU C Library.
147e83
+
147e83
+   The GNU C Library is free software; you can redistribute it and/or
147e83
+   modify it under the terms of the GNU Lesser General Public
147e83
+   License as published by the Free Software Foundation; either
147e83
+   version 2.1 of the License, or (at your option) any later version.
147e83
+
147e83
+   The GNU C Library is distributed in the hope that it will be useful,
147e83
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
147e83
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
147e83
+   Lesser General Public License for more details.
147e83
+
147e83
+   You should have received a copy of the GNU Lesser General Public
147e83
+   License along with the GNU C Library; if not, see
147e83
+   <http://www.gnu.org/licenses/>.  */
147e83
+
147e83
+#if defined HAVE_S390_VX_ASM_SUPPORT && !defined NOT_IN_libc
147e83
+# include <wchar.h>
147e83
+# include <ifunc-resolve.h>
147e83
+
147e83
+s390_vx_libc_ifunc (__wcsncpy)
147e83
+weak_alias (__wcsncpy, wcsncpy)
147e83
+
147e83
+#else
147e83
+# include <wcsmbs/wcsncpy.c>
147e83
+#endif /* !(defined HAVE_S390_VX_ASM_SUPPORT && !defined NOT_IN_libc) */
147e83
diff --git a/sysdeps/s390/s390-32/multiarch/strncpy.c b/sysdeps/s390/s390-32/multiarch/strncpy.c
147e83
new file mode 100644
147e83
index 0000000..bc9c336
147e83
--- /dev/null
147e83
+++ b/sysdeps/s390/s390-32/multiarch/strncpy.c
147e83
@@ -0,0 +1,21 @@
147e83
+/* Multiple versions of strncpy.
147e83
+   Copyright (C) 2015 Free Software Foundation, Inc.
147e83
+   This file is part of the GNU C Library.
147e83
+
147e83
+   The GNU C Library is free software; you can redistribute it and/or
147e83
+   modify it under the terms of the GNU Lesser General Public
147e83
+   License as published by the Free Software Foundation; either
147e83
+   version 2.1 of the License, or (at your option) any later version.
147e83
+
147e83
+   The GNU C Library is distributed in the hope that it will be useful,
147e83
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
147e83
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
147e83
+   Lesser General Public License for more details.
147e83
+
147e83
+   You should have received a copy of the GNU Lesser General Public
147e83
+   License along with the GNU C Library; if not, see
147e83
+   <http://www.gnu.org/licenses/>.  */
147e83
+
147e83
+/* This wrapper-file is needed, because otherwise file
147e83
+   sysdeps/s390/s390-[32|64]/strncpy.S will be used.  */
147e83
+#include <sysdeps/s390/multiarch/strncpy.c>
147e83
diff --git a/sysdeps/s390/s390-64/multiarch/strncpy.c b/sysdeps/s390/s390-64/multiarch/strncpy.c
147e83
new file mode 100644
147e83
index 0000000..bc9c336
147e83
--- /dev/null
147e83
+++ b/sysdeps/s390/s390-64/multiarch/strncpy.c
147e83
@@ -0,0 +1,21 @@
147e83
+/* Multiple versions of strncpy.
147e83
+   Copyright (C) 2015 Free Software Foundation, Inc.
147e83
+   This file is part of the GNU C Library.
147e83
+
147e83
+   The GNU C Library is free software; you can redistribute it and/or
147e83
+   modify it under the terms of the GNU Lesser General Public
147e83
+   License as published by the Free Software Foundation; either
147e83
+   version 2.1 of the License, or (at your option) any later version.
147e83
+
147e83
+   The GNU C Library is distributed in the hope that it will be useful,
147e83
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
147e83
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
147e83
+   Lesser General Public License for more details.
147e83
+
147e83
+   You should have received a copy of the GNU Lesser General Public
147e83
+   License along with the GNU C Library; if not, see
147e83
+   <http://www.gnu.org/licenses/>.  */
147e83
+
147e83
+/* This wrapper-file is needed, because otherwise file
147e83
+   sysdeps/s390/s390-[32|64]/strncpy.S will be used.  */
147e83
+#include <sysdeps/s390/multiarch/strncpy.c>
147e83
diff --git a/wcsmbs/Makefile b/wcsmbs/Makefile
147e83
index 3b91b6d..c26ab0c 100644
147e83
--- a/wcsmbs/Makefile
147e83
+++ b/wcsmbs/Makefile
147e83
@@ -42,7 +42,7 @@ routines := wcscat wcschr wcscmp wcscpy wcscspn wcsdup wcslen wcsncat \
147e83
 	    mbrtoc16 c16rtomb
147e83
 
147e83
 strop-tests :=  wcscmp wmemcmp wcslen wcschr wcsrchr wcscpy wcsnlen \
147e83
-		wcpcpy
147e83
+		wcpcpy wcsncpy
147e83
 tests := tst-wcstof wcsmbs-tst1 tst-wcsnlen tst-btowc tst-mbrtowc \
147e83
 	 tst-wcrtomb tst-wcpncpy tst-mbsrtowcs tst-wchar-h tst-mbrtowc2 \
147e83
 	 tst-c16c32-1 wcsatcliff $(addprefix test-,$(strop-tests))
147e83
diff --git a/wcsmbs/test-wcsncpy-ifunc.c b/wcsmbs/test-wcsncpy-ifunc.c
147e83
new file mode 100644
147e83
index 0000000..0141b38
147e83
--- /dev/null
147e83
+++ b/wcsmbs/test-wcsncpy-ifunc.c
147e83
@@ -0,0 +1,20 @@
147e83
+/* Test and measure IFUNC implementations of wcsncpy function.
147e83
+   Copyright (C) 2015 Free Software Foundation, Inc.
147e83
+   This file is part of the GNU C Library.
147e83
+
147e83
+   The GNU C Library is free software; you can redistribute it and/or
147e83
+   modify it under the terms of the GNU Lesser General Public
147e83
+   License as published by the Free Software Foundation; either
147e83
+   version 2.1 of the License, or (at your option) any later version.
147e83
+
147e83
+   The GNU C Library is distributed in the hope that it will be useful,
147e83
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
147e83
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
147e83
+   Lesser General Public License for more details.
147e83
+
147e83
+   You should have received a copy of the GNU Lesser General Public
147e83
+   License along with the GNU C Library; if not, see
147e83
+   <http://www.gnu.org/licenses/>.  */
147e83
+
147e83
+#define TEST_IFUNC 1
147e83
+#include "test-wcsncpy.c"
147e83
diff --git a/wcsmbs/test-wcsncpy.c b/wcsmbs/test-wcsncpy.c
147e83
new file mode 100644
147e83
index 0000000..27de6f8
147e83
--- /dev/null
147e83
+++ b/wcsmbs/test-wcsncpy.c
147e83
@@ -0,0 +1,20 @@
147e83
+/* Test wcsncpy functions.
147e83
+   Copyright (C) 2015 Free Software Foundation, Inc.
147e83
+   This file is part of the GNU C Library.
147e83
+
147e83
+   The GNU C Library is free software; you can redistribute it and/or
147e83
+   modify it under the terms of the GNU Lesser General Public
147e83
+   License as published by the Free Software Foundation; either
147e83
+   version 2.1 of the License, or (at your option) any later version.
147e83
+
147e83
+   The GNU C Library is distributed in the hope that it will be useful,
147e83
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
147e83
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
147e83
+   Lesser General Public License for more details.
147e83
+
147e83
+   You should have received a copy of the GNU Lesser General Public
147e83
+   License along with the GNU C Library; if not, see
147e83
+   <http://www.gnu.org/licenses/>.  */
147e83
+
147e83
+#define WIDE 1
147e83
+#include "../string/test-strncpy.c"
147e83
diff --git a/wcsmbs/wcsncpy.c b/wcsmbs/wcsncpy.c
147e83
index f134c36..2e3cebe 100644
147e83
--- a/wcsmbs/wcsncpy.c
147e83
+++ b/wcsmbs/wcsncpy.c
147e83
@@ -18,6 +18,9 @@
147e83
 
147e83
 #include <wchar.h>
147e83
 
147e83
+#ifdef WCSNCPY
147e83
+# define __wcsncpy WCSNCPY
147e83
+#endif
147e83
 
147e83
 /* Copy no more than N wide-characters of SRC to DEST.	*/
147e83
 wchar_t *
147e83
@@ -83,4 +86,6 @@ __wcsncpy (dest, src, n)
147e83
 
147e83
   return s;
147e83
 }
147e83
+#ifndef WCSNCPY
147e83
 weak_alias (__wcsncpy, wcsncpy)
147e83
+#endif
147e83
-- 
147e83
2.3.0
147e83