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 uint16_t byteswap(_In_
const uint16_t value)
50 return _byteswap_ushort(value);
51#elif defined(_MSC_VER)
52 uint16_t t = (value & 0x00ff) << 8;
56 return __builtin_bswap16(value);
60 inline uint32_t byteswap(_In_
const uint32_t value)
63 return _byteswap_ulong(value);
64#elif defined(_MSC_VER)
65 uint32_t t = (value & 0x000000ff) << 24;
66 t |= (value & 0x0000ff00) << 8;
67 t |= (value & 0x00ff0000) >> 8;
71 return __builtin_bswap32(value);
75 inline uint64_t byteswap(_In_
const uint64_t value)
78 return _byteswap_uint64(value);
79#elif defined(_MSC_VER)
80 uint64_t t = (value & 0x00000000000000ff) << 56;
81 t |= (value & 0x000000000000ff00) << 40;
82 t |= (value & 0x0000000000ff0000) << 24;
83 t |= (value & 0x00000000ff000000) << 8;
84 t |= (value & 0x000000ff00000000) >> 8;
85 t |= (value & 0x0000ff0000000000) >> 24;
86 t |= (value & 0x00ff000000000000) >> 40;
90 return __builtin_bswap64(value);
94 inline int16_t byteswap(_In_
const int16_t value) {
return byteswap(
static_cast<uint16_t
>(value)); }
95 inline int32_t byteswap(_In_
const int32_t value) {
return byteswap(
static_cast<uint32_t
>(value)); }
96 inline int64_t byteswap(_In_
const int64_t value) {
return byteswap(
static_cast<uint64_t
>(value)); }
97 inline float byteswap(_In_
const float value) {
return byteswap(*
reinterpret_cast<const uint32_t*
>(&value)); }
98 inline double byteswap(_In_
const double value) {
return byteswap(*
reinterpret_cast<const uint64_t*
>(&value)); }
100 inline void byteswap(_Inout_ uint16_t* value) { assert(value); *value = byteswap(*value); }
101 inline void byteswap(_Inout_ uint32_t* value) { assert(value); *value = byteswap(*value); }
102 inline void byteswap(_Inout_ uint64_t* value) { assert(value); *value = byteswap(*value); }
104 inline void byteswap(_Inout_ int16_t* value) { byteswap(
reinterpret_cast<uint16_t*
>(value)); }
105 inline void byteswap(_Inout_ int32_t* value) { byteswap(
reinterpret_cast<uint32_t*
>(value)); }
106 inline void byteswap(_Inout_ int64_t* value) { byteswap(
reinterpret_cast<uint64_t*
>(value)); }
107 inline void byteswap(_Inout_
float* value) { byteswap(
reinterpret_cast<uint32_t*
>(value)); }
108 inline void byteswap(_Inout_
double* value) { byteswap(
reinterpret_cast<uint64_t*
>(value)); }
111#if BYTE_ORDER == BIG_ENDIAN
112#define LE2HE(x) stdex::byteswap(x)
114#define HE2LE(x) stdex::byteswap(x)
118#define BE2HE(x) stdex::byteswap(x)
120#define HE2BE(x) stdex::byteswap(x)