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
19 #include <type_traits>
20 #if defined(_MSC_VER) && !defined(_DEBUG)
24 #if defined(__linux__) || defined(__GNU__) || defined(__HAIKU__)
27 #include <sys/machine.h>
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
36 #define BYTE_ORDER LITTLE_ENDIAN
39 #if !defined(BYTE_ORDER) && !defined(_WIN32)
40 #include <machine/endian.h>
47 #if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
48 constexpr bool IsBigEndianHost = true;
50 constexpr bool IsBigEndianHost = false;
53 static const bool IsLittleEndianHost = !IsBigEndianHost;
55 /// SwapByteOrder_16 - This function returns a byte-swapped representation of
56 /// the 16-bit argument.
57 inline uint16_t SwapByteOrder_16(uint16_t value) {
58 #if defined(_MSC_VER) && !defined(_DEBUG)
59 // The DLL version of the runtime lacks these functions (bug!?), but in a
60 // release build they're replaced with BSWAP instructions anyway.
61 return _byteswap_ushort(value);
63 uint16_t Hi = value << 8;
64 uint16_t Lo = value >> 8;
69 /// This function returns a byte-swapped representation of the 32-bit argument.
70 inline uint32_t SwapByteOrder_32(uint32_t value) {
71 #if defined(__llvm__) || (defined(__GNUC__) && !defined(__ICC))
72 return __builtin_bswap32(value);
73 #elif defined(_MSC_VER) && !defined(_DEBUG)
74 return _byteswap_ulong(value);
76 uint32_t Byte0 = value & 0x000000FF;
77 uint32_t Byte1 = value & 0x0000FF00;
78 uint32_t Byte2 = value & 0x00FF0000;
79 uint32_t Byte3 = value & 0xFF000000;
80 return (Byte0 << 24) | (Byte1 << 8) | (Byte2 >> 8) | (Byte3 >> 24);
84 /// This function returns a byte-swapped representation of the 64-bit argument.
85 inline uint64_t SwapByteOrder_64(uint64_t value) {
86 #if defined(__llvm__) || (defined(__GNUC__) && !defined(__ICC))
87 return __builtin_bswap64(value);
88 #elif defined(_MSC_VER) && !defined(_DEBUG)
89 return _byteswap_uint64(value);
91 uint64_t Hi = SwapByteOrder_32(uint32_t(value));
92 uint32_t Lo = SwapByteOrder_32(uint32_t(value >> 32));
93 return (Hi << 32) | Lo;
97 inline unsigned char getSwappedBytes(unsigned char C) { return C; }
98 inline signed char getSwappedBytes(signed char C) { return C; }
99 inline char getSwappedBytes(char C) { return C; }
101 inline unsigned short getSwappedBytes(unsigned short C) { return SwapByteOrder_16(C); }
102 inline signed short getSwappedBytes( signed short C) { return SwapByteOrder_16(C); }
104 inline unsigned int getSwappedBytes(unsigned int C) { return SwapByteOrder_32(C); }
105 inline signed int getSwappedBytes( signed int C) { return SwapByteOrder_32(C); }
107 #if __LONG_MAX__ == __INT_MAX__
108 inline unsigned long getSwappedBytes(unsigned long C) { return SwapByteOrder_32(C); }
109 inline signed long getSwappedBytes( signed long C) { return SwapByteOrder_32(C); }
110 #elif __LONG_MAX__ == __LONG_LONG_MAX__
111 inline unsigned long getSwappedBytes(unsigned long C) { return SwapByteOrder_64(C); }
112 inline signed long getSwappedBytes( signed long C) { return SwapByteOrder_64(C); }
114 #error "Unknown long size!"
117 inline unsigned long long getSwappedBytes(unsigned long long C) {
118 return SwapByteOrder_64(C);
120 inline signed long long getSwappedBytes(signed long long C) {
121 return SwapByteOrder_64(C);
124 inline float getSwappedBytes(float C) {
130 out.i = SwapByteOrder_32(in.i);
134 inline double getSwappedBytes(double C) {
140 out.i = SwapByteOrder_64(in.i);
144 template <typename T>
145 inline std::enable_if_t<std::is_enum<T>::value, T> getSwappedBytes(T C) {
146 return static_cast<T>(
147 getSwappedBytes(static_cast<std::underlying_type_t<T>>(C)));
151 inline void swapByteOrder(T &Value) {
152 Value = getSwappedBytes(Value);
155 } // end namespace sys
156 } // end namespace llvm