15 #ifndef WPIUTIL_WPI_SWAPBYTEORDER_H
16 #define WPIUTIL_WPI_SWAPBYTEORDER_H
18 #include "wpi/Compiler.h"
21 #if defined(_MSC_VER) && !defined(_DEBUG)
30 inline uint16_t SwapByteOrder_16(uint16_t value) {
31 #if defined(_MSC_VER) && !defined(_DEBUG)
34 return _byteswap_ushort(value);
36 uint16_t Hi = value << 8;
37 uint16_t Lo = value >> 8;
44 inline uint32_t SwapByteOrder_32(uint32_t value) {
45 #if defined(__llvm__) || (LLVM_GNUC_PREREQ(4, 3, 0) && !defined(__ICC))
46 return __builtin_bswap32(value);
47 #elif defined(_MSC_VER) && !defined(_DEBUG)
48 return _byteswap_ulong(value);
50 uint32_t Byte0 = value & 0x000000FF;
51 uint32_t Byte1 = value & 0x0000FF00;
52 uint32_t Byte2 = value & 0x00FF0000;
53 uint32_t Byte3 = value & 0xFF000000;
54 return (Byte0 << 24) | (Byte1 << 8) | (Byte2 >> 8) | (Byte3 >> 24);
60 inline uint64_t SwapByteOrder_64(uint64_t value) {
61 #if defined(__llvm__) || (LLVM_GNUC_PREREQ(4, 3, 0) && !defined(__ICC))
62 return __builtin_bswap64(value);
63 #elif defined(_MSC_VER) && !defined(_DEBUG)
64 return _byteswap_uint64(value);
66 uint64_t Hi = SwapByteOrder_32(uint32_t(value));
67 uint32_t Lo = SwapByteOrder_32(uint32_t(value >> 32));
68 return (Hi << 32) | Lo;
72 inline unsigned char getSwappedBytes(
unsigned char C) {
return C; }
73 inline signed char getSwappedBytes(
signed char C) {
return C; }
74 inline char getSwappedBytes(
char C) {
return C; }
76 inline unsigned short getSwappedBytes(
unsigned short C) {
return SwapByteOrder_16(C); }
77 inline signed short getSwappedBytes(
signed short C) {
return SwapByteOrder_16(C); }
79 inline unsigned int getSwappedBytes(
unsigned int C) {
return SwapByteOrder_32(C); }
80 inline signed int getSwappedBytes(
signed int C) {
return SwapByteOrder_32(C); }
82 #if __LONG_MAX__ == __INT_MAX__
83 inline unsigned long getSwappedBytes(
unsigned long C) {
return SwapByteOrder_32(C); }
84 inline signed long getSwappedBytes(
signed long C) {
return SwapByteOrder_32(C); }
85 #elif __LONG_MAX__ == __LONG_LONG_MAX__
86 inline unsigned long getSwappedBytes(
unsigned long C) {
return SwapByteOrder_64(C); }
87 inline signed long getSwappedBytes(
signed long C) {
return SwapByteOrder_64(C); }
89 #error "Unknown long size!"
92 inline unsigned long long getSwappedBytes(
unsigned long long C) {
93 return SwapByteOrder_64(C);
95 inline signed long long getSwappedBytes(
signed long long C) {
96 return SwapByteOrder_64(C);
99 inline float getSwappedBytes(
float C) {
105 out.i = SwapByteOrder_32(in.i);
109 inline double getSwappedBytes(
double C) {
115 out.i = SwapByteOrder_64(in.i);
120 inline void swapByteOrder(T &Value) {
121 Value = getSwappedBytes(Value);