[PATCH 5/6] newlib: str[n]cat: optimize skipping of the destination string

Alexey Lapshin <[email protected]>
Newsgroups gmane.comp.lib.newlib
Message-ID <[email protected]>
Prepare pointer to be aligned and than use word-size iterator on aligned
memory.
---
 newlib/libc/string/strcat.c  | 18 +++++++++---------
 newlib/libc/string/strncat.c | 17 +++++++++--------
 2 files changed, 18 insertions(+), 17 deletions(-)

diff --git a/newlib/libc/string/strcat.c b/newlib/libc/string/strcat.c
index 71dd1db75..47c53a5d2 100644
--- a/newlib/libc/string/strcat.c
+++ b/newlib/libc/string/strcat.c
@@ -50,17 +50,17 @@ strcat (char *__restrict s1,
 #else
   char *s = s1;
 
+  /* Skip unaligned memory in s1.  */
+  while (UNALIGNED_X(s1) && *s1)
+    s1++;
 
-  /* Skip over the data in s1 as quickly as possible.  */
-  if (!UNALIGNED_X(s1))
-    {
-      unsigned long *aligned_s1 = (unsigned long *)s1;
-      while (!DETECT_NULL(*aligned_s1))
-	aligned_s1++;
-
-      s1 = (char *)aligned_s1;
-    }
+  /* Skip over the aligned data in s1 as quickly as possible.  */
+  unsigned long *aligned_s1 = (unsigned long *)s1;
+  while (!DETECT_NULL(*aligned_s1))
+    aligned_s1++;
+  s1 = (char *)aligned_s1;
 
+  /* Find string terminator.  */
   while (*s1)
     s1++;
 
diff --git a/newlib/libc/string/strncat.c b/newlib/libc/string/strncat.c
index 01f20f681..fc9fe5b1c 100644
--- a/newlib/libc/string/strncat.c
+++ b/newlib/libc/string/strncat.c
@@ -59,16 +59,17 @@ strncat (char *__restrict s1,
 #else
   char *s = s1;
 
-  /* Skip over the data in s1 as quickly as possible.  */
-  if (!UNALIGNED_X(s1))
-    {
-      unsigned long *aligned_s1 = (unsigned long *)s1;
-      while (!DETECT_NULL(*aligned_s1))
-	aligned_s1++;
+  /* Skip unaligned memory in s1.  */
+  while (UNALIGNED_X(s1) && *s1)
+    s1++;
 
-      s1 = (char *)aligned_s1;
-    }
+  /* Skip over the aligned data in s1 as quickly as possible.  */
+  unsigned long *aligned_s1 = (unsigned long *)s1;
+  while (!DETECT_NULL(*aligned_s1))
+    aligned_s1++;
+  s1 = (char *)aligned_s1;
 
+  /* Find string terminator.  */
   while (*s1)
     s1++;
 
-- 
2.43.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.