]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
string: Improve generic strrchr with memrchr and strlen
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Thu, 2 Feb 2023 16:44:13 +0000 (13:44 -0300)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Mon, 6 Feb 2023 19:19:35 +0000 (16:19 -0300)
Now that both strlen and memrchr have word vectorized implementation,
it should be faster to implement strrchr based on memrchr over the
string length instead of calling strchr on a loop.

Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc-linux-gnu,
and powerpc64-linux-gnu by removing the arch-specific assembly
implementation and disabling multi-arch (it covers both LE and BE
for 64 and 32 bits).

string/strrchr.c

index 3c6e715d3b4b9759e2b13d7cd98248131c6ac11a..7b76dea4e0586c74455227fd6d43e43bae867d09 100644 (file)
 char *
 STRRCHR (const char *s, int c)
 {
-  const char *found, *p;
-
-  c = (unsigned char) c;
-
-  /* Since strchr is fast, we use it rather than the obvious loop.  */
-
-  if (c == '\0')
-    return strchr (s, '\0');
-
-  found = NULL;
-  while ((p = strchr (s, c)) != NULL)
-    {
-      found = p;
-      s = p + 1;
-    }
-
-  return (char *) found;
+  return __memrchr (s, c, strlen (s) + 1);
 }
 
 #ifdef weak_alias