utils/bits.h: Add macros for swapping endianness in place.
authorMark Wooding <mdw@distorted.org.uk>
Sat, 26 May 2018 15:32:01 +0000 (16:32 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 26 May 2018 22:24:18 +0000 (23:24 +0100)
Many processors and compilers provide handy instructions for doing this,
and it's a handy primitive for loading and storing.

The HTOLw etc. macros turn out to be remarkably slippery to define.
It's unfortunately nonsensical to speak about an integer value having an
endianness.

utils/bits.3
utils/bits.h

index 7eb1e2b..7b1c715 100644 (file)
@@ -95,6 +95,23 @@ bits \- portable bit manipulation macros
 .\" @ROL64_
 .\" @ROR64_
 .\"
+.\" ENDSWAP16
+.\" ENDSWAP32
+.\" ENDSWAP64
+.\"
+.\" BTOH16
+.\" LTOH16
+.\" HTOB16
+.\" HTOL16
+.\" BTOH32
+.\" LTOH32
+.\" HTOB32
+.\" HTOL32
+.\" BTOH64
+.\" LTOH64
+.\" HTOB64
+.\" HTOL64
+.\"
 .\" @GETBYTE
 .\" @PUTBYTE
 .\"
@@ -370,6 +387,85 @@ left or right, respectively, by
 .I n
 places.
 .
+.SS "Byte order conversions"
+For each size suffix
+.IR w ,
+the macro invocation
+.BI ENDSWAP w ( x )
+returns the
+.IR w -bit
+value
+.IR x
+with its bytes reversed.  The
+.B ENDSWAP8
+macro does nothing (except truncate its operand to 8 bits), but is
+provided for the sake of completeness.
+.PP
+A
+.I big-endian
+representation stores the most significant octet of an integer at the
+lowest address, with the following octets in decreasing order of
+significance.  A
+.I little-endian
+representation instead stores the
+.I least
+significant octet at the lowest address, with the following octets in
+increasing order of significance.  An environment has a preferred order
+for arranging the constituent octets of an integer of some given size in
+memory; this might be either the big- or little-endian representation
+just described, or something else strange.
+.PP
+It might be possible to rearrange the bits in an integer so that, when
+that integer is stored to memory in the environment's preferred manner,
+you end up with the big- or little-endian representation of the original
+integer; and, similarly, it might be possible to load a big- or
+little-endian representation of an integer into a variable using the
+environment's preferred ordering and then rearrange the bits so as to
+recover the integer value originally represented.  If the environment is
+sufficiently strange, these things might not be possible, but this is
+actually quite rare.
+.PP
+Say that an integer has been converted to
+.I big-
+or
+.I "little-endian form"
+if, when it is stored in memory in the environment's preferred manner,
+one ends up with a big- or little-endian representation of the original
+integer.  Equivalently, if one starts with a big- or little-endian
+representation of some integer, and loads it into a variable using the
+environment's preferred manner, one ends up with the big- or
+little-endian form of the original integer.
+.PP
+If these things are possible, then the following macros are defined.
+.TP
+.BI HTOL w ( x )
+Convert a
+.IR w -bit
+integer
+.I x
+to little-endian form.
+.TP
+.BI HTOB w ( x )
+Convert a
+.IR w -bit
+integer
+.I x
+to big-endian form.
+.TP
+.BI LTOH w ( x )
+Convert a
+.IR w -bit
+integer
+.I x
+from little-endian form.
+.TP
+.BI BTOH w ( x )
+Convert a
+.IR w -bit
+integer
+.I x
+from big-endian form.
+.
 .SS "Load and store"
 The macro invocation
 .BI GETBYTE( p ", " o )
index 4279170..3b12f96 100644 (file)
@@ -314,6 +314,103 @@ typedef unsigned char octet, uint8;
    } while (0)
 #endif
 
+/* --- Endianness swapping --- */
+
+#ifndef ENDSWAP8
+#  define ENDSWAP8(x) U8(x)
+#endif
+#ifndef ENDSWAP16
+#  define ENDSWAP16(x)                                                 \
+       ((((uint16)(x) >> 8)&0xff) |                                    \
+        (((uint16)(x)&0xff) << 8))
+#endif
+#ifndef ENDSWAP24
+#  define ENDSWAP24(x)                                                 \
+       ((((uint24)(x) >> 16)&0xff) |                                   \
+        ((uint24)(x)&0xff00) |                                         \
+        ((uint24)((x)&0xff) << 16))
+#endif
+#ifndef ENDSWAP32
+#  define ENDSWAP32(x)                                                 \
+       (ENDSWAP16(((uint32)(x) >> 16)&0xffff) |                        \
+        ((uint32)ENDSWAP16((x)&0xffff) << 16))
+#endif
+#if defined(HAVE_UINT64) && !defined(ENDSWAP64)
+#  define ENDSWAP64(x)                                                 \
+       (ENDSWAP32(((uint64)(x) >> 32)&0xffffffff) |                    \
+        ((uint64)ENDSWAP32((x)&0xffffffff) << 32))
+#endif
+#ifdef HAVE_UINT64
+#  define ENDSWAP64_(z, x)                                             \
+       ((z).i = ENDSWAP64((x).i))
+#else
+#  define ENDSWAP64_(z, x)                                             \
+       ((z).lo = ENDSWAP32((x).hi),                                    \
+        (z).hi = ENDSWAP32((x).lo))
+#endif
+
+#define MLIB_LITTLE_ENDIAN 1234
+#define MLIB_BIG_ENDIAN 4321
+#if defined(__ORDER_LITTLE_ENDIAN__) &&                                        \
+    __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#  define MLIB_BYTE_ORDER MLIB_LITTLE_ENDIAN
+#elif defined(__ORDER_BIG_ENDIAN__) &&                                 \
+      __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+#  define MLIB_BYTE_ORDER MLIB_BIG_ENDIAN
+#endif
+
+#if MLIB_BYTE_ORDER == MLIB_LITTLE_ENDIAN
+#  define HTOL16(x) (x)
+#  define LTOH16(x) (x)
+#  define HTOB16(x) ENDSWAP16(x)
+#  define BTOH16(x) ENDSWAP16(x)
+#  define HTOL24(x) (x)
+#  define LTOH24(x) (x)
+#  define HTOB24(x) ENDSWAP24(x)
+#  define BTOH24(x) ENDSWAP24(x)
+#  define HTOL32(x) (x)
+#  define LTOH32(x) (x)
+#  define HTOB32(x) ENDSWAP32(x)
+#  define BTOH32(x) ENDSWAP32(x)
+#  ifdef HAVE_UINT64
+#    define HTOL64(x) (x)
+#    define LTOH64(x) (x)
+#    define HTOB64(x) ENDSWAP64(x)
+#    define BTOH64(x) ENDSWAP64(x)
+#  endif
+#  define HTOL64_(z, x) ASSIGN64(z, x)
+#  define LTOH64_(z, x) ASSIGN64(z, x)
+#  define HTOB64_(z, x) ENDSWAP64_(z, x)
+#  define BTOH64_(z, x) ENDSWAP64_(z, x)
+#elif MLIB_BYTE_ORDER == MLIB_BIG_ENDIAN
+#  define HTOL16(x) ENDSWAP16(x)
+#  define LTOH16(x) ENDSWAP16(x)
+#  define HTOB16(x) (x)
+#  define BTOH16(x) (x)
+#  define HTOL24(x) ENDSWAP24(x)
+#  define LTOH24(x) ENDSWAP24(x)
+#  define HTOB24(x) (x)
+#  define BTOH24(x) (x)
+#  define HTOL32(x) ENDSWAP32(x)
+#  define LTOH32(x) ENDSWAP32(x)
+#  define HTOB32(x) (x)
+#  define BTOH32(x) (x)
+#  ifdef HAVE_UINT64
+#    define HTOL64(x) ENDSWAP64(x)
+#    define LTOH64(x) ENDSWAP64(x)
+#    define HTOB64(x) (x)
+#    define BTOH64(x) (x)
+#    define HTOL64_(z, x) ENDSWAP64_(z, x)
+#    define LTOH64_(z, x) ENDSWAP64_(z, x)
+#    define HTOB64_(z, x) ((z).i = (x).i)
+#    define BTOH64_(z, x) ((z).i = (x).i)
+#  endif
+#  define HTOL64_(z, x) ENDSWAP64_(z, x)
+#  define LTOH64_(z, x) ENDSWAP64_(z, x)
+#  define HTOB64_(z, x) ASSIGN64(z, x)
+#  define BTOH64_(z, x) ASSIGN64(z, x)
+#endif
+
 /* --- Storage and retrieval --- */
 
 #define GETBYTE(p, o) (((octet *)(p))[o] & MASK8)