cvs commit: src/sys/sys endian.h src/share/man/man9 byteorder.9
Ruslan Ermilov
ru at FreeBSD.org
Fri Apr 4 00:52:44 PST 2003
On Thu, Apr 03, 2003 at 06:42:29PM -0500, Mike Barcroft wrote:
> Nate Lawson <nate at root.org> writes:
> > 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.
> > >
> > > Revision Changes Path
> > > 1.3 +39 -1 src/share/man/man9/byteorder.9
> > > 1.3 +108 -0 src/sys/sys/endian.h
> >
> > This is really great! I have wanted this for a while. Just a few
> > questions. Have the standards folks had a look at the API? Second, it
> > appears the *enc functions have args in reverse order (void *, uint32_t
> > for example). Any thoughts on bcopy vs. memcpy ordering of args?
>
> There aren't any standards that have these type of functions. The
> closest thing is ntohl() and friends. We copied OpenBSD for the
> function and header names, so it might be worthwhile seeing if they
> want to provide these functions too.
>
Now that this is a hunting season on <sys/endian.h>, I'd like
to also add versions of "host to big/little endian" functions
suitable for use in initializers (computable at build time).
Would anyone seriously object to committing this?
I need these for kgzip(8) and btxld(8), FWIW. David, you are
supposed to like it too. ;)
%%%
Index: endian.h
===================================================================
RCS file: /home/ncvs/src/sys/sys/endian.h,v
retrieving revision 1.5
diff -u -r1.5 endian.h
--- endian.h 3 Apr 2003 11:32:01 -0000 1.5
+++ endian.h 4 Apr 2003 08:41:29 -0000
@@ -56,37 +56,77 @@
#define bswap64(x) __bswap64(x)
/*
+ * 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))
#define htobe32(x) bswap32((x))
+#define HTOBE32(x) BSWAP32((x))
#define htobe64(x) bswap64((x))
+#define HTOBE64(x) BSWAP64((x))
#define htole16(x) ((uint16_t)(x))
+#define HTOLE16(x) ((uint16_t)(x))
#define htole32(x) ((uint32_t)(x))
+#define HTOLE32(x) ((uint32_t)(x))
#define htole64(x) ((uint64_t)(x))
+#define HTOLE64(x) ((uint64_t)(x))
#define be16toh(x) bswap16((x))
+#define BE16TOH(x) BSWAP16((x))
#define be32toh(x) bswap32((x))
+#define BE32TOH(x) BSWAP32((x))
#define be64toh(x) bswap64((x))
+#define BE64TOH(x) BSWAP64((x))
#define le16toh(x) ((uint16_t)(x))
+#define LE16TOH(x) ((uint16_t)(x))
#define le32toh(x) ((uint32_t)(x))
+#define LE32TOH(x) ((uint32_t)(x))
#define le64toh(x) ((uint64_t)(x))
+#define LE64TOH(x) ((uint64_t)(x))
#else /* _BYTE_ORDER != _LITTLE_ENDIAN */
#define htobe16(x) ((uint16_t)(x))
+#define HTOBE16(x) ((uint16_t)(x))
#define htobe32(x) ((uint32_t)(x))
+#define HTOBE32(x) ((uint32_t)(x))
#define htobe64(x) ((uint64_t)(x))
+#define HTOBE64(x) ((uint64_t)(x))
#define htole16(x) bswap16((x))
+#define HTOLE16(x) BSWAP16((x))
#define htole32(x) bswap32((x))
+#define HTOLE32(x) BSWAP32((x))
#define htole64(x) bswap64((x))
+#define HTOLE64(x) BSWAP64((x))
#define be16toh(x) ((uint16_t)(x))
+#define BE16TOH(x) ((uint16_t)(x))
#define be32toh(x) ((uint32_t)(x))
+#define BE32TOH(x) ((uint32_t)(x))
#define be64toh(x) ((uint64_t)(x))
+#define BE64TOH(x) ((uint64_t)(x))
#define le16toh(x) bswap16((x))
+#define LE16TOH(x) BSWAP16((x))
#define le32toh(x) bswap32((x))
+#define LE32TOH(x) BSWAP32((x))
#define le64toh(x) bswap64((x))
+#define LE64TOH(x) BSWAP64((x))
#endif /* _BYTE_ORDER == _LITTLE_ENDIAN */
/* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
%%%
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-src/attachments/20030404/d145f995/attachment.bin
More information about the cvs-src
mailing list