cgv
Loading...
Searching...
No Matches
endian.h
1#pragma once
2
4
5namespace cgv {
6 namespace utils {
7
9enum class endian {
10#if defined(_MSC_VER) && !defined(__clang__)
11 little = 0, big = 1, native = little
12#else
13 little = __ORDER_LITTLE_ENDIAN__, big = __ORDER_BIG_ENDIAN__, native = __BYTE_ORDER__
14#endif
15};
16namespace detail {
17 void swap(uint8_t& a, uint8_t& b) { uint8_t t = a; a = b; b = t; }
18 template <uint32_t N> void byte_swap(uint8_t* b);
19 template <> void byte_swap<1>(uint8_t* b) { }
20 template <> void byte_swap<2>(uint8_t* b) { swap(b[0], b[1]); }
21 template <> void byte_swap<4>(uint8_t* b) { swap(b[0], b[3]); swap(b[1], b[2]); }
22 template <> void byte_swap<8>(uint8_t* b) { swap(b[0], b[7]); swap(b[1], b[6]); swap(b[2], b[5]); swap(b[3], b[4]); }
23
24 template <typename T, bool no_swap = endian::big == endian::native>
25 struct BE2native { static void exec(T& v) {}; };
26
27 template <typename T> struct BE2native<T, false> {
28 static void exec(T& v) { byte_swap<sizeof(T)>(reinterpret_cast<uint8_t*>(&v)); }
29 };
30}
32template <typename T> void big_endian_to_native(T& v) { detail::BE2native<T>::exec(v); }
33
34 }
35}
void big_endian_to_native(T &v)
convert value of a numeric type T from big endian to native endian of host
Definition endian.h:32
endian
enum providing access to native endian of host system
Definition endian.h:9
this header is dependency free
Definition print.h:11