Re: Endianness conversion functions

Christian Biere <[email protected]>
Newsgroups gmane.os.netbsd.devel.general
Message-ID <20070118233400.GB3234@cyclonus>
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.