SwapByteOrder.h revision 360784
1//===- SwapByteOrder.h - Generic and optimized byte swaps -------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file declares generic and optimized functions to swap the byte order of
10// an integral type.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_SUPPORT_SWAPBYTEORDER_H
15#define LLVM_SUPPORT_SWAPBYTEORDER_H
16
17#include "llvm/Support/Compiler.h"
18#include "llvm/Support/DataTypes.h"
19#include <cstddef>
20#include <type_traits>
21#if defined(_MSC_VER) && !defined(_DEBUG)
22#include <stdlib.h>
23#endif
24
25#if defined(__linux__) || defined(__GNU__) || defined(__HAIKU__)
26#include <endian.h>
27#elif defined(_AIX)
28#include <sys/machine.h>
29#elif defined(__sun)
30/* Solaris provides _BIG_ENDIAN/_LITTLE_ENDIAN selector in sys/types.h */
31#include <sys/types.h>
32#define BIG_ENDIAN 4321
33#define LITTLE_ENDIAN 1234
34#if defined(_BIG_ENDIAN)
35#define BYTE_ORDER BIG_ENDIAN
36#else
37#define BYTE_ORDER LITTLE_ENDIAN
38#endif
39#else
40#if !defined(BYTE_ORDER) && !defined(_WIN32)
41#include <machine/endian.h>
42#endif
43#endif
44
45namespace llvm {
46namespace sys {
47
48#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
49constexpr bool IsBigEndianHost = true;
50#else
51constexpr bool IsBigEndianHost = false;
52#endif
53
54static const bool IsLittleEndianHost = !IsBigEndianHost;
55
56/// SwapByteOrder_16 - This function returns a byte-swapped representation of
57/// the 16-bit argument.
58inline uint16_t SwapByteOrder_16(uint16_t value) {
59#if defined(_MSC_VER) && !defined(_DEBUG)
60  // The DLL version of the runtime lacks these functions (bug!?), but in a
61  // release build they're replaced with BSWAP instructions anyway.
62  return _byteswap_ushort(value);
63#else
64  uint16_t Hi = value << 8;
65  uint16_t Lo = value >> 8;
66  return Hi | Lo;
67#endif
68}
69
70/// This function returns a byte-swapped representation of the 32-bit argument.
71inline uint32_t SwapByteOrder_32(uint32_t value) {
72#if defined(__llvm__) || (defined(__GNUC__) && !defined(__ICC))
73  return __builtin_bswap32(value);
74#elif defined(_MSC_VER) && !defined(_DEBUG)
75  return _byteswap_ulong(value);
76#else
77  uint32_t Byte0 = value & 0x000000FF;
78  uint32_t Byte1 = value & 0x0000FF00;
79  uint32_t Byte2 = value & 0x00FF0000;
80  uint32_t Byte3 = value & 0xFF000000;
81  return (Byte0 << 24) | (Byte1 << 8) | (Byte2 >> 8) | (Byte3 >> 24);
82#endif
83}
84
85/// This function returns a byte-swapped representation of the 64-bit argument.
86inline uint64_t SwapByteOrder_64(uint64_t value) {
87#if defined(__llvm__) || (defined(__GNUC__) && !defined(__ICC))
88  return __builtin_bswap64(value);
89#elif defined(_MSC_VER) && !defined(_DEBUG)
90  return _byteswap_uint64(value);
91#else
92  uint64_t Hi = SwapByteOrder_32(uint32_t(value));
93  uint32_t Lo = SwapByteOrder_32(uint32_t(value >> 32));
94  return (Hi << 32) | Lo;
95#endif
96}
97
98inline unsigned char  getSwappedBytes(unsigned char C) { return C; }
99inline   signed char  getSwappedBytes(signed char C) { return C; }
100inline          char  getSwappedBytes(char C) { return C; }
101
102inline unsigned short getSwappedBytes(unsigned short C) { return SwapByteOrder_16(C); }
103inline   signed short getSwappedBytes(  signed short C) { return SwapByteOrder_16(C); }
104
105inline unsigned int   getSwappedBytes(unsigned int   C) { return SwapByteOrder_32(C); }
106inline   signed int   getSwappedBytes(  signed int   C) { return SwapByteOrder_32(C); }
107
108#if __LONG_MAX__ == __INT_MAX__
109inline unsigned long  getSwappedBytes(unsigned long  C) { return SwapByteOrder_32(C); }
110inline   signed long  getSwappedBytes(  signed long  C) { return SwapByteOrder_32(C); }
111#elif __LONG_MAX__ == __LONG_LONG_MAX__
112inline unsigned long  getSwappedBytes(unsigned long  C) { return SwapByteOrder_64(C); }
113inline   signed long  getSwappedBytes(  signed long  C) { return SwapByteOrder_64(C); }
114#else
115#error "Unknown long size!"
116#endif
117
118inline unsigned long long getSwappedBytes(unsigned long long C) {
119  return SwapByteOrder_64(C);
120}
121inline signed long long getSwappedBytes(signed long long C) {
122  return SwapByteOrder_64(C);
123}
124
125inline float getSwappedBytes(float C) {
126  union {
127    uint32_t i;
128    float f;
129  } in, out;
130  in.f = C;
131  out.i = SwapByteOrder_32(in.i);
132  return out.f;
133}
134
135inline double getSwappedBytes(double C) {
136  union {
137    uint64_t i;
138    double d;
139  } in, out;
140  in.d = C;
141  out.i = SwapByteOrder_64(in.i);
142  return out.d;
143}
144
145template <typename T>
146inline typename std::enable_if<std::is_enum<T>::value, T>::type
147getSwappedBytes(T C) {
148  return static_cast<T>(
149      getSwappedBytes(static_cast<typename std::underlying_type<T>::type>(C)));
150}
151
152template<typename T>
153inline void swapByteOrder(T &Value) {
154  Value = getSwappedBytes(Value);
155}
156
157} // end namespace sys
158} // end namespace llvm
159
160#endif
161