Date: Sat, 5 Mar 2011 00:17:34 +0000 (UTC) From: Jeff Roberson <jeff@FreeBSD.org> To: src-committers@freebsd.org, svn-src-projects@freebsd.org Subject: svn commit: r219280 - in projects/ofed/head/sys: amd64/include i386/include Message-ID: <201103050017.p250HYWE077260@svn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: jeff Date: Sat Mar 5 00:17:34 2011 New Revision: 219280 URL: http://svn.freebsd.org/changeset/base/219280 Log: - Tidy up endian conversions with input from bde. Consolidate casts, improve style, make the const macros more generic/mi friendly. This version keeps inlines and macros as this is the only combination that will work with gcc when byte order conversions are used in static initializations. Modified: projects/ofed/head/sys/amd64/include/endian.h projects/ofed/head/sys/i386/include/endian.h Modified: projects/ofed/head/sys/amd64/include/endian.h ============================================================================== --- projects/ofed/head/sys/amd64/include/endian.h Fri Mar 4 23:43:10 2011 (r219279) +++ projects/ofed/head/sys/amd64/include/endian.h Sat Mar 5 00:17:34 2011 (r219280) @@ -69,57 +69,58 @@ extern "C" { #if defined(__GNUCLIKE_ASM) && defined(__GNUCLIKE_BUILTIN_CONSTANT_P) -static __inline __uint32_t -__byte_swap_int_var(__uint32_t x) +#define __bswap64_const(_x) \ + (((_x) >> 56) | \ + (((_x) >> 40) & (0xffUL << 8)) | \ + (((_x) >> 24) & (0xffUL << 16)) | \ + (((_x) >> 8) & (0xffUL << 24)) | \ + (((_x) << 8) & (0xffUL << 32)) | \ + (((_x) << 24) & (0xffUL << 40)) | \ + (((_x) << 40) & (0xffUL << 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_var(__uint64_t _x) { - register __uint32_t _x; - _x = x; __asm ("bswap %0" : "+r" (_x)); return (_x); } -#define __byte_swap_int_const(x) \ - (__uint32_t)((((x) & 0xff000000) >> 24) | \ - (((x) & 0x00ff0000) >> 8) | \ - (((x) & 0x0000ff00) << 8) | \ - (((x) & 0x000000ff) << 24)) -#define __bswap32(x) (__builtin_constant_p(x) ? \ - __byte_swap_int_const(x) : __byte_swap_int_var(x)) - -static __inline __uint64_t -__byte_swap_long_var(__uint64_t x) +static __inline __uint32_t +__bswap32_var(__uint32_t _x) { - register __uint64_t _x; - _x = x; __asm ("bswap %0" : "+r" (_x)); return (_x); } -#define __byte_swap_long_const(x) \ - (__uint64_t)((((__uint64_t)x >> 56) | \ - (((__uint64_t)x >> 40) & 0xff00) | \ - (((__uint64_t)x >> 24) & 0xff0000) | \ - (((__uint64_t)x >> 8) & 0xff000000) | \ - (((__uint64_t)x << 8) & (0xffull << 32)) | \ - (((__uint64_t)x << 24) & (0xffull << 40)) | \ - (((__uint64_t)x << 40) & (0xffull << 48)) | \ - (((__uint64_t)x << 56)))) - -#define __bswap64(x) (__builtin_constant_p(x) ? \ - __byte_swap_long_const(x) : __byte_swap_long_var(x)) - -#define __byte_swap_short_const(_x) (__uint16_t)((_x) << 8 | (_x) >> 8) - static __inline __uint16_t -__byte_swap_short_var(__uint16_t x) +__bswap16_var(__uint16_t _x) { - return __byte_swap_short_const(x); + + return (__bswap16_const(_x)); } -#define __bswap16(x) (__builtin_constant_p(x) ? \ - __byte_swap_short_const(x) : __byte_swap_short_var(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) Modified: projects/ofed/head/sys/i386/include/endian.h ============================================================================== --- projects/ofed/head/sys/i386/include/endian.h Fri Mar 4 23:43:10 2011 (r219279) +++ projects/ofed/head/sys/i386/include/endian.h Sat Mar 5 00:17:34 2011 (r219280) @@ -69,46 +69,58 @@ extern "C" { #if defined(__GNUCLIKE_ASM) && defined(__GNUCLIKE_BUILTIN_CONSTANT_P) -static __inline __uint32_t -__byte_swap_int_var(__uint32_t x) +#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_var(__uint64_t __x) { - register __uint32_t _x; - _x = x; - __asm ("bswap %0" : "+r" (_x)); - return (_x); + return __bswap64_const(__x); } -#define __byte_swap_int_const(x) \ - (__uint32_t)((((x) & 0xff000000) >> 24) | \ - (((x) & 0x00ff0000) >> 8) | \ - (((x) & 0x0000ff00) << 8) | \ - (((x) & 0x000000ff) << 24)) -#define __bswap32(x) (__builtin_constant_p(x) ? \ - __byte_swap_int_const(x) : __byte_swap_int_var(x)) - -#define __byte_swap_64_const(x) \ - (__uint64_t)((((__uint64_t)x >> 56) | \ - (((__uint64_t)x >> 40) & 0xff00) | \ - (((__uint64_t)x >> 24) & 0xff0000) | \ - (((__uint64_t)x >> 8) & 0xff000000) | \ - (((__uint64_t)x << 8) & (0xffull << 32)) | \ - (((__uint64_t)x << 24) & (0xffull << 40)) | \ - (((__uint64_t)x << 40) & (0xffull << 48)) | \ - (((__uint64_t)x << 56)))) -#define __bswap64(x) __byte_swap_64_const(x) +static __inline __uint32_t +__bswap32_var(__uint32_t _x) +{ -#define __byte_swap_short_const(_x) (__uint16_t)((_x) << 8 | (_x) >> 8) + __asm ("bswap %0" : "+r" (_x)); + return (_x); +} static __inline __uint16_t -__byte_swap_short_var(__uint16_t x) +__bswap16_var(__uint16_t _x) { - return __byte_swap_short_const(x); + + return (__bswap16_const(_x)); } -#define __bswap16(x) (__builtin_constant_p(x) ? \ - __byte_swap_short_const(x) : __byte_swap_short_var(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)
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201103050017.p250HYWE077260>