Date: Fri, 30 Mar 2012 12:25:28 +0400 From: Andrey Chernov <ache@FreeBSD.ORG> To: Dimitry Andric <dim@FreeBSD.ORG> Cc: svn-src-head@FreeBSD.ORG, svn-src-all@FreeBSD.ORG, src-committers@FreeBSD.ORG Subject: Re: svn commit: r233684 - head/sys/x86/include Message-ID: <20120330082528.GA47173@vniz.net> In-Reply-To: <201203292331.q2TNVmwN014920@svn.freebsd.org> References: <201203292331.q2TNVmwN014920@svn.freebsd.org>
next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, Mar 29, 2012 at 11:31:48PM +0000, Dimitry Andric wrote: > However, the arguments are not properly masked, which results in the > wrong value being calculated in some instances. For example, > bswap32(0x12345678) returns 0x7c563412, and bswap64(0x123456789abcdef0) > returns 0xfcdefc9a7c563412. Is sign extension considered in that place? Shifting any signed value to ">>" direction (char, short, int, etc.) replicates sign bit, so cast to corresponding unsigned value must be done first, which may take less instructions, than masking (I am not sure about this part, just guessing). Casting in that case applies to the argument (x) not to result (x >> YY). > > #define __bswap16_gen(x) (__uint16_t)((x) << 8 | (x) >> 8) > #define __bswap32_gen(x) \ > - (((__uint32_t)__bswap16(x) << 16) | __bswap16((x) >> 16)) > + (((__uint32_t)__bswap16((x) & 0xffff) << 16) | __bswap16((x) >> 16)) > #define __bswap64_gen(x) \ > - (((__uint64_t)__bswap32(x) << 32) | __bswap32((x) >> 32)) > + (((__uint64_t)__bswap32((x) & 0xffffffff) << 32) | __bswap32((x) >> 32)) > > #ifdef __GNUCLIKE_BUILTIN_CONSTANT_P > #define __bswap16(x) \ -- http://ache.vniz.net/
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20120330082528.GA47173>