00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef LLVM_SYSTEM_SWAP_BYTE_ORDER_H
00016 #define LLVM_SYSTEM_SWAP_BYTE_ORDER_H
00017
00018 #include "llvm/Support/DataTypes.h"
00019 #include <cstddef>
00020 #include <limits>
00021
00022 namespace llvm {
00023 namespace sys {
00024
00027 inline uint16_t SwapByteOrder_16(uint16_t value) {
00028 #if defined(_MSC_VER) && !defined(_DEBUG)
00029
00030
00031 return _byteswap_ushort(value);
00032 #else
00033 uint16_t Hi = value << 8;
00034 uint16_t Lo = value >> 8;
00035 return Hi | Lo;
00036 #endif
00037 }
00038
00041 inline uint32_t SwapByteOrder_32(uint32_t value) {
00042 #if defined(__llvm__) || \
00043 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) && !defined(__ICC)
00044 return __builtin_bswap32(value);
00045 #elif defined(_MSC_VER) && !defined(_DEBUG)
00046 return _byteswap_ulong(value);
00047 #else
00048 uint32_t Byte0 = value & 0x000000FF;
00049 uint32_t Byte1 = value & 0x0000FF00;
00050 uint32_t Byte2 = value & 0x00FF0000;
00051 uint32_t Byte3 = value & 0xFF000000;
00052 return (Byte0 << 24) | (Byte1 << 8) | (Byte2 >> 8) | (Byte3 >> 24);
00053 #endif
00054 }
00055
00058 inline uint64_t SwapByteOrder_64(uint64_t value) {
00059 #if defined(__llvm__) || \
00060 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) && !defined(__ICC)
00061 return __builtin_bswap64(value);
00062 #elif defined(_MSC_VER) && !defined(_DEBUG)
00063 return _byteswap_uint64(value);
00064 #else
00065 uint64_t Hi = SwapByteOrder_32(uint32_t(value));
00066 uint32_t Lo = SwapByteOrder_32(uint32_t(value >> 32));
00067 return (Hi << 32) | Lo;
00068 #endif
00069 }
00070
00071 inline unsigned char SwapByteOrder(unsigned char C) { return C; }
00072 inline signed char SwapByteOrder(signed char C) { return C; }
00073 inline char SwapByteOrder(char C) { return C; }
00074
00075 inline unsigned short SwapByteOrder(unsigned short C) { return SwapByteOrder_16(C); }
00076 inline signed short SwapByteOrder( signed short C) { return SwapByteOrder_16(C); }
00077
00078 inline unsigned int SwapByteOrder(unsigned int C) { return SwapByteOrder_32(C); }
00079 inline signed int SwapByteOrder( signed int C) { return SwapByteOrder_32(C); }
00080
00081 #if __LONG_MAX__ == __INT_MAX__
00082 inline unsigned long SwapByteOrder(unsigned long C) { return SwapByteOrder_32(C); }
00083 inline signed long SwapByteOrder( signed long C) { return SwapByteOrder_32(C); }
00084 #elif __LONG_MAX__ == __LONG_LONG_MAX__
00085 inline unsigned long SwapByteOrder(unsigned long C) { return SwapByteOrder_64(C); }
00086 inline signed long SwapByteOrder( signed long C) { return SwapByteOrder_64(C); }
00087 #else
00088 #error "Unknown long size!"
00089 #endif
00090
00091 inline unsigned long long SwapByteOrder(unsigned long long C) {
00092 return SwapByteOrder_64(C);
00093 }
00094 inline signed long long SwapByteOrder(signed long long C) {
00095 return SwapByteOrder_64(C);
00096 }
00097
00098 }
00099 }
00100
00101 #endif