]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
wcsmbs: optimize wcsncat
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Tue, 5 Feb 2019 20:43:18 +0000 (18:43 -0200)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Wed, 27 Feb 2019 13:00:37 +0000 (10:00 -0300)
This patch rewrites wcsncat using wcslen, wcsnlen, and wmemcpy.  This is
similar to the optimization done on strncat by 3eb38795db and e80514b5a8.

Checked on x86_64-linux-gnu.

* wcsmbs/wcsncat.c (wcsncat): Rewrite using wcslen, wcsnlen, and
wmemcpy.

ChangeLog
wcsmbs/wcsncat.c

index 24062754616503c58a58275cb8a41389925f83f4..d51188cda98a69be1a72fc3a81450a7f9622f27a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,8 @@
 2019-02-27  Adhemerval Zanella  <adhemerval.zanella@linaro.org>
 
+       * wcsmbs/wcsncat.c (wcsncat): Rewrite using wcslen, wcsnlen, and
+       wmemcpy.
+
        * wcsmbs/wcscpy.c (__wcpcpy): Rewrite using wcslen and wmemcpy.
 
        * include/wchar.h (__wcscpy): New prototype.
index 65e9b226c6872fa8b25b9b31b9e7804d5d1a45fd..cb6fe71e194cb87c29c0a8f9c9672856a29a1415 100644 (file)
 wchar_t *
 WCSNCAT (wchar_t *dest, const wchar_t *src, size_t n)
 {
-  wchar_t c;
-  wchar_t * const s = dest;
+  wchar_t *ret = dest;
 
-  /* Find the end of DEST.  */
-  do
-    c = *dest++;
-  while (c != L'\0');
+  /* Find the end of dest.  */
+  dest += __wcslen (dest);
 
-  /* Make DEST point before next character, so we can increment
-     it while memory is read (wins on pipelined cpus). */
-  dest -= 2;
+  size_t ds = __wcsnlen (src, n);
 
-  if (n >= 4)
-    {
-      size_t n4 = n >> 2;
-      do
-       {
-         c = *src++;
-         *++dest = c;
-         if (c == L'\0')
-           return s;
-         c = *src++;
-         *++dest = c;
-         if (c == L'\0')
-           return s;
-         c = *src++;
-         *++dest = c;
-         if (c == L'\0')
-           return s;
-         c = *src++;
-         *++dest = c;
-         if (c == L'\0')
-           return s;
-       } while (--n4 > 0);
-      n &= 3;
-    }
+  dest[ds] = L'\0';
+  __wmemcpy (dest, src, ds);
 
-  while (n > 0)
-    {
-      c = *src++;
-      *++dest = c;
-      if (c == L'\0')
-       return s;
-      n--;
-    }
-
-  if (c != L'\0')
-    *++dest = L'\0';
-
-  return s;
+  return ret;
 }