This is the mail archive of the libc-hacker@sources.redhat.com mailing list for the glibc project.

Note that libc-hacker is a closed list. You may look at the archives of this list, but subscription and posting are not open.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix IA-64 and S390 bits/byteswap.h


Hi!

On IA-64 and S390, if __bswap_{16,32,64}(*x++) and similar constructs are
used (heavily e.g. in sunrpc on IA-64), gcc emits tons of
operation on `something' may be undefined warnings.
It is because __bswap_constant_* macros are used on the original x,
so gcc sees if (__builtin_constant_p (*x++)) __v = ((*x++) >> 8) | ((*x++) << 8);
else something and warns before optimizing the __builtin_constant_p
and the false branch away.
Following patch shuts it up, plus wraps for non-GCC the macros in static
__inline functions like sysdeps/generic/bits/byteswap.h does, so that
it doesn't evaluate arguments multiple times.

2003-01-07  Jakub Jelinek  <jakub@redhat.com>

	* sysdeps/ia64/bits/byteswap.h [__GNUC__ >= 2] (__bswap_16,
	__bswap_32, __bswap_64): Put x into temporary variable
	to avoid warnings.
	[!__GNUC__] (__bswap_16, __bswap_32, __bswap_64): Change into static
	(inline) functions.
	* sysdeps/s390/bits/byteswap.h [__GNUC__ >= 2] (__bswap_16,
	__bswap_32, __bswap_64): Put x into temporary variable
	to avoid warnings.
	[!__GNUC__] (__bswap_16, __bswap_32, __bswap_64): Change into static
	(inline) functions.
	* sysdeps/i386/bits/byteswap.h [!__GNUC__] (__bswap_16, __bswap_32):
	Likewise.

--- libc/sysdeps/ia64/bits/byteswap.h	31 Jan 2002 21:33:01 -0000	1.1.1.4
+++ libc/sysdeps/ia64/bits/byteswap.h	7 Jan 2003 10:11:25 -0000	1.3
@@ -1,5 +1,5 @@
 /* Macros to swap the order of bytes in integer values.
-   Copyright (C) 1997, 1998, 2000, 2002 Free Software Foundation, Inc.
+   Copyright (C) 1997, 1998, 2000, 2002, 2003 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
@@ -31,18 +31,22 @@
 #if defined __GNUC__ && __GNUC__ >= 2
 # define __bswap_16(x) \
      (__extension__							      \
-      ({ register unsigned short int __v;				      \
+      ({ register unsigned short int __v, __x = (x);			      \
 	 if (__builtin_constant_p (x))					      \
-	   __v = __bswap_constant_16 (x);				      \
+	   __v = __bswap_constant_16 (__x);				      \
 	 else								      \
 	   __asm__ __volatile__ ("shl %0 = %1, 48 ;;"			      \
 				 "mux1 %0 = %0, @rev ;;"		      \
 				 : "=r" (__v)				      \
-				 : "r" ((unsigned short int) (x)));	      \
+				 : "r" ((unsigned short int) (__x)));	      \
 	 __v; }))
 #else
 /* This is better than nothing.  */
-# define __bswap_16(x) __bswap_constant_16 (x)
+static __inline unsigned short int
+__bswap_16 (unsigned short int __bsx)
+{
+  return __bswap_constant_16 (__bsx);
+}
 #endif
 
 
@@ -54,17 +58,21 @@
 #if defined __GNUC__ && __GNUC__ >= 2
 # define __bswap_32(x) \
      (__extension__							      \
-      ({ register unsigned int __v;					      \
+      ({ register unsigned int __v, __x = (x);				      \
 	 if (__builtin_constant_p (x))					      \
-	   __v = __bswap_constant_32 (x);				      \
+	   __v = __bswap_constant_32 (__x);				      \
 	 else								      \
 	   __asm__ __volatile__ ("shl %0 = %1, 32 ;;"			      \
 				 "mux1 %0 = %0, @rev ;;"		      \
 				 : "=r" (__v)				      \
-				 : "r" ((unsigned int) (x)));		      \
+				 : "r" ((unsigned int) (__x)));		      \
 	 __v; }))
 #else
-# define __bswap_32(x) __bswap_constant_32 (x)
+static __inline unsigned int
+__bswap_32 (unsigned int __bsx)
+{
+  return __bswap_constant_32 (__bsx);
+}
 #endif
 
 
@@ -82,17 +90,21 @@
 #if defined __GNUC__ && __GNUC__ >= 2
 # define __bswap_64(x) \
      (__extension__							      \
-      ({ register unsigned long int __v;				      \
+      ({ register unsigned long int __v, __x = (x);			      \
 	 if (__builtin_constant_p (x))					      \
-	   __v = __bswap_constant_64 (x);				      \
+	   __v = __bswap_constant_64 (__x);				      \
 	 else								      \
 	   __asm__ __volatile__ ("mux1 %0 = %1, @rev ;;"		      \
 				 : "=r" (__v)				      \
-				 : "r" ((unsigned long int) (x)));	      \
+				 : "r" ((unsigned long int) (__x)));	      \
          __v; }))
 
 #else
-# define __bswap_64(x) __bswap_constant_64 (x)
+static __inline unsigned long int
+__bswap_64 (unsigned long int __bsx)
+{
+  return __bswap_constant_64 (__bsx);
+}
 #endif
 
 #endif /* _BITS_BYTESWAP_H */
--- libc/sysdeps/s390/bits/byteswap.h	24 Jul 2002 15:29:44 -0000	1.1.1.2
+++ libc/sysdeps/s390/bits/byteswap.h	7 Jan 2003 22:12:51 -0000	1.6
@@ -1,5 +1,5 @@
 /* Macros to swap the order of bytes in integer values.  s390 version.
-   Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
+   Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
    Contributed by Martin Schwidefsky (schwidefsky@de.ibm.com).
    This file is part of the GNU C Library.
 
@@ -35,11 +35,11 @@
 # if __WORDSIZE == 64
 #  define __bswap_16(x) \
      (__extension__							      \
-      ({ unsigned short int __v;		                              \
+      ({ unsigned short int __v, __x = (x);	                              \
 	 if (__builtin_constant_p (x))					      \
-	   __v = __bswap_constant_16 (x);				      \
+	   __v = __bswap_constant_16 (__x);				      \
 	 else {								      \
-           unsigned short int __tmp = (unsigned short int) (x);               \
+           unsigned short int __tmp = (unsigned short int) (__x);             \
            __asm__ __volatile__ (                                             \
               "lrvh %0,%1"                                                    \
               : "=&d" (__v) : "m" (__tmp) );                                  \
@@ -48,11 +48,11 @@
 # else
 #  define __bswap_16(x) \
      (__extension__							      \
-      ({ unsigned short int __v;		                              \
+      ({ unsigned short int __v, __x = (x);	                              \
 	 if (__builtin_constant_p (x))					      \
-	   __v = __bswap_constant_16 (x);				      \
+	   __v = __bswap_constant_16 (__x);				      \
 	 else {								      \
-           unsigned short int __tmp = (unsigned short int) (x);               \
+           unsigned short int __tmp = (unsigned short int) (__x);             \
            __asm__ __volatile__ (                                             \
               "sr   %0,%0\n"                                                  \
               "la   1,%1\n"                                                   \
@@ -64,7 +64,11 @@
 # endif
 #else
 /* This is better than nothing.  */
-#define __bswap_16(x) __bswap_constant_16 (x)
+static __inline unsigned short int
+__bswap_16 (unsigned short int __bsx)
+{
+  return __bswap_constant_16 (__bsx);
+}
 #endif
 
 /* Swap bytes in 32 bit value.  */
@@ -76,11 +80,11 @@
 # if __WORDSIZE == 64
 #  define __bswap_32(x) \
      (__extension__							      \
-      ({ unsigned int __v;					              \
+      ({ unsigned int __v, __x = (x);				              \
 	 if (__builtin_constant_p (x))					      \
-	   __v = __bswap_constant_32 (x);				      \
+	   __v = __bswap_constant_32 (__x);				      \
 	 else {								      \
-           unsigned int __tmp = (unsigned int) (x);                           \
+           unsigned int __tmp = (unsigned int) (__x);                         \
            __asm__ __volatile__ (                                             \
               "lrv   %0,%1"                                                   \
               : "=&d" (__v) : "m" (__tmp));                                   \
@@ -89,11 +93,11 @@
 # else
 #  define __bswap_32(x) \
      (__extension__							      \
-      ({ unsigned int __v;				                      \
+      ({ unsigned int __v, __x = (x);			                      \
 	 if (__builtin_constant_p (x))					      \
-	   __v = __bswap_constant_32 (x);				      \
+	   __v = __bswap_constant_32 (__x);				      \
 	 else {								      \
-           unsigned int __tmp = (unsigned int) (x);                           \
+           unsigned int __tmp = (unsigned int) (__x);                         \
            __asm__ __volatile__ (                                             \
               "la    1,%1\n"                                                  \
               "icm   %0,8,3(1)\n"                                             \
@@ -105,7 +109,11 @@
 	 __v; }))
 # endif
 #else
-# define __bswap_32(x) __bswap_constant_32 (x)
+static __inline unsigned int
+__bswap_32 (unsigned int __bsx)
+{
+  return __bswap_constant_32 (__bsx);
+}
 #endif
 
 /* Swap bytes in 64 bit value.  */
@@ -119,11 +127,11 @@
 # if __WORDSIZE == 64
 #  define __bswap_64(x) \
      (__extension__							      \
-      ({ unsigned long __w;					              \
+      ({ unsigned long __w, __x = (x);				              \
 	 if (__builtin_constant_p (x))					      \
-	   __w = __bswap_constant_64 (x);				      \
+	   __w = __bswap_constant_64 (__x);				      \
 	 else {								      \
-           unsigned long __tmp = (unsigned long) (x);                         \
+           unsigned long __tmp = (unsigned long) (__x);                       \
            __asm__ __volatile__ (                                             \
               "lrvg  %0,%1"                                                   \
               : "=&d" (__w) : "m" (__tmp));                                   \
@@ -140,7 +148,11 @@
           __r.__ll; })
 # endif
 #else
-# define __bswap_64(x) __bswap_constant_64 (x)
+static __inline unsigned long long int
+__bswap_64 (unsigned long long int __bsx)
+{
+  return __bswap_constant_64 (__bsx);
+}
 #endif
 
 #endif /* _BITS_BYTESWAP_H */
--- libc/sysdeps/i386/bits/byteswap.h	12 Dec 2002 15:26:20 -0000	1.1.1.7
+++ libc/sysdeps/i386/bits/byteswap.h	12 Dec 2002 16:34:13 -0000	1.3
@@ -28,8 +28,9 @@
 #define __bswap_constant_16(x) \
      ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
 
-#if defined __GNUC__ && __GNUC__ >= 2
-# define __bswap_16(x) \
+#ifdef __GNUC__
+# if __GNUC__ >= 2
+#  define __bswap_16(x) \
      (__extension__							      \
       ({ register unsigned short int __v, __x = (x);			      \
 	 if (__builtin_constant_p (__x))				      \
@@ -40,24 +41,31 @@
  		    : "0" (__x)						      \
  		    : "cc");						      \
 	 __v; }))
-#else
+# else
 /* This is better than nothing.  */
-# define __bswap_16(x) \
+#  define __bswap_16(x) \
      (__extension__							      \
       ({ register unsigned short int __x = (x); __bswap_constant_16 (__x); }))
+# endif
+#else
+static __inline unsigned short int
+__bswap_16 (unsigned short int __bsx)
+{
+  return __bswap_constant_16 (__bsx);
+}
 #endif
 
-
 /* Swap bytes in 32 bit value.  */
 #define __bswap_constant_32(x) \
      ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) |		      \
       (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))
 
-#if defined __GNUC__ && __GNUC__ >= 2
+#ifdef __GNUC__
+# if __GNUC__ >= 2
 /* To swap the bytes in a word the i486 processors and up provide the
    `bswap' opcode.  On i386 we have to use three instructions.  */
-# if !defined __i486__ && !defined __pentium__ && !defined __pentiumpro__
-#  define __bswap_32(x)							      \
+#  if !defined __i486__ && !defined __pentium__ && !defined __pentiumpro__
+#   define __bswap_32(x)						      \
      (__extension__							      \
       ({ register unsigned int __v, __x = (x);				      \
 	 if (__builtin_constant_p (__x))				      \
@@ -70,8 +78,8 @@
 		    : "0" (__x)						      \
 		    : "cc");						      \
 	 __v; }))
-# else
-#  define __bswap_32(x) \
+#  else
+#   define __bswap_32(x) \
      (__extension__							      \
       ({ register unsigned int __v, __x = (x);				      \
 	 if (__builtin_constant_p (__x))				      \
@@ -79,11 +87,18 @@
 	 else								      \
 	   __asm__ ("bswap %0" : "=r" (__v) : "0" (__x));		      \
 	 __v; }))
-# endif
-#else
-# define __bswap_32(x) \
+#  endif
+# else
+#  define __bswap_32(x) \
      (__extension__							      \
       ({ register unsigned int __x = (x); __bswap_constant_32 (__x); }))
+# endif
+#else
+static __inline unsigned int
+__bswap_32 (unsigned int __bsx)
+{
+  return __bswap_constant_32 (__bsx);
+}
 #endif
 
 

	Jakub


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