Re: Endian portability #defines in GLIB
- From: Manish Singh <yosh gimp org>
- To: Josh MacDonald <jmacd cs berkeley edu>
- Cc: gtk-devel-list redhat com
- Subject: Re: Endian portability #defines in GLIB
- Date: Thu, 22 Oct 1998 17:20:43 -0700
> Perhaps we could add #defines for ntohs, htons, ntohl, and htonl
> implemented in terms of the more verbose forms Tim suggests.
Ok, I've revised the endian stuff somewhat. Especially, I've special
cased different sized types. If there aren't any objections, I will
commit this:
/* Portable endian checks and conversions
*/
#define G_LITTLE_ENDIAN 1234
#define G_BIG_ENDIAN 4321
#define G_PDP_ENDIAN 3412 /* unused, need specific PDP check */
#ifdef WORDS_BIGENDIAN
#define G_BYTE_ORDER G_BIG_ENDIAN
#else
#define G_BYTE_ORDER G_LITTLE_ENDIAN
#endif
#define GUINT16_SWAP_LE_BE(val) ((guint16) ( \
(((guint16) (val) & (guint16) 0x00ffU) << 8) | \
(((guint16) (val) & (guint16) 0xff00U) >> 8)))
#define GUINT32_SWAP_LE_BE(val) ((guint32) ( \
(((guint32) (val) & (guint32) 0x0x000000ffU) << 24) | \
(((guint32) (val) & (guint32) 0x0x0000ff00U) << 8) | \
(((guint32) (val) & (guint32) 0x0x00ff0000U) >> 8) | \
(((guint32) (val) & (guint32) 0x0xff000000U) >> 24)))
#ifdef HAVE_GINT64
#define GUINT64_SWAP_LE_BE(val) ((guint64) ( \
(((guint64) (val) & (guint64) 0x00000000000000ffU) << 56) | \
(((guint64) (val) & (guint64) 0x000000000000ff00U) << 40) | \
(((guint64) (val) & (guint64) 0x0000000000ff0000U) << 24) | \
(((guint64) (val) & (guint64) 0x00000000ff000000U) << 8) | \
(((guint64) (val) & (guint64) 0x000000ff00000000U) >> 8) | \
(((guint64) (val) & (guint64) 0x0000ff0000000000U) >> 24) | \
(((guint64) (val) & (guint64) 0x00ff000000000000U) >> 40) | \
(((guint64) (val) & (guint64) 0xff00000000000000U) >> 56)))
#endif
#define GUINT16_SWAP_LE_PDP(val) ((guint16) (val))
#define GUINT16_SWAP_BE_PDP(val) (GUINT16_SWAP_LE_BE (val))
#define GUINT32_SWAP_LE_PDP(val) ((guint32) ( \
(((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
(((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
#define GUINT32_SWAP_BE_PDP(val) ((guint32) ( \
(((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
(((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
# define GINT16_TO_LE(val) ((gint16) (val))
# define GUINT16_TO_LE(val) ((guint16) (val))
# define GINT16_TO_BE(val) ((gint16) GUINT16_SWAP_LE_BE (val))
# define GUINT16_TO_BE(val) (GUINT16_SWAP_LE_BE (val))
# define GINT16_FROM_LE(val) ((gint16) (val))
# define GUINT16_FROM_LE(val) ((guint16) (val))
# define GINT16_FROM_BE(val) ((gint16) GUINT16_SWAP_LE_BE (val))
# define GUINT16_FROM_BE(val) (GUINT16_SWAP_LE_BE (val))
# define GINT32_TO_LE(val) ((gint32) (val))
# define GUINT32_TO_LE(val) ((guint32) (val))
# define GINT32_TO_BE(val) ((gint32) GUINT32_SWAP_LE_BE (val))
# define GUINT32_TO_BE(val) (GUINT32_SWAP_LE_BE (val))
# define GINT32_FROM_LE(val) ((gint32) (val))
# define GUINT32_FROM_LE(val) ((guint32) (val))
# define GINT32_FROM_BE(val) ((gint32) GUINT32_SWAP_LE_BE (val))
# define GUINT32_FROM_BE(val) (GUINT32_SWAP_LE_BE (val))
# ifdef HAVE_GINT64
# define GINT64_TO_LE(val) ((gint64) (val))
# define GUINT64_TO_LE(val) ((guint64) (val))
# define GINT64_TO_BE(val) ((gint64) GUINT64_SWAP_LE_BE (val))
# define GUINT64_TO_BE(val) (GUINT64_SWAP_LE_BE (val))
# define GINT64_FROM_LE(val) ((gint64) (val))
# define GUINT64_FROM_LE(val) ((guint64) (val))
# define GINT64_FROM_BE(val) ((gint64) GUINT64_SWAP_LE_BE (val))
# define GUINT64_FROM_BE(val) (GUINT64_SWAP_LE_BE (val))
# endif
#elif G_BYTE_ORDER == G_BIG_ENDIAN
# define GINT16_TO_BE(val) ((gint16) (val))
# define GUINT16_TO_BE(val) ((guint16) (val))
# define GINT16_TO_BE(val) ((gint16) GUINT16_SWAP_LE_BE (val))
# define GUINT16_TO_BE(val) (GUINT16_SWAP_LE_BE (val))
# define GINT16_FROM_BE(val) ((gint16) (val))
# define GUINT16_FROM_BE(val) ((guint16) (val))
# define GINT16_FROM_LE(val) ((gint16) GUINT16_SWAP_LE_BE (val))
# define GUINT16_FROM_LE(val) (GUINT16_SWAP_LE_BE (val))
# define GINT32_TO_BE(val) ((gint32) (val))
# define GUINT32_TO_BE(val) ((guint32) (val))
# define GINT32_TO_LE(val) ((gint32) GUINT32_SWAP_LE_BE (val))
# define GUINT32_TO_LE(val) (GUINT32_SWAP_LE_BE (val))
# define GINT32_FROM_BE(val) ((gint32) (val))
# define GUINT32_FROM_BE(val) ((guint32) (val))
# define GINT32_FROM_LE(val) ((gint32) GUINT32_SWAP_LE_BE (val))
# define GUINT32_FROM_LE(val) (GUINT32_SWAP_LE_BE (val))
# ifdef HAVE_GINT64
# define GINT64_TO_BE(val) ((gint64) (val))
# define GUINT64_TO_BE(val) ((guint64) (val))
# define GINT64_TO_LE(val) ((gint64) GUINT64_SWAP_LE_BE (val))
# define GUINT64_TO_LE(val) (GUINT64_SWAP_LE_BE (val))
# define GINT64_FROM_BE(val) ((gint64) (val))
# define GUINT64_FROM_BE(val) ((guint64) (val))
# define GINT64_FROM_LE(val) ((gint64) GUINT64_SWAP_LE_BE (val))
# define GUINT64_FROM_LE(val) (GUINT64_SWAP_LE_BE (val))
# endif
#else
/* PDP stuff not implemented */
#endif
#if (SIZEOF_LONG == 8)
# define GLONG_TO_LE(val) ((glong) GINT64_TO_LE (val))
# define GULONG_TO_LE(val) ((gulong) GUINT64_TO_LE (val))
# define GLONG_TO_BE(val) ((glong) GINT64_TO_BE (val))
# define GULONG_TO_BE(val) ((gulong) GUINT64_TO_BE (val))
# define GLONG_FROM_LE(val) ((glong) GINT64_FROM_LE (val))
# define GULONG_FROM_LE(val) ((gulong) GUINT64_FROM_LE (val))
# define GLONG_FROM_BE(val) ((glong) GINT64_FROM_BE (val))
# define GULONG_FROM_BE(val) ((gulong) GUINT64_FROM_BE (val))
#elif (SIZEOF_LONG == 4)
# define GLONG_TO_LE(val) ((glong) GINT32_TO_LE (val))
# define GULONG_TO_LE(val) ((gulong) GUINT32_TO_LE (val))
# define GLONG_TO_BE(val) ((glong) GINT32_TO_BE (val))
# define GULONG_TO_BE(val) ((gulong) GUINT32_TO_BE (val))
# define GLONG_FROM_LE(val) ((glong) GINT32_FROM_LE (val))
# define GULONG_FROM_LE(val) ((gulong) GUINT32_FROM_LE (val))
# define GLONG_FROM_BE(val) ((glong) GINT32_FROM_BE (val))
# define GULONG_FROM_BE(val) ((gulong) GUINT32_FROM_BE (val))
#endif
#if (SIZEOF_INT == 8)
# define GINT_TO_LE(val) ((gint) GINT64_TO_LE (val))
# define GUINT_TO_LE(val) ((guint) GUINT64_TO_LE (val))
# define GINT_TO_BE(val) ((gint) GINT64_TO_BE (val))
# define GUINT_TO_BE(val) ((guint) GUINT64_TO_BE (val))
# define GINT_FROM_LE(val) ((gint) GINT64_FROM_LE (val))
# define GUINT_FROM_LE(val) ((guint) GUINT64_FROM_LE (val))
# define GINT_FROM_BE(val) ((gint) GINT64_FROM_BE (val))
# define GUINT_FROM_BE(val) ((guint) GUINT64_FROM_BE (val))
#elif (SIZEOF_INT == 4)
# define GINT_TO_LE(val) ((gint) GINT32_TO_LE (val))
# define GUINT_TO_LE(val) ((guint) GUINT32_TO_LE (val))
# define GINT_TO_BE(val) ((gint) GINT32_TO_BE (val))
# define GUINT_TO_BE(val) ((guint) GUINT32_TO_BE (val))
# define GINT_FROM_LE(val) ((gint) GINT32_FROM_LE (val))
# define GUINT_FROM_LE(val) ((guint) GUINT32_FROM_LE (val))
# define GINT_FROM_BE(val) ((gint) GINT32_FROM_BE (val))
# define GUINT_FROM_BE(val) ((guint) GUINT32_FROM_BE (val))
#elif (SIZEOF_INT == 2)
# define GINT_TO_LE(val) ((gint) GINT16_TO_LE (val))
# define GUINT_TO_LE(val) ((guint) GUINT16_TO_LE (val))
# define GINT_TO_BE(val) ((gint) GINT16_TO_BE (val))
# define GUINT_TO_BE(val) ((guint) GUINT16_TO_BE (val))
# define GINT_FROM_LE(val) ((gint) GINT16_FROM_LE (val))
# define GUINT_FROM_LE(val) ((guint) GUINT16_FROM_LE (val))
# define GINT_FROM_BE(val) ((gint) GINT16_FROM_BE (val))
# define GUINT_FROM_BE(val) ((guint) GUINT16_FROM_BE (val))
#endif
#define g_ntohl(val) (GUINT32_FROM_BE (val))
#define g_ntohs(val) (GUINT16_FROM_BE (val))
#define g_htonl(val) (GUINT32_TO_BE (val))
#define g_htons(val) (GUINT16_TO_BE (val))
-Yosh
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]