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

Ruslan Ermilov ru at FreeBSD.org
Fri Apr 4 12:15:01 PST 2003


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 
> > > > >     share/man/man9       byteorder.9 
> > > > >   Log:
> > > > >   Add inline functions {be,le}{16,32,64}{enc,dec}() for encoding decoding
> > > > >   into byte strings of unknown alignment.
> > 
> >  /*
> > + * 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 == _LITTLE_ENDIAN
> >  #define	htobe16(x)	bswap16((x))
> > +#define	HTOBE16(x)	BSWAP16((x))
> 
> I don't mind the addition of the macros but I don't like the
> implementation.  Too many unnecessary casts and overly complicated.
> 
I've borrowed them from sys/sparc64/include/endian.h.
The type casts are necessary, otherwise you'll get
``warning: left shift count >= 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 = 0xefcdab8967452301;

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


Cheers,
-- 
Ruslan Ermilov		Sysadmin and DBA,
ru at sunbay.com		Sunbay Software AG,
ru at 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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/cvs-all/attachments/20030404/680a9d4f/attachment.bin


More information about the cvs-all mailing list