Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 4 Apr 2003 11:52:00 +0300
From:      Ruslan Ermilov <ru@FreeBSD.org>
To:        Mike Barcroft <mike@FreeBSD.org>
Cc:        cvs-all@FreeBSD.org
Subject:   Re: cvs commit: src/sys/sys endian.h src/share/man/man9 byteorder.9
Message-ID:  <20030404085200.GA1765@sunbay.com>
In-Reply-To: <20030403184229.D18209@espresso.bsdmike.org>
References:  <20030403085821.AD2A537B407@hub.freebsd.org> <Pine.BSF.4.21.0304031545540.15187-100000@root.org> <20030403184229.D18209@espresso.bsdmike.org>

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

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

On Thu, Apr 03, 2003 at 06:42:29PM -0500, Mike Barcroft wrote:
> Nate Lawson <nate@root.org> writes:
> > 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 deco=
ding
> > >   into byte strings of unknown alignment.
> > >  =20
> > >   Revision  Changes    Path
> > >   1.3       +39 -1     src/share/man/man9/byteorder.9
> > >   1.3       +108 -0    src/sys/sys/endian.h
> >=20
> > 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?
>=20
> 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.
>=20
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
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
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)
=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 litt=
le
  * 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))
 #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))
=20
 #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 !=3D _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))
=20
 #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 =3D=3D _LITTLE_ENDIAN */
=20
 /* Alignment-agnostic encode/decode bytestream to/from little/big endian. =
*/
%%%


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

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

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

iD8DBQE+jUewUkv4P6juNwoRAq6+AJ0fvu3yZbZbdKa/MAQBJ3wAxDc1dgCfZkWZ
pwrb/x8UQgXCqD+7B3yAcjI=
=cPdH
-----END PGP SIGNATURE-----

--ew6BAiZeqk4r7MaW--



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