13#define LITTLE_ENDIAN 1234
16#define BIG_ENDIAN 4321
20#if REG_DWORD == REG_DWORD_LITTLE_ENDIAN
21#define BYTE_ORDER LITTLE_ENDIAN
22#elif REG_DWORD == REG_DWORD_BIG_ENDIAN
23#define BYTE_ORDER BIG_ENDIAN
25#elif defined(__APPLE__)
26#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
27#define BYTE_ORDER LITTLE_ENDIAN
28#elif __BYTE_ORDER == __ORDER_BIG_ENDIAN__
29#define BYTE_ORDER BIG_ENDIAN
33#if __BYTE_ORDER == __LITTLE_ENDIAN
34#define BYTE_ORDER LITTLE_ENDIAN
35#elif __BYTE_ORDER == __BIG_ENDIAN
36#define BYTE_ORDER BIG_ENDIAN
46 inline uint8_t byteswap(_In_
const uint8_t value)
51 inline uint16_t byteswap(_In_
const uint16_t value)
54 return _byteswap_ushort(value);
55#elif defined(_MSC_VER)
56 uint16_t t = (value & 0x00ff) << 8;
60 return __builtin_bswap16(value);
64 inline uint32_t byteswap(_In_
const uint32_t value)
67 return _byteswap_ulong(value);
68#elif defined(_MSC_VER)
69 uint32_t t = (value & 0x000000ff) << 24;
70 t |= (value & 0x0000ff00) << 8;
71 t |= (value & 0x00ff0000) >> 8;
75 return __builtin_bswap32(value);
79 inline uint64_t byteswap(_In_
const uint64_t value)
82 return _byteswap_uint64(value);
83#elif defined(_MSC_VER)
84 uint64_t t = (value & 0x00000000000000ff) << 56;
85 t |= (value & 0x000000000000ff00) << 40;
86 t |= (value & 0x0000000000ff0000) << 24;
87 t |= (value & 0x00000000ff000000) << 8;
88 t |= (value & 0x000000ff00000000) >> 8;
89 t |= (value & 0x0000ff0000000000) >> 24;
90 t |= (value & 0x00ff000000000000) >> 40;
94 return __builtin_bswap64(value);
98 inline int8_t byteswap(_In_
const char value) {
return byteswap(
static_cast<uint8_t
>(value)); }
99 inline int8_t byteswap(_In_
const int8_t value) {
return byteswap(
static_cast<uint8_t
>(value)); }
100 inline int16_t byteswap(_In_
const int16_t value) {
return byteswap(
static_cast<uint16_t
>(value)); }
101 inline int32_t byteswap(_In_
const int32_t value) {
return byteswap(
static_cast<uint32_t
>(value)); }
102 inline int64_t byteswap(_In_
const int64_t value) {
return byteswap(
static_cast<uint64_t
>(value)); }
104 inline float byteswap(_In_
const float value)
106 uint32_t r = byteswap(*
reinterpret_cast<const uint32_t*
>(&value));
107 return *
reinterpret_cast<float*
>(&r);
110 inline double byteswap(_In_
const double value)
112 uint64_t r = byteswap(*
reinterpret_cast<const uint64_t*
>(&value));
113 return *
reinterpret_cast<double*
>(&r);
116 inline void byteswap(_Inout_ uint8_t* value) { _Assume_(value); *value = byteswap(*value); }
117 inline void byteswap(_Inout_ uint16_t* value) { _Assume_(value); *value = byteswap(*value); }
118 inline void byteswap(_Inout_ uint32_t* value) { _Assume_(value); *value = byteswap(*value); }
119 inline void byteswap(_Inout_ uint64_t* value) { _Assume_(value); *value = byteswap(*value); }
121 inline void byteswap(_Inout_
char* value) { byteswap(
reinterpret_cast<uint8_t*
>(value)); }
122 inline void byteswap(_Inout_ int8_t* value) { byteswap(
reinterpret_cast<uint8_t*
>(value)); }
123 inline void byteswap(_Inout_ int16_t* value) { byteswap(
reinterpret_cast<uint16_t*
>(value)); }
124 inline void byteswap(_Inout_ int32_t* value) { byteswap(
reinterpret_cast<uint32_t*
>(value)); }
125 inline void byteswap(_Inout_ int64_t* value) { byteswap(
reinterpret_cast<uint64_t*
>(value)); }
126 inline void byteswap(_Inout_
float* value) { byteswap(
reinterpret_cast<uint32_t*
>(value)); }
127 inline void byteswap(_Inout_
double* value) { byteswap(
reinterpret_cast<uint64_t*
>(value)); }
130#if BYTE_ORDER == BIG_ENDIAN
131#define LE2HE(x) stdex::byteswap(x)
133#define HE2LE(x) stdex::byteswap(x)
137#define BE2HE(x) stdex::byteswap(x)
139#define HE2BE(x) stdex::byteswap(x)