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
22namespace llvm {
23namespace sys {
24
25/// SwapByteOrder_16 - This function returns a byte-swapped representation of
26/// the 16-bit argument.
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.
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.
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
71inline unsigned char  getSwappedBytes(unsigned char C) { return C; }
72inline   signed char  getSwappedBytes(signed char C) { return C; }
73inline          char  getSwappedBytes(char C) { return C; }
74
75inline unsigned short getSwappedBytes(unsigned short C) { return SwapByteOrder_16(C); }
76inline   signed short getSwappedBytes(  signed short C) { return SwapByteOrder_16(C); }
77
78inline unsigned int   getSwappedBytes(unsigned int   C) { return SwapByteOrder_32(C); }
79inline   signed int   getSwappedBytes(  signed int   C) { return SwapByteOrder_32(C); }
80
81#if __LONG_MAX__ == __INT_MAX__
82inline unsigned long  getSwappedBytes(unsigned long  C) { return SwapByteOrder_32(C); }
83inline   signed long  getSwappedBytes(  signed long  C) { return SwapByteOrder_32(C); }
84#elif __LONG_MAX__ == __LONG_LONG_MAX__
85inline unsigned long  getSwappedBytes(unsigned long  C) { return SwapByteOrder_64(C); }
86inline   signed long  getSwappedBytes(  signed long  C) { return SwapByteOrder_64(C); }
87#else
88#error "Unknown long size!"
89#endif
90
91inline unsigned long long getSwappedBytes(unsigned long long C) {
92  return SwapByteOrder_64(C);
93}
94inline signed long long getSwappedBytes(signed long long C) {
95  return SwapByteOrder_64(C);
96}
97
98template<typename T>
99inline void swapByteOrder(T &Value) {
100  Value = getSwappedBytes(Value);
101}
102
103} // end namespace sys
104} // end namespace llvm
105
106#endif
107