Subject: Endianness conversion functions
To: None <tech-misc@NetBSD.org>
From: Christian Biere <christianbiere@gmx.de>
List: tech-misc
Date: 01/18/2007 21:30:44
Hi,

are there any objections against the patch below? These could then
completely replace the almost identical code in sys/fs/cd9660/iso.h
as well as sys/fs/msdosfs/bpb.h and -DUNALIGNED_ACCESS could be removed
from the relevant Makefiles.

-- 
Christian

--- /usr/src/sys/sys/endian.h	2006-05-23 17:35:31.000000000 +0200
+++ endian.h	2007-01-18 21:03:06.000000000 +0100
@@ -193,107 +193,155 @@
 static __inline void __unused
 be16enc(void *buf, uint16_t u)
 {
+#if defined(__NO_STRICT_ALIGNMENT)
+	*(uint16_t *)buf = htobe16(u);
+#else
 	uint8_t *p = (uint8_t *)buf;
 
 	p[0] = ((unsigned)u >> 8) & 0xff;
 	p[1] = u & 0xff;
+#endif	/* __NO_STRICT_ALIGNMENT */
 }
 
 static __inline void __unused
 le16enc(void *buf, uint16_t u)
 {
+#if defined(__NO_STRICT_ALIGNMENT)
+	*(uint16_t *)buf = htole16(u);
+#else
 	uint8_t *p = (uint8_t *)buf;
 
 	p[0] = u & 0xff;
 	p[1] = ((unsigned)u >> 8) & 0xff;
+#endif	/* __NO_STRICT_ALIGNMENT */
 }
 
 static __inline uint16_t __unused
 be16dec(const void *buf)
 {
+#if defined(__NO_STRICT_ALIGNMENT)
+	return betoh16(*(const uint16_t *)buf);
+#else
 	const uint8_t *p = (const uint8_t *)buf;
 
 	return ((p[0] << 8) | p[1]);
+#endif	/* __NO_STRICT_ALIGNMENT */
 }
 
 static __inline uint16_t __unused
 le16dec(const void *buf)
 {
+#if defined(__NO_STRICT_ALIGNMENT)
+	return letoh16(*(const uint16_t *)buf);
+#else
 	const uint8_t *p = (const uint8_t *)buf;
 
 	return ((p[1] << 8) | p[0]);
+#endif	/* __NO_STRICT_ALIGNMENT */
 }
 
 static __inline void __unused
 be32enc(void *buf, uint32_t u)
 {
+#if defined(__NO_STRICT_ALIGNMENT)
+	return *(uint32_t *)buf = htobe32(u);
+#else
 	uint8_t *p = (uint8_t *)buf;
 
 	p[0] = (u >> 24) & 0xff;
 	p[1] = (u >> 16) & 0xff;
 	p[2] = (u >> 8) & 0xff;
 	p[3] = u & 0xff;
+#endif	/* __NO_STRICT_ALIGNMENT */
 }
 
 static __inline void __unused
 le32enc(void *buf, uint32_t u)
 {
+#if defined(__NO_STRICT_ALIGNMENT)
+	return *(uint32_t *)buf = htole32(u);
+#else
 	uint8_t *p = (uint8_t *)buf;
 
 	p[0] = u & 0xff;
 	p[1] = (u >> 8) & 0xff;
 	p[2] = (u >> 16) & 0xff;
 	p[3] = (u >> 24) & 0xff;
+#endif	/* __NO_STRICT_ALIGNMENT */
 }
 
 static __inline uint32_t __unused
 be32dec(const void *buf)
 {
+#if defined(__NO_STRICT_ALIGNMENT)
+	return be32toh(*(const uint32_t *)buf);
+#else
 	const uint8_t *p = (const uint8_t *)buf;
 
 	return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
+#endif	/* __NO_STRICT_ALIGNMENT */
 }
 
 static __inline uint32_t __unused
 le32dec(const void *buf)
 {
+#if defined(__NO_STRICT_ALIGNMENT)
+	return le32toh(*(const uint32_t *)buf);
+#else
 	const uint8_t *p = (const uint8_t *)buf;
 
 	return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
+#endif	/* __NO_STRICT_ALIGNMENT */
 }
 
 static __inline void __unused
 be64enc(void *buf, uint64_t u)
 {
+#if defined(__NO_STRICT_ALIGNMENT)
+	return *(uint64_t *)buf = htobe64(u);
+#else
 	uint8_t *p = (uint8_t *)buf;
 
 	be32enc(p, (uint32_t)(u >> 32));
 	be32enc(p + 4, (uint32_t)(u & 0xffffffffULL));
+#endif	/* __NO_STRICT_ALIGNMENT */
 }
 
 static __inline void __unused
 le64enc(void *buf, uint64_t u)
 {
+#if defined(__NO_STRICT_ALIGNMENT)
+	return *(uint64_t *)buf = htole64(u);
+#else
 	uint8_t *p = (uint8_t *)buf;
 
 	le32enc(p, (uint32_t)(u & 0xffffffffULL));
 	le32enc(p + 4, (uint32_t)(u >> 32));
+#endif	/* __NO_STRICT_ALIGNMENT */
 }
 
 static __inline uint64_t __unused
 be64dec(const void *buf)
 {
+#if defined(__NO_STRICT_ALIGNMENT)
+	return be64toh(*(const uint64_t *)buf);
+#else
 	const uint8_t *p = (const uint8_t *)buf;
 
 	return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
+#endif	/* __NO_STRICT_ALIGNMENT */
 }
 
 static __inline uint64_t __unused
 le64dec(const void *buf)
 {
+#if defined(__NO_STRICT_ALIGNMENT)
+	return le64toh(*(const uint64_t *)buf);
+#else
 	const uint8_t *p = (const uint8_t *)buf;
 
 	return (le32dec(p) | ((uint64_t)le32dec(p + 4) << 32));
+#endif	/* __NO_STRICT_ALIGNMENT */
 }
 
 #endif /* !_LOCORE */