[2/7] wcsmbs: optimize wcpncpy

Message ID 20190206145850.22003-2-adhemerval.zanella@linaro.org
State New
Headers show
Series
  • [1/7] wcsmbs: optimize wcpcpy
Related show

Commit Message

Adhemerval Zanella Feb. 6, 2019, 2:58 p.m.
This patch rewrites wcpncpy using wcslen, wmemcpy, and wmemset.  This is
similar to the optimization done on stpncpy by 48497aba8e.

Checked on x86_64-linux-gnu.

        * wcsmbs/wcpncpy.c (__wcpcpy): Rewrite using wcslen, wmemcpy, and
	wmemset.
---
 wcsmbs/wcpncpy.c | 58 +++++-------------------------------------------
 1 file changed, 6 insertions(+), 52 deletions(-)

Patch

diff --git a/wcsmbs/wcpncpy.c b/wcsmbs/wcpncpy.c
index 7568f3b11c..7147affdd5 100644
--- a/wcsmbs/wcpncpy.c
+++ b/wcsmbs/wcpncpy.c
@@ -17,6 +17,7 @@ 
    <http://www.gnu.org/licenses/>.  */
 
 #include <wchar.h>
+#include <string.h>
 
 #ifdef WCPNCPY
 # define __wcpncpy WCPNCPY
@@ -27,59 +28,12 @@ 
 wchar_t *
 __wcpncpy (wchar_t *dest, const wchar_t *src, size_t n)
 {
-  wint_t c;
-  wchar_t *const s = dest;
-
-  if (n >= 4)
-    {
-      size_t n4 = n >> 2;
-
-      for (;;)
-	{
-	  c = *src++;
-	  *dest++ = c;
-	  if (c == L'\0')
-	    break;
-	  c = *src++;
-	  *dest++ = c;
-	  if (c == L'\0')
-	    break;
-	  c = *src++;
-	  *dest++ = c;
-	  if (c == L'\0')
-	    break;
-	  c = *src++;
-	  *dest++ = c;
-	  if (c == L'\0')
-	    break;
-	  if (--n4 == 0)
-	    goto last_chars;
-	}
-      n -= dest - s;
-      goto zero_fill;
-    }
-
- last_chars:
-  n &= 3;
-  if (n == 0)
+  size_t size = __wcsnlen (src, n);
+  __wmemcpy (dest, src, size);
+  dest += size;
+  if (size == n)
     return dest;
-
-  for (;;)
-    {
-      c = *src++;
-      --n;
-      *dest++ = c;
-      if (c == L'\0')
-	break;
-      if (n == 0)
-	return dest;
-    }
-
- zero_fill:
-  while (n-- > 0)
-    dest[n] = L'\0';
-
-  return dest - 1;
+  return wmemset (dest, L'\0', (n - size));
 }
 
 #ifndef WCPNCPY