/home/arjun/llvm-project/llvm/include/llvm/Support/SwapByteOrder.h
Line | Count | Source (jump to first uncovered line) |
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 | | |
44 | | namespace llvm { |
45 | | |
46 | | /// ByteSwap_16 - This function returns a byte-swapped representation of |
47 | | /// the 16-bit argument. |
48 | 0 | inline 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 | 0 | uint16_t Lo = value >> 8; |
56 | 0 | return Hi | Lo; |
57 | 0 | #endif |
58 | 0 | } |
59 | | |
60 | | /// This function returns a byte-swapped representation of the 32-bit argument. |
61 | 0 | inline uint32_t ByteSwap_32(uint32_t value) { |
62 | 0 | #if defined(__llvm__) || (defined(__GNUC__) && !defined(__ICC)) |
63 | 0 | 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. |
76 | 0 | inline uint64_t ByteSwap_64(uint64_t value) { |
77 | 0 | #if defined(__llvm__) || (defined(__GNUC__) && !defined(__ICC)) |
78 | 0 | 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 | | |
88 | | namespace sys { |
89 | | |
90 | | #if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN |
91 | | constexpr bool IsBigEndianHost = true; |
92 | | #else |
93 | | constexpr bool IsBigEndianHost = false; |
94 | | #endif |
95 | | |
96 | | static const bool IsLittleEndianHost = !IsBigEndianHost; |
97 | | |
98 | 0 | inline unsigned char getSwappedBytes(unsigned char C) { return C; } |
99 | 0 | inline signed char getSwappedBytes(signed char C) { return C; } |
100 | 0 | inline char getSwappedBytes(char C) { return C; } |
101 | | |
102 | 0 | inline unsigned short getSwappedBytes(unsigned short C) { return ByteSwap_16(C); } |
103 | 0 | inline signed short getSwappedBytes( signed short C) { return ByteSwap_16(C); } |
104 | | |
105 | 0 | inline unsigned int getSwappedBytes(unsigned int C) { return ByteSwap_32(C); } |
106 | 0 | inline signed int getSwappedBytes( signed int C) { return ByteSwap_32(C); } |
107 | | |
108 | 0 | inline unsigned long getSwappedBytes(unsigned long C) { |
109 | 0 | // Handle LLP64 and LP64 platforms. |
110 | 0 | return sizeof(long) == sizeof(int) ? ByteSwap_32((uint32_t)C) |
111 | 0 | : ByteSwap_64((uint64_t)C); |
112 | 0 | } |
113 | 0 | inline signed long getSwappedBytes(signed long C) { |
114 | 0 | // Handle LLP64 and LP64 platforms. |
115 | 0 | return sizeof(long) == sizeof(int) ? ByteSwap_32((uint32_t)C) |
116 | 0 | : ByteSwap_64((uint64_t)C); |
117 | 0 | } |
118 | | |
119 | 0 | inline unsigned long long getSwappedBytes(unsigned long long C) { |
120 | 0 | return ByteSwap_64(C); |
121 | 0 | } |
122 | 0 | inline signed long long getSwappedBytes(signed long long C) { |
123 | 0 | return ByteSwap_64(C); |
124 | 0 | } |
125 | | |
126 | 0 | inline float getSwappedBytes(float C) { |
127 | 0 | union { |
128 | 0 | uint32_t i; |
129 | 0 | float f; |
130 | 0 | } in, out; |
131 | 0 | in.f = C; |
132 | 0 | out.i = ByteSwap_32(in.i); |
133 | 0 | return out.f; |
134 | 0 | } |
135 | | |
136 | 0 | inline double getSwappedBytes(double C) { |
137 | 0 | union { |
138 | 0 | uint64_t i; |
139 | 0 | double d; |
140 | 0 | } in, out; |
141 | 0 | in.d = C; |
142 | 0 | out.i = ByteSwap_64(in.i); |
143 | 0 | return out.d; |
144 | 0 | } |
145 | | |
146 | | template <typename T> |
147 | | inline 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 | | |
152 | | template<typename T> |
153 | 0 | inline void swapByteOrder(T &Value) { |
154 | 0 | Value = getSwappedBytes(Value); |
155 | 0 | } Unexecuted instantiation: _ZN4llvm3sys13swapByteOrderImEEvRT_ Unexecuted instantiation: _ZN4llvm3sys13swapByteOrderIjEEvRT_ Unexecuted instantiation: _ZN4llvm3sys13swapByteOrderItEEvRT_ |
156 | | |
157 | | } // end namespace sys |
158 | | } // end namespace llvm |
159 | | |
160 | | #endif |