cvs commit: src/sys/sys endian.h src/share/man/man9 byteorder.9

Bruce Evans bde at zeta.org.au
Fri Apr 4 23:08:22 PST 2003


On Fri, 4 Apr 2003, M. Warner Losh wrote:

> In message: <20030404085200.GA1765 at sunbay.com>
>             Ruslan Ermilov <ru at FreeBSD.org> writes:
> : +#define	BSWAP64(x)	(uint64_t) \
> : +	(((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)))
>
> 0xffull or 0xffULL might be better than the casts here.  This does
> assume that 0ull == (uint64_t)0, which does hold for all our
> architectures...

It would be worse because it breaks compilation by C compilers (C90), but
not because it assumes that 0ull == (uint64_t)0.  Unsigned long long has
at least 64 bits in C99, and nothing more or less is needed for the casts
of the literal constants.

Casting of all of the instances of (x) that aren't masked later, and
of the result, is needed if x can have any type other than uint64_t.
E.g., if x is uint128_t and all of its bits are 1's then, (x) >> 56
is too large; if x is int64_t and negative and has any value other
than -1, then (x) >> 56 leaves too many bits set.

Bruce


More information about the cvs-src mailing list