UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
ByteSwap.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
6#include "HAL/PlatformCrt.h"
7
8// These macros are not safe to use unless data is UNSIGNED!
9#define BYTESWAP_ORDER16_unsigned(x) ((((x) >> 8) & 0xff) + (((x) << 8) & 0xff00))
10#define BYTESWAP_ORDER32_unsigned(x) (((x) >> 24) + (((x) >> 8) & 0xff00) + (((x) << 8) & 0xff0000) + ((x) << 24))
11
12// Encapsulate Byte swapping generic versions for benchmarking purpose (compare the generic vs intrinsic performance: See ByteSwapTest.cpp).
13namespace Internal
14{
15 static UE_FORCEINLINE_HINT uint16 ByteSwapGeneric16(uint16 Value)
16 {
18 }
19
20 static UE_FORCEINLINE_HINT uint32 ByteSwapGeneric32(uint32 Value)
21 {
23 }
24
25 static inline uint64 ByteSwapGeneric64(uint64 Value)
26 {
27 Value = ((Value << 8) & 0xFF00FF00FF00FF00ULL ) | ((Value >> 8) & 0x00FF00FF00FF00FFULL);
28 Value = ((Value << 16) & 0xFFFF0000FFFF0000ULL ) | ((Value >> 16) & 0x0000FFFF0000FFFFULL);
29 return (Value << 32) | (Value >> 32);
30 }
31} // namespace Internal
32
33// Defines the intrinsic version if available (faster). Do not use directly. Use BYTESWAP_ORDERxx() or ByteSwap() functions
34#if defined(_MSC_VER)
35 #define UE_BYTESWAP_INTRINSIC_PRIVATE_16(Val) _byteswap_ushort(Val);
36 #define UE_BYTESWAP_INTRINSIC_PRIVATE_32(Val) _byteswap_ulong(Val);
37 #define UE_BYTESWAP_INTRINSIC_PRIVATE_64(Val) _byteswap_uint64(Val);
38#elif defined(__clang__)
39 #if (__has_builtin(__builtin_bswap16))
40 #define UE_BYTESWAP_INTRINSIC_PRIVATE_16(Val) __builtin_bswap16(Val);
41 #endif
42 #if (__has_builtin(__builtin_bswap32))
43 #define UE_BYTESWAP_INTRINSIC_PRIVATE_32(Val) __builtin_bswap32(Val);
44 #endif
45 #if (__has_builtin(__builtin_bswap64))
46 #define UE_BYTESWAP_INTRINSIC_PRIVATE_64(Val) __builtin_bswap64(Val);
47 #endif
48#endif
49
51{
52#if defined(UE_BYTESWAP_INTRINSIC_PRIVATE_16)
54#else
55 return Internal::ByteSwapGeneric16(Val);
56#endif
57}
58
60{
61 return static_cast<int16>(BYTESWAP_ORDER16(static_cast<uint16>(Val)));
62}
63
65{
66#if defined(UE_BYTESWAP_INTRINSIC_PRIVATE_32)
68#else
69 return Internal::ByteSwapGeneric32(Val);
70#endif
71}
72
74{
75 return static_cast<int32>(BYTESWAP_ORDER32(static_cast<uint32>(val)));
76}
77
79{
80#if defined(UE_BYTESWAP_INTRINSIC_PRIVATE_64)
82#else
83 return Internal::ByteSwapGeneric64(Value);
84#endif
85}
86
88{
89 return static_cast<int64>(BYTESWAP_ORDER64(static_cast<uint64>(Value)));
90}
91
92inline float BYTESWAP_ORDERF(float val)
93{
94 uint32 uval = BYTESWAP_ORDER32(*reinterpret_cast<const uint32*>(&val));
95 return *reinterpret_cast<const float*>(&uval);
96}
97
98inline double BYTESWAP_ORDERD(double val)
99{
100 uint64 uval = BYTESWAP_ORDER64(*reinterpret_cast<const uint64*>(&val));
101 return *reinterpret_cast<const double*>(&uval);
102}
103
104#if PLATFORM_TCHAR_IS_UTF8CHAR
105
107{
108 static_assert(sizeof(TCHAR) == 1, "Assuming TCHAR is 1 byte wide.");
109
110 // No need to byteswap single bytes.
111}
112
113#else
114
116{
117 static_assert(sizeof(TCHAR) == sizeof(uint16), "Assuming TCHAR is 2 bytes wide.");
118 for (TCHAR* c = str; *c; ++c)
119 {
120 *c = BYTESWAP_ORDER16(static_cast<uint16>(*c));
121 }
122}
123
124#endif
125
126// General byte swapping.
127#if PLATFORM_LITTLE_ENDIAN
128 #define INTEL_ORDER16(x) (x)
129 #define INTEL_ORDER32(x) (x)
130 #define INTEL_ORDERF(x) (x)
131 #define INTEL_ORDER64(x) (x)
132 #define INTEL_ORDER_TCHARARRAY(x)
133 #define NETWORK_ORDER16(x) BYTESWAP_ORDER16(x)
134 #define NETWORK_ORDER32(x) BYTESWAP_ORDER32(x)
135 #define NETWORK_ORDERF(x) BYTESWAP_ORDERF(x)
136 #define NETWORK_ORDER64(x) BYTESWAP_ORDER64(x)
137 #define NETWORK_ORDER_TCHARARRAY(x) BYTESWAP_ORDER_TCHARARRAY(x)
138#else
139 #define INTEL_ORDER16(x) BYTESWAP_ORDER16(x)
140 #define INTEL_ORDER32(x) BYTESWAP_ORDER32(x)
141 #define INTEL_ORDERF(x) BYTESWAP_ORDERF(x)
142 #define INTEL_ORDER64(x) BYTESWAP_ORDER64(x)
143 #define INTEL_ORDER_TCHARARRAY(x) BYTESWAP_ORDER_TCHARARRAY(x)
144 #define NETWORK_ORDER16(x) (x)
145 #define NETWORK_ORDER32(x) (x)
146 #define NETWORK_ORDERF(x) (x)
147 #define NETWORK_ORDER64(x) (x)
148 #define NETWORK_ORDER_TCHARARRAY(x)
149#endif
150
151// Implementations that better mix with generic template code.
152template<typename T> T ByteSwap(T Value); // Left unimplemented to get link error when a specialization is missing.
153template<> inline int16 ByteSwap(int16 Value) { return BYTESWAP_ORDER16(Value); }
154template<> inline uint16 ByteSwap(uint16 Value) { return BYTESWAP_ORDER16(Value); }
155template<> inline int32 ByteSwap(int32 Value) { return BYTESWAP_ORDER32(Value); }
156template<> inline uint32 ByteSwap(uint32 Value) { return BYTESWAP_ORDER32(Value); }
157template<> inline int64 ByteSwap(int64 Value) { return BYTESWAP_ORDER64(Value); }
158template<> inline uint64 ByteSwap(uint64 Value) { return BYTESWAP_ORDER64(Value); }
159template<> inline float ByteSwap(float Value) { return BYTESWAP_ORDERF(Value); }
160template<> inline double ByteSwap(double Value) { return BYTESWAP_ORDERD(Value); }
161template<> inline char16_t ByteSwap(char16_t Value) { return static_cast<char16_t>(BYTESWAP_ORDER16(static_cast<uint16>(Value))); }
double BYTESWAP_ORDERD(double val)
Definition ByteSwap.h:98
T ByteSwap(T Value)
#define BYTESWAP_ORDER16_unsigned(x)
Definition ByteSwap.h:9
uint64 BYTESWAP_ORDER64(uint64 Value)
Definition ByteSwap.h:78
float BYTESWAP_ORDERF(float val)
Definition ByteSwap.h:92
uint32 BYTESWAP_ORDER32(uint32 Val)
Definition ByteSwap.h:64
void BYTESWAP_ORDER_TCHARARRAY(TCHAR *str)
Definition ByteSwap.h:115
#define BYTESWAP_ORDER32_unsigned(x)
Definition ByteSwap.h:10
uint16 BYTESWAP_ORDER16(uint16 Val)
Definition ByteSwap.h:50
FPlatformTypes::int16 int16
A 16-bit signed integer.
Definition Platform.h:1123
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
FPlatformTypes::int64 int64
A 64-bit signed integer.
Definition Platform.h:1127
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
#define UE_FORCEINLINE_HINT
Definition Platform.h:723
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
float Val(const FString &Value)
Definition UnrealMath.cpp:3163
uint16_t uint16
Definition binka_ue_file_header.h:7
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition ByteSwap.h:14