From patchwork Wed Oct 7 14:30:12 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wilco Dijkstra X-Patchwork-Id: 8970 Received: (qmail 22109 invoked by alias); 7 Oct 2015 14:30:26 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 22098 invoked by uid 89); 7 Oct 2015 14:30:25 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.8 required=5.0 tests=AWL, BAYES_00, SPF_PASS autolearn=ham version=3.3.2 X-HELO: eu-smtp-delivery-143.mimecast.com From: "Wilco Dijkstra" To: "GNU C Library" Subject: [PATCH] Use strlen when searching for a nul char Date: Wed, 7 Oct 2015 15:30:12 +0100 Message-ID: <003a01d1010c$ad404240$07c0c6c0$@com> MIME-Version: 1.0 X-MC-Unique: SiNpp9TPQ1eQ0PnozB9x-Q-1 Expand strchr (s, '\0') in C/C++ to use strlen. This is faster on most targets as strlen is a simpler function. Passes GLIBC tests. I'm planning to do the same for strrchr, strchrnul and rawmemchr in future patches as people frequently use all of these to find the end of a string. OK for commit? ChangeLog: 2015-10-07 Wilco Dijkstra wdijkstr@arm.com * string/string.h (strchr): Use strlen when searching for nul char. * string/bits/string.h (strchr): Remove define. --- string/bits/string2.h | 19 ------------------- string/string.h | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/string/bits/string2.h b/string/bits/string2.h index 7645176..db6457e 100644 --- a/string/bits/string2.h +++ b/string/bits/string2.h @@ -387,25 +387,6 @@ __mempcpy_small (void *__dest, char __src1, # endif #endif - -/* Return pointer to C in S. */ -#ifndef _HAVE_STRING_ARCH_strchr -extern void *__rawmemchr (const void *__s, int __c); -# if __GNUC_PREREQ (3, 2) -# define strchr(s, c) \ - (__extension__ (__builtin_constant_p (c) && !__builtin_constant_p (s) \ - && (c) == '\0' \ - ? (char *) __rawmemchr (s, c) \ - : __builtin_strchr (s, c))) -# else -# define strchr(s, c) \ - (__extension__ (__builtin_constant_p (c) && (c) == '\0' \ - ? (char *) __rawmemchr (s, c) \ - : strchr (s, c))) -# endif -#endif - - /* Copy SRC to DEST. */ #if (!defined _HAVE_STRING_ARCH_strcpy && !__GNUC_PREREQ (3, 0)) \ || defined _FORCE_INLINES diff --git a/string/string.h b/string/string.h index 3ab7103..599f2db 100644 --- a/string/string.h +++ b/string/string.h @@ -217,12 +217,16 @@ extern const char *strchr (const char *__s, int __c) __extern_always_inline char * strchr (char *__s, int __c) __THROW { + if (__builtin_constant_p (__c) && __c == '\0') + return __s + __builtin_strlen ((const char *) __s); return __builtin_strchr (__s, __c); } __extern_always_inline const char * strchr (const char *__s, int __c) __THROW { + if (__builtin_constant_p (__c) && __c == '\0') + return __s + __builtin_strlen (__s); return __builtin_strchr (__s, __c); } # endif @@ -230,6 +234,19 @@ strchr (const char *__s, int __c) __THROW #else extern char *strchr (const char *__s, int __c) __THROW __attribute_pure__ __nonnull ((1)); + +# if defined __OPTIMIZE__ && defined __extern_always_inline \ + && __GNUC_PREREQ (3,2) && !defined _FORCE_INLINES \ + && !defined _HAVE_STRING_ARCH_strchr +__extern_always_inline char * +strchr (const char *__s, int __c) +{ + if (__builtin_constant_p (__c) && __c == '\0') + return (char *)__s + __builtin_strlen (__s); + return __builtin_strchr (__s, __c); +} +#endif + #endif /* Find the last occurrence of C in S. */ #ifdef __CORRECT_ISO_CPP_STRING_H_PROTO