1 //===- SwapByteOrder.h - Generic and optimized byte swaps -------*- 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 LLVM_SUPPORT_SWAPBYTEORDER_H
15 #define LLVM_SUPPORT_SWAPBYTEORDER_H
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/DataTypes.h"
20 #include <type_traits>
21 #if defined(_MSC_VER) && !defined(_DEBUG)
25 #if defined(__linux__) || defined(__GNU__) || defined(__HAIKU__)
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
40 #if !defined(BYTE_ORDER) && !defined(_WIN32)
41 #include <machine/endian.h>
48 #if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
49 constexpr bool IsBigEndianHost
= true;
51 constexpr bool IsBigEndianHost
= false;
54 static const bool IsLittleEndianHost
= !IsBigEndianHost
;
56 /// SwapByteOrder_16 - This function returns a byte-swapped representation of
57 /// the 16-bit argument.
58 inline 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
);
64 uint16_t Hi
= value
<< 8;
65 uint16_t Lo
= value
>> 8;
70 /// This function returns a byte-swapped representation of the 32-bit argument.
71 inline 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
);
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);
85 /// This function returns a byte-swapped representation of the 64-bit argument.
86 inline 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
);
92 uint64_t Hi
= SwapByteOrder_32(uint32_t(value
));
93 uint32_t Lo
= SwapByteOrder_32(uint32_t(value
>> 32));
94 return (Hi
<< 32) | Lo
;
98 inline unsigned char getSwappedBytes(unsigned char C
) { return C
; }
99 inline signed char getSwappedBytes(signed char C
) { return C
; }
100 inline char getSwappedBytes(char C
) { return C
; }
102 inline unsigned short getSwappedBytes(unsigned short C
) { return SwapByteOrder_16(C
); }
103 inline signed short getSwappedBytes( signed short C
) { return SwapByteOrder_16(C
); }
105 inline unsigned int getSwappedBytes(unsigned int C
) { return SwapByteOrder_32(C
); }
106 inline signed int getSwappedBytes( signed int C
) { return SwapByteOrder_32(C
); }
108 #if __LONG_MAX__ == __INT_MAX__
109 inline unsigned long getSwappedBytes(unsigned long C
) { return SwapByteOrder_32(C
); }
110 inline signed long getSwappedBytes( signed long C
) { return SwapByteOrder_32(C
); }
111 #elif __LONG_MAX__ == __LONG_LONG_MAX__
112 inline unsigned long getSwappedBytes(unsigned long C
) { return SwapByteOrder_64(C
); }
113 inline signed long getSwappedBytes( signed long C
) { return SwapByteOrder_64(C
); }
115 #error "Unknown long size!"
118 inline unsigned long long getSwappedBytes(unsigned long long C
) {
119 return SwapByteOrder_64(C
);
121 inline signed long long getSwappedBytes(signed long long C
) {
122 return SwapByteOrder_64(C
);
125 inline float getSwappedBytes(float C
) {
131 out
.i
= SwapByteOrder_32(in
.i
);
135 inline double getSwappedBytes(double C
) {
141 out
.i
= SwapByteOrder_64(in
.i
);
145 template <typename T
>
146 inline typename
std::enable_if
<std::is_enum
<T
>::value
, T
>::type
147 getSwappedBytes(T C
) {
148 return static_cast<T
>(
149 getSwappedBytes(static_cast<typename
std::underlying_type
<T
>::type
>(C
)));
153 inline void swapByteOrder(T
&Value
) {
154 Value
= getSwappedBytes(Value
);
157 } // end namespace sys
158 } // end namespace llvm