diff mbox series

[5/7] wcsmbs: optimize wcsncat

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

Commit Message

Adhemerval Zanella Feb. 6, 2019, 2:58 p.m. UTC
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.
---
 wcsmbs/wcsncat.c | 53 +++++++-----------------------------------------
 1 file changed, 7 insertions(+), 46 deletions(-)

-- 
2.17.1

Comments

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

On 06/02/2019 12:58, Adhemerval Zanella wrote:
> 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.

> ---

>  wcsmbs/wcsncat.c | 53 +++++++-----------------------------------------

>  1 file changed, 7 insertions(+), 46 deletions(-)

> 

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

> index 65e9b226c6..cb6fe71e19 100644

> --- a/wcsmbs/wcsncat.c

> +++ b/wcsmbs/wcsncat.c

> @@ -26,54 +26,15 @@

>  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;

>  }

>
diff mbox series

Patch

diff --git a/wcsmbs/wcsncat.c b/wcsmbs/wcsncat.c
index 65e9b226c6..cb6fe71e19 100644
--- a/wcsmbs/wcsncat.c
+++ b/wcsmbs/wcsncat.c
@@ -26,54 +26,15 @@ 
 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;
 }