This is the mail archive of the libc-alpha@sourceware.cygnus.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

pure string functions



Hi,

Jan has developed a page for gcc to optimize pure functions.  He also
developed a patch to add the pure attribute to a number of string
functions in glibc.

The gcc manual documents pure functions as:

======================================================================
Many functions have no effects except the return value and their
return value depends only on the parameters and/or global variables.
Such a function can be subject
to common subexpression elimination and loop optimization just as an
arithmetic operator would be.  These functions should be declared
with the attribute @code{pure}.  For example,

@smallexample
int square (int) __attribute__ ((pure));
@end smallexample

@noindent
says that the hypothetical function @code{square} is safe to call
fewer times than the program says.

Some of common examples of pure functions are @code{strlen} or @code{memcmp}.
Interesting non-pure functions are functions with infinite loops or those
depending on volatile memory or other system resource, that may change between
two consetuctive calls (such as @code{feof} in multithreding environment).
======================================================================

I've tested his glibc patch thoroughly on Alpha and Intel and it a
does make a difference in glibc.

Uli, can I commit this patch?  

After this patch is in, others might want to go through gcc and add
pure to a number of other functions.

Andreas

2000-05-11  Jan Hubicka  <jh@suse.cz>

	* misc/sys/cdefs.h: Add support for pure attribute.

	* include/string.h: Add __attribute_pure__ to various functions.
	* string/string.h: Likewise.

============================================================
Index: include/string.h
--- include/string.h	1999/12/19 18:32:05	1.9
+++ include/string.h	2000/05/11 08:23:44
@@ -5,23 +5,29 @@
 extern void *__memccpy (void *__dest, __const void *__src,
 			  int __c, size_t __n);
 
-extern size_t __strnlen (__const char *__string, size_t __maxlen);
+extern size_t __strnlen (__const char *__string, size_t __maxlen)
+     __attribute_pure__;
 
 extern char *__strsep (char **__stringp, __const char *__delim);
 
-extern int __strverscmp (__const char *__s1, __const char *__s2);
+extern int __strverscmp (__const char *__s1, __const char *__s2)
+     __attribute_pure__;
 
 extern int __strncasecmp (__const char *__s1, __const char *__s2,
-			  size_t __n);
+			  size_t __n)
+     __attribute_pure__;
 
 extern char *__strndup (__const char *__string, size_t __n)
      __attribute_malloc__;
 
-extern void *__rawmemchr (__const void *__s, int __c);
+extern void *__rawmemchr (__const void *__s, int __c)
+     __attribute_pure__;
 
-extern char *__strchrnul (__const char *__s, int __c);
+extern char *__strchrnul (__const char *__s, int __c)
+     __attribute_pure__;
 
-extern void *__memrchr (__const void *__s, int __c, size_t __n);
+extern void *__memrchr (__const void *__s, int __c, size_t __n)
+     __attribute_pure__;
 
 /* Now the real definitions.  We do this here since some of the functions
    above are defined as macros in the headers.  */
============================================================
Index: string/string.h
--- string/string.h	2000/02/24 08:17:11	1.73
+++ string/string.h	2000/05/11 08:23:44
@@ -55,18 +55,20 @@
 
 /* Compare N bytes of S1 and S2.  */
 extern int memcmp (__const void *__s1, __const void *__s2, size_t __n)
-     __THROW;
+     __THROW __attribute_pure__;
 
 /* Search N bytes of S for C.  */
-extern void *memchr (__const void *__s, int __c, size_t __n) __THROW;
+extern void *memchr (__const void *__s, int __c, size_t __n)
+      __THROW __attribute_pure__;
 
 #ifdef __USE_GNU
 /* Search in S for C.  This is similar to `memchr' but there is no
    length limit.  */
-extern void *rawmemchr (__const void *__s, int __c) __THROW;
+extern void *rawmemchr (__const void *__s, int __c) __THROW __attribute_pure__;
 
 /* Search N bytes of S for the final occurrence of C.  */
-extern void *memrchr (__const void *__s, int __c, size_t __n) __THROW;
+extern void *memrchr (__const void *__s, int __c, size_t __n)
+      __THROW __attribute_pure__;
 #endif
 
 
@@ -85,13 +87,15 @@
 		      size_t __n) __THROW;
 
 /* Compare S1 and S2.  */
-extern int strcmp (__const char *__s1, __const char *__s2) __THROW;
+extern int strcmp (__const char *__s1, __const char *__s2)
+     __THROW __attribute_pure__;
 /* Compare N characters of S1 and S2.  */
 extern int strncmp (__const char *__s1, __const char *__s2, size_t __n)
-     __THROW;
+     __THROW __attribute_pure__;
 
 /* Compare the collated forms of S1 and S2.  */
-extern int strcoll (__const char *__s1, __const char *__s2) __THROW;
+extern int strcoll (__const char *__s1, __const char *__s2)
+     __THROW __attribute_pure__;
 /* Put a transformation of SRC into no more than N bytes of DEST.  */
 extern size_t strxfrm (char *__restrict __dest,
 		       __const char *__restrict __src, size_t __n) __THROW;
@@ -104,7 +108,7 @@
 
 /* Compare the collated forms of S1 and S2 using rules from L.  */
 extern int __strcoll_l (__const char *__s1, __const char *__s2, __locale_t __l)
-     __THROW;
+     __THROW __attribute_pure__;
 /* Put a transformation of SRC into no more than N bytes of DEST.  */
 extern size_t __strxfrm_l (char *__dest, __const char *__src, size_t __n,
 			   __locale_t __l) __THROW;
@@ -148,33 +152,37 @@
 #endif
 
 /* Find the first occurrence of C in S.  */
-extern char *strchr (__const char *__s, int __c) __THROW;
+extern char *strchr (__const char *__s, int __c) __THROW __attribute_pure__;
 /* Find the last occurrence of C in S.  */
-extern char *strrchr (__const char *__s, int __c) __THROW;
+extern char *strrchr (__const char *__s, int __c) __THROW __attribute_pure__;
 
 #ifdef __USE_GNU
 /* This funciton is similar to `strchr'.  But it returns a pointer to
    the closing NUL byte in case C is not found in S.  */
-extern char *strchrnul (__const char *__s, int __c) __THROW;
+extern char *strchrnul (__const char *__s, int __c) __THROW __attribute_pure__;
 #endif
 
 /* Return the length of the initial segment of S which
    consists entirely of characters not in REJECT.  */
-extern size_t strcspn (__const char *__s, __const char *__reject) __THROW;
+extern size_t strcspn (__const char *__s, __const char *__reject)
+     __THROW __attribute_pure__;
 /* Return the length of the initial segment of S which
    consists entirely of characters in ACCEPT.  */
-extern size_t strspn (__const char *__s, __const char *__accept) __THROW;
+extern size_t strspn (__const char *__s, __const char *__accept)
+     __THROW __attribute_pure__;
 /* Find the first occurrence in S of any character in ACCEPT.  */
-extern char *strpbrk (__const char *__s, __const char *__accept) __THROW;
+extern char *strpbrk (__const char *__s, __const char *__accept)
+     __THROW __attribute_pure__;
 /* Find the first occurrence of NEEDLE in HAYSTACK.  */
-extern char *strstr (__const char *__haystack, __const char *__needle) __THROW;
+extern char *strstr (__const char *__haystack, __const char *__needle)
+     __THROW __attribute_pure__;
 
 #ifdef __USE_GNU
 /* Similar to `strstr' but this function ignores the case of both strings.  */
 extern char *__strcasestr (__const char *__haystack, __const char *__needle)
-     __THROW;
+     __THROW __attribute_pure__;
 extern char *strcasestr (__const char *__haystack, __const char *__needle)
-     __THROW;
+     __THROW __attribute_pure__;
 #endif
 
 /* Divide S into tokens separated by characters in DELIM.  */
@@ -196,7 +204,8 @@
    NEEDLE is NEEDLELEN bytes long;
    HAYSTACK is HAYSTACKLEN bytes long.  */
 extern void *memmem (__const void *__haystack, size_t __haystacklen,
-		     __const void *__needle, size_t __needlelen) __THROW;
+		     __const void *__needle, size_t __needlelen)
+     __THROW __attribute_pure__;
 
 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
    last written byte.  */
@@ -208,12 +217,13 @@
 
 
 /* Return the length of S.  */
-extern size_t strlen (__const char *__s) __THROW;
+extern size_t strlen (__const char *__s) __THROW __attribute_pure__;
 
 #ifdef	__USE_GNU
 /* Find the length of STRING, but scan at most MAXLEN characters.
    If no '\0' terminator is found in that many characters, return MAXLEN.  */
-extern size_t strnlen (__const char *__string, size_t __maxlen) __THROW;
+extern size_t strnlen (__const char *__string, size_t __maxlen)
+     __THROW __attribute_pure__;
 #endif
 
 
@@ -238,13 +248,14 @@
 extern void bzero (void *__s, size_t __n) __THROW;
 
 /* Compare N bytes of S1 and S2 (same as memcmp).  */
-extern int bcmp (__const void *__s1, __const void *__s2, size_t __n) __THROW;
+extern int bcmp (__const void *__s1, __const void *__s2, size_t __n)
+     __THROW __attribute_pure__;
 
 /* Find the first occurrence of C in S (same as strchr).  */
-extern char *index (__const char *__s, int __c) __THROW;
+extern char *index (__const char *__s, int __c) __THROW __attribute_pure__;
 
 /* Find the last occurrence of C in S (same as strrchr).  */
-extern char *rindex (__const char *__s, int __c) __THROW;
+extern char *rindex (__const char *__s, int __c) __THROW __attribute_pure__;
 
 /* Return the position of the first bit set in I, or 0 if none are set.
    The least-significant bit is position 1, the most-significant 32.  */
@@ -262,22 +273,25 @@
 # endif
 
 /* Compare S1 and S2, ignoring case.  */
-extern int __strcasecmp (__const char *__s1, __const char *__s2) __THROW;
-extern int strcasecmp (__const char *__s1, __const char *__s2) __THROW;
+extern int __strcasecmp (__const char *__s1, __const char *__s2)
+     __THROW __attribute_pure__;
+extern int strcasecmp (__const char *__s1, __const char *__s2)
+     __THROW __attribute_pure__;
 
 /* Compare no more than N chars of S1 and S2, ignoring case.  */
 extern int strncasecmp (__const char *__s1, __const char *__s2, size_t __n)
-     __THROW;
+     __THROW __attribute_pure__;
 #endif /* Use BSD.  */
 
 #ifdef	__USE_GNU
 /* Again versions of a few functions which use the given locale instead
    of the global one.  */
 extern int __strcasecmp_l (__const char *__s1, __const char *__s2,
-			   __locale_t __loc) __THROW;
+			   __locale_t __loc) __THROW __attribute_pure__;
 
 extern int __strncasecmp_l (__const char *__s1, __const char *__s2,
-			    size_t __n, __locale_t __loc) __THROW;
+			    size_t __n, __locale_t __loc)
+     __THROW __attribute_pure__;
 #endif
 
 #ifdef	__USE_BSD
@@ -289,7 +303,8 @@
 
 #ifdef	__USE_GNU
 /* Compare S1 and S2 as strings holding name & indices/version numbers.  */
-extern int strverscmp (__const char *__s1, __const char *__s2) __THROW;
+extern int strverscmp (__const char *__s1, __const char *__s2)
+     __THROW __attribute_pure__;
 
 /* Return a string describing the meaning of the signal number in SIG.  */
 extern char *strsignal (int __sig) __THROW;
============================================================
Index: misc/sys/cdefs.h
--- misc/sys/cdefs.h	2000/03/31 07:19:43	1.33
+++ misc/sys/cdefs.h	2000/05/11 08:23:44
@@ -123,6 +123,15 @@
 # define __attribute_malloc__ /* Ignore */
 #endif
 
+/* At some point during the gcc 2.96 development the `pure' attribute
+   for functions was introduced.  We don't want to use it unconditionally
+   (although this would be possible) since it generates warnings.  */
+#if __GNUC_PREREQ (2,96)
+# define __attribute_pure__ __attribute__ ((__pure__))
+#else
+# define __attribute_pure__ /* Ignore */
+#endif
+
 /* It is possible to compile containing GCC extensions even if GCC is
    run in pedantic mode if the uses are carefully marked using the
    `__extension__' keyword.  But this is not generally available before

-- 
 Andreas Jaeger
  SuSE Labs aj@suse.de
   private aj@arthur.rhein-neckar.de

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]