aboutsummaryrefslogtreecommitdiffhomepage
path: root/util
diff options
context:
space:
mode:
authorGravatar David Bremner <david@tethera.net>2013-11-25 22:55:24 -0400
committerGravatar David Bremner <david@tethera.net>2013-11-27 07:43:29 -0400
commitb9f0e6923d645a044f837d61a9343ea16d56504e (patch)
treee87b30e7f490038ce9db6768fe7e1b02e4d98de6 /util
parent7f07bfd6d0189afcd9d260f6f560056dd3476f66 (diff)
util: detect byte order
Unfortunately old versions of GCC and clang do not provide byte order macros, so we re-invent them. If UTIL_BYTE_ORDER is not defined or defined to 0, we fall back to macros supported by recent versions of GCC and clang
Diffstat (limited to 'util')
-rw-r--r--util/endian-util.h38
1 files changed, 38 insertions, 0 deletions
diff --git a/util/endian-util.h b/util/endian-util.h
new file mode 100644
index 00000000..bc80c40b
--- /dev/null
+++ b/util/endian-util.h
@@ -0,0 +1,38 @@
+/* this file mimics the macros present in recent GCC and CLANG */
+
+#ifndef _ENDIAN_UTIL_H
+#define _ENDIAN_UTIL_H
+
+/* This are prefixed with UTIL to avoid collisions
+ *
+ * You can use something like the following to define UTIL_BYTE_ORDER
+ * in a configure script.
+ */
+#if 0
+#include <stdio.h>
+#include <stdint.h>
+uint32_t test = 0x34333231;
+int main() { printf("%.4s\n", (const char*)&test); return 0; }
+#endif
+
+#define UTIL_ORDER_BIG_ENDIAN 4321
+#define UTIL_ORDER_LITTLE_ENDIAN 1234
+
+
+#if !defined(UTIL_BYTE_ORDER) || ((UTIL_BYTE_ORDER != UTIL_ORDER_BIG_ENDIAN) && \
+ (UTIL_BYTE_ORDER != UTIL_ORDER_LITTLE_ENDIAN))
+#undef UTIL_BYTE_ORDER
+#ifdef __BYTE_ORDER__
+# if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
+# define UTIL_BYTE_ORDER UTIL_ORDER_LITTLE_ENDIAN
+# elif (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
+# define UTIL_BYTE_ORDER UTIL_ORDER_BIG_ENDIAN
+# else
+# error "Unsupported __BYTE_ORDER__"
+# endif
+#else
+# error "UTIL_BYTE_ORDER not correctly defined and __BYTE_ORDER__ not defined."
+#endif
+#endif
+
+#endif