This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PATCH] Use strlen when searching for a nul char
- From: "Wilco Dijkstra" <wdijkstr at arm dot com>
- To: "GNU C Library" <libc-alpha at sourceware dot org>
- Date: Wed, 7 Oct 2015 15:30:12 +0100
- Subject: [PATCH] Use strlen when searching for a nul char
- Authentication-results: sourceware.org; auth=none
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
--
1.9.1