1 //===- SwapByteOrder.h - Generic and optimized byte swaps -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares generic and optimized functions to swap the byte order of
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SYSTEM_SWAP_BYTE_ORDER_H
16 #define LLVM_SYSTEM_SWAP_BYTE_ORDER_H
18 #include "llvm/Support/DataTypes.h"
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);
33 uint16_t Hi = value << 8;
34 uint16_t Lo = value >> 8;
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);
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);
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);
65 uint64_t Hi = SwapByteOrder_32(uint32_t(value));
66 uint32_t Lo = SwapByteOrder_32(uint32_t(value >> 32));
67 return (Hi << 32) | Lo;
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; }
75 inline unsigned short SwapByteOrder(unsigned short C) { return SwapByteOrder_16(C); }
76 inline signed short SwapByteOrder( signed short C) { return SwapByteOrder_16(C); }
78 inline unsigned int SwapByteOrder(unsigned int C) { return SwapByteOrder_32(C); }
79 inline signed int SwapByteOrder( signed int C) { return SwapByteOrder_32(C); }
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); }
88 #error "Unknown long size!"
91 inline unsigned long long SwapByteOrder(unsigned long long C) {
92 return SwapByteOrder_64(C);
94 inline signed long long SwapByteOrder(signed long long C) {
95 return SwapByteOrder_64(C);
98 } // end namespace sys
99 } // end namespace llvm