From owner-freebsd-arch@FreeBSD.ORG Wed Jan 4 23:39:24 2012 Return-Path: Delivered-To: freebsd-arch@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 9F68C106566C for ; Wed, 4 Jan 2012 23:39:24 +0000 (UTC) (envelope-from ed@hoeg.nl) Received: from mx0.hoeg.nl (mx0.hoeg.nl [IPv6:2a01:4f8:101:5343::aa]) by mx1.freebsd.org (Postfix) with ESMTP id 131608FC12 for ; Wed, 4 Jan 2012 23:39:24 +0000 (UTC) Received: by mx0.hoeg.nl (Postfix, from userid 1000) id 40A542A2A387; Thu, 5 Jan 2012 00:39:23 +0100 (CET) Date: Thu, 5 Jan 2012 00:39:23 +0100 From: Ed Schouten To: Marcel Moolenaar Message-ID: <20120104233923.GY1895@hoeg.nl> References: <306FD881-6140-4DE2-AFF1-95C8079E4187@xcllnt.net> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="zNngJsVJxur72n6A" Content-Disposition: inline In-Reply-To: <306FD881-6140-4DE2-AFF1-95C8079E4187@xcllnt.net> User-Agent: Mutt/1.5.21 (2010-09-15) Cc: FreeBSD Arch Subject: Re: ntohq/htonq? X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 04 Jan 2012 23:39:24 -0000 --zNngJsVJxur72n6A Content-Type: multipart/mixed; boundary="GpIToxGFjxEkdZJB" Content-Disposition: inline --GpIToxGFjxEkdZJB Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Marcel, others, * Marcel Moolenaar , 20110914 04:36: > I did some googling and htonq and ntohq seem to be de > facto names used, but oddly enough no OS has them defined. > It's surreal. Are there better alternatives we should > migrate to? I just sent a patch to stefanf@ to change to be built on top of a new macro __generic(), which can act as a transition aid towards C11's _Generic(). If we really want to add support for htonq() and ntohq(), I would rather see us gain a hton() and ntoh() that are implemented as type-generic macros. That way we won't up having htonllll() by the year 2050. Also, it will allow people to `wrap' it into more fancy macros: #define DECODE_FIELD(x) do { foo->a_ ## x =3D ntoh(bar->b_ ## x); } while (= 0) Proof of concept patch (for illustration purposes only) for attached to this email. --=20 Ed Schouten WWW: http://80386.nl/ --GpIToxGFjxEkdZJB Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="endian.diff" Content-Transfer-Encoding: quoted-printable Index: sys/sys/cdefs.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 --- sys/sys/cdefs.h (revision 229516) +++ sys/sys/cdefs.h (working copy) @@ -248,6 +248,24 @@ #endif #endif =20 +/* + * Emulation of C11 _Generic(). Unlike the previously defined C11 + * keywords, it is not possible to implement this using exactly the same + * syntax. Therefore implement something similar under the name + * __generic(). Unlike _Generic(), this macro can only distinguish + * between a single type, so it requires nested invocations to + * distinguish multiple cases. + */ + +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >=3D 201112L +#define __generic(expr, t, yes, no) \ + _Generic(expr, t: yes, default: no) +#elif __GNUC_PREREQ__(3, 1) +#define __generic(expr, t, yes, no) \ + __builtin_choose_expr( \ + __builtin_types_compatible_p(__typeof(expr), t), yes, no) +#endif + #if __GNUC_PREREQ__(2, 96) #define __malloc_like __attribute__((__malloc__)) #define __pure __attribute__((__pure__)) Index: sys/sys/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 --- sys/sys/endian.h (revision 229516) +++ sys/sys/endian.h (working copy) @@ -202,4 +202,27 @@ le32enc(p + 4, (uint32_t)(u >> 32)); } =20 +#ifdef __generic + +/* + * Type-generic byte order macros. + */ + +#define __endian_op(x, func16, func32, func64) \ + __endian_op2(x, 8, x, \ + __endian_op2(x, 16, func16, \ + __endian_op2(x, 32, func32, \ + __endian_op2(x, 64, func64, (void)0)))) +#define __endian_op2(x, size, func, rest) \ + __generic(x, __uint ## size ## _t, func, \ + __generic(x, __int ## size ## _t, func, rest)) + +#define bswap(x) __endian_op(x, bswap16(x), bswap32(x), bswap64(x)) +#define htobe(x) __endian_op(x, htobe16(x), htobe32(x), htobe64(x)) +#define betoh(x) __endian_op(x, be16toh(x), be32toh(x), be64toh(x)) +#define htole(x) __endian_op(x, htole16(x), htole32(x), htole64(x)) +#define letoh(x) __endian_op(x, le16toh(x), le32toh(x), le64toh(x)) + +#endif /* __generic */ + #endif /* _SYS_ENDIAN_H_ */ --GpIToxGFjxEkdZJB-- --zNngJsVJxur72n6A Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (FreeBSD) iQIcBAEBAgAGBQJPBOMqAAoJEG5e2P40kaK71d4QAKP8oEk74xaVKOFzAaHqeYVi wrZ+CcOlGSmemQNGsk10co7+96LOxiaMjskShbY/ZiayONJTKTRHfee6wlQmaM4/ 8a843MDCf8pLbpYN4m1H7Yl98AyH5P5Rt4Mi7vloo6WPzQmueci9nm0JtIkHiARC 8buzpBqsuSODMpmRVbHd15JDTwUz1OWDWaJ+vkXKEbUpT00F12WTxxikiFls5iv7 ra1NOaLJgp7+I2b3dhoTFeymFFdHaRFravyl2VE38HxqpZfVC+zxJ+pdezyMNvA6 IhL6ya8BE+xEoapmA1Pmu9I9I6Cel7aIciGiNW+DpQDxQco6vO5bZKgN4g+H+VzG 8RAu2q7Wka/j17nmkFJ71h9bM9BXFeuoOnKCssiwZsZdjwbCr0onUkO1cIYwvXNX NWzA2QrG3Q/P2lX3cID8LpEKwHNXkUlzSF5nm6mqrMDmkUacN5blwlHIV0d/zTVj 9sYr+LugS4RJilthJSWZx40d5bNKa2pFnJvFoPbKkM+g9NGQRXEAlCtquoVzMBd4 XVOmOY6Lqfe13PsmA6cJvfx4t3S5wWFFT3tPC635jxnuuT3/gGJ7g4erdsgMk2u9 TUhe1wdgXcwy+ILPtTu/cuDjSHwoqbRhkQ+wyG8P2q34DpsI2DJYfap72Ip1veMv JO0Kit2XUfPr8jIYBHmK =Ht6m -----END PGP SIGNATURE----- --zNngJsVJxur72n6A--