From owner-svn-src-projects@FreeBSD.ORG Sat Mar 5 00:17:34 2011 Return-Path: Delivered-To: svn-src-projects@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id D827D1065676; Sat, 5 Mar 2011 00:17:34 +0000 (UTC) (envelope-from jeff@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id C6BE88FC13; Sat, 5 Mar 2011 00:17:34 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id p250HYdA077263; Sat, 5 Mar 2011 00:17:34 GMT (envelope-from jeff@svn.freebsd.org) Received: (from jeff@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id p250HYWE077260; Sat, 5 Mar 2011 00:17:34 GMT (envelope-from jeff@svn.freebsd.org) Message-Id: <201103050017.p250HYWE077260@svn.freebsd.org> From: Jeff Roberson Date: Sat, 5 Mar 2011 00:17:34 +0000 (UTC) To: src-committers@freebsd.org, svn-src-projects@freebsd.org X-SVN-Group: projects MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r219280 - in projects/ofed/head/sys: amd64/include i386/include X-BeenThere: svn-src-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the src " projects" tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 05 Mar 2011 00:17:34 -0000 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)