diff mbox

Use strlen when searching for a nul char

Message ID AM3PR08MB0088858A7DD08414E65F8375836D0@AM3PR08MB0088.eurprd08.prod.outlook.com
State New
Headers show

Commit Message

Wilco Dijkstra April 20, 2016, 2:50 p.m. UTC
Mike Frysinger wrote:
> the issue isn't what version of gcc is used to build glibc.  this is an
> installed header, and we made guarantees that the installed headers work
> with much older versions of gcc at runtime.  those guarantees don't hard
> extend to pure-optimizations, but typically we wait much longer for those
> versions to cycle out of common use.  dropping code that requires gcc7
> (which doesn't even exist yet) doesn't fall into that bucket.

OK, here is the patch with the old define left in:

Comments

Mike Frysinger April 20, 2016, 3:24 p.m. UTC | #1
On 20 Apr 2016 14:50, Wilco Dijkstra wrote:
>  #ifndef _HAVE_STRING_ARCH_strchr
>  extern void *__rawmemchr (const void *__s, int __c);
> -#  define strchr(s, c) \
> +# define strchr(s, c) \
>    (__extension__ (__builtin_constant_p (c) && !__builtin_constant_p (s)	      \
>  		  && (c) == '\0'					      \
>  		  ? (char *) __rawmemchr (s, c)				      \
>  		  : __builtin_strchr (s, c)))
>  #endif
>  
> +#ifndef _HAVE_STRING_ARCH_rawmemchr
> +extern void *__rawmemchr (const void *__s, int __c);
> +# define __rawmemchr(s, c) \
> +    (__extension__ ({ char *__s = (char *)(s);		\
> +      __builtin_constant_p (c) && (c) == '\0'		\
> +      ? (void *)(__s + strlen (__s))			\
> +      : __rawmemchr (__s, (c));}))
> +# ifdef __USE_GNU
> +#  define rawmemchr(s,c) __rawmemchr ((s), (c))
> +# endif
> +#endif

this relies on _HAVE_STRING_ARCH_strchr & _HAVE_STRING_ARCH_rawmemchr
being in sync in order for the strchr->strlen rewrite.  should we just
do the strlen change in strchr itself too ?
-mike
Wilco Dijkstra April 20, 2016, 3:52 p.m. UTC | #2
Mike Frysinger wrote:
> this relies on _HAVE_STRING_ARCH_strchr & _HAVE_STRING_ARCH_rawmemchr
> being in sync in order for the strchr->strlen rewrite.  should we just
> do the strlen change in strchr itself too ?

If a target only defines _HAVE_STRING_ARCH_rawmemchr then presumably
it supports a fast rawmemchr, so it might be expected that strchr defers to rawmemchr
as it does today rather than using strlen.

If a target only defines _HAVE_STRING_ARCH_strchr then rawmemchr
would still use strlen, which is fine.

Wilco

>  #ifndef _HAVE_STRING_ARCH_strchr
>  extern void *__rawmemchr (const void *__s, int __c);
> -#  define strchr(s, c) \
> +# define strchr(s, c) \
>    (__extension__ (__builtin_constant_p (c) && !__builtin_constant_p (s)            \
>                 && (c) == '\0'                                              \
>                 ? (char *) __rawmemchr (s, c)                               \
>                 : __builtin_strchr (s, c)))
>  #endif
>
> +#ifndef _HAVE_STRING_ARCH_rawmemchr
> +extern void *__rawmemchr (const void *__s, int __c);
> +# define __rawmemchr(s, c) \
> +    (__extension__ ({ char *__s = (char *)(s);               \
> +      __builtin_constant_p (c) && (c) == '\0'                \
> +      ? (void *)(__s + strlen (__s))                 \
> +      : __rawmemchr (__s, (c));}))
> +# ifdef __USE_GNU
> +#  define rawmemchr(s,c) __rawmemchr ((s), (c))
> +# endif
> +#endif
diff mbox

Patch

diff --git a/string/bits/string2.h b/string/bits/string2.h
index 80987602f34ded483854bcea86dabd5b81e42a18..62edc93b0f04249a504078caeee8fca192e91df8 100644
--- a/string/bits/string2.h
+++ b/string/bits/string2.h
@@ -60,13 +60,25 @@ 
 
 #ifndef _HAVE_STRING_ARCH_strchr
 extern void *__rawmemchr (const void *__s, int __c);
-#  define strchr(s, c) \
+# define strchr(s, c) \
   (__extension__ (__builtin_constant_p (c) && !__builtin_constant_p (s)	      \
 		  && (c) == '\0'					      \
 		  ? (char *) __rawmemchr (s, c)				      \
 		  : __builtin_strchr (s, c)))
 #endif
 
+#ifndef _HAVE_STRING_ARCH_rawmemchr
+extern void *__rawmemchr (const void *__s, int __c);
+# define __rawmemchr(s, c) \
+    (__extension__ ({ char *__s = (char *)(s);		\
+      __builtin_constant_p (c) && (c) == '\0'		\
+      ? (void *)(__s + strlen (__s))			\
+      : __rawmemchr (__s, (c));}))
+# ifdef __USE_GNU
+#  define rawmemchr(s,c) __rawmemchr ((s), (c))
+# endif
+#endif
+
 
 /* Copy SRC to DEST, returning pointer to final NUL byte.  */
 #ifdef __USE_GNU