Re: Endian portability #defines in GLIB



Perhaps we could add #defines for ntohs, htons, ntohl, and htonl
implemented in terms of the more verbose forms Tim suggests.

And why put arch-specific #if rules when autoconf can test this?  

-josh

Quoting Tim Janik (timj@gtk.org):
> On Sun, 18 Oct 1998, Manish Singh wrote:
> 
> > This was a feature requested by someone on irc, and I think it's good idea.
> > 
> > Simply put:
> > 
> > #ifdef WORDS_BIGENDIAN
> > #define G_BIG_ENDIAN
> > #else
> > #define G_LITTLE_ENDIAN
> > #endif
> > 
> > in glib.h. WORDS_BIGENDIAN is derived from the built-in autoconf test.
> > 
> > Just a small, simple addition to enhance glib as a portability lib.
> 
> this should probably go along with some conversion macros, i.e.
> seomthing like (from bytesex.h, endian.h and byteorder.h)
> 
> #define G_LITTLE_ENDIAN 1234
> #define G_BIG_ENDIAN    4321
> #define G_PDP_ENDIAN    3412
> 
> #if defined(__i386__) || defined(__alpha__) \
>         || (defined(__mips__) && (defined(MIPSEL) || defined (__MIPSEL__)))
> # define G_BYTE_ORDER   1234
> #elif defined(__mc68000__) || defined (__sparc__) || defined (__PPC__) \
>         || (defined(__mips__) && (defined(MIPSEB) || defined (__MIPSEB__)))
> # define G_BYTE_ORDER   4321
> #else /* need some PDP specific defines */
> # error architecture not supported by GLib
> #endif
> 
> #define	GULONG_SWAP_LE_BE(long_val)	(((gulong) \
>     (((gulong) (long_val)) & 0x000000ffU) << 24) | \
>     (((gulong) (long_val)) & 0x0000ff00U) <<  8) | \
>     (((gulong) (long_val)) & 0x00ff0000U) >>  8) | \
>     (((gulong) (long_val)) & 0xff000000U) >> 24)))
> #define	GULONG_SWAP_LE_PDP(long_val)	(((gulong) \
>     (((gulong) (long_val)) & 0x0000ffffU) << 16) | \
>     (((gulong) (long_val)) & 0xffff0000U) >> 16)))
> #define	GULONG_SWAP_BE_PDP(long_val)	(((gulong) \
>     (((gulong) (long_val)) & 0x000000ffU) << 8) | \
>     (((gulong) (long_val)) & 0x0000ff00U) >> 8) | \
>     (((gulong) (long_val)) & 0x00ff0000U) << 8) | \
>     (((gulong) (long_val)) & 0xff000000U) >> 8)))
> 
> 
> #if	G_BYTE_ORDER == G_LITTLE_ENDIAN
> #  define GLONG_TO_LE(long_val) 	((glong) (long_val))
> #  define GULONG_TO_LE(long_val) 	((gulong) (long_val))
> #  define GLONG_TO_BE(long_val) 	((glong) GULONG_SWAP_LE_BE (long_val))
> #  define GULONG_TO_BE(long_val) 	(GULONG_SWAP_LE_BE (long_val))
> #  define GLONG_FROM_LE(long_val) 	((glong) (long_val))
> #  define GULONG_FROM_LE(long_val) 	((gulong) (long_val))
> #  define GLONG_FROM_BE(long_val) 	((glong) GULONG_SWAP_LE_BE (long_val))
> #  define GULONG_FROM_BE(long_val) 	(GULONG_SWAP_LE_BE (long_val))
> #elif	G_BYTE_ORDER == G_BIG_ENDIAN
> #  define GLONG_TO_LE(long_val) 	((glong) GULONG_SWAP_LE_BE (long_val))
> #  define GULONG_TO_LE(long_val) 	(GULONG_SWAP_LE_BE (long_val))
> #  define GLONG_TO_BE(long_val) 	((glong) (long_val))
> #  define GULONG_TO_BE(long_val) 	((gulong) (long_val))
> #  define GLONG_FROM_LE(long_val) 	((glong) GULONG_SWAP_LE_BE (long_val))
> #  define GULONG_FROM_LE(long_val) 	(GULONG_SWAP_LE_BE (long_val))
> #  define GLONG_FROM_BE(long_val) 	((glong) (long_val))
> #  define GULONG_FROM_BE(long_val) 	((gulong) (long_val))
> #else
> /* PDP stuff */
> #endif



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]