svn commit: r233684 - head/sys/x86/include
Andrey Chernov
ache at FreeBSD.ORG
Fri Mar 30 08:25:35 UTC 2012
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/
More information about the svn-src-head
mailing list