Subject: Re: Endianness conversion functions
To: None <tech-misc@NetBSD.org>
From: Christian Biere <christianbiere@gmx.de>
List: tech-misc
Date: 01/19/2007 00:34:00
Christian Biere wrote:
> Krister Walfridsson wrote:
> > so I would prefer to instead
> > change sys/fs/cd9660/iso.h and sys/fs/msdosfs/bpb.h to use a correct
> > implementation (using e.g. memcpy, which for sufficiently new gcc
> > should generate as efficient code but without the potential lossage
> > arising from aliasing issues.)
> Even with -O0 there's no call to memcpy() only to this function. With -O1 the
> code is always inlined even if inline isn't specified. The applies to be32toh()
> except for additional byteswap code. The shift/or version is always slower at
> each optimization level. So it seems the memcpy() version should always be used
> - unless this doesn't apply to other archs.
So what about this?
#define __GEN_ENCODE(bits, endian) \
static __inline void __unused \
endian ## bits ## enc(void *buf, uint ## bits ## _t u) \
{ \
u = hto ## endian ## bits (u); \
memcpy(buf, &u, sizeof(u)); \
}
__GEN_ENCODE(16, be)
__GEN_ENCODE(32, be)
__GEN_ENCODE(64, be)
__GEN_ENCODE(16, le)
__GEN_ENCODE(32, le)
__GEN_ENCODE(64, le)
#undef __GEN_ENCODE
#define __GEN_DECODE(bits, endian) \
static __inline uint16_t __unused \
endian ## bits ## dec(const void *buf) \
{ \
uint ## bits ## _t u; \
memcpy(&u, buf, sizeof(u)); \
return endian ## bits ## toh (u); \
}
__GEN_DECODE(16, be)
__GEN_DECODE(32, be)
__GEN_DECODE(64, be)
__GEN_DECODE(16, le)
__GEN_DECODE(32, le)
__GEN_DECODE(64, le)
#undef __GEN_DECODE
--
Christian