summaryrefslogtreecommitdiffstats
path: root/include/osmocom/core
diff options
context:
space:
mode:
authorHolger Hans Peter Freyther <holger@moiji-mobile.com>2015-03-22 09:03:42 +0100
committerHolger Hans Peter Freyther <holger@moiji-mobile.com>2015-03-22 09:03:42 +0100
commit249a81b7ff57d39af843cd267b60c0ce0400ce8d (patch)
treeeefee95a51e4c9ffa2810bb993c074b0097cc6df /include/osmocom/core
parent789334640f2c34e58b9f3a8c19c57ac15bd2ed38 (diff)
endian: Be able to detect endian on GNU and BSD
On GNU __BYTE_ORDER and __LITTLE_ENDIAN is defined and the include is "endian.h" on FreeBSD it is "sys/endian.h" and LITTLE_ENDIAN/_LITTLE_ENDIAN and BYTE_ORDER/_BYTE_ORDER is defined. Create a header file that defines OSMO_IS_LITTLE_ENDIAN and OSMO_IS_BIG_ENDIAN and can be used as #if OSMO_IS_LITTLE_ENDIAN do_little_endian #elif OSMO_IS_BIG_ENDIAN do_big_endian #else #error "Unknown endian" #endif
Diffstat (limited to 'include/osmocom/core')
-rw-r--r--include/osmocom/core/endian.h38
1 files changed, 38 insertions, 0 deletions
diff --git a/include/osmocom/core/endian.h b/include/osmocom/core/endian.h
new file mode 100644
index 00000000..c890fd72
--- /dev/null
+++ b/include/osmocom/core/endian.h
@@ -0,0 +1,38 @@
+#pragma once
+
+/**
+ * GNU and FreeBSD have various ways to express the
+ * endianess but none of them is similiar enough. This
+ * will create two defines that allows to decide on the
+ * endian. The following will be defined to either 0 or
+ * 1 at the end of the file.
+ *
+ * OSMO_IS_LITTLE_ENDIAN
+ * OSMO_IS_BIG_ENDIAN
+ *
+ */
+
+#if defined(__FreeBSD__)
+#include <sys/endian.h>
+ #if BYTE_ORDER == LITTLE_ENDIAN
+ #define OSMO_IS_LITTLE_ENDIAN 1
+ #define OSMO_IS_BIG_ENDIAN 0
+ #elif BYTE_ORDER == BIG_ENDIAN
+ #define OSMO_IS_LITTLE_ENDIAN 0
+ #define OSMO_IS_BIG_ENDIAN 1
+ #else
+ #error "Unknown endian"
+ #endif
+#else
+#include <endian.h>
+ #if __BYTE_ORDER == __LITTLE_ENDIAN
+ #define OSMO_IS_LITTLE_ENDIAN 1
+ #define OSMO_IS_BIG_ENDIAN 0
+ #elif __BYTE_ORDER == __BIG_ENDIAN
+ #define OSMO_IS_LITTLE_ENDIAN 0
+ #define OSMO_IS_BIG_ENDIAN 1
+ #else
+ #error "Unknown endian"
+ #endif
+#endif
+