about summary refs log tree commit diff
path: root/string
diff options
context:
space:
mode:
Diffstat (limited to 'string')
-rw-r--r--string/Makefile14
-rw-r--r--string/bits/string2.h292
-rw-r--r--string/inl-tester.c6
-rw-r--r--string/stratcliff.c5
-rw-r--r--string/string.h44
-rw-r--r--string/tester.c30
-rw-r--r--string/tst-strlen.c4
7 files changed, 373 insertions, 22 deletions
diff --git a/string/Makefile b/string/Makefile
index ac04557ddc..8596bc1b24 100644
--- a/string/Makefile
+++ b/string/Makefile
@@ -30,7 +30,7 @@ routines	:= strcat strchr strcmp strcoll strcpy strcspn		\
 		   strncat strncmp strncpy				\
 		   strrchr strpbrk strsignal strspn strstr strtok	\
 		   strtok_r strxfrm memchr memcmp memmove memset	\
-		   bcopy bzero ffs stpcpy stpncpy			\
+		   mempcpy bcopy bzero ffs stpcpy stpncpy		\
 		   strcasecmp strncase strcasecmp_l strncase_l		\
 		   memccpy memcpy wordcopy strsep			\
 		   swab strfry memfrob memmem				\
@@ -40,17 +40,19 @@ routines	:= strcat strchr strcmp strcoll strcpy strcspn		\
 		   envz basename					\
 		   strcoll_l strxfrm_l
 
-tests		:= tester testcopy test-ffs tst-strlen stratcliff \
-	           tst-svc
+tests		:= tester inl-tester testcopy test-ffs tst-strlen	\
+		   stratcliff tst-svc
 distribute	:= memcopy.h pagecopy.h tst-svc.expect
 
 
 include ../Rules
 
 tester-ENV = LANGUAGE=C
-CFLAGS-tester.c = -fno-builtin -D__NO_STRING_INLINES
-CFLAGS-tst-strlen.c = -fno-builtin -D__NO_STRING_INLINES
-CFLAGS-stratcliff.c = -fno-builtin -D__NO_STRING_INLINES
+inl-tester-ENV = LANGUAGE=C
+CFLAGS-tester.c = -fno-builtin
+CFLAGS-inl-tester.c = -fno-builtin
+CFLAGS-tst-strlen.c = -fno-builtin
+CFLAGS-stratcliff.c = -fno-builtin
 
 tests: $(objpfx)tst-svc.out
 	cmp tst-svc.expect $(objpfx)tst-svc.out
diff --git a/string/bits/string2.h b/string/bits/string2.h
new file mode 100644
index 0000000000..b943fe9aa0
--- /dev/null
+++ b/string/bits/string2.h
@@ -0,0 +1,292 @@
+/* Machine-independant string function optimizations.
+   Copyright (C) 1997 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
+
+#ifndef _BITS_STRING2_H
+#define _BITS_STRING2_H	1
+
+/* Unlike the definitions in the header <bits/string.h> the
+   definitions contained here are not optimizing down to assembler
+   level.  These optimizations are not always a good idea since this
+   means the code size increases a lot.  Instead the definitions here
+   optimize some functions in a way which does not dramatically
+   increase the code size and which does not use assembler.  The main
+   trick is to use GNU CC's `__builtin_constant_p' function.
+
+   Every function XXX which has a defined version in
+   <bits/string.h> must be accompanied by a have _HAVE_STRING_ARCH_XXX
+   to make sure we don't get redefinitions.
+
+   We must use here macros instead of inline functions since the
+   trick won't work with the later.  */
+
+#ifdef __cplusplus
+# define __STRING_INLINE inline
+#else
+# define __STRING_INLINE extern __inline
+#endif
+
+/* We need some more types.  */
+#include <bits/types.h>
+
+
+/* Copy SRC to DEST.  */
+#ifndef _HAVE_STRING_ARCH_strcpy
+# define strcpy(dest, src) \
+  (__extension__ (__builtin_constant_p (src)				      \
+		  ? (strlen (src) + 1 <= 8				      \
+		     ? __strcpy_small (dest, src, strlen (src) + 1)	      \
+		     : (char *) memcpy (dest, src, strlen (src) + 1))	      \
+		  : strcpy (dest, src)))
+
+__STRING_INLINE char *
+__strcpy_small (char *__dest, __const char *__src, size_t __srclen)
+{
+  register char *__tmp = __dest;
+  switch (__srclen)
+    {
+    case 7:
+      *((__uint16_t *) __tmp)++ = *((__uint16_t *) __src)++;
+    case 5:
+      *((__uint32_t *) __tmp)++ = *((__uint32_t *) __src)++;
+      *((unsigned char *) __tmp) = '\0';
+      break;
+
+    case 8:
+      *((__uint32_t *) __tmp)++ = *((__uint32_t *) __src)++;
+    case 4:
+      *((__uint32_t *) __tmp) = *((__uint32_t *) __src);
+      break;
+
+    case 6:
+      *((__uint32_t *) __tmp)++ = *((__uint32_t *) __src)++;
+    case 2:
+      *((__uint16_t *) __tmp) = *((__uint16_t *) __src);
+      break;
+
+    case 3:
+      *((__uint16_t *) __tmp)++ = *((__uint16_t *) __src)++;
+    case 1:
+      *((unsigned char *) __tmp) = '\0';
+      break;
+
+    default:
+      break;
+    }
+  return __dest;
+}
+#endif
+
+
+/* Copy SRC to DEST, returning pointer to final NUL byte.  */
+#ifdef __USE_GNU
+# ifndef _HAVE_STRING_ARCH_stpcpy
+#  define __stpcpy(dest, src) \
+  (__extension__ (__builtin_constant_p (src)				      \
+		  ? (strlen (src) + 1 <= 8				      \
+		     ? __stpcpy_small (dest, src, strlen (src) + 1)	      \
+		     : ((char *) __mempcpy (dest, src, strlen (src) + 1) - 1))\
+		  : __stpcpy (dest, src)))
+/* In glibc we use this function frequently but for namespace reasons
+   we have to use the name `__stpcpy'.  */
+#  define stpcpy(dest, src) __stpcpy (dest, src)
+
+__STRING_INLINE char *
+__stpcpy_small (char *__dest, __const char *__src, size_t __srclen)
+{
+  register char *__tmp = __dest;
+  switch (__srclen)
+    {
+    case 7:
+      *((__uint16_t *) __tmp)++ = *((__uint16_t *) __src)++;
+    case 5:
+      *((__uint32_t *) __tmp)++ = *((__uint32_t *) __src)++;
+      *((unsigned char *) __tmp) = '\0';
+      return __tmp;
+
+    case 8:
+      *((__uint32_t *) __tmp)++ = *((__uint32_t *) __src)++;
+    case 4:
+      *((__uint32_t *) __tmp) = *((__uint32_t *) __src);
+      return __tmp + 3;
+
+    case 6:
+      *((__uint32_t *) __tmp)++ = *((__uint32_t *) __src)++;
+    case 2:
+      *((__uint16_t *) __tmp) = *((__uint16_t *) __src);
+      return __tmp + 1;
+
+    case 3:
+      *((__uint16_t *) __tmp)++ = *((__uint16_t *) __src)++;
+    case 1:
+      *((unsigned char *) __tmp) = '\0';
+      return __tmp;
+
+    default:
+      break;
+    }
+  /* This should never happen.  */
+  return NULL;
+}
+# endif
+#endif
+
+
+/* Copy no more than N characters of SRC to DEST.  */
+#ifndef _HAVE_STRING_ARCH_strncpy
+# if defined _HAVE_STRING_ARCH_memset && defined _HAVE_STRING_ARCH_mempcpy
+#  define strncpy(dest, src, n) \
+  (__extension__ (__builtin_constant_p (src) && __builtin_constant_p (n)      \
+		  ? (strlen (src) + 1 >= ((size_t) (n))			      \
+		     ? (char *) memcpy (dest, src, n)			      \
+		     : (memset (__mempcpy (dest, src, strlen (src)), '\0',    \
+				n - strlen (src)),			      \
+			dest))						      \
+		  : strncpy (dest, src, n)))
+# else
+#  define strncpy(dest, src, n) \
+  (__extension__ (__builtin_constant_p (src) && __builtin_constant_p (n)      \
+		  ? (strlen (src) + 1 >= ((size_t) (n))			      \
+		     ? (char *) memcpy (dest, src, n)			      \
+		     : strncpy (dest, src, n))				      \
+		  : strncpy (dest, src, n)))
+# endif
+#endif
+
+
+/* Append no more than N characters from SRC onto DEST.  */
+#ifndef _HAVE_STRING_ARCH_strncat
+# ifdef _HAVE_STRING_ARCH_strchr
+#  define strncat(dest, src, n) \
+  (__extension__ (__builtin_constant_p (src) && __builtin_constant_p (n)      \
+		  ? (strlen (src) < ((size_t) (n))			      \
+		     ? strcat (dest, src)				      \
+		     : (memcpy (strchr (dest, '\0'), src, n), dest))	      \
+		  : strncat (dest, src, n)))
+# else
+#  define strncat(dest, src, n) \
+  (__extension__ (__builtin_constant_p (src) && __builtin_constant_p (n)      \
+		  ? (strlen (src) < ((size_t) (n))			      \
+		     ? strcat (dest, src)				      \
+		     : strncat (dest, src, n))				      \
+		  : strncat (dest, src, n)))
+# endif
+#endif
+
+
+/* Compare N characters of S1 and S2.  */
+#ifndef _HAVE_STRING_ARCH_strncmp
+# define strncmp(s1, s2, n) \
+  (__extension__ (__builtin_constant_p (s1) && strlen (s1) < ((size_t) (n))   \
+		  ? strcmp (s1, s2)					      \
+		  : (__builtin_constant_p (s2) && strlen (s2) < ((size_t) (n))\
+		     ? strcmp (s1, s2)					      \
+		     : strncmp (s1, s2, n))))
+#endif
+
+
+/* Return the length of the initial segment of S which
+   consists entirely of characters not in REJECT.  */
+#ifndef _HAVE_STRING_ARCH_strcspn
+# define strcspn(s, reject) \
+  (__extension__ (__builtin_constant_p (reject)				      \
+		  ? (((const char *) (reject))[0] == '\0'		      \
+		     ? strlen (s)					      \
+		     : (((const char *) (reject))[1] == '\0'		      \
+			? __strcspn_c1 (s, ((((const char *) (reject))[0]     \
+					     & 0xff) << 8))		      \
+			: strcspn (s, reject)))	      \
+		  : strcspn (s, reject)))
+
+__STRING_INLINE size_t
+__strcspn_c1 (__const char *__s, char __reject)
+{
+  register size_t __result = 0;
+  while (__s[__result] != '\0' && __s[__result] != __reject)
+    ++__result;
+  return __result;
+}
+#endif
+
+
+/* Return the length of the initial segment of S which
+   consists entirely of characters in ACCEPT.  */
+#ifndef _HAVE_STRING_ARCH_strspn
+# define strspn(s, accept) \
+  (__extension__ (__builtin_constant_p (accept)				      \
+		  ? (((const char *) (accept))[0] == '\0'		      \
+		     ? 0						      \
+		     : (((const char *) (accept))[1] == '\0'		      \
+			? __strspn_c1 (s, ((const char *) (accept))[0])	      \
+			: strspn (s, accept)))				      \
+		  : strspn (s, accept)))
+
+__STRING_INLINE size_t
+__strspn_c1 (__const char *__s, char __accept)
+{
+  register size_t __result = 0;
+  /* Please note that __accept never can be '\0'.  */
+  while (__s[__result] == __accept)
+    ++__result;
+  return __result;
+}
+#endif
+
+
+/* Find the first occurrence in S of any character in ACCEPT.  */
+#ifndef _HAVE_STRING_ARCH_strpbrk
+# define strpbrk(s, accept) \
+  (__extension__ (__builtin_constant_p (accept)				      \
+		  ? (((const char *) (accept))[0] == '\0'		      \
+		     ? NULL						      \
+		     : (((const char *) (accept))[1] == '\0'		      \
+			? strchr (s, ((const char *) (accept))[0])	      \
+			: strpbrk (s, accept)))				      \
+		  : strpbrk (s, accept)))
+#endif
+
+
+/* Find the first occurrence of NEEDLE in HAYSTACK.  */
+#ifndef _HAVE_STRING_ARCH_strstr
+# define strstr(haystack, needle) \
+  (__extension__ (__builtin_constant_p (needle)				      \
+		  ? (((const char *) (needle))[0] == '\0'		      \
+		     ? haystack						      \
+		     : (((const char *) (needle))[1] == '\0'		      \
+			? strchr (haystack, ((const char *) (needle))[0])     \
+			: strstr (haystack, needle)))			      \
+		  : strstr (haystack, needle)))
+#endif
+
+
+#ifdef __USE_GNU
+# ifndef _HAVE_STRING_ARCH_strnlen
+extern __inline size_t
+strnlen (__const char *__string, size_t __maxlen)
+{
+  __const char *__end = (__const char *) memchr (__string, '\0', __maxlen);
+  return __end ? __end - __string : __maxlen;
+}
+# endif
+#endif
+
+
+#undef __STRING_INLINE
+
+#endif /* bits/string2.h */
diff --git a/string/inl-tester.c b/string/inl-tester.c
new file mode 100644
index 0000000000..88528e5ed8
--- /dev/null
+++ b/string/inl-tester.c
@@ -0,0 +1,6 @@
+/* We want to test the inline functions here.  */
+
+#define DO_STRING_INLINES
+#undef __USE_STRING_INLINES
+#define __USE_STRING_INLINES 1
+#include "tester.c"
diff --git a/string/stratcliff.c b/string/stratcliff.c
index 6115d2f8db..ae1b7ae0ca 100644
--- a/string/stratcliff.c
+++ b/string/stratcliff.c
@@ -19,6 +19,11 @@
    Boston, MA 02111-1307, USA.  */
 
 #define _GNU_SOURCE 1
+
+/* Make sure we don't test the optimized inline functions if we want to
+   test the real implementation.  */
+#undef __USE_STRING_INLINES
+
 #include <errno.h>
 #include <stdio.h>
 #include <string.h>
diff --git a/string/string.h b/string/string.h
index 3cac382529..560638be8e 100644
--- a/string/string.h
+++ b/string/string.h
@@ -171,6 +171,13 @@ extern char *strtok_r __P ((char *__s, __const char *__delim,
    HAYSTACK is HAYSTACKLEN bytes long.  */
 extern __ptr_t memmem __P ((__const __ptr_t __haystack, size_t __haystacklen,
 			    __const __ptr_t __needle, size_t __needlelen));
+
+/* Copy N bytes of SRC to DEST, return pointer to bytes after the
+   last written byte.  */
+extern __ptr_t __mempcpy __P ((__ptr_t __restrict __dest,
+			       __const __ptr_t __restrict __src, size_t __n));
+extern __ptr_t mempcpy __P ((__ptr_t __restrict __dest,
+			     __const __ptr_t __restrict __src, size_t __n));
 #endif
 
 
@@ -274,21 +281,30 @@ extern char *basename __P ((__const char *__filename));
 #endif
 
 
-/* Some functions might be implemented as optimized inline assembler
-   functions.  Only include this file if we really want them.  */
-#if defined __USE_STRING_INLINES && defined __OPTIMIZE__
-# include <bits/string.h>
-#endif
-
-
-/* Now provide some generic optimizations.  */
 #if defined __GNUC__ && __GNUC__ >= 2 && defined __OPTIMIZE__
-extern __inline size_t
-strnlen (__const char *__string, size_t __maxlen)
-{
-  __const char *__end = (__const char *) memchr (__string, '\0', __maxlen);
-  return __end ? __end - __string : __maxlen;
-}
+/* When using GNU CC we provide some optimized versions of selected
+   functions from this header.  There are two kinds of optimizations:
+
+   - machine-dependent optmizations, most probably using inline
+     assembler code; these could be quite expensive since the code
+     size could increase significantly.
+     These optimizations are not used unless the symbol
+	__USE_STRING_INLINES
+     is defined before including this header
+
+   - machine-independent optimizations which do not increase the
+     code size significantly and which optimize mainly situations
+     where one or more arguments are compile-time constants.
+     These optimizations are used always when the compiler is
+     taught to optimized.  */
+
+/* Get the machine-dependent optimizations if wanted.  */
+# ifdef __USE_STRING_INLINES
+#  include <bits/string.h>
+# endif
+
+/* These are generic optimizations which do not add too much inline code.  */
+# include <bits/string2.h>
 #endif
 
 __END_DECLS
diff --git a/string/tester.c b/string/tester.c
index 0e65442063..dfb1258864 100644
--- a/string/tester.c
+++ b/string/tester.c
@@ -1,6 +1,32 @@
+/* Tester for string functions.
+   Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
+
 #ifndef _GNU_SOURCE
 #define _GNU_SOURCE
 #endif
+
+/* Make sure we don't test the optimized inline functions if we want to
+   test the real implementation.  */
+#if !defined DO_STRING_INLINES
+#undef __USE_STRING_INLINES
+#endif
+
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -222,7 +248,7 @@ main (void)
 
   (void) strcpy (one, "abcdefgh");
   (void) strncpy (one, "xyz", 2);
-  equal (one, "xycdefgh", 3);		/* Copy cut by count. */
+  equal (one, "xycdefgh", 3);			/* Copy cut by count. */
 
   (void) strcpy (one, "abcdefgh");
   (void) strncpy (one, "xyz", 3);		/* Copy cut just before NUL. */
@@ -231,7 +257,7 @@ main (void)
   (void) strcpy (one, "abcdefgh");
   (void) strncpy (one, "xyz", 4);		/* Copy just includes NUL. */
   equal (one, "xyz", 5);
-  equal (one+4, "efgh", 6);		/* Wrote too much? */
+  equal (one+4, "efgh", 6);			/* Wrote too much? */
 
   (void) strcpy (one, "abcdefgh");
   (void) strncpy (one, "xyz", 5);		/* Copy includes padding. */
diff --git a/string/tst-strlen.c b/string/tst-strlen.c
index 4acd4045f5..c43cb1a4d6 100644
--- a/string/tst-strlen.c
+++ b/string/tst-strlen.c
@@ -1,3 +1,7 @@
+/* Make sure we don't test the optimized inline functions if we want to
+   test the real implementation.  */
+#undef __USE_STRING_INLINES
+
 #include <stdio.h>
 #include <string.h>