strcasestr: check if ne[0] is in hs with strchr or strpbrk as does strstr
Checks
Commit Message
---
string/strcasestr.c | 31 +++++++++++++++++++++++++++++++
1 file changed, 31 insertions(+)
Comments
On 14/10/23 05:32, James Tirta Halim wrote:
> ---
> string/strcasestr.c | 31 +++++++++++++++++++++++++++++++
> 1 file changed, 31 insertions(+)
>
> diff --git a/string/strcasestr.c b/string/strcasestr.c
> index 2f6b4f8641..aca41211dd 100644
> --- a/string/strcasestr.c
> +++ b/string/strcasestr.c
> @@ -55,6 +55,30 @@
> #endif
>
>
> +static inline char *__attribute__ ((always_inline))
> +strcasechr (const char *s, char c)
> +{
> + if (isalpha(c)) {
> + /* May have optimized strcspn? */
> +#if defined __sparc__ || defined __sparc || defined __x86_64__ || defined _M_X64 || defined __s390x__ || defined i386 || defined __i386__ || defined __i386 || defined _M_IX86 || defined __PPC64__ || defined __ppc64__ || defined _ARCH_PPC64 || _ARCH_PWR8
> + const char a[] = {tolower(c), toupper(c), '\0'};
We have the sysdep folder exactly to provide such macros if requires, for instance
the file sysdeps/generic/string-fzi.h.
So if this optimization is really worth, the best way to provide would be through
a generic implementation like:
static __always_inline char *
strcasechr (const char *s, char c)
{
/* Generic implementation. */
}
And then on each architecture where using strcspn is better to add a string-xxx.h
override.
> + s = (char *)strcspn(s, a);
> +#else
> + c = tolower(c);
> + while (*s && tolower(*s) != c)
> + ++s;
> +#endif
> + if (*s != '\0')
> + return (char *)s;
> + } else {
> + s = strchr(s, c);
> + if (s != NULL)
> + return (char *)s;
> + }
> + return NULL;
> +}
> +
> +
> /* Find the first occurrence of NEEDLE in HAYSTACK, using
> case-insensitive comparison. This function gives unspecified
> results in multibyte locales. */
> @@ -68,6 +92,10 @@ STRCASESTR (const char *haystack, const char *needle)
> if (needle[0] == '\0')
> return (char *) haystack;
>
> + haystack = strcasechr (haystack, *needle);
> + if (haystack == NULL || needle[1] == '\0')
> + return (char *) haystack;
> +
> /* Ensure HAYSTACK length is at least as long as NEEDLE length.
> Since a match may occur early on in a huge HAYSTACK, use strnlen
> and read ahead a few cachelines for improved performance. */
> @@ -75,6 +103,9 @@ STRCASESTR (const char *haystack, const char *needle)
> haystack_len = __strnlen (haystack, needle_len + 256);
> if (haystack_len < needle_len)
> return NULL;
> +
> + if (strncasecmp (haystack, needle, needle_len) == 0)
> + return (char *) haystack;
>
> /* Perform the search. Abstract memory is considered to be an array
> of 'unsigned char' values, not an array of 'char' values. See
Hi Adhemerval/James,
> We have the sysdep folder exactly to provide such macros if requires, for instance
> the file sysdeps/generic/string-fzi.h.
>
> So if this optimization is really worth, the best way to provide would be through
> a generic implementation like:
I ran it on my x86 machine (with proper AVX-512), and the "optimized" strcspn is
slower than the simple byte loop on bench-strcasestr. While the benchmark could
likely be improved, it shows this target specific optimization isn't worth doing.
Cheers,
Wilco
On Mon, Oct 16, 2023 at 8:52 AM Wilco Dijkstra <Wilco.Dijkstra@arm.com> wrote:
>
> Hi Adhemerval/James,
>
> > We have the sysdep folder exactly to provide such macros if requires, for instance
> > the file sysdeps/generic/string-fzi.h.
> >
> > So if this optimization is really worth, the best way to provide would be through
> > a generic implementation like:
>
> I ran it on my x86 machine (with proper AVX-512), and the "optimized" strcspn is
> slower than the simple byte loop on bench-strcasestr.
"optimized" being the sse4 version?
> While the benchmark could
> likely be improved, it shows this target specific optimization isn't worth doing.
>
> Cheers,
> Wilco
Hi Noah,
>> I ran it on my x86 machine (with proper AVX-512), and the "optimized" strcspn is
>> slower than the simple byte loop on bench-strcasestr.
>
> "optimized" being the sse4 version?
No idea, I just ran the benchmark as a smoke test. However this code looks wrong:
+ s = (char *)strcspn(s, a);
Cheers,
Wilco
@@ -55,6 +55,30 @@
#endif
+static inline char *__attribute__ ((always_inline))
+strcasechr (const char *s, char c)
+{
+ if (isalpha(c)) {
+ /* May have optimized strcspn? */
+#if defined __sparc__ || defined __sparc || defined __x86_64__ || defined _M_X64 || defined __s390x__ || defined i386 || defined __i386__ || defined __i386 || defined _M_IX86 || defined __PPC64__ || defined __ppc64__ || defined _ARCH_PPC64 || _ARCH_PWR8
+ const char a[] = {tolower(c), toupper(c), '\0'};
+ s = (char *)strcspn(s, a);
+#else
+ c = tolower(c);
+ while (*s && tolower(*s) != c)
+ ++s;
+#endif
+ if (*s != '\0')
+ return (char *)s;
+ } else {
+ s = strchr(s, c);
+ if (s != NULL)
+ return (char *)s;
+ }
+ return NULL;
+}
+
+
/* Find the first occurrence of NEEDLE in HAYSTACK, using
case-insensitive comparison. This function gives unspecified
results in multibyte locales. */
@@ -68,6 +92,10 @@ STRCASESTR (const char *haystack, const char *needle)
if (needle[0] == '\0')
return (char *) haystack;
+ haystack = strcasechr (haystack, *needle);
+ if (haystack == NULL || needle[1] == '\0')
+ return (char *) haystack;
+
/* Ensure HAYSTACK length is at least as long as NEEDLE length.
Since a match may occur early on in a huge HAYSTACK, use strnlen
and read ahead a few cachelines for improved performance. */
@@ -75,6 +103,9 @@ STRCASESTR (const char *haystack, const char *needle)
haystack_len = __strnlen (haystack, needle_len + 256);
if (haystack_len < needle_len)
return NULL;
+
+ if (strncasecmp (haystack, needle, needle_len) == 0)
+ return (char *) haystack;
/* Perform the search. Abstract memory is considered to be an array
of 'unsigned char' values, not an array of 'char' values. See