[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Some improvements in ntoh and hton macro
From: |
bastien |
Subject: |
Some improvements in ntoh and hton macro |
Date: |
Sun, 20 Oct 2002 15:25:44 +0200 |
Where I write network code I think they are a lack about a ntoh function on
the 64 bits integers.
Moreover I think a type generic ntoh and hton macro (like type generic math
in tgmath.h ) is useful.
I implement this like :
/*begin code */
/* type generic bswap */
#define __bswap(x)\
( __extension__ (\
{\
__typeof__(x) __result=x;\
switch(sizeof(x))\
{\
case 1:__result=x;break;\
case 2:__result=__bswap_16(x);break;\
case 4:__result=__bswap_32(x);break;\
case 8:__result=__bswap_64(x);break;\
}\
__result;\
}))
/* We can optimize calls to the conversion functions. Either nothing has to
be
done or we are using directly the byte-swapping functions which often can be
inlined. */
# if __BYTE_ORDER == __BIG_ENDIAN
/* The host byte order is the same as network byte order, so these functions
are all just identity. */
# define ntohl(x) (x)
# define ntohs(x) (x)
# define htonl(x) (x)
# define htons(x) (x)
/* my improvement */
# define htonll(x) (x)
# define ntohll(x) (x)
# define ntoh(x) (x)
# define hton(x) (x)
/* end */
# else
# if __BYTE_ORDER == __LITTLE_ENDIAN
# define ntohl(x) __bswap_32 (x)
# define ntohs(x) __bswap_16 (x)
# define htonl(x) __bswap_32 (x)
# define htons(x) __bswap_16 (x)
/*my improvement */
# define htonll(x) __bswap_64 (x)
# define ntohll(x) __bswap_64 (x)
# define ntoh(x) __bswap (x)
# define hton(x) __bswap (x)
/*end */
# endif
# endif
/* end code */
Gcc optimize very well this with the O flag but I think It is a good idea to
define this only in _GNU_SOURCE scope
I hope a positive answer
___________________________________________________________
Do You Yahoo!? -- Une adresse @yahoo.fr gratuite et en français !
Yahoo! Mail : http://fr.mail.yahoo.com
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- Some improvements in ntoh and hton macro,
bastien <=