[llvm] [cmake] Add possibility to use ChooseMSVCCRT.cmake when include LLVM library
[llvm-core.git] / include / llvm / Support / SwapByteOrder.h
blobec60a214cd005698672b9217019d99dd0ef3ec9f
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.
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"
19 #include <cstddef>
20 #include <type_traits>
21 #if defined(_MSC_VER) && !defined(_DEBUG)
22 #include <stdlib.h>
23 #endif
25 namespace llvm {
26 namespace sys {
28 /// SwapByteOrder_16 - This function returns a byte-swapped representation of
29 /// the 16-bit argument.
30 inline uint16_t SwapByteOrder_16(uint16_t value) {
31 #if defined(_MSC_VER) && !defined(_DEBUG)
32 // The DLL version of the runtime lacks these functions (bug!?), but in a
33 // release build they're replaced with BSWAP instructions anyway.
34 return _byteswap_ushort(value);
35 #else
36 uint16_t Hi = value << 8;
37 uint16_t Lo = value >> 8;
38 return Hi | Lo;
39 #endif
42 /// This function returns a byte-swapped representation of the 32-bit argument.
43 inline uint32_t SwapByteOrder_32(uint32_t value) {
44 #if defined(__llvm__) || (defined(__GNUC__) && !defined(__ICC))
45 return __builtin_bswap32(value);
46 #elif defined(_MSC_VER) && !defined(_DEBUG)
47 return _byteswap_ulong(value);
48 #else
49 uint32_t Byte0 = value & 0x000000FF;
50 uint32_t Byte1 = value & 0x0000FF00;
51 uint32_t Byte2 = value & 0x00FF0000;
52 uint32_t Byte3 = value & 0xFF000000;
53 return (Byte0 << 24) | (Byte1 << 8) | (Byte2 >> 8) | (Byte3 >> 24);
54 #endif
57 /// This function returns a byte-swapped representation of the 64-bit argument.
58 inline uint64_t SwapByteOrder_64(uint64_t value) {
59 #if defined(__llvm__) || (defined(__GNUC__) && !defined(__ICC))
60 return __builtin_bswap64(value);
61 #elif defined(_MSC_VER) && !defined(_DEBUG)
62 return _byteswap_uint64(value);
63 #else
64 uint64_t Hi = SwapByteOrder_32(uint32_t(value));
65 uint32_t Lo = SwapByteOrder_32(uint32_t(value >> 32));
66 return (Hi << 32) | Lo;
67 #endif
70 inline unsigned char getSwappedBytes(unsigned char C) { return C; }
71 inline signed char getSwappedBytes(signed char C) { return C; }
72 inline char getSwappedBytes(char C) { return C; }
74 inline unsigned short getSwappedBytes(unsigned short C) { return SwapByteOrder_16(C); }
75 inline signed short getSwappedBytes( signed short C) { return SwapByteOrder_16(C); }
77 inline unsigned int getSwappedBytes(unsigned int C) { return SwapByteOrder_32(C); }
78 inline signed int getSwappedBytes( signed int C) { return SwapByteOrder_32(C); }
80 #if __LONG_MAX__ == __INT_MAX__
81 inline unsigned long getSwappedBytes(unsigned long C) { return SwapByteOrder_32(C); }
82 inline signed long getSwappedBytes( signed long C) { return SwapByteOrder_32(C); }
83 #elif __LONG_MAX__ == __LONG_LONG_MAX__
84 inline unsigned long getSwappedBytes(unsigned long C) { return SwapByteOrder_64(C); }
85 inline signed long getSwappedBytes( signed long C) { return SwapByteOrder_64(C); }
86 #else
87 #error "Unknown long size!"
88 #endif
90 inline unsigned long long getSwappedBytes(unsigned long long C) {
91 return SwapByteOrder_64(C);
93 inline signed long long getSwappedBytes(signed long long C) {
94 return SwapByteOrder_64(C);
97 inline float getSwappedBytes(float C) {
98 union {
99 uint32_t i;
100 float f;
101 } in, out;
102 in.f = C;
103 out.i = SwapByteOrder_32(in.i);
104 return out.f;
107 inline double getSwappedBytes(double C) {
108 union {
109 uint64_t i;
110 double d;
111 } in, out;
112 in.d = C;
113 out.i = SwapByteOrder_64(in.i);
114 return out.d;
117 template <typename T>
118 inline typename std::enable_if<std::is_enum<T>::value, T>::type
119 getSwappedBytes(T C) {
120 return static_cast<T>(
121 getSwappedBytes(static_cast<typename std::underlying_type<T>::type>(C)));
124 template<typename T>
125 inline void swapByteOrder(T &Value) {
126 Value = getSwappedBytes(Value);
129 } // end namespace sys
130 } // end namespace llvm
132 #endif