1 //===- endian.h - Endianness support ----------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file declares generic and optimized functions to swap the byte order of
12 //===----------------------------------------------------------------------===//
14 #ifndef ORC_RT_ENDIAN_H
15 #define ORC_RT_ENDIAN_H
19 #include <type_traits>
20 #if defined(_MSC_VER) && !defined(_DEBUG)
24 #if defined(__linux__) || defined(__GNU__) || defined(__HAIKU__) || \
25 defined(__Fuchsia__) || defined(__EMSCRIPTEN__)
28 #include <sys/machine.h>
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
37 #define BYTE_ORDER LITTLE_ENDIAN
39 #elif defined(__MVS__)
40 #define BIG_ENDIAN 4321
41 #define LITTLE_ENDIAN 1234
42 #define BYTE_ORDER BIG_ENDIAN
44 #if !defined(BYTE_ORDER) && !defined(_WIN32)
45 #include <machine/endian.h>
51 /// ByteSwap_16 - This function returns a byte-swapped representation of
52 /// the 16-bit argument.
53 inline uint16_t ByteSwap_16(uint16_t value
) {
54 #if defined(_MSC_VER) && !defined(_DEBUG)
55 // The DLL version of the runtime lacks these functions (bug!?), but in a
56 // release build they're replaced with BSWAP instructions anyway.
57 return _byteswap_ushort(value
);
59 uint16_t Hi
= value
<< 8;
60 uint16_t Lo
= value
>> 8;
65 /// This function returns a byte-swapped representation of the 32-bit argument.
66 inline uint32_t ByteSwap_32(uint32_t value
) {
67 #if defined(__llvm__) || (defined(__GNUC__) && !defined(__ICC))
68 return __builtin_bswap32(value
);
69 #elif defined(_MSC_VER) && !defined(_DEBUG)
70 return _byteswap_ulong(value
);
72 uint32_t Byte0
= value
& 0x000000FF;
73 uint32_t Byte1
= value
& 0x0000FF00;
74 uint32_t Byte2
= value
& 0x00FF0000;
75 uint32_t Byte3
= value
& 0xFF000000;
76 return (Byte0
<< 24) | (Byte1
<< 8) | (Byte2
>> 8) | (Byte3
>> 24);
80 /// This function returns a byte-swapped representation of the 64-bit argument.
81 inline uint64_t ByteSwap_64(uint64_t value
) {
82 #if defined(__llvm__) || (defined(__GNUC__) && !defined(__ICC))
83 return __builtin_bswap64(value
);
84 #elif defined(_MSC_VER) && !defined(_DEBUG)
85 return _byteswap_uint64(value
);
87 uint64_t Hi
= ByteSwap_32(uint32_t(value
));
88 uint32_t Lo
= ByteSwap_32(uint32_t(value
>> 32));
89 return (Hi
<< 32) | Lo
;
93 #if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
94 constexpr bool IsBigEndianHost
= true;
96 constexpr bool IsBigEndianHost
= false;
99 static const bool IsLittleEndianHost
= !IsBigEndianHost
;
101 inline unsigned char getSwappedBytes(unsigned char C
) { return C
; }
102 inline signed char getSwappedBytes(signed char C
) { return C
; }
103 inline char getSwappedBytes(char C
) { return C
; }
105 inline unsigned short getSwappedBytes(unsigned short C
) {
106 return ByteSwap_16(C
);
108 inline signed short getSwappedBytes(signed short C
) { return ByteSwap_16(C
); }
110 inline unsigned int getSwappedBytes(unsigned int C
) { return ByteSwap_32(C
); }
111 inline signed int getSwappedBytes(signed int C
) { return ByteSwap_32(C
); }
113 inline unsigned long getSwappedBytes(unsigned 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
);
118 inline signed long getSwappedBytes(signed long C
) {
119 // Handle LLP64 and LP64 platforms.
120 return sizeof(long) == sizeof(int) ? ByteSwap_32((uint32_t)C
)
121 : ByteSwap_64((uint64_t)C
);
124 inline unsigned long long getSwappedBytes(unsigned long long C
) {
125 return ByteSwap_64(C
);
127 inline signed long long getSwappedBytes(signed long long C
) {
128 return ByteSwap_64(C
);
131 template <typename T
>
132 inline std::enable_if_t
<std::is_enum
<T
>::value
, T
> getSwappedBytes(T C
) {
133 return static_cast<T
>(
134 getSwappedBytes(static_cast<std::underlying_type_t
<T
>>(C
)));
137 template <typename T
> inline void swapByteOrder(T
&Value
) {
138 Value
= getSwappedBytes(Value
);
141 } // end namespace __orc_rt
143 #endif // ORC_RT_ENDIAN_H