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 <cstddef>
18#include <cstdint>
19#include <type_traits>
20#if defined(_MSC_VER) && !defined(_DEBUG)
21#include <stdlib.h>
22#endif
23
24#if defined(__linux__) || defined(__GNU__) || defined(__HAIKU__)
25#include <endian.h>
26#elif defined(_AIX)
27#include <sys/machine.h>
28#elif defined(__sun)
29/* Solaris provides _BIG_ENDIAN/_LITTLE_ENDIAN selector in sys/types.h */
30#include <sys/types.h>
31#define BIG_ENDIAN 4321
32#define LITTLE_ENDIAN 1234
33#if defined(_BIG_ENDIAN)
34#define BYTE_ORDER BIG_ENDIAN
35#else
36#define BYTE_ORDER LITTLE_ENDIAN
37#endif
38#else
39#if !defined(BYTE_ORDER) && !defined(_WIN32)
40#include <machine/endian.h>
41#endif
42#endif
43
44namespace llvm {
45
46/// ByteSwap_16 - This function returns a byte-swapped representation of
47/// the 16-bit argument.
48inline uint16_t ByteSwap_16(uint16_t value) {
49#if defined(_MSC_VER) && !defined(_DEBUG)
50 // The DLL version of the runtime lacks these functions (bug!?), but in a
51 // release build they're replaced with BSWAP instructions anyway.
52 return _byteswap_ushort(value);
53#else
54 uint16_t Hi = value << 8;
55 uint16_t Lo = value >> 8;
56 return Hi | Lo;
57#endif
58}
59
60/// This function returns a byte-swapped representation of the 32-bit argument.
61inline uint32_t ByteSwap_32(uint32_t value) {
62#if defined(__llvm__) || (defined(__GNUC__) && !defined(__ICC))
63 return __builtin_bswap32(value);
64#elif defined(_MSC_VER) && !defined(_DEBUG)
65 return _byteswap_ulong(value);
66#else
67 uint32_t Byte0 = value & 0x000000FF;
68 uint32_t Byte1 = value & 0x0000FF00;
69 uint32_t Byte2 = value & 0x00FF0000;
70 uint32_t Byte3 = value & 0xFF000000;
71 return (Byte0 << 24) | (Byte1 << 8) | (Byte2 >> 8) | (Byte3 >> 24);
72#endif
73}
74
75/// This function returns a byte-swapped representation of the 64-bit argument.
76inline uint64_t ByteSwap_64(uint64_t value) {
77#if defined(__llvm__) || (defined(__GNUC__) && !defined(__ICC))
78 return __builtin_bswap64(value);
79#elif defined(_MSC_VER) && !defined(_DEBUG)
80 return _byteswap_uint64(value);
81#else
82 uint64_t Hi = ByteSwap_32(uint32_t(value));
83 uint32_t Lo = ByteSwap_32(uint32_t(value >> 32));
84 return (Hi << 32) | Lo;
85#endif
86}
87
88namespace sys {
89
90#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
91constexpr bool IsBigEndianHost = true;
92#else
93constexpr bool IsBigEndianHost = false;
94#endif
95
96static const bool IsLittleEndianHost = !IsBigEndianHost;
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 ByteSwap_16(C); }
103inline signed short getSwappedBytes( signed short C) { return ByteSwap_16(C); }
104
105inline unsigned int getSwappedBytes(unsigned int C) { return ByteSwap_32(C); }
106inline signed int getSwappedBytes( signed int C) { return ByteSwap_32(C); }
107
108inline unsigned long getSwappedBytes(unsigned long C) {
109 // Handle LLP64 and LP64 platforms.
110 return sizeof(long) == sizeof(int) ? ByteSwap_32((uint32_t)C)
111 : ByteSwap_64((uint64_t)C);
112}
113inline signed long getSwappedBytes(signed long C) {
114 // Handle LLP64 and LP64 platforms.
115 return sizeof(long) == sizeof(int) ? ByteSwap_32((uint32_t)C)
116 : ByteSwap_64((uint64_t)C);
117}
118
119inline unsigned long long getSwappedBytes(unsigned long long C) {
120 return ByteSwap_64(C);
121}
122inline signed long long getSwappedBytes(signed long long C) {
123 return ByteSwap_64(C);
124}
125
126inline float getSwappedBytes(float C) {
127 union {
128 uint32_t i;
129 float f;
130 } in, out;
131 in.f = C;
132 out.i = ByteSwap_32(in.i);
133 return out.f;
134}
135
136inline double getSwappedBytes(double C) {
137 union {
138 uint64_t i;
139 double d;
140 } in, out;
141 in.d = C;
142 out.i = ByteSwap_64(in.i);
143 return out.d;
144}
145
146template <typename T>
147inline std::enable_if_t<std::is_enum<T>::value, T> getSwappedBytes(T C) {
148 return static_cast<T>(
149 getSwappedBytes(static_cast<std::underlying_type_t<T>>(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