[PATCH/RFC] byteswap: unify common definitions

Mike Frysinger vapier at gentoo.org
Mon Jul 20 20:49:00 UTC 2009


The majority of the byteswap functions are the same across all arches, so
setup a common header to provide definitions if they don't exist.  This
allows arches to override only the ones they actually want to implement
with inline assembly.

Signed-off-by: Mike Frysinger <vapier at gentoo.org>
---
seems to build for me ... any comments before i commit ?

 libc/sysdeps/linux/bfin/bits/byteswap.h          |   92 +++-------------
 libc/sysdeps/linux/common/bits/byteswap-common.h |  108 ++++++++++++++++++
 libc/sysdeps/linux/common/bits/byteswap.h        |   88 +---------------
 libc/sysdeps/linux/cris/bits/byteswap.h          |   79 ++------------
 libc/sysdeps/linux/h8300/bits/byteswap.h         |   61 +++--------
 libc/sysdeps/linux/i386/bits/byteswap.h          |  121 ++++-----------------
 libc/sysdeps/linux/ia64/bits/byteswap.h          |  100 ++++--------------
 libc/sysdeps/linux/m68k/bits/byteswap.h          |   50 ++-------
 libc/sysdeps/linux/v850/bits/byteswap.h          |   48 ++-------
 libc/sysdeps/linux/x86_64/bits/byteswap.h        |  127 ++++++----------------
 10 files changed, 242 insertions(+), 632 deletions(-)
 create mode 100644 libc/sysdeps/linux/common/bits/byteswap-common.h

diff --git a/libc/sysdeps/linux/bfin/bits/byteswap.h b/libc/sysdeps/linux/bfin/bits/byteswap.h
index 74b87d9..4fd3c99 100644
--- a/libc/sysdeps/linux/bfin/bits/byteswap.h
+++ b/libc/sysdeps/linux/bfin/bits/byteswap.h
@@ -7,88 +7,28 @@
  * Licensed under the GPL-2 or later.
  */
 
-#if !defined _BYTESWAP_H && !defined _NETINET_IN_H
-# error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead."
-#endif
-
-#ifndef _BITS_BYTESWAP_H
-#define _BITS_BYTESWAP_H 1
+#ifndef _ASM_BITS_BYTESWAP_H
+#define _ASM_BITS_BYTESWAP_H 1
 
-/* Swap bytes in 16 bit value.  */
-#define __bswap_constant_16(x) \
-     ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8))
-
-#ifdef __GNUC__
-# define __bswap_16(x) \
+#define __bswap_non_constant_16(x)
      (__extension__							      \
-      ({ register unsigned short int __v, __x = (x);			      \
-	 if (__builtin_constant_p (__x))				      \
-	   __v = __bswap_constant_16 (__x);				      \
-	 else								      \
-	   __asm__ ("%0 = PACK (%1.L, %1.L);"				      \
-		    "%0 >>= 8;"						      \
-		    : "=d" (__v)					      \
-		    : "d" (__x));					      \
+      ({ register unsigned short int __v;				      \
+	 __asm__ ("%0 = PACK (%1.L, %1.L);"				      \
+		  "%0 >>= 8;"						      \
+		  : "=d" (__v)						      \
+		  : "d" (x));						      \
 	 __v; }))
-#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) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >>  8) |	      \
-      (((x) & 0x0000ff00u) <<  8) | (((x) & 0x000000ffu) << 24))
-
-#ifdef __GNUC__
-# define __bswap_32(x) \
+#define __bswap_non_constant_32(x) \
      (__extension__							      \
-      ({ register unsigned int __v, __x = (x);				      \
-	 if (__builtin_constant_p (__x))				      \
-	   __v = __bswap_constant_32 (__x);				      \
-	 else								      \
-	   __asm__ ("%1 = %0 >> 8 (V);"					      \
-		    "%0 = %0 << 8 (V);"					      \
-		    "%0 = %0 | %1;"					      \
-		    "%1 = PACK(%0.L, %0.H);"				      \
-		    : "+d"(__x), "=&d"(__v));				      \
+      ({ register unsigned int __v;					      \
+	 __asm__ ("%1 = %0 >> 8 (V);"					      \
+		  "%0 = %0 << 8 (V);"					      \
+		  "%0 = %0 | %1;"					      \
+		  "%1 = PACK(%0.L, %0.H);"				      \
+		  : "+d"(x), "=&d"(__v));				      \
 	 __v; }))
-#else
-static __inline unsigned int
-__bswap_32 (unsigned int __bsx)
-{
-  return __bswap_constant_32 (__bsx);
-}
-#endif
 
-#if defined __GNUC__ && __GNUC__ >= 2
-/* Swap bytes in 64 bit value.  */
-# define __bswap_constant_64(x) \
-     ((((x) & 0xff00000000000000ull) >> 56)				      \
-      | (((x) & 0x00ff000000000000ull) >> 40)				      \
-      | (((x) & 0x0000ff0000000000ull) >> 24)				      \
-      | (((x) & 0x000000ff00000000ull) >> 8)				      \
-      | (((x) & 0x00000000ff000000ull) << 8)				      \
-      | (((x) & 0x0000000000ff0000ull) << 24)				      \
-      | (((x) & 0x000000000000ff00ull) << 40)				      \
-      | (((x) & 0x00000000000000ffull) << 56))
-
-# define __bswap_64(x) \
-     (__extension__							      \
-      ({ union { __extension__ unsigned long long int __ll;		      \
-		 unsigned int __l[2]; } __w, __r;			      \
-         if (__builtin_constant_p (x))					      \
-	   __r.__ll = __bswap_constant_64 (x);				      \
-	 else								      \
-	   {								      \
-	     __w.__ll = (x);						      \
-	     __r.__l[0] = __bswap_32 (__w.__l[1]);			      \
-	     __r.__l[1] = __bswap_32 (__w.__l[0]);			      \
-	   }								      \
-	 __r.__ll; }))
 #endif
 
-#endif /* _BITS_BYTESWAP_H */
+#include <bits/byteswap-common.h>
diff --git a/libc/sysdeps/linux/common/bits/byteswap-common.h b/libc/sysdeps/linux/common/bits/byteswap-common.h
new file mode 100644
index 0000000..8586ed0
--- /dev/null
+++ b/libc/sysdeps/linux/common/bits/byteswap-common.h
@@ -0,0 +1,108 @@
+/* Macros to swap the order of bytes in integer values.
+   Copyright (C) 1997,1998,2000,2001,2002,2005 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 Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 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
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+#if !defined _BYTESWAP_H && !defined _NETINET_IN_H
+# error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead."
+#endif
+
+#ifndef _BITS_BYTESWAP_H
+#define _BITS_BYTESWAP_H 1
+
+/* Swap bytes in 16 bit value.  */
+#define __bswap_constant_16(x) \
+     ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8))
+
+#ifndef __bswap_non_constant_16
+# define __bswap_non_constant_16(x) __bswap_constant_16(x)
+#endif
+#ifdef __GNUC__
+# define __bswap_16(x) \
+    (__extension__							      \
+     ({ unsigned short int __bsv, __bsx = (x);				      \
+	if (__builtin_constant_p (__bsx))				      \
+	  __bsv = __bswap_constant_16 (__bsx);				      \
+	else								      \
+	  __bsv = __bswap_non_constant_16 (__bsx);			      \
+	__bsv; }))
+#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) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >>  8) |	      \
+      (((x) & 0x0000ff00u) <<  8) | (((x) & 0x000000ffu) << 24))
+
+#ifndef __bswap_non_constant_32
+# define __bswap_non_constant_32(x) __bswap_constant_32(x)
+#endif
+#ifdef __GNUC__
+# define __bswap_32(x) \
+    (__extension__							      \
+     ({ unsigned int __bsv, __bsx = (x);				      \
+	if (__builtin_constant_p (__bsx))				      \
+	  __bsv = __bswap_constant_32 (__bsx);				      \
+	else								      \
+	  __bsv = __bswap_non_constant_32 (__bsx);			      \
+	__bsv; }))
+#else
+static __inline unsigned int
+__bswap_32 (unsigned int __bsx)
+{
+  return __bswap_constant_32 (__bsx);
+}
+#endif
+
+#if defined __GNUC__ && __GNUC__ >= 2
+/* Swap bytes in 64 bit value.  */
+# define __bswap_constant_64(x) \
+     ((((x) & 0xff00000000000000ull) >> 56)				      \
+      | (((x) & 0x00ff000000000000ull) >> 40)				      \
+      | (((x) & 0x0000ff0000000000ull) >> 24)				      \
+      | (((x) & 0x000000ff00000000ull) >> 8)				      \
+      | (((x) & 0x00000000ff000000ull) << 8)				      \
+      | (((x) & 0x0000000000ff0000ull) << 24)				      \
+      | (((x) & 0x000000000000ff00ull) << 40)				      \
+      | (((x) & 0x00000000000000ffull) << 56))
+
+# ifndef __bswap_non_constant_64
+#  define __bswap_non_constant_64(x) \
+     (__extension__							      \
+      ({ union { __extension__ unsigned long long int __ll;		      \
+		 unsigned int __l[2]; } __w, __r;			      \
+	 __w.__ll = (x);						      \
+	 __r.__l[0] = __bswap_non_constant_32 (__w.__l[1]);		      \
+	 __r.__l[1] = __bswap_non_constant_32 (__w.__l[0]);		      \
+	 __r.__ll; }))
+# endif
+# define __bswap_64(x) \
+     (__extension__							      \
+      ({ __extension__ unsigned long long int __ll;			      \
+         if (__builtin_constant_p (x))					      \
+	   __ll = __bswap_constant_64 (x);				      \
+	 else								      \
+	   __ll = __bswap_non_constant_64 (x);				      \
+	 __ll; }))
+#endif
+
+#endif /* _BITS_BYTESWAP_H */
diff --git a/libc/sysdeps/linux/common/bits/byteswap.h b/libc/sysdeps/linux/common/bits/byteswap.h
index 949ed0b..3f02506 100644
--- a/libc/sysdeps/linux/common/bits/byteswap.h
+++ b/libc/sysdeps/linux/common/bits/byteswap.h
@@ -1,87 +1 @@
-/* Macros to swap the order of bytes in integer values.
-   Copyright (C) 1997,1998,2000,2001,2002,2005 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 Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 2.1 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
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public
-   License along with the GNU C Library; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-   02111-1307 USA.  */
-
-#if !defined _BYTESWAP_H && !defined _NETINET_IN_H
-# error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead."
-#endif
-
-#ifndef _BITS_BYTESWAP_H
-#define _BITS_BYTESWAP_H 1
-
-/* Swap bytes in 16 bit value.  */
-#define __bswap_constant_16(x) \
-     ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8))
-
-#ifdef __GNUC__
-# define __bswap_16(x) \
-    (__extension__							      \
-     ({ unsigned short int __bsx = (x); __bswap_constant_16 (__bsx); }))
-#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) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >>  8) |	      \
-      (((x) & 0x0000ff00u) <<  8) | (((x) & 0x000000ffu) << 24))
-
-#ifdef __GNUC__
-# define __bswap_32(x) \
-  (__extension__							      \
-   ({ register unsigned int __bsx = (x); __bswap_constant_32 (__bsx); }))
-#else
-static __inline unsigned int
-__bswap_32 (unsigned int __bsx)
-{
-  return __bswap_constant_32 (__bsx);
-}
-#endif
-
-#if defined __GNUC__ && __GNUC__ >= 2
-/* Swap bytes in 64 bit value.  */
-# define __bswap_constant_64(x) \
-     ((((x) & 0xff00000000000000ull) >> 56)				      \
-      | (((x) & 0x00ff000000000000ull) >> 40)				      \
-      | (((x) & 0x0000ff0000000000ull) >> 24)				      \
-      | (((x) & 0x000000ff00000000ull) >> 8)				      \
-      | (((x) & 0x00000000ff000000ull) << 8)				      \
-      | (((x) & 0x0000000000ff0000ull) << 24)				      \
-      | (((x) & 0x000000000000ff00ull) << 40)				      \
-      | (((x) & 0x00000000000000ffull) << 56))
-
-# define __bswap_64(x) \
-     (__extension__							      \
-      ({ union { __extension__ unsigned long long int __ll;		      \
-		 unsigned int __l[2]; } __w, __r;			      \
-         if (__builtin_constant_p (x))					      \
-	   __r.__ll = __bswap_constant_64 (x);				      \
-	 else								      \
-	   {								      \
-	     __w.__ll = (x);						      \
-	     __r.__l[0] = __bswap_32 (__w.__l[1]);			      \
-	     __r.__l[1] = __bswap_32 (__w.__l[0]);			      \
-	   }								      \
-	 __r.__ll; }))
-#endif
-
-#endif /* _BITS_BYTESWAP_H */
+#include <bits/byteswap-common.h>
diff --git a/libc/sysdeps/linux/cris/bits/byteswap.h b/libc/sysdeps/linux/cris/bits/byteswap.h
index 4027456..a82f9c0 100644
--- a/libc/sysdeps/linux/cris/bits/byteswap.h
+++ b/libc/sysdeps/linux/cris/bits/byteswap.h
@@ -1,83 +1,20 @@
-#ifndef _BITS_BYTESWAP_H
-#define _BITS_BYTESWAP_H 1
+#ifndef _ASM_BITS_BYTESWAP_H
+#define _ASM_BITS_BYTESWAP_H 1
 
 /* CRIS specific byte swap operations: 16, 32 and 64-bit */
 
-/* Swap bytes in 16 bit value.  */
-#define __bswap_constant_16(x) \
-({ \
-	unsigned short __x = (x); \
-	((unsigned short)( \
-		(((unsigned short)(__x) & (unsigned short)0x00ffu) << 8) |   \
-		(((unsigned short)(__x) & (unsigned short)0xff00u) >> 8) )); \
-})
-
-#if defined __GNUC__ && __GNUC__ >= 2
-#  define __bswap_16(x) \
+#define __bswap_non_constant_16(x) \
 	__extension__ 							\
 	({ unsigned short __bswap_16_v; 				\
-	   if (__builtin_constant_p (x)) 				\
-	   	__bswap_16_v = __bswap_constant_16 (x); 		\
-	   else 							\
-	   	__asm__ ("swapb %0" : "=r" (__bswap_16_v) : "0" (x)); 	\
+	   __asm__ ("swapb %0" : "=r" (__bswap_16_v) : "0" (x)); 	\
 	   __bswap_16_v; })
-#else
-#  define __bswap_16(x) __bswap_constant_16 (x)
-#endif
-
-
-/* Swap bytes in 32 bit value.  */
-#define __bswap_constant_32(x) \
-({ \
-	unsigned long __x = (x); \
-	((unsigned long)( \
-		(((unsigned long)(__x) & (unsigned long)0x000000fful) << 24) | \
-		(((unsigned long)(__x) & (unsigned long)0x0000ff00ul) <<  8) | \
-		(((unsigned long)(__x) & (unsigned long)0x00ff0000ul) >>  8) | \
-		(((unsigned long)(__x) & (unsigned long)0xff000000ul) >> 24) )); \
-})
 
-#if defined __GNUC__ && __GNUC__ >= 2
-#  define __bswap_32(x) \
+#define __bswap_non_constant_32(x) \
 	__extension__ 							\
-	({ unsigned long __bswap_32_v; 					\
-	   if (__builtin_constant_p (x)) 				\
-	   	__bswap_32_v = __bswap_constant_32 (x); 		\
-	   else 							\
-	   	__asm__ ("swapwb %0" : "=r" (__bswap_32_v) : "0" (x)); 	\
+	({ unsigned int __bswap_32_v; 					\
+	   __asm__ ("swapwb %0" : "=r" (__bswap_32_v) : "0" (x)); 	\
 	   __bswap_32_v; })
-#else
-#  define __bswap_32(x) __bswap_constant_32 (x)
-#endif
-
-
-/* Swap bytes in 64 bit value.  */
-# define __bswap_constant_64(x) \
-     ((((x) & 0xff00000000000000ull) >> 56)                                   \
-      | (((x) & 0x00ff000000000000ull) >> 40)                                 \
-      | (((x) & 0x0000ff0000000000ull) >> 24)                                 \
-      | (((x) & 0x000000ff00000000ull) >> 8)                                  \
-      | (((x) & 0x00000000ff000000ull) << 8)                                  \
-      | (((x) & 0x0000000000ff0000ull) << 24)                                 \
-      | (((x) & 0x000000000000ff00ull) << 40)                                 \
-      | (((x) & 0x00000000000000ffull) << 56))
 
-#if defined __GNUC__ && __GNUC__ >= 2
-# define __bswap_64(x) \
-     (__extension__                                                           \
-      ({ union { __extension__ unsigned long long int __ll;                   \
-                 unsigned int __l[2]; } __w, __r;                             \
-         if (__builtin_constant_p (x))                                        \
-           __r.__ll = __bswap_constant_64 (x);                                \
-         else                                                                 \
-           {                                                                  \
-             __w.__ll = (x);                                                  \
-             __r.__l[0] = __bswap_32 (__w.__l[1]);                            \
-             __r.__l[1] = __bswap_32 (__w.__l[0]);                            \
-           }                                                                  \
-         __r.__ll; }))
-#else
-#  define __bswap_64(x) __bswap_constant_64 (x)
 #endif
 
-#endif /* _BITS_BYTESWAP_H */
+#include <bits/byteswap-common.h>
diff --git a/libc/sysdeps/linux/h8300/bits/byteswap.h b/libc/sysdeps/linux/h8300/bits/byteswap.h
index dc56467..781a4db 100644
--- a/libc/sysdeps/linux/h8300/bits/byteswap.h
+++ b/libc/sysdeps/linux/h8300/bits/byteswap.h
@@ -17,51 +17,24 @@
    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.  */
 
-#if !defined _BYTESWAP_H && !defined _NETINET_IN_H
-# error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead."
-#endif
-
-/* Swap bytes in 16 bit value.  We don't provide an assembler version
-   because GCC is smart enough to generate optimal assembler output, and
-   this allows for better cse.  */
-#define __bswap_16(x) \
-  ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
+#ifndef _ASM_BITS_BYTESWAP_H
+#define _ASM_BITS_BYTESWAP_H 1
 
-/* 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
-# define __bswap_32(x) \
-  __extension__					\
-  ({ unsigned int __v;				\
-     if (__builtin_constant_p (x))		\
-       __v = __bswap_constant_32 (x);		\
-     else					\
-       __asm__ __volatile__ ("mov.l %0,er0\n\t"	\
-			     "mov.b r0l,r1h\n\t"	\
-			     "mov.b r0h,r1l\n\t"	\
-			     "mov.w r1,e1\n\t"	\
-			     "mov.w e0,r0\n\t"	\
-			     "mov.b r0l,r1h\n\t"	\
-			     "mov.b r0h,r1l\n\t"	\
-			     "mov.l er1,%0"	\
-			     : "=d" (__v)	\
-			     : "0" (x): "er0","er1");	\
+#define __bswap_non_constant_32(x) \
+  __extension__						\
+  ({ unsigned int __v;					\
+     __asm__ __volatile__ ("mov.l %0,er0\n\t"		\
+			   "mov.b r0l,r1h\n\t"		\
+			   "mov.b r0h,r1l\n\t"		\
+			   "mov.w r1,e1\n\t"		\
+			   "mov.w e0,r0\n\t"		\
+			   "mov.b r0l,r1h\n\t"		\
+			   "mov.b r0h,r1l\n\t"		\
+			   "mov.l er1,%0"		\
+			   : "=d" (__v)			\
+			   : "0" (x): "er0","er1");	\
      __v; })
-#else
-# define __bswap_32(x) __bswap_constant_32 (x)
-#endif
 
-#if defined __GNUC__ && __GNUC__ >= 2
-/* Swap bytes in 64 bit value.  */
-# define __bswap_64(x) \
-  __extension__						\
-  ({ union { unsigned long long int __ll;		\
-	     unsigned long int __l[2]; } __v, __r;	\
-     __v.__ll = (x);					\
-     __r.__l[0] = __bswap_32 (__v.__l[1]);		\
-     __r.__l[1] = __bswap_32 (__v.__l[0]);		\
-     __r.__ll; })
 #endif
+
+#include <bits/byteswap-common.h>
diff --git a/libc/sysdeps/linux/i386/bits/byteswap.h b/libc/sysdeps/linux/i386/bits/byteswap.h
index 32b2192..052eda3 100644
--- a/libc/sysdeps/linux/i386/bits/byteswap.h
+++ b/libc/sysdeps/linux/i386/bits/byteswap.h
@@ -17,117 +17,40 @@
    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA.  */
 
-#if !defined _BYTESWAP_H && !defined _NETINET_IN_H
-# error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead."
-#endif
-
-#ifndef _BITS_BYTESWAP_H
-#define _BITS_BYTESWAP_H 1
+#ifndef _ASM_BITS_BYTESWAP_H
+#define _ASM_BITS_BYTESWAP_H 1
 
-/* Swap bytes in 16 bit value.  */
-#define __bswap_constant_16(x) \
-     ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
-
-#ifdef __GNUC__
-# if __GNUC__ >= 2
-#  define __bswap_16(x) \
+#define __bswap_non_constant_16(x) \
      (__extension__							      \
-      ({ register unsigned short int __v, __x = (x);			      \
-	 if (__builtin_constant_p (__x))				      \
-	   __v = __bswap_constant_16 (__x);				      \
-	 else								      \
-	   __asm__ ("rorw $8, %w0"					      \
-		    : "=r" (__v)					      \
-		    : "0" (__x)						      \
-		    : "cc");						      \
+      ({ register unsigned short int __v;				      \
+	 __asm__ ("rorw $8, %w0"					      \
+		  : "=r" (__v)						      \
+		  : "0" (x)						      \
+		  : "cc");						      \
 	 __v; }))
-# else
-/* This is better than nothing.  */
-#  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))
 
-#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__ \
+#if !defined __i486__ && !defined __pentium__ && !defined __pentiumpro__ \
       && !defined __pentium4__
-#   define __bswap_32(x)						      \
+# define __bswap_non_constant_32(x) \
      (__extension__							      \
-      ({ register unsigned int __v, __x = (x);				      \
-	 if (__builtin_constant_p (__x))				      \
-	   __v = __bswap_constant_32 (__x);				      \
-	 else								      \
-	   __asm__ ("rorw $8, %w0;"					      \
-		    "rorl $16, %0;"					      \
-		    "rorw $8, %w0"					      \
-		    : "=r" (__v)					      \
-		    : "0" (__x)						      \
-		    : "cc");						      \
+      ({ register unsigned int __v;					      \
+	 __asm__ ("rorw $8, %w0;"					      \
+		  "rorl $16, %0;"					      \
+		  "rorw $8, %w0"					      \
+		  : "=r" (__v)						      \
+		  : "0" (x)						      \
+		  : "cc");						      \
 	 __v; }))
-#  else
-#   define __bswap_32(x) \
+#else
+# define __bswap_non_constant_32(x) \
      (__extension__							      \
-      ({ register unsigned int __v, __x = (x);				      \
-	 if (__builtin_constant_p (__x))				      \
-	   __v = __bswap_constant_32 (__x);				      \
-	 else								      \
-	   __asm__ ("bswap %0" : "=r" (__v) : "0" (__x));		      \
+      ({ register unsigned int __v;					      \
+	 __asm__ ("bswap %0" : "=r" (__v) : "0" (x));			      \
 	 __v; }))
-#  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
 
-
-#if defined __GNUC__ && __GNUC__ >= 2
-/* Swap bytes in 64 bit value.  */
-#define __bswap_constant_64(x) \
-     ((((x) & 0xff00000000000000ull) >> 56)				      \
-      | (((x) & 0x00ff000000000000ull) >> 40)				      \
-      | (((x) & 0x0000ff0000000000ull) >> 24)				      \
-      | (((x) & 0x000000ff00000000ull) >> 8)				      \
-      | (((x) & 0x00000000ff000000ull) << 8)				      \
-      | (((x) & 0x0000000000ff0000ull) << 24)				      \
-      | (((x) & 0x000000000000ff00ull) << 40)				      \
-      | (((x) & 0x00000000000000ffull) << 56))
-
-# define __bswap_64(x) \
-     (__extension__							      \
-      ({ union { __extension__ unsigned long long int __ll;		      \
-		 unsigned long int __l[2]; } __w, __r;			      \
-         if (__builtin_constant_p (x))					      \
-	   __r.__ll = __bswap_constant_64 (x);				      \
-	 else								      \
-	   {								      \
-	     __w.__ll = (x);						      \
-	     __r.__l[0] = __bswap_32 (__w.__l[1]);			      \
-	     __r.__l[1] = __bswap_32 (__w.__l[0]);			      \
-	   }								      \
-	 __r.__ll; }))
 #endif
 
-#endif /* _BITS_BYTESWAP_H */
+#include <bits/byteswap-common.h>
diff --git a/libc/sysdeps/linux/ia64/bits/byteswap.h b/libc/sysdeps/linux/ia64/bits/byteswap.h
index 6862aa0..346c82c 100644
--- a/libc/sysdeps/linux/ia64/bits/byteswap.h
+++ b/libc/sysdeps/linux/ia64/bits/byteswap.h
@@ -17,94 +17,36 @@
    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA.  */
 
-#if !defined _BYTESWAP_H && !defined _NETINET_IN_H
-# error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead."
-#endif
-
-#ifndef _BITS_BYTESWAP_H
-#define _BITS_BYTESWAP_H 1
-
-/* Swap bytes in 16 bit value.  */
-#define __bswap_constant_16(x) \
-     ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
+#ifndef _ASM_BITS_BYTESWAP_H
+#define _ASM_BITS_BYTESWAP_H 1
 
-#if defined __GNUC__ && __GNUC__ >= 2
-# define __bswap_16(x) \
+#define __bswap_non_constant_16(x) \
      (__extension__							      \
-      ({ register unsigned short int __v, __x = (x);			      \
-	 if (__builtin_constant_p (x))					      \
-	   __v = __bswap_constant_16 (__x);				      \
-	 else								      \
-	   __asm__ __volatile__ ("shl %0 = %1, 48 ;;"			      \
-				 "mux1 %0 = %0, @rev ;;"		      \
-				 : "=r" (__v)				      \
-				 : "r" ((unsigned short int) (__x)));	      \
+      ({ register unsigned short int __v;				      \
+	 __asm__ __volatile__ ("shl %0 = %1, 48 ;;"			      \
+				"mux1 %0 = %0, @rev ;;"			      \
+				: "=r" (__v)				      \
+				: "r" ((unsigned short int) (x)));	      \
 	 __v; }))
-#else
-/* This is better than nothing.  */
-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
-# define __bswap_32(x) \
+#define __bswap_non_constant_32(x) \
      (__extension__							      \
-      ({ register unsigned int __v, __x = (x);				      \
-	 if (__builtin_constant_p (x))					      \
-	   __v = __bswap_constant_32 (__x);				      \
-	 else								      \
-	   __asm__ __volatile__ ("shl %0 = %1, 32 ;;"			      \
-				 "mux1 %0 = %0, @rev ;;"		      \
-				 : "=r" (__v)				      \
-				 : "r" ((unsigned int) (__x)));		      \
+      ({ register unsigned int __v;					      \
+	 __asm__ __volatile__ ("shl %0 = %1, 32 ;;"			      \
+				"mux1 %0 = %0, @rev ;;"			      \
+				: "=r" (__v)				      \
+				: "r" ((unsigned int) (x)));		      \
 	 __v; }))
-#else
-static __inline unsigned int
-__bswap_32 (unsigned int __bsx)
-{
-  return __bswap_constant_32 (__bsx);
-}
-#endif
-
 
-/* Swap bytes in 64 bit value.  */
-#define __bswap_constant_64(x) \
-     ((((x) & 0xff00000000000000ul) >> 56)				      \
-      | (((x) & 0x00ff000000000000ul) >>  40)				      \
-      | (((x) & 0x0000ff0000000000ul) >> 24)				      \
-      | (((x) & 0x000000ff00000000ul) >> 8)				      \
-      | (((x) & 0x00000000ff000000ul) << 8)				      \
-      | (((x) & 0x0000000000ff0000ul) << 24)				      \
-      | (((x) & 0x000000000000ff00ul) << 40)				      \
-      | (((x) & 0x00000000000000fful) << 56))
-
-#if defined __GNUC__ && __GNUC__ >= 2
-# define __bswap_64(x) \
+#define __bswap_non_constant_64(x) \
      (__extension__							      \
-      ({ register unsigned long int __v, __x = (x);			      \
-	 if (__builtin_constant_p (x))					      \
-	   __v = __bswap_constant_64 (__x);				      \
-	 else								      \
-	   __asm__ __volatile__ ("mux1 %0 = %1, @rev ;;"		      \
-				 : "=r" (__v)				      \
-				 : "r" ((unsigned long int) (__x)));	      \
+      ({ register unsigned long int __v;				      \
+	 __asm__ __volatile__ ("mux1 %0 = %1, @rev ;;"			      \
+				: "=r" (__v)				      \
+				: "r" ((unsigned long int) (x)));	      \
          __v; }))
+#endif
 
-#else
-static __inline unsigned long int
-__bswap_64 (unsigned long int __bsx)
-{
-  return __bswap_constant_64 (__bsx);
-}
 #endif
 
-#endif /* _BITS_BYTESWAP_H */
+#include <bits/byteswap-common.h>
diff --git a/libc/sysdeps/linux/m68k/bits/byteswap.h b/libc/sysdeps/linux/m68k/bits/byteswap.h
index f5ec916..4884742 100644
--- a/libc/sysdeps/linux/m68k/bits/byteswap.h
+++ b/libc/sysdeps/linux/m68k/bits/byteswap.h
@@ -17,51 +17,21 @@
    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA.  */
 
-#if !defined _BYTESWAP_H && !defined _NETINET_IN_H
-# error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead."
-#endif
-
-#ifndef _BITS_BYTESWAP_H
-#define _BITS_BYTESWAP_H 1
-
-/* Swap bytes in 16 bit value.  We don't provide an assembler version
-   because GCC is smart enough to generate optimal assembler output, and
-   this allows for better cse.  */
-#define __bswap_16(x) \
-  ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
-
-/* Swap bytes in 32 bit value.  */
-#define __bswap_constant_32(x) \
-  ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) | \
-   (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))
+#ifndef _ASM_BITS_BYTESWAP_H
+#define _ASM_BITS_BYTESWAP_H 1
 
-#if defined __GNUC__ && __GNUC__ >= 2 && !defined __mcoldfire__
-# define __bswap_32(x) \
+#if !defined __mcoldfire__
+# define __bswap_non_constant_32(x) \
   __extension__							\
   ({ unsigned int __bswap_32_v;					\
-     if (__builtin_constant_p (x))				\
-       __bswap_32_v = __bswap_constant_32 (x);			\
-     else							\
-       __asm__ __volatile__ ("ror%.w %#8, %0;"			\
-			     "swap %0;"				\
-			     "ror%.w %#8, %0"			\
-			     : "=d" (__bswap_32_v)		\
-			     : "0" ((unsigned int) (x)));	\
+     __asm__ __volatile__ ("ror%.w %#8, %0;"			\
+			   "swap %0;"				\
+			   "ror%.w %#8, %0"			\
+			   : "=d" (__bswap_32_v)		\
+			   : "0" ((unsigned int) (x)));		\
      __bswap_32_v; })
-#else
-# define __bswap_32(x) __bswap_constant_32 (x)
 #endif
 
-#if defined __GNUC__ && __GNUC__ >= 2
-/* Swap bytes in 64 bit value.  */
-# define __bswap_64(x) \
-  __extension__								\
-  ({ union { unsigned long long int __ll;				\
-	     unsigned long int __l[2]; } __bswap_64_v, __bswap_64_r;	\
-     __bswap_64_v.__ll = (x);						\
-     __bswap_64_r.__l[0] = __bswap_32 (__bswap_64_v.__l[1]);		\
-     __bswap_64_r.__l[1] = __bswap_32 (__bswap_64_v.__l[0]);		\
-     __bswap_64_r.__ll; })
 #endif
 
-#endif /* _BITS_BYTESWAP_H */
+#include <bits/byteswap-common.h>
diff --git a/libc/sysdeps/linux/v850/bits/byteswap.h b/libc/sysdeps/linux/v850/bits/byteswap.h
index 44fbee4..b2e1ddb 100644
--- a/libc/sysdeps/linux/v850/bits/byteswap.h
+++ b/libc/sysdeps/linux/v850/bits/byteswap.h
@@ -11,53 +11,21 @@
  * directory of this archive for more details.
  */
 
-#if !defined _BYTESWAP_H && !defined _NETINET_IN_H
-# error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead."
-#endif
-
-/* Swap bytes in 16 bit value.  */
-#define __bswap_constant_16(x) \
-  ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
+#ifndef _ASM_BITS_BYTESWAP_H
+#define _ASM_BITS_BYTESWAP_H 1
 
-#ifdef __GNUC__
-# define __bswap_16(x)							      \
+#define __bswap_non_constant_16(x) \
     (__extension__							      \
      ({ unsigned long int __bswap_16_v;					      \
-        if (__builtin_constant_p (x))					      \
-          __bswap_16_v = __bswap_constant_16 (x);			      \
-        else								      \
-	  __asm__ ("bsh %1, %0" : "=r" (__bswap_16_v) : "r" (x));	      \
+	__asm__ ("bsh %1, %0" : "=r" (__bswap_16_v) : "r" (x));		      \
 	__bswap_16_v; }))
-#else
-# define __bswap_16(x) __bswap_constant_16 (x)
-#endif
 
-/* Swap bytes in 32 bit value.  */
-#define __bswap_constant_32(x) \
-  ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) | \
-   (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))
-
-#ifdef __GNUC__
-# define __bswap_32(x)							      \
+# define __bswap_non_constant_32(x) \
     (__extension__							      \
      ({ unsigned long int __bswap_32_v;					      \
-        if (__builtin_constant_p (x))					      \
-          __bswap_32_v = __bswap_constant_32 (x);			      \
-        else								      \
-	  __asm__ ("bsw %1, %0" : "=r" (__bswap_32_v) : "r" (x));	      \
+	__asm__ ("bsw %1, %0" : "=r" (__bswap_32_v) : "r" (x));		      \
 	__bswap_32_v; }))
-#else
-# define __bswap_32(x) __bswap_constant_32 (x)
-#endif
 
-#if defined __GNUC__ && __GNUC__ >= 2
-/* Swap bytes in 64 bit value.  */
-# define __bswap_64(x)							      \
-     (__extension__							      \
-      ({ union { unsigned long long int __ll;				      \
-		 unsigned long int __l[2]; } __bswap_64_v, __bswap_64_r;      \
-	 __bswap_64_v.__ll = (x);					      \
-	 __bswap_64_r.__l[0] = __bswap_32 (__bswap_64_v.__l[1]);	      \
-	 __bswap_64_r.__l[1] = __bswap_32 (__bswap_64_v.__l[0]);	      \
-	 __bswap_64_r.__ll; }))
 #endif
+
+#include <bits/byteswap-common.h>
diff --git a/libc/sysdeps/linux/x86_64/bits/byteswap.h b/libc/sysdeps/linux/x86_64/bits/byteswap.h
index e1c861c..a9cf23d 100644
--- a/libc/sysdeps/linux/x86_64/bits/byteswap.h
+++ b/libc/sysdeps/linux/x86_64/bits/byteswap.h
@@ -17,117 +17,52 @@
    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA.  */
 
-#if !defined _BYTESWAP_H && !defined _NETINET_IN_H
-# error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead."
-#endif
-
-#ifndef _BITS_BYTESWAP_H
-#define _BITS_BYTESWAP_H 1
+#ifndef _ASM_BITS_BYTESWAP_H
+#define _ASM_BITS_BYTESWAP_H 1
 
 #include <bits/wordsize.h>
 
-/* Swap bytes in 16 bit value.  */
-#define __bswap_constant_16(x) \
-     ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
-
-#if defined __GNUC__ && __GNUC__ >= 2
-# define __bswap_16(x) \
+#define __bswap_non_constant_16(x) \
      (__extension__							      \
-      ({ register unsigned short int __v, __x = (x);			      \
-	 if (__builtin_constant_p (__x))				      \
-	   __v = __bswap_constant_16 (__x);				      \
-	 else								      \
-	   __asm__ ("rorw $8, %w0"					      \
-		    : "=r" (__v)					      \
-		    : "0" (__x)						      \
-		    : "cc");						      \
+      ({ register unsigned short int __v;				      \
+	 __asm__ ("rorw $8, %w0"					      \
+		  : "=r" (__v)						      \
+		  : "0" (x)						      \
+		  : "cc");						      \
 	 __v; }))
-#else
-/* This is better than nothing.  */
-# define __bswap_16(x) \
-     (__extension__							      \
-      ({ register unsigned short int __x = (x); __bswap_constant_16 (__x); }))
-#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
-# if __WORDSIZE == 64 || (defined __i486__ || defined __pentium__	      \
-			  || defined __pentiumpro__ || defined __pentium4__   \
-			  || defined __k8__ || defined __athlon__	      \
-			  || defined __k6__)
+#if __WORDSIZE == 64 || (defined __i486__ || defined __pentium__	      \
+		     || defined __pentiumpro__ || defined __pentium4__	      \
+		     || defined __k8__ || defined __athlon__		      \
+		     || defined __k6__)
 /* To swap the bytes in a word the i486 processors and up provide the
    `bswap' opcode.  On i386 we have to use three instructions.  */
-#  define __bswap_32(x) \
+# define __bswap_non_constant_32(x) \
      (__extension__							      \
-      ({ register unsigned int __v, __x = (x);				      \
-	 if (__builtin_constant_p (__x))				      \
-	   __v = __bswap_constant_32 (__x);				      \
-	 else								      \
-	   __asm__ ("bswap %0" : "=r" (__v) : "0" (__x));		      \
+      ({ register unsigned int __v;					      \
+	 __asm__ ("bswap %0" : "=r" (__v) : "0" (x));			      \
 	 __v; }))
-# else
-#  define __bswap_32(x)							      \
-     (__extension__							      \
-      ({ register unsigned int __v, __x = (x);				      \
-	 if (__builtin_constant_p (__x))				      \
-	   __v = __bswap_constant_32 (__x);				      \
-	 else								      \
-	   __asm__ ("rorw $8, %w0;"					      \
-		    "rorl $16, %0;"					      \
-		    "rorw $8, %w0"					      \
-		    : "=r" (__v)					      \
-		    : "0" (__x)						      \
-		    : "cc");						      \
-	 __v; }))
-# endif
 #else
-# define __bswap_32(x) \
+# define __bswap_non_constant_32(x) \
      (__extension__							      \
-      ({ register unsigned int __x = (x); __bswap_constant_32 (__x); }))
+      ({ register unsigned int __v;					      \
+	 __asm__ ("rorw $8, %w0;"					      \
+		  "rorl $16, %0;"					      \
+		  "rorw $8, %w0"					      \
+		  : "=r" (__v)						      \
+		  : "0" (x)						      \
+		  : "cc");						      \
+	 __v; }))
 #endif
 
-
-#if defined __GNUC__ && __GNUC__ >= 2
-/* Swap bytes in 64 bit value.  */
-# define __bswap_constant_64(x) \
-     ((((x) & 0xff00000000000000ull) >> 56)				      \
-      | (((x) & 0x00ff000000000000ull) >> 40)				      \
-      | (((x) & 0x0000ff0000000000ull) >> 24)				      \
-      | (((x) & 0x000000ff00000000ull) >> 8)				      \
-      | (((x) & 0x00000000ff000000ull) << 8)				      \
-      | (((x) & 0x0000000000ff0000ull) << 24)				      \
-      | (((x) & 0x000000000000ff00ull) << 40)				      \
-      | (((x) & 0x00000000000000ffull) << 56))
-
-# if __WORDSIZE == 64
-#  define __bswap_64(x) \
+#if __WORDSIZE == 64
+# define __bswap_non_constant_64(x) \
      (__extension__							      \
-      ({ register unsigned long __v, __x = (x);				      \
-	 if (__builtin_constant_p (__x))				      \
-	   __v = __bswap_constant_64 (__x);				      \
-	 else								      \
-	   __asm__ ("bswap %q0" : "=r" (__v) : "0" (__x));		      \
+      ({ register unsigned long __v;					      \
+	 __asm__ ("bswap %q0" : "=r" (__v) : "0" (x));			      \
 	 __v; }))
-# else
-#  define __bswap_64(x) \
-     (__extension__                                                           \
-      ({ union { __extension__ unsigned long long int __ll;                   \
-                 unsigned int __l[2]; } __w, __r;                             \
-         if (__builtin_constant_p (x))                                        \
-           __r.__ll = __bswap_constant_64 (x);                                \
-         else                                                                 \
-           {                                                                  \
-             __w.__ll = (x);                                                  \
-             __r.__l[0] = __bswap_32 (__w.__l[1]);                            \
-             __r.__l[1] = __bswap_32 (__w.__l[0]);                            \
-           }                                                                  \
-         __r.__ll; }))
-# endif
 #endif
 
-#endif /* _BITS_BYTESWAP_H */
+#endif
+
+#include <bits/byteswap-common.h>
-- 
1.6.3.3



More information about the uClibc mailing list