summaryrefslogtreecommitdiffstats
path: root/sys/i386
diff options
context:
space:
mode:
Diffstat (limited to 'sys/i386')
-rw-r--r--sys/i386/include/endian.h67
1 files changed, 38 insertions, 29 deletions
diff --git a/sys/i386/include/endian.h b/sys/i386/include/endian.h
index 6522ec4..c09dfb1 100644
--- a/sys/i386/include/endian.h
+++ b/sys/i386/include/endian.h
@@ -69,50 +69,59 @@ extern "C" {
#if defined(__GNUCLIKE_ASM) && defined(__GNUCLIKE_BUILTIN_CONSTANT_P)
-#define __byte_swap_int_var(x) \
-__extension__ ({ register __uint32_t __X = (x); \
- __asm ("bswap %0" : "+r" (__X)); \
- __X; })
-
-#ifdef __OPTIMIZE__
-
-#define __byte_swap_int_const(x) \
- ((((x) & 0xff000000) >> 24) | \
- (((x) & 0x00ff0000) >> 8) | \
- (((x) & 0x0000ff00) << 8) | \
- (((x) & 0x000000ff) << 24))
-#define __byte_swap_int(x) (__builtin_constant_p(x) ? \
- __byte_swap_int_const(x) : __byte_swap_int_var(x))
-
-#else /* __OPTIMIZE__ */
-
-#define __byte_swap_int(x) __byte_swap_int_var(x)
-
-#endif /* __OPTIMIZE__ */
+#define __bswap64_const(_x) \
+ (((_x) >> 56) | \
+ (((_x) >> 40) & (0xffULL << 8)) | \
+ (((_x) >> 24) & (0xffULL << 16)) | \
+ (((_x) >> 8) & (0xffULL << 24)) | \
+ (((_x) << 8) & (0xffULL << 32)) | \
+ (((_x) << 24) & (0xffULL << 40)) | \
+ (((_x) << 40) & (0xffULL << 48)) | \
+ ((_x) << 56))
+
+#define __bswap32_const(_x) \
+ (((_x) >> 24) | \
+ (((_x) & (0xff << 16)) >> 8) | \
+ (((_x) & (0xff << 8)) << 8) | \
+ ((_x) << 24))
+
+#define __bswap16_const(_x) (__uint16_t)((_x) << 8 | (_x) >> 8)
static __inline __uint64_t
-__bswap64(__uint64_t _x)
+__bswap64_var(__uint64_t __x)
{
- return ((_x >> 56) | ((_x >> 40) & 0xff00) | ((_x >> 24) & 0xff0000) |
- ((_x >> 8) & 0xff000000) | ((_x << 8) & ((__uint64_t)0xff << 32)) |
- ((_x << 24) & ((__uint64_t)0xff << 40)) |
- ((_x << 40) & ((__uint64_t)0xff << 48)) | ((_x << 56)));
+ return __bswap64_const(__x);
}
+
static __inline __uint32_t
-__bswap32(__uint32_t _x)
+__bswap32_var(__uint32_t _x)
{
- return (__byte_swap_int(_x));
+ __asm ("bswap %0" : "+r" (_x));
+ return (_x);
}
static __inline __uint16_t
-__bswap16(__uint16_t _x)
+__bswap16_var(__uint16_t _x)
{
- return (_x << 8 | _x >> 8);
+
+ return (__bswap16_const(_x));
}
+#define __bswap64(_x) \
+ (__builtin_constant_p(_x) ? \
+ __bswap64_const((__uint64_t)(_x)) : __bswap64_var(_x))
+
+#define __bswap32(_x) \
+ (__builtin_constant_p(_x) ? \
+ __bswap32_const((__uint32_t)(_x)) : __bswap32_var(_x))
+
+#define __bswap16(_x) \
+ (__builtin_constant_p(_x) ? \
+ __bswap16_const((__uint16_t)(_x)) : __bswap16_var(_x))
+
#define __htonl(x) __bswap32(x)
#define __htons(x) __bswap16(x)
#define __ntohl(x) __bswap32(x)
OpenPOWER on IntegriCloud