[6/7] wcsmbs: optimize wcsncpy

Message ID 20190206145850.22003-6-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 wcsncpy using wcsnlen, wmemset, and wmemcpy.  This is
similar to the optimization done on strncpy by f6482cf29d and 6423d4754c.

Checked on x86_64-linux-gnu.

	* wcsmbs/wcsncpy.c (__wcsncpy): Rewrite using wcsnlen, wmemset, and
	wmemcpy.
---
 wcsmbs/wcsncpy.c | 60 ++++--------------------------------------------
 1 file changed, 4 insertions(+), 56 deletions(-)

-- 
2.17.1

Comments

Adhemerval Zanella Feb. 26, 2019, 6:12 p.m. | #1
If no one opposes it, I will commit this shortly.

On 06/02/2019 12:58, Adhemerval Zanella wrote:
> This patch rewrites wcsncpy using wcsnlen, wmemset, and wmemcpy.  This is

> similar to the optimization done on strncpy by f6482cf29d and 6423d4754c.

> 

> Checked on x86_64-linux-gnu.

> 

> 	* wcsmbs/wcsncpy.c (__wcsncpy): Rewrite using wcsnlen, wmemset, and

> 	wmemcpy.

> ---

>  wcsmbs/wcsncpy.c | 60 ++++--------------------------------------------

>  1 file changed, 4 insertions(+), 56 deletions(-)

> 

> diff --git a/wcsmbs/wcsncpy.c b/wcsmbs/wcsncpy.c

> index 2fd523c815..d5d7f4ad86 100644

> --- a/wcsmbs/wcsncpy.c

> +++ b/wcsmbs/wcsncpy.c

> @@ -26,62 +26,10 @@

>  wchar_t *

>  __wcsncpy (wchar_t *dest, const wchar_t *src, size_t n)

>  {

> -  wint_t c;

> -  wchar_t *const s = dest;

> -

> -  --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 = n - (dest - s) - 1;

> -      if (n == 0)

> -	return s;

> -      goto zero_fill;

> -    }

> -

> - last_chars:

> -  n &= 3;

> -  if (n == 0)

> -    return s;

> -

> -  do

> -    {

> -      c = *src++;

> -      *++dest = c;

> -      if (--n == 0)

> -	return s;

> -    }

> -  while (c != L'\0');

> -

> - zero_fill:

> -  do

> -    *++dest = L'\0';

> -  while (--n > 0);

> -

> -  return s;

> +  size_t size = __wcsnlen (src, n);

> +  if (size != n)

> +    __wmemset (dest + size, L'\0', n - size);

> +  return __wmemcpy (dest, src, size);

>  }

>  #ifndef WCSNCPY

>  weak_alias (__wcsncpy, wcsncpy)

>

Patch

diff --git a/wcsmbs/wcsncpy.c b/wcsmbs/wcsncpy.c
index 2fd523c815..d5d7f4ad86 100644
--- a/wcsmbs/wcsncpy.c
+++ b/wcsmbs/wcsncpy.c
@@ -26,62 +26,10 @@ 
 wchar_t *
 __wcsncpy (wchar_t *dest, const wchar_t *src, size_t n)
 {
-  wint_t c;
-  wchar_t *const s = dest;
-
-  --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 = n - (dest - s) - 1;
-      if (n == 0)
-	return s;
-      goto zero_fill;
-    }
-
- last_chars:
-  n &= 3;
-  if (n == 0)
-    return s;
-
-  do
-    {
-      c = *src++;
-      *++dest = c;
-      if (--n == 0)
-	return s;
-    }
-  while (c != L'\0');
-
- zero_fill:
-  do
-    *++dest = L'\0';
-  while (--n > 0);
-
-  return s;
+  size_t size = __wcsnlen (src, n);
+  if (size != n)
+    __wmemset (dest + size, L'\0', n - size);
+  return __wmemcpy (dest, src, size);
 }
 #ifndef WCSNCPY
 weak_alias (__wcsncpy, wcsncpy)