[7/7] wcsmbs: optimize wcsnlen

Message ID 20190206145850.22003-7-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 wcsnlen using wmemchr.  The generic wmemchr
already uses the strategy (loop unrolling and tail handling) and
by using it it allows architectures that have optimized wmemchr
(s390 and x86_64) to optimize wcsnlen as well.

Checked on x86_64-linux-gnu.

	* wcsmbs/wcsnlen.c (__wcsnlen): Rewrite using wmemchr.
---
 wcsmbs/wcsnlen.c | 22 ++++------------------
 1 file changed, 4 insertions(+), 18 deletions(-)

Patch

diff --git a/wcsmbs/wcsnlen.c b/wcsmbs/wcsnlen.c
index f3d5fc1fba..d38c7e4953 100644
--- a/wcsmbs/wcsnlen.c
+++ b/wcsmbs/wcsnlen.c
@@ -26,24 +26,10 @@ 
 size_t
 __wcsnlen (const wchar_t *s, size_t maxlen)
 {
-  size_t len = 0;
-
-  while (maxlen > 0 && s[len] != L'\0')
-    {
-      ++len;
-      if (--maxlen == 0 || s[len] == L'\0')
-	return len;
-      ++len;
-      if (--maxlen == 0 || s[len] == L'\0')
-	return len;
-      ++len;
-      if (--maxlen == 0 || s[len] == L'\0')
-	return len;
-      ++len;
-      --maxlen;
-    }
-
-  return len;
+  const wchar_t *ret = __wmemchr (s, L'\0', maxlen);
+  if (ret)
+    maxlen = ret - s;
+  return maxlen;
 }
 #ifndef WCSNLEN
 weak_alias (__wcsnlen, wcsnlen)