SwapByteOrder.h revision 218885
1218885Sdim//===- SwapByteOrder.h - Generic and optimized byte swaps -------*- C++ -*-===// 2218885Sdim// 3218885Sdim// The LLVM Compiler Infrastructure 4218885Sdim// 5218885Sdim// This file is distributed under the University of Illinois Open Source 6218885Sdim// License. See LICENSE.TXT for details. 7218885Sdim// 8218885Sdim//===----------------------------------------------------------------------===// 9218885Sdim// 10218885Sdim// This file declares generic and optimized functions to swap the byte order of 11218885Sdim// an integral type. 12218885Sdim// 13218885Sdim//===----------------------------------------------------------------------===// 14218885Sdim 15218885Sdim#ifndef LLVM_SYSTEM_SWAP_BYTE_ORDER_H 16218885Sdim#define LLVM_SYSTEM_SWAP_BYTE_ORDER_H 17218885Sdim 18218885Sdim#include "llvm/Support/DataTypes.h" 19218885Sdim#include <cstddef> 20218885Sdim#include <limits> 21218885Sdim 22218885Sdimnamespace llvm { 23218885Sdimnamespace sys { 24218885Sdim 25218885Sdim/// SwapByteOrder_16 - This function returns a byte-swapped representation of 26218885Sdim/// the 16-bit argument. 27218885Sdiminline uint16_t SwapByteOrder_16(uint16_t value) { 28218885Sdim#if defined(_MSC_VER) && !defined(_DEBUG) 29218885Sdim // The DLL version of the runtime lacks these functions (bug!?), but in a 30218885Sdim // release build they're replaced with BSWAP instructions anyway. 31218885Sdim return _byteswap_ushort(value); 32218885Sdim#else 33218885Sdim uint16_t Hi = value << 8; 34218885Sdim uint16_t Lo = value >> 8; 35218885Sdim return Hi | Lo; 36218885Sdim#endif 37218885Sdim} 38218885Sdim 39218885Sdim/// SwapByteOrder_32 - This function returns a byte-swapped representation of 40218885Sdim/// the 32-bit argument. 41218885Sdiminline uint32_t SwapByteOrder_32(uint32_t value) { 42218885Sdim#if defined(__llvm__) || \ 43218885Sdim(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) && !defined(__ICC) 44218885Sdim return __builtin_bswap32(value); 45218885Sdim#elif defined(_MSC_VER) && !defined(_DEBUG) 46218885Sdim return _byteswap_ulong(value); 47218885Sdim#else 48218885Sdim uint32_t Byte0 = value & 0x000000FF; 49218885Sdim uint32_t Byte1 = value & 0x0000FF00; 50218885Sdim uint32_t Byte2 = value & 0x00FF0000; 51218885Sdim uint32_t Byte3 = value & 0xFF000000; 52218885Sdim return (Byte0 << 24) | (Byte1 << 8) | (Byte2 >> 8) | (Byte3 >> 24); 53218885Sdim#endif 54218885Sdim} 55218885Sdim 56218885Sdim/// SwapByteOrder_64 - This function returns a byte-swapped representation of 57218885Sdim/// the 64-bit argument. 58218885Sdiminline uint64_t SwapByteOrder_64(uint64_t value) { 59218885Sdim#if defined(__llvm__) || \ 60218885Sdim(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) && !defined(__ICC) 61218885Sdim return __builtin_bswap64(value); 62218885Sdim#elif defined(_MSC_VER) && !defined(_DEBUG) 63218885Sdim return _byteswap_uint64(value); 64218885Sdim#else 65218885Sdim uint64_t Hi = SwapByteOrder_32(uint32_t(value)); 66218885Sdim uint32_t Lo = SwapByteOrder_32(uint32_t(value >> 32)); 67218885Sdim return (Hi << 32) | Lo; 68218885Sdim#endif 69218885Sdim} 70218885Sdim 71218885Sdiminline unsigned char SwapByteOrder(unsigned char C) { return C; } 72218885Sdiminline signed char SwapByteOrder(signed char C) { return C; } 73218885Sdiminline char SwapByteOrder(char C) { return C; } 74218885Sdim 75218885Sdiminline unsigned short SwapByteOrder(unsigned short C) { return SwapByteOrder_16(C); } 76218885Sdiminline signed short SwapByteOrder( signed short C) { return SwapByteOrder_16(C); } 77218885Sdim 78218885Sdiminline unsigned int SwapByteOrder(unsigned int C) { return SwapByteOrder_32(C); } 79218885Sdiminline signed int SwapByteOrder( signed int C) { return SwapByteOrder_32(C); } 80218885Sdim 81218885Sdim#if __LONG_MAX__ == __INT_MAX__ 82218885Sdiminline unsigned long SwapByteOrder(unsigned long C) { return SwapByteOrder_32(C); } 83218885Sdiminline signed long SwapByteOrder( signed long C) { return SwapByteOrder_32(C); } 84218885Sdim#elif __LONG_MAX__ == __LONG_LONG_MAX__ 85218885Sdiminline unsigned long SwapByteOrder(unsigned long C) { return SwapByteOrder_64(C); } 86218885Sdiminline signed long SwapByteOrder( signed long C) { return SwapByteOrder_64(C); } 87218885Sdim#else 88218885Sdim#error "Unknown long size!" 89218885Sdim#endif 90218885Sdim 91218885Sdiminline unsigned long long SwapByteOrder(unsigned long long C) { 92218885Sdim return SwapByteOrder_64(C); 93218885Sdim} 94218885Sdiminline signed long long SwapByteOrder(signed long long C) { 95218885Sdim return SwapByteOrder_64(C); 96218885Sdim} 97218885Sdim 98218885Sdim} // end namespace sys 99218885Sdim} // end namespace llvm 100218885Sdim 101218885Sdim#endif 102