diff --git a/include/duckhandy/endianness.hpp b/include/duckhandy/endianness.hpp
new file mode 100644
index 0000000..0da8d0c
--- /dev/null
+++ b/include/duckhandy/endianness.hpp
@@ -0,0 +1,179 @@
+/* Copyright 2016, 2017 Michele Santullo
+ * This file is part of "duckhandy".
+ *
+ * "duckhandy" 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.
+ *
+ * "duckhandy" 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 "duckhandy". If not, see .
+ */
+
+#ifndef id1A975372553B45BC8C4E42CDBDD97497
+#define id1A975372553B45BC8C4E42CDBDD97497
+
+#include
+#include
+
+namespace dhandy {
+ template constexpr T htobe (T parV);
+ template constexpr T htole (T parV);
+ template constexpr T betoh (T parV);
+ template constexpr T letoh (T parV);
+
+ template <>
+ [[gnu::pure]] inline constexpr uint8_t htobe (uint8_t parV) {
+ return parV;
+ }
+ template <>
+ [[gnu::pure]] inline constexpr uint16_t htobe (uint16_t parV) {
+#if __BYTE_ORDER == __BIG_ENDIAN
+ return parV;
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+ return __builtin_bswap16(parV);
+#else
+# error "Unsupported endianness"
+#endif
+ }
+ template <>
+ [[gnu::pure]] inline constexpr uint32_t htobe (uint32_t parV) {
+#if __BYTE_ORDER == __BIG_ENDIAN
+ return parV;
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+ return __builtin_bswap32(parV);
+#else
+# error "Unsupported endianness"
+#endif
+ }
+ template <>
+ [[gnu::pure]] inline constexpr uint64_t htobe (uint64_t parV) {
+#if __BYTE_ORDER == __BIG_ENDIAN
+ return parV;
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+ return __builtin_bswap64(parV);
+#else
+# error "Unsupported endianness"
+#endif
+ }
+ template <>
+ [[gnu::pure]] inline constexpr int8_t htobe (int8_t parV) {
+ return parV;
+ }
+ template <>
+ [[gnu::pure]] inline constexpr int16_t htobe (int16_t parV) {
+#if __BYTE_ORDER == __BIG_ENDIAN
+ return parV;
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+ return static_cast(__builtin_bswap16(static_cast(parV)));
+#else
+# error "Unsupported endianness"
+#endif
+ }
+ template <>
+ [[gnu::pure]] inline constexpr int32_t htobe (int32_t parV) {
+#if __BYTE_ORDER == __BIG_ENDIAN
+ return parV;
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+ return static_cast(__builtin_bswap32(static_cast(parV)));
+#else
+# error "Unsupported endianness"
+#endif
+ }
+ template <>
+ [[gnu::pure]] inline constexpr int64_t htobe (int64_t parV) {
+#if __BYTE_ORDER == __BIG_ENDIAN
+ return parV;
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+ return static_cast(__builtin_bswap64(static_cast(parV)));
+#else
+# error "Unsupported endianness"
+#endif
+ }
+
+ template <>
+ [[gnu::pure]] inline constexpr uint8_t htole (uint8_t parV) {
+ return parV;
+ }
+ template <>
+ [[gnu::pure]] inline constexpr uint16_t htole (uint16_t parV) {
+#if __BYTE_ORDER == __BIG_ENDIAN
+ return __builtin_bswap16(parV);
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+ return parV;
+#else
+# error "Unsupported endianness"
+#endif
+ }
+ template <>
+ [[gnu::pure]] inline constexpr uint32_t htole (uint32_t parV) {
+#if __BYTE_ORDER == __BIG_ENDIAN
+ return __builtin_bswap32(parV);
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+ return parV;
+#else
+# error "Unsupported endianness"
+#endif
+ }
+ template <>
+ [[gnu::pure]] inline constexpr uint64_t htole (uint64_t parV) {
+#if __BYTE_ORDER == __BIG_ENDIAN
+ return __builtin_bswap64(parV);
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+ return parV;
+#else
+# error "Unsupported endianness"
+#endif
+ }
+ template <>
+ [[gnu::pure]] inline constexpr int8_t htole (int8_t parV) {
+ return parV;
+ }
+ template <>
+ [[gnu::pure]] inline constexpr int16_t htole (int16_t parV) {
+#if __BYTE_ORDER == __BIG_ENDIAN
+ return static_cast(__builtin_bswap16(static_cast(parV)));
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+ return parV;
+#else
+# error "Unsupported endianness"
+#endif
+ }
+ template <>
+ [[gnu::pure]] inline constexpr int32_t htole (int32_t parV) {
+#if __BYTE_ORDER == __BIG_ENDIAN
+ return static_cast(__builtin_bswap32(static_cast(parV)));
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+ return parV;
+#else
+# error "Unsupported endianness"
+#endif
+ }
+ template <>
+ [[gnu::pure]] inline constexpr int64_t htole (int64_t parV) {
+#if __BYTE_ORDER == __BIG_ENDIAN
+ return static_cast(__builtin_bswap64(static_cast(parV)));
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+ return parV;
+#else
+# error "Unsupported endianness"
+#endif
+ }
+
+ template
+ [[gnu::pure]] inline constexpr T betoh (T parV) {
+ return htobe(parV);
+ }
+
+ template
+ [[gnu::pure]] inline constexpr T letoh (T parV) {
+ return htole(parV);
+ }
+} //namespace dhandy
+
+#endif
diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt
index 98e12db..4798277 100644
--- a/test/unit/CMakeLists.txt
+++ b/test/unit/CMakeLists.txt
@@ -3,6 +3,7 @@ project(dhandy_unit_test CXX)
add_executable(${PROJECT_NAME}
main.cpp
lexical_cast_test.cpp
+ endianness_test.cpp
)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD_REQUIRED ON)
diff --git a/test/unit/endianness_test.cpp b/test/unit/endianness_test.cpp
new file mode 100644
index 0000000..4e2d584
--- /dev/null
+++ b/test/unit/endianness_test.cpp
@@ -0,0 +1,120 @@
+/* Copyright 2017, Michele Santullo
+ * This file is part of "duckhandy".
+ *
+ * "duckhandy" 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.
+ *
+ * "duckhandy" 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 "duckhandy". If not, see .
+ */
+
+#include "catch.hpp"
+#include "duckhandy/endianness.hpp"
+#include
+
+TEST_CASE ("Host to little-endian", "[htole][endianness]") {
+ using dhandy::htole;
+#if __BYTE_ORDER == __BIG_ENDIAN
+ static_assert(htole(0x12) == 0x12, "Wrong conversion");
+ static_assert(htole(0x1234) == 0x3412, "Wrong conversion");
+ static_assert(htole(0x12345678) == 0x78563412, "Wrong conversion");
+ static_assert(htole(0x123456789ABCDEF0) == 0xF0DEBC9A78563412, "Wrong conversion");
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+ static_assert(htole(0x12) == 0x12, "Wrong conversion");
+ static_assert(htole(0x1234) == 0x1234, "Wrong conversion");
+ static_assert(htole(0x12345678) == 0x12345678, "Wrong conversion");
+ static_assert(htole(0x123456789ABCDEF0) == 0x123456789ABCDEF0, "Wrong conversion");
+#endif
+
+ CHECK(htole(0x12) == 0x12);
+ CHECK(htole(0x1234) == htole16(0x1234));
+ CHECK(htole(0x12345678) == htole32(0x12345678));
+ CHECK(htole(0x123456789ABCDEF0) == htole64(0x123456789ABCDEF0));
+
+ CHECK(htole(0xFE) == static_cast(0xFE));
+ CHECK(htole(0xFEDC) == static_cast(htole16(0xFEDC)));
+ CHECK(htole(0xFEDCBA09) == static_cast(htole32(0xFEDCBA09)));
+ CHECK(htole(0xFEDCBA0987654321) == static_cast(htole64(0xFEDCBA0987654321)));
+}
+
+TEST_CASE ("Little-endian to host", "[letoh][endianness]") {
+ using dhandy::letoh;
+#if __BYTE_ORDER == __BIG_ENDIAN
+ static_assert(letoh(0x12) == 0x12, "Wrong conversion");
+ static_assert(letoh(0x1234) == 0x3412, "Wrong conversion");
+ static_assert(letoh(0x12345678) == 0x78563412, "Wrong conversion");
+ static_assert(letoh(0x123456789ABCDEF0) == 0xF0DEBC9A78563412, "Wrong conversion");
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+ static_assert(letoh(0x12) == 0x12, "Wrong conversion");
+ static_assert(letoh(0x1234) == 0x1234, "Wrong conversion");
+ static_assert(letoh(0x12345678) == 0x12345678, "Wrong conversion");
+ static_assert(letoh(0x123456789ABCDEF0) == 0x123456789ABCDEF0, "Wrong conversion");
+#endif
+
+ CHECK(letoh(0x12) == 0x12);
+ CHECK(letoh(0x1234) == le16toh(0x1234));
+ CHECK(letoh(0x12345678) == le32toh(0x12345678));
+ CHECK(letoh(0x123456789ABCDEF0) == le64toh(0x123456789ABCDEF0));
+
+ CHECK(letoh(0xFE) == static_cast(0xFE));
+ CHECK(letoh(0xFEDC) == static_cast(le16toh(0xFEDC)));
+ CHECK(letoh(0xFEDCBA09) == static_cast(le32toh(0xFEDCBA09)));
+ CHECK(letoh(0xFEDCBA0987654321) == static_cast(le64toh(0xFEDCBA0987654321)));
+}
+
+TEST_CASE ("Host to big-endian", "[htobe][endianness]") {
+ using dhandy::htobe;
+#if __BYTE_ORDER == __BIG_ENDIAN
+ static_assert(htobe(0x12) == 0x12, "Wrong conversion");
+ static_assert(htobe(0x1234) == 0x1234, "Wrong conversion");
+ static_assert(htobe(0x12345678) == 0x12345678, "Wrong conversion");
+ static_assert(htobe(0x123456789ABCDEF0) == 0x123456789ABCDEF0, "Wrong conversion");
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+ static_assert(htobe(0x12) == 0x12, "Wrong conversion");
+ static_assert(htobe(0x1234) == 0x3412, "Wrong conversion");
+ static_assert(htobe(0x12345678) == 0x78563412, "Wrong conversion");
+ static_assert(htobe(0x123456789ABCDEF0) == 0xF0DEBC9A78563412, "Wrong conversion");
+#endif
+
+ CHECK(htobe(0x12) == 0x12);
+ CHECK(htobe(0x1234) == htobe16(0x1234));
+ CHECK(htobe(0x12345678) == htobe32(0x12345678));
+ CHECK(htobe(0x123456789ABCDEF0) == htobe64(0x123456789ABCDEF0));
+
+ CHECK(htobe(0xFE) == static_cast(0xFE));
+ CHECK(htobe(0xFEDC) == static_cast(htobe16(0xFEDC)));
+ CHECK(htobe(0xFEDCBA09) == static_cast(htobe32(0xFEDCBA09)));
+ CHECK(htobe(0xFEDCBA0987654321) == static_cast(htobe64(0xFEDCBA0987654321)));
+}
+
+TEST_CASE ("Big-endian to host", "[betoh][endianness]") {
+ using dhandy::betoh;
+#if __BYTE_ORDER == __BIG_ENDIAN
+ static_assert(betoh(0x12) == 0x12, "Wrong conversion");
+ static_assert(betoh(0x1234) == 0x1234, "Wrong conversion");
+ static_assert(betoh(0x12345678) == 0x12345678, "Wrong conversion");
+ static_assert(betoh(0x123456789ABCDEF0) == 0x123456789ABCDEF0, "Wrong conversion");
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+ static_assert(betoh(0x12) == 0x12, "Wrong conversion");
+ static_assert(betoh(0x1234) == 0x3412, "Wrong conversion");
+ static_assert(betoh(0x12345678) == 0x78563412, "Wrong conversion");
+ static_assert(betoh(0x123456789ABCDEF0) == 0xF0DEBC9A78563412, "Wrong conversion");
+#endif
+
+ CHECK(betoh(0x12) == 0x12);
+ CHECK(betoh(0x1234) == be16toh(0x1234));
+ CHECK(betoh(0x12345678) == be32toh(0x12345678));
+ CHECK(betoh(0x123456789ABCDEF0) == be64toh(0x123456789ABCDEF0));
+
+ CHECK(betoh(0xFE) == static_cast(0xFE));
+ CHECK(betoh(0xFEDC) == static_cast(be16toh(0xFEDC)));
+ CHECK(betoh(0xFEDCBA09) == static_cast(be32toh(0xFEDCBA09)));
+ CHECK(betoh(0xFEDCBA0987654321) == static_cast(be64toh(0xFEDCBA0987654321)));
+}