Date: Thu, 5 Jan 2012 00:39:23 +0100 From: Ed Schouten <ed@80386.nl> To: Marcel Moolenaar <marcel@xcllnt.net> Cc: FreeBSD Arch <freebsd-arch@freebsd.org> Subject: Re: ntohq/htonq? Message-ID: <20120104233923.GY1895@hoeg.nl> In-Reply-To: <306FD881-6140-4DE2-AFF1-95C8079E4187@xcllnt.net> References: <306FD881-6140-4DE2-AFF1-95C8079E4187@xcllnt.net>
next in thread | previous in thread | raw e-mail | index | archive | help
--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 <marcel@xcllnt.net>, 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 <tgmath.h> 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 <sys/endian.h> attached to this email. --=20 Ed Schouten <ed@80386.nl> 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--
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20120104233923.GY1895>