14#define LITTLE_ENDIAN 1234
17#define BIG_ENDIAN 4321
21#if REG_DWORD == REG_DWORD_LITTLE_ENDIAN
22#define BYTE_ORDER LITTLE_ENDIAN
23#elif REG_DWORD == REG_DWORD_BIG_ENDIAN
24#define BYTE_ORDER BIG_ENDIAN
26#elif defined(__APPLE__)
27#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
28#define BYTE_ORDER LITTLE_ENDIAN
29#elif __BYTE_ORDER == __ORDER_BIG_ENDIAN__
30#define BYTE_ORDER BIG_ENDIAN
34#if __BYTE_ORDER == __LITTLE_ENDIAN
35#define BYTE_ORDER LITTLE_ENDIAN
36#elif __BYTE_ORDER == __BIG_ENDIAN
37#define BYTE_ORDER BIG_ENDIAN
47 inline uint8_t byteswap(_In_
const uint8_t value)
52 inline uint16_t byteswap(_In_
const uint16_t value)
55 return _byteswap_ushort(value);
56#elif defined(_MSC_VER)
57 uint16_t t = (value & 0x00ff) << 8;
61 return __builtin_bswap16(value);
65 inline uint32_t byteswap(_In_
const uint32_t value)
68 return _byteswap_ulong(value);
69#elif defined(_MSC_VER)
70 uint32_t t = (value & 0x000000ff) << 24;
71 t |= (value & 0x0000ff00) << 8;
72 t |= (value & 0x00ff0000) >> 8;
76 return __builtin_bswap32(value);
80 inline uint64_t byteswap(_In_
const uint64_t value)
83 return _byteswap_uint64(value);
84#elif defined(_MSC_VER)
85 uint64_t t = (value & 0x00000000000000ff) << 56;
86 t |= (value & 0x000000000000ff00) << 40;
87 t |= (value & 0x0000000000ff0000) << 24;
88 t |= (value & 0x00000000ff000000) << 8;
89 t |= (value & 0x000000ff00000000) >> 8;
90 t |= (value & 0x0000ff0000000000) >> 24;
91 t |= (value & 0x00ff000000000000) >> 40;
95 return __builtin_bswap64(value);
99 inline int8_t byteswap(_In_
const char value) {
return byteswap(
static_cast<uint8_t
>(value)); }
100 inline int8_t byteswap(_In_
const int8_t value) {
return byteswap(
static_cast<uint8_t
>(value)); }
101 inline int16_t byteswap(_In_
const int16_t value) {
return byteswap(
static_cast<uint16_t
>(value)); }
102 inline int32_t byteswap(_In_
const int32_t value) {
return byteswap(
static_cast<uint32_t
>(value)); }
103 inline int64_t byteswap(_In_
const int64_t value) {
return byteswap(
static_cast<uint64_t
>(value)); }
104 inline float byteswap(_In_
const float value) {
return byteswap(*
reinterpret_cast<const uint32_t*
>(&value)); }
105 inline double byteswap(_In_
const double value) {
return byteswap(*
reinterpret_cast<const uint64_t*
>(&value)); }
107 inline void byteswap(_Inout_ uint8_t* value) { assert(value); *value = byteswap(*value); }
108 inline void byteswap(_Inout_ uint16_t* value) { assert(value); *value = byteswap(*value); }
109 inline void byteswap(_Inout_ uint32_t* value) { assert(value); *value = byteswap(*value); }
110 inline void byteswap(_Inout_ uint64_t* value) { assert(value); *value = byteswap(*value); }
112 inline void byteswap(_Inout_
char* value) { byteswap(
reinterpret_cast<uint8_t*
>(value)); }
113 inline void byteswap(_Inout_ int8_t* value) { byteswap(
reinterpret_cast<uint8_t*
>(value)); }
114 inline void byteswap(_Inout_ int16_t* value) { byteswap(
reinterpret_cast<uint16_t*
>(value)); }
115 inline void byteswap(_Inout_ int32_t* value) { byteswap(
reinterpret_cast<uint32_t*
>(value)); }
116 inline void byteswap(_Inout_ int64_t* value) { byteswap(
reinterpret_cast<uint64_t*
>(value)); }
117 inline void byteswap(_Inout_
float* value) { byteswap(
reinterpret_cast<uint32_t*
>(value)); }
118 inline void byteswap(_Inout_
double* value) { byteswap(
reinterpret_cast<uint64_t*
>(value)); }
121#if BYTE_ORDER == BIG_ENDIAN
122#define LE2HE(x) stdex::byteswap(x)
124#define HE2LE(x) stdex::byteswap(x)
128#define BE2HE(x) stdex::byteswap(x)
130#define HE2BE(x) stdex::byteswap(x)