diff --git a/Makefile.am b/Makefile.am
index 36bee967..ee3c1975 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -7,49 +7,51 @@ AM_LDFLAGS = $(BOOST_LDFLAGS) $(BOOST_FILESYSTEM_LIB) $(COMMON_LDFLAGS)
SUBDIRS = test
UTIL_INCLUDE = \
- annotations.hpp \
- backtrace.hpp \
- debug.hpp \
- enable_if.hpp \
- except.hpp \
- fixed.hpp \
- float.hpp \
- io.hpp \
- ip.hpp \
- json.hpp \
- maths.hpp \
- matrix.hpp \
- nocopy.hpp \
- point.hpp \
- pool.hpp \
- range.hpp \
- region.hpp \
- signal.hpp \
- stream.hpp \
- string.hpp \
- types.hpp \
- vector.hpp \
+ annotations.hpp \
+ backtrace.hpp \
+ debug.hpp \
+ enable_if.hpp \
+ endian.hpp \
+ except.hpp \
+ fixed.hpp \
+ float.hpp \
+ io.hpp \
+ ip.hpp \
+ json.hpp \
+ maths.hpp \
+ matrix.hpp \
+ nocopy.hpp \
+ point.hpp \
+ pool.hpp \
+ range.hpp \
+ region.hpp \
+ signal.hpp \
+ stream.hpp \
+ string.hpp \
+ types.hpp \
+ vector.hpp \
version.hpp
-UTIL_FILES = \
- debug.cpp \
- except.cpp \
- fixed.cpp \
- float.cpp \
- io.cpp \
- ip.cpp \
- json.cpp \
- maths.cpp \
- matrix.cpp \
- point.cpp \
- pool.cpp \
- range.cpp \
- region.cpp \
- signal.cpp \
- stream.cpp \
- string.cpp \
- types.cpp \
- vector.cpp \
+UTIL_FILES = \
+ debug.cpp \
+ endian.cpp \
+ except.cpp \
+ fixed.cpp \
+ float.cpp \
+ io.cpp \
+ ip.cpp \
+ json.cpp \
+ maths.cpp \
+ matrix.cpp \
+ point.cpp \
+ pool.cpp \
+ range.cpp \
+ region.cpp \
+ signal.cpp \
+ stream.cpp \
+ string.cpp \
+ types.cpp \
+ vector.cpp \
version.cpp
diff --git a/endian.cpp b/endian.cpp
new file mode 100644
index 00000000..25486464
--- /dev/null
+++ b/endian.cpp
@@ -0,0 +1,20 @@
+/*
+ * This file is part of libgim.
+ *
+ * libgim is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ *
+ * libgim is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with libgim. If not, see .
+ *
+ * Copyright 2010 Danny Robson
+ */
+
+#include "endian.hpp"
diff --git a/endian.hpp b/endian.hpp
new file mode 100644
index 00000000..5f26ed7d
--- /dev/null
+++ b/endian.hpp
@@ -0,0 +1,76 @@
+/*
+ * This file is part of libgim.
+ *
+ * libgim is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ *
+ * libgim is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with libgim. If not, see .
+ *
+ * Copyright 2010 Danny Robson
+ */
+
+#ifndef __UTIL_ENDIAN_HPP
+#define __UTIL_ENDIAN_HPP
+
+#include
+
+
+enum {
+ ENDIAN_BIG,
+ ENDIAN_LITTLE
+};
+
+
+constexpr uint8_t hton (uint8_t h) { return h; }
+constexpr uint8_t ntoh (uint8_t n) { return n; }
+
+
+constexpr uint16_t hton (uint16_t h) {
+ return ((h & 0xFF00) >> 8) |
+ ((h & 0x00FF) << 8);
+}
+
+constexpr uint16_t ntoh (uint16_t n)
+ { return hton (n); }
+
+
+constexpr uint32_t hton (uint32_t h) {
+ return __builtin_bswap32 (h);
+
+ //return (h & 0xFF000000U) >> 24 |
+ // (h & 0x00FF0000U) >> 8 |
+ // (h & 0x0000FF00U) << 8 |
+ // (h & 0x000000FFU) << 24;
+}
+
+
+constexpr uint32_t ntoh (uint32_t n)
+ { return hton (n); }
+
+
+constexpr uint64_t hton (uint64_t h) {
+ return __builtin_bswap64 (h);
+
+ // return (h & 0xFF00000000000000UL) >> 56 |
+ // (h & 0x00FF000000000000UL) >> 40 |
+ // (h & 0x0000FF0000000000UL) >> 24 |
+ // (h & 0x000000FF00000000UL) >> 8 |
+ // (h & 0x00000000FF000000UL) << 8 |
+ // (h & 0x0000000000FF0000UL) << 24 |
+ // (h & 0x000000000000FF00UL) << 40 |
+ // (h & 0x00000000000000FFUL) << 56;
+}
+
+
+constexpr uint64_t ntoh (uint64_t n)
+ { return hton (n); }
+
+#endif