1 //===- SwapByteOrder.h - Generic and optimized byte swaps -------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file declares generic and optimized functions to swap the byte order of 11 // an integral type. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_SUPPORT_SWAPBYTEORDER_H 16 #define LLVM_SUPPORT_SWAPBYTEORDER_H 17 18 #include "llvm/Support/DataTypes.h" 19 #include <cstddef> 20 #include <limits> 21 22 namespace llvm { 23 namespace sys { 24 25 /// SwapByteOrder_16 - This function returns a byte-swapped representation of 26 /// the 16-bit argument. SwapByteOrder_16(uint16_t value)27inline uint16_t SwapByteOrder_16(uint16_t value) { 28 #if defined(_MSC_VER) && !defined(_DEBUG) 29 // The DLL version of the runtime lacks these functions (bug!?), but in a 30 // release build they're replaced with BSWAP instructions anyway. 31 return _byteswap_ushort(value); 32 #else 33 uint16_t Hi = value << 8; 34 uint16_t Lo = value >> 8; 35 return Hi | Lo; 36 #endif 37 } 38 39 /// SwapByteOrder_32 - This function returns a byte-swapped representation of 40 /// the 32-bit argument. SwapByteOrder_32(uint32_t value)41inline uint32_t SwapByteOrder_32(uint32_t value) { 42 #if defined(__llvm__) || \ 43 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) && !defined(__ICC) 44 return __builtin_bswap32(value); 45 #elif defined(_MSC_VER) && !defined(_DEBUG) 46 return _byteswap_ulong(value); 47 #else 48 uint32_t Byte0 = value & 0x000000FF; 49 uint32_t Byte1 = value & 0x0000FF00; 50 uint32_t Byte2 = value & 0x00FF0000; 51 uint32_t Byte3 = value & 0xFF000000; 52 return (Byte0 << 24) | (Byte1 << 8) | (Byte2 >> 8) | (Byte3 >> 24); 53 #endif 54 } 55 56 /// SwapByteOrder_64 - This function returns a byte-swapped representation of 57 /// the 64-bit argument. SwapByteOrder_64(uint64_t value)58inline uint64_t SwapByteOrder_64(uint64_t value) { 59 #if defined(__llvm__) || \ 60 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) && !defined(__ICC) 61 return __builtin_bswap64(value); 62 #elif defined(_MSC_VER) && !defined(_DEBUG) 63 return _byteswap_uint64(value); 64 #else 65 uint64_t Hi = SwapByteOrder_32(uint32_t(value)); 66 uint32_t Lo = SwapByteOrder_32(uint32_t(value >> 32)); 67 return (Hi << 32) | Lo; 68 #endif 69 } 70 getSwappedBytes(unsigned char C)71inline unsigned char getSwappedBytes(unsigned char C) { return C; } getSwappedBytes(signed char C)72inline signed char getSwappedBytes(signed char C) { return C; } getSwappedBytes(char C)73inline char getSwappedBytes(char C) { return C; } 74 getSwappedBytes(unsigned short C)75inline unsigned short getSwappedBytes(unsigned short C) { return SwapByteOrder_16(C); } getSwappedBytes(signed short C)76inline signed short getSwappedBytes( signed short C) { return SwapByteOrder_16(C); } 77 getSwappedBytes(unsigned int C)78inline unsigned int getSwappedBytes(unsigned int C) { return SwapByteOrder_32(C); } getSwappedBytes(signed int C)79inline signed int getSwappedBytes( signed int C) { return SwapByteOrder_32(C); } 80 81 #if __LONG_MAX__ == __INT_MAX__ getSwappedBytes(unsigned long C)82inline unsigned long getSwappedBytes(unsigned long C) { return SwapByteOrder_32(C); } getSwappedBytes(signed long C)83inline signed long getSwappedBytes( signed long C) { return SwapByteOrder_32(C); } 84 #elif __LONG_MAX__ == __LONG_LONG_MAX__ getSwappedBytes(unsigned long C)85inline unsigned long getSwappedBytes(unsigned long C) { return SwapByteOrder_64(C); } getSwappedBytes(signed long C)86inline signed long getSwappedBytes( signed long C) { return SwapByteOrder_64(C); } 87 #else 88 #error "Unknown long size!" 89 #endif 90 getSwappedBytes(unsigned long long C)91inline unsigned long long getSwappedBytes(unsigned long long C) { 92 return SwapByteOrder_64(C); 93 } getSwappedBytes(signed long long C)94inline signed long long getSwappedBytes(signed long long C) { 95 return SwapByteOrder_64(C); 96 } 97 98 template<typename T> swapByteOrder(T & Value)99inline void swapByteOrder(T &Value) { 100 Value = getSwappedBytes(Value); 101 } 102 103 } // end namespace sys 104 } // end namespace llvm 105 106 #endif 107