Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 5 Apr 2003 17:08:11 +1000 (EST)
From:      Bruce Evans <bde@zeta.org.au>
To:        "M. Warner Losh" <imp@bsdimp.com>
Cc:        mike@FreeBSD.org
Subject:   Re: cvs commit: src/sys/sys endian.h src/share/man/man9 byteorder.9
Message-ID:  <20030405165535.X37179@gamplex.bde.org>
In-Reply-To: <20030404.160823.91026001.imp@bsdimp.com>
References:  <Pine.BSF.4.21.0304031545540.15187-100000@root.org> <20030404085200.GA1765@sunbay.com> <20030404.160823.91026001.imp@bsdimp.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Fri, 4 Apr 2003, M. Warner Losh wrote:

> In message: <20030404085200.GA1765@sunbay.com>
>             Ruslan Ermilov <ru@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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20030405165535.X37179>