summaryrefslogtreecommitdiffstats
path: root/include/platform.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/platform.h')
-rw-r--r--include/platform.h88
1 files changed, 88 insertions, 0 deletions
diff --git a/include/platform.h b/include/platform.h
new file mode 100644
index 0000000..837c87d
--- /dev/null
+++ b/include/platform.h
@@ -0,0 +1,88 @@
+#ifndef VL_ENDIAN_H_INCLUDED
+#define VL_ENDIAN_H_INCLUDED
+
+#include "config.h"
+#include <stdint.h>
+#include <stddef.h>
+
+#ifdef __GNUC__
+#define VL_SWAP16_EXPR(_u) __builtin_bswap16(_u)
+#define VL_SWAP32_EXPR(_u) __builtin_bswap32(_u)
+#define VL_SWAP64_EXPR(_u) __builtin_bswap64(_u)
+#else
+
+#define VL_SWAP16_EXPR(_u) (uint16_t)((_u) << 8 | (_u) >> 8)
+
+#define VL_SWAP32_EXPR(_u) (uint32_t) \
+ ((((_u) & 0xFF000000u) >> 24) | \
+ (((_u) & 0x00FF0000u) >> 8) | \
+ (((_u) & 0x0000FF00u) << 8) | \
+ (((_u) & 0x000000FFu) << 24))
+
+#define VL_SWAP64_EXPR(_u) (uint64_t) \
+ ((((_u) & UINT64_C(0xFF00000000000000)) >> 56) | \
+ (((_u) & UINT64_C(0x00FF000000000000)) >> 40) | \
+ (((_u) & UINT64_C(0x0000FF0000000000)) >> 24) | \
+ (((_u) & UINT64_C(0x000000FF00000000)) >> 8) | \
+ (((_u) & UINT64_C(0x00000000FF000000)) << 8) | \
+ (((_u) & UINT64_C(0x0000000000FF0000)) << 24) | \
+ (((_u) & UINT64_C(0x000000000000FF00)) << 40) | \
+ (((_u) & UINT64_C(0x00000000000000FF)) << 56))
+
+#endif
+
+static inline uint16_t vl_swap16(uint16_t in)
+{
+ return VL_SWAP16_EXPR(in);
+}
+
+static inline uint32_t vl_swap32(uint32_t in)
+{
+ return VL_SWAP32_EXPR(in);
+}
+
+static inline uint64_t vl_swap64(uint64_t in)
+{
+ return VL_SWAP64_EXPR(in);
+}
+
+/* avoid leaking non-public macro definitions -- encourage the use of inline functions instead */
+#undef VL_SWAP16_EXPR
+#undef VL_SWAP32_EXPR
+#undef VL_SWAP64_EXPR
+
+#if VL_ENDIAN_LITTLE
+#define vl_htobe16(_x) vl_swap16(_x)
+#define vl_htobe32(_x) vl_swap32(_x)
+#define vl_htobe64(_x) vl_swap64(_x)
+
+#define vl_htole16(_x) (uint16_t)(_x)
+#define vl_htole32(_x) (uint32_t)(_x)
+#define vl_htole64(_x) (uint64_t)(_x)
+
+#define vl_betoh16(_x) vl_swap16(_x)
+#define vl_betoh32(_x) vl_swap32(_x)
+#define vl_betoh64(_x) vl_swap64(_x)
+
+#define vl_letoh16(_x) (uint16_t)(_x)
+#define vl_letoh32(_x) (uint32_t)(_x)
+#define vl_letoh64(_x) (uint64_t)(_x)
+#else /* VL_ENDIAN_BIG */
+#define vl_htobe16(_x) (uint16_t)(_x)
+#define vl_htobe32(_x) (uint32_t)(_x)
+#define vl_htobe64(_x) (uint64_t)(_x)
+
+#define vl_htole16(_x) vl_swap16(_x)
+#define vl_htole32(_x) vl_swap32(_x)
+#define vl_htole64(_x) vl_swap64(_x)
+
+#define vl_betoh16(_x) (uint16_t)(_x)
+#define vl_betoh32(_x) (uint32_t)(_x)
+#define vl_betoh64(_x) (uint64_t)(_x)
+
+#define vl_letoh16(_x) vl_swap16(_x)
+#define vl_letoh32(_x) vl_swap32(_x)
+#define vl_letoh64(_x) vl_swap64(_x)
+#endif
+
+#endif