UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
FloatPacker.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 "Logging/LogMacros.h"
7
9
10
15{
16public:
17 enum { MantissaBits = 23 };
18 enum { ExponentBits = 8 };
19 enum { SignShift = 31 };
20 enum { ExponentBias = 127 };
21
22 enum { MantissaMask = 0x007fffff };
23 enum { ExponentMask = 0x7f800000 };
24 enum { SignMask = 0x80000000 };
25
26 typedef float FloatType;
28
30 {
31 return *(PackedType*)&Value;
32 }
33
35 {
36 return *(FloatType*)&Value;
37 }
38};
39
40
44template<uint32 NumExponentBits, uint32 NumMantissaBits, bool bRound, typename FloatInfo=FFloatInfo_IEEE32>
46{
47public:
49
50 enum { MantissaShift = FloatInfo::MantissaBits - NumMantissaBits };
51 enum { ExponentBias = (1 << (NumExponentBits-1)) - 1 };
53
54 enum { MantissaMask = (1 << NumMantissaBits) - 1 };
56 enum { SignMask = 1 << SignShift };
57
58 enum { MinExponent = -ExponentBias - 1 };
60
61 typedef typename FloatInfo::PackedType PackedType;
62 typedef typename FloatInfo::FloatType FloatType;
63
65 {
66 if ( Value == (FloatType) 0.0 )
67 {
68 return (PackedType) 0;
69 }
70
71 const PackedType ValuePacked = FloatInfo::ToPackedType( Value );
72
73 // Extract mantissa, exponent, sign.
74 PackedType Mantissa = ValuePacked & FloatInfo::MantissaMask;
75 int32 Exponent = (ValuePacked & FloatInfo::ExponentMask) >> FloatInfo::MantissaBits;
76 const PackedType Sign = ValuePacked >> FloatInfo::SignShift;
77
78 // Subtract IEEE's bias.
79 Exponent -= FloatInfo::ExponentBias;
80
81 if ( bRound )
82 {
83 Mantissa += (1 << (MantissaShift-1));
84 if ( Mantissa & (1 << FloatInfo::MantissaBits) )
85 {
86 Mantissa = 0;
87 ++Exponent;
88 }
89 }
90
91 // Shift the mantissa to the right
92 Mantissa >>= MantissaShift;
93
94 //UE_LOG(LogFloatPacker, Log, TEXT("fp: exp: %i (%i,%i)"), Exponent, (int32)MinExponent, (int32)MaxExponent );
95 if ( Exponent < MinExponent )
96 {
97 return (PackedType) 0;
98 }
99 if ( Exponent > MaxExponent )
100 {
101 Exponent = MaxExponent;
102 }
103
104 // Add our bias.
105 Exponent -= MinExponent;
106
107 return (Sign << SignShift) | (Exponent << NumMantissaBits) | (Mantissa);
108 }
109
111 {
112 if ( Value == (PackedType) 0 )
113 {
114 return (FloatType) 0.0;
115 }
116
117 // Extract mantissa, exponent, sign.
118 PackedType Mantissa = Value & MantissaMask;
119 int32 Exponent = (Value & ExponentMask) >> NumMantissaBits;
120 const PackedType Sign = Value >> SignShift;
121
122 // Subtract our bias.
123 Exponent += MinExponent;
124 // Add IEEE's bias.
125 Exponent += FloatInfo::ExponentBias;
126
127 Mantissa <<= MantissaShift;
128
129 return FloatInfo::ToFloatType( (Sign << FloatInfo::SignShift) | (Exponent << FloatInfo::MantissaBits) | (Mantissa) );
130 }
131#if 0
133 {
134 if ( Value == (FloatType) 0.0 )
135 {
136 return (PackedType) 0;
137 }
138
139 const PackedType ValuePacked = FloatInfo::ToPackedType( Value );
140
141 // Extract mantissa, exponent, sign.
142 PackedType Mantissa = ValuePacked & FloatInfo::MantissaMask;
143 int32 Exponent = (ValuePacked & FloatInfo::ExponentMask) >> FloatInfo::MantissaBits;
144 //const PackedType Sign = ValuePacked >> FloatInfo::SignShift;
145
146 // Subtract IEEE's bias.
147 Exponent -= FloatInfo::ExponentBias;
148
149 if ( bRound )
150 {
151 Mantissa += (1 << (MantissaShift-1));
152 if ( Mantissa & (1 << FloatInfo::MantissaBits) )
153 {
154 Mantissa = 0;
155 ++Exponent;
156 }
157 }
158
159 // Shift the mantissa to the right
160 Mantissa >>= MantissaShift;
161
162 //UE_LOG(LogFloatPacker, Log, TEXT("fp: exp: %i (%i,%i)"), Exponent, (int32)MinExponent, (int32)MaxExponent );
163 if ( Exponent < MinExponent )
164 {
165 if ( Exponent < MinExponent-1 )
166 {
167 return (PackedType) 0;
168 }
169 Exponent = MinExponent;
170 }
171 if ( Exponent > MaxExponent )
172 {
173 Exponent = MaxExponent;
174 }
175
176 // Add our bias.
177 Exponent -= MinExponent;
178
179 return (Exponent << NumMantissaBits) | (Mantissa);
180 }
181
183 {
184 if ( Value == (PackedType) 0 )
185 {
186 return (FloatType) 0.0;
187 }
188
189 // Extract mantissa, exponent, sign.
190 PackedType Mantissa = Value & MantissaMask;
191 int32 Exponent = (Value & ExponentMask) >> NumMantissaBits;
192 //const PackedType Sign = Value >> SignShift;
193
194 // Subtract our bias.
195 Exponent += MinExponent;
196 // Add IEEE's bias.
197 Exponent += FloatInfo::ExponentBias;
198
199 Mantissa <<= MantissaShift;
200
201 return FloatInfo::ToFloatType( (Exponent << FloatInfo::MantissaBits) | (Mantissa) );
202 }
203#endif
204};
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define DECLARE_LOG_CATEGORY_EXTERN(CategoryName, DefaultVerbosity, CompileTimeVerbosity)
Definition LogMacros.h:361
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition FloatPacker.h:15
@ ExponentMask
Definition FloatPacker.h:23
@ ExponentBits
Definition FloatPacker.h:18
uint32 PackedType
Definition FloatPacker.h:27
@ MantissaMask
Definition FloatPacker.h:22
@ MantissaBits
Definition FloatPacker.h:17
static PackedType ToPackedType(FloatType Value)
Definition FloatPacker.h:29
@ SignMask
Definition FloatPacker.h:24
float FloatType
Definition FloatPacker.h:26
static FloatType ToFloatType(PackedType Value)
Definition FloatPacker.h:34
@ ExponentBias
Definition FloatPacker.h:20
@ SignShift
Definition FloatPacker.h:19
Definition FloatPacker.h:46
PackedType Encode(FloatType Value) const
Definition FloatPacker.h:64
@ MinExponent
Definition FloatPacker.h:58
FloatInfo::FloatType FloatType
Definition FloatPacker.h:62
@ ExponentMask
Definition FloatPacker.h:55
@ ExponentBias
Definition FloatPacker.h:51
FloatInfo::PackedType PackedType
Definition FloatPacker.h:61
@ MantissaMask
Definition FloatPacker.h:54
@ SignMask
Definition FloatPacker.h:56
@ SignShift
Definition FloatPacker.h:52
@ MantissaShift
Definition FloatPacker.h:50
FloatType Decode(PackedType Value) const
Definition FloatPacker.h:110
@ MaxExponent
Definition FloatPacker.h:59
@ NumOutputsBits
Definition FloatPacker.h:48