Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- SwapByteOrder.h - Generic and optimized byte swaps -------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 17 | #include <cstddef> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 18 | #include <cstdint> |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 19 | #include <type_traits> |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 20 | #if defined(_MSC_VER) && !defined(_DEBUG) |
| 21 | #include <stdlib.h> |
| 22 | #endif |
| 23 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 24 | #if defined(__linux__) || defined(__GNU__) || defined(__HAIKU__) || \ |
| 25 | defined(__Fuchsia__) || defined(__EMSCRIPTEN__) |
| 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 | #elif defined(__MVS__) |
| 40 | #define BIG_ENDIAN 4321 |
| 41 | #define LITTLE_ENDIAN 1234 |
| 42 | #define BYTE_ORDER BIG_ENDIAN |
| 43 | #else |
| 44 | #if !defined(BYTE_ORDER) && !defined(_WIN32) |
| 45 | #include <machine/endian.h> |
| 46 | #endif |
| 47 | #endif |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 48 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 49 | namespace llvm { |
| 50 | |
| 51 | /// ByteSwap_16 - This function returns a byte-swapped representation of |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 52 | /// the 16-bit argument. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 53 | inline uint16_t ByteSwap_16(uint16_t value) { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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); |
| 58 | #else |
| 59 | uint16_t Hi = value << 8; |
| 60 | uint16_t Lo = value >> 8; |
| 61 | return Hi | Lo; |
| 62 | #endif |
| 63 | } |
| 64 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 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)) |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 68 | return __builtin_bswap32(value); |
| 69 | #elif defined(_MSC_VER) && !defined(_DEBUG) |
| 70 | return _byteswap_ulong(value); |
| 71 | #else |
| 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); |
| 77 | #endif |
| 78 | } |
| 79 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 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)) |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 83 | return __builtin_bswap64(value); |
| 84 | #elif defined(_MSC_VER) && !defined(_DEBUG) |
| 85 | return _byteswap_uint64(value); |
| 86 | #else |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 87 | uint64_t Hi = ByteSwap_32(uint32_t(value)); |
| 88 | uint32_t Lo = ByteSwap_32(uint32_t(value >> 32)); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 89 | return (Hi << 32) | Lo; |
| 90 | #endif |
| 91 | } |
| 92 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 93 | namespace sys { |
| 94 | |
| 95 | #if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN |
| 96 | constexpr bool IsBigEndianHost = true; |
| 97 | #else |
| 98 | constexpr bool IsBigEndianHost = false; |
| 99 | #endif |
| 100 | |
| 101 | static const bool IsLittleEndianHost = !IsBigEndianHost; |
| 102 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 103 | inline unsigned char getSwappedBytes(unsigned char C) { return C; } |
| 104 | inline signed char getSwappedBytes(signed char C) { return C; } |
| 105 | inline char getSwappedBytes(char C) { return C; } |
| 106 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 107 | inline unsigned short getSwappedBytes(unsigned short C) { return ByteSwap_16(C); } |
| 108 | inline signed short getSwappedBytes( signed short C) { return ByteSwap_16(C); } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 109 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 110 | inline unsigned int getSwappedBytes(unsigned int C) { return ByteSwap_32(C); } |
| 111 | inline signed int getSwappedBytes( signed int C) { return ByteSwap_32(C); } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 112 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 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); |
| 117 | } |
| 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); |
| 122 | } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 123 | |
| 124 | inline unsigned long long getSwappedBytes(unsigned long long C) { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 125 | return ByteSwap_64(C); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 126 | } |
| 127 | inline signed long long getSwappedBytes(signed long long C) { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 128 | return ByteSwap_64(C); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | inline float getSwappedBytes(float C) { |
| 132 | union { |
| 133 | uint32_t i; |
| 134 | float f; |
| 135 | } in, out; |
| 136 | in.f = C; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 137 | out.i = ByteSwap_32(in.i); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 138 | return out.f; |
| 139 | } |
| 140 | |
| 141 | inline double getSwappedBytes(double C) { |
| 142 | union { |
| 143 | uint64_t i; |
| 144 | double d; |
| 145 | } in, out; |
| 146 | in.d = C; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 147 | out.i = ByteSwap_64(in.i); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 148 | return out.d; |
| 149 | } |
| 150 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 151 | template <typename T> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 152 | inline std::enable_if_t<std::is_enum<T>::value, T> getSwappedBytes(T C) { |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 153 | return static_cast<T>( |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 154 | getSwappedBytes(static_cast<std::underlying_type_t<T>>(C))); |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 157 | template<typename T> |
| 158 | inline void swapByteOrder(T &Value) { |
| 159 | Value = getSwappedBytes(Value); |
| 160 | } |
| 161 | |
| 162 | } // end namespace sys |
| 163 | } // end namespace llvm |
| 164 | |
| 165 | #endif |