summaryrefslogtreecommitdiff
path: root/include/util/endian.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'include/util/endian.hpp')
-rw-r--r--include/util/endian.hpp21
1 files changed, 21 insertions, 0 deletions
diff --git a/include/util/endian.hpp b/include/util/endian.hpp
new file mode 100644
index 0000000..18e49f6
--- /dev/null
+++ b/include/util/endian.hpp
@@ -0,0 +1,21 @@
+#pragma once
+
+#include <bit>
+#include <concepts>
+
+template <std::integral T>
+ requires(sizeof(T) <= 8)
+T to_little(T x)
+{
+ if constexpr (std::endian::native == std::endian::little || sizeof(T) == 1) {
+ return x;
+ }
+ switch (sizeof(T)) {
+ case 2:
+ return __builtin_bswap16(x);
+ case 4:
+ return __builtin_bswap32(x);
+ case 8:
+ return __builtin_bswap64(x);
+ }
+} \ No newline at end of file