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. 27 inline 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. 41 inline 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. 58 inline 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 71 inline unsigned char SwapByteOrder(unsigned char C) { return C; } 72 inline signed char SwapByteOrder(signed char C) { return C; } 73 inline char SwapByteOrder(char C) { return C; } 74 75 inline unsigned short SwapByteOrder(unsigned short C) { return SwapByteOrder_16(C); } 76 inline signed short SwapByteOrder( signed short C) { return SwapByteOrder_16(C); } 77 78 inline unsigned int SwapByteOrder(unsigned int C) { return SwapByteOrder_32(C); } 79 inline signed int SwapByteOrder( signed int C) { return SwapByteOrder_32(C); } 80 81 #if __LONG_MAX__ == __INT_MAX__ 82 inline unsigned long SwapByteOrder(unsigned long C) { return SwapByteOrder_32(C); } 83 inline signed long SwapByteOrder( signed long C) { return SwapByteOrder_32(C); } 84 #elif __LONG_MAX__ == __LONG_LONG_MAX__ 85 inline unsigned long SwapByteOrder(unsigned long C) { return SwapByteOrder_64(C); } 86 inline signed long SwapByteOrder( signed long C) { return SwapByteOrder_64(C); } 87 #else 88 #error "Unknown long size!" 89 #endif 90 91 inline unsigned long long SwapByteOrder(unsigned long long C) { 92 return SwapByteOrder_64(C); 93 } 94 inline signed long long SwapByteOrder(signed long long C) { 95 return SwapByteOrder_64(C); 96 } 97 98 } // end namespace sys 99 } // end namespace llvm 100 101 #endif 102