This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
PATCH: Use__builtin_bswap[32|64] in i386/x86_64 bits/byteswap.h
- From: "H.J. Lu" <hjl dot tools at gmail dot com>
- To: Richard Henderson <rth at twiddle dot net>
- Cc: GNU C Library <libc-alpha at sourceware dot org>
- Date: Mon, 19 Mar 2012 10:33:59 -0700
- Subject: PATCH: Use__builtin_bswap[32|64] in i386/x86_64 bits/byteswap.h
On Sat, Mar 17, 2012 at 10:21 AM, Richard Henderson <rth@twiddle.net> wrote:
> On 03/17/2012 10:11 AM, H.J. Lu wrote:
>> GCC 4.7.0 defines __corei7__ for -march=corei7. ?This patch adds
>> __corei7__ check in i386/x86_64 bits/byteswap.h. ?OK to install?
>
> Isn't this (and the last couple added) recent enough that we can just
> defer to the gcc builtin? ?I.e.
>
> #if __GCC_PREREQ(4,2)
> # define __bswap_16(x) ?__builtin_bswap32((unsigned)(x) << 16)
> # define __bswap_32(x) ?__builtin_bswap32(x)
> #else
> ... rest
> #endif
>
Here is a patch to use __builtin_bswap[32|64] in i386/x86_64 bits/byteswap.h
for GCC >= 4.2. I didn't use __builtin_bswap32 for __bswap_16 since it is
less efficient:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52624
Thanks.
--
H.J.
--
2012-03-19 H.J. Lu <hongjiu.lu@intel.com>
* sysdeps/i386/bits/byteswap.h: Include <features.h>.
(__bswap_32): Use __builtin_bswap32 for GCC >= 4.2.
(__bswap_64): Use __builtin_bswap64 for GCC >= 4.2.
2012-03-19 H.J. Lu <hongjiu.lu@intel.com>
* sysdeps/i386/bits/byteswap.h: Include <features.h>.
(__bswap_32): Use __builtin_bswap32 for GCC >= 4.2.
(__bswap_64): Use __builtin_bswap64 for GCC >= 4.2.
* sysdeps/x86_64/bits/byteswap.h: Likewise.
diff --git a/sysdeps/i386/bits/byteswap.h b/sysdeps/i386/bits/byteswap.h
index 4a159d1..64a58ff 100644
--- a/sysdeps/i386/bits/byteswap.h
+++ b/sysdeps/i386/bits/byteswap.h
@@ -24,6 +24,8 @@
#ifndef _BITS_BYTESWAP_H
#define _BITS_BYTESWAP_H 1
+#include <features.h>
+
/* Swap bytes in 16 bit value. */
#define __bswap_constant_16(x) \
((unsigned short int) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)))
@@ -61,42 +63,8 @@ __bswap_16 (unsigned short int __bsx)
((((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__ \
- && !defined __pentium4__ && !defined __k8__ && !defined __athlon__ \
- && !defined __k6__ && !defined __nocona__ && !defined __core2__ \
- && !defined __geode__ && !defined __amdfam10__
-# 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; }))
-# else
-# define __bswap_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)); \
- __v; }))
-# endif
-# else
-# define __bswap_32(x) \
- (__extension__ \
- ({ register unsigned int __x = (x); __bswap_constant_32 (__x); }))
-# endif
+#if __GNUC_PREREQ (4,2)
+# define __bswap_32(x) __builtin_bswap32 (x)
#else
static __inline unsigned int
__bswap_32 (unsigned int __bsx)
@@ -108,7 +76,10 @@ __bswap_32 (unsigned int __bsx)
#if defined __GNUC__ && __GNUC__ >= 2
/* Swap bytes in 64 bit value. */
-# define __bswap_constant_64(x) \
+# if __GNUC_PREREQ (4,2)
+# define __bswap_64(x) __builtin_bswap64 (x)
+# else
+# define __bswap_constant_64(x) \
(__extension__ ((((x) & 0xff00000000000000ull) >> 56) \
| (((x) & 0x00ff000000000000ull) >> 40) \
| (((x) & 0x0000ff0000000000ull) >> 24) \
@@ -118,7 +89,7 @@ __bswap_32 (unsigned int __bsx)
| (((x) & 0x000000000000ff00ull) << 40) \
| (((x) & 0x00000000000000ffull) << 56)))
-# define __bswap_64(x) \
+# define __bswap_64(x) \
(__extension__ \
({ union { __extension__ unsigned long long int __ll; \
unsigned long int __l[2]; } __w, __r; \
@@ -131,6 +102,7 @@ __bswap_32 (unsigned int __bsx)
__r.__l[1] = __bswap_32 (__w.__l[0]); \
} \
__r.__ll; }))
+# endif
#endif
#endif /* _BITS_BYTESWAP_H */
diff --git a/sysdeps/x86_64/bits/byteswap.h b/sysdeps/x86_64/bits/byteswap.h
index 5094a05..d570d0c 100644
--- a/sysdeps/x86_64/bits/byteswap.h
+++ b/sysdeps/x86_64/bits/byteswap.h
@@ -24,6 +24,7 @@
#ifndef _BITS_BYTESWAP_H
#define _BITS_BYTESWAP_H 1
+#include <features.h>
#include <bits/wordsize.h>
/* Swap bytes in 16 bit value. */
@@ -56,38 +57,8 @@
((((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__ || defined __nocona__ \
- || defined __core2__ || defined __geode__ \
- || defined __amdfam10__)
-/* 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) \
- (__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)); \
- __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
+#if __GNUC_PREREQ (4,2)
+# define __bswap_32(x) __builtin_bswap32 (x)
#else
# define __bswap_32(x) \
(__extension__ \
@@ -97,7 +68,10 @@
#if defined __GNUC__ && __GNUC__ >= 2
/* Swap bytes in 64 bit value. */
-# define __bswap_constant_64(x) \
+# if __GNUC_PREREQ (4,2)
+# define __bswap_64(x) __builtin_bswap64 (x)
+# else
+# define __bswap_constant_64(x) \
(__extension__ ((((x) & 0xff00000000000000ull) >> 56) \
| (((x) & 0x00ff000000000000ull) >> 40) \
| (((x) & 0x0000ff0000000000ull) >> 24) \
@@ -107,8 +81,8 @@
| (((x) & 0x000000000000ff00ull) << 40) \
| (((x) & 0x00000000000000ffull) << 56)))
-# if __WORDSIZE == 64
-# define __bswap_64(x) \
+# if __WORDSIZE == 64
+# define __bswap_64(x) \
(__extension__ \
({ register unsigned long __v, __x = (x); \
if (__builtin_constant_p (__x)) \
@@ -116,8 +90,8 @@
else \
__asm__ ("bswap %q0" : "=r" (__v) : "0" (__x)); \
__v; }))
-# else
-# define __bswap_64(x) \
+# else
+# define __bswap_64(x) \
(__extension__ \
({ union { __extension__ unsigned long long int __ll; \
unsigned int __l[2]; } __w, __r; \
@@ -130,6 +104,7 @@
__r.__l[1] = __bswap_32 (__w.__l[0]); \
} \
__r.__ll; }))
+# endif
# endif
#endif