Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 4 Apr 2003 23:14:43 +0300
From:      Ruslan Ermilov <ru@FreeBSD.org>
To:        Nate Lawson <nate@root.org>
Cc:        Mike Barcroft <mike@FreeBSD.org>
Subject:   Re: cvs commit: src/sys/sys endian.h src/share/man/man9 byteorder.9
Message-ID:  <20030404201443.GA82587@sunbay.com>
In-Reply-To: <Pine.BSF.4.21.0304041150530.17052-100000@root.org>
References:  <20030404085200.GA1765@sunbay.com> <Pine.BSF.4.21.0304041150530.17052-100000@root.org>

next in thread | previous in thread | raw e-mail | index | archive | help

--G4iJoqBmSsgzjUCe
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Fri, Apr 04, 2003 at 11:54:07AM -0800, Nate Lawson wrote:
> On Fri, 4 Apr 2003, Ruslan Ermilov wrote:
> > > > On Thu, 3 Apr 2003, Poul-Henning Kamp wrote:
> > > > >   Modified files:
> > > > >     sys/sys              endian.h=20
> > > > >     share/man/man9       byteorder.9=20
> > > > >   Log:
> > > > >   Add inline functions {be,le}{16,32,64}{enc,dec}() for encoding =
decoding
> > > > >   into byte strings of unknown alignment.
> >=20
> >  /*
> > + * General byte order swapping macros.
> > + */
> > +#define	BSWAP16(x)	(uint16_t) \
> > +	(((x) >> 8) | ((x) << 8))
> > +
> > +#define	BSWAP32(x)	(uint32_t) \
> > +	(((x) >> 24) | (((x) >> 8) & 0xff00) | \
> > +	(((x) << 8) & 0xff0000) | ((x) << 24))
> > +
> > +#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)))
> > +
> > +/*
> >   * Host to big endian, host to little endian, big endian to host, and =
little
> >   * endian to host byte order functions as detailed in byteorder(9).
> >   */
> >  #if _BYTE_ORDER =3D=3D _LITTLE_ENDIAN
> >  #define	htobe16(x)	bswap16((x))
> > +#define	HTOBE16(x)	BSWAP16((x))
>=20
> I don't mind the addition of the macros but I don't like the
> implementation.  Too many unnecessary casts and overly complicated.
>=20
I've borrowed them from sys/sparc64/include/endian.h.
The type casts are necessary, otherwise you'll get
``warning: left shift count >=3D width of type'', and
it should be trivial to test with this program:

#include <sys/types.h>
#include <sys/endian.h>

#include <stdio.h>

int
main(void)
{
        uint64_t i =3D 0xefcdab8967452301;

        printf("%jx\n", i);
        i =3D BSWAP64(i);
        printf("%jx\n", i);
        return (0);
}


Cheers,
--=20
Ruslan Ermilov		Sysadmin and DBA,
ru@sunbay.com		Sunbay Software AG,
ru@FreeBSD.org		FreeBSD committer,
+380.652.512.251	Simferopol, Ukraine

http://www.FreeBSD.org	The Power To Serve
http://www.oracle.com	Enabling The Information Age

--G4iJoqBmSsgzjUCe
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (FreeBSD)

iD8DBQE+jeezUkv4P6juNwoRAo1DAJ9VpbRVojIELdXmWu5EQKBnNwBcvACfYhqm
WLhVhnjsjuc0UEfrJP4shfU=
=qe6b
-----END PGP SIGNATURE-----

--G4iJoqBmSsgzjUCe--



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