Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
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
11/**
12 *
13 */
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;
27 typedef uint32 PackedType;
28
30 {
31 return *(PackedType*)&Value;
32 }
33
35 {
36 return *(FloatType*)&Value;
37 }
38};
39
40
41/**
42 *
43 */
44template<uint32 NumExponentBits, uint32 NumMantissaBits, bool bRound, typename FloatInfo=FFloatInfo_IEEE32>
46{
47public:
48 enum { NumOutputsBits = NumExponentBits + NumMantissaBits + 1 };
49
50 enum { MantissaShift = FloatInfo::MantissaBits - NumMantissaBits };
51 enum { ExponentBias = (1 << (NumExponentBits-1)) - 1 };
52 enum { SignShift = NumExponentBits + NumMantissaBits };
53
54 enum { MantissaMask = (1 << NumMantissaBits) - 1 };
55 enum { ExponentMask = ((1 << NumExponentBits)-1) << NumMantissaBits };
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
72
73 // Extract mantissa, exponent, sign.
77
78 // Subtract IEEE's bias.
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
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 {
102 }
103
104 // Add our bias.
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.
120 const PackedType Sign = Value >> SignShift;
121
122 // Subtract our bias.
124 // Add IEEE's bias.
126
128
130 }
131#if 0
133 {
134 if ( Value == (FloatType) 0.0 )
135 {
136 return (PackedType) 0;
137 }
138
140
141 // Extract mantissa, exponent, sign.
144 //const PackedType Sign = ValuePacked >> FloatInfo::SignShift;
145
146 // Subtract IEEE's bias.
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
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 }
170 }
171 if ( Exponent > MaxExponent )
172 {
174 }
175
176 // Add our bias.
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.
192 //const PackedType Sign = Value >> SignShift;
193
194 // Subtract our bias.
196 // Add IEEE's bias.
198
200
202 }
203#endif
204};
DECLARE_LOG_CATEGORY_EXTERN(LogFloatPacker, Log, All)
static PackedType ToPackedType(FloatType Value)
Definition FloatPacker.h:29
static FloatType ToFloatType(PackedType Value)
Definition FloatPacker.h:34
PackedType Encode(FloatType Value) const
Definition FloatPacker.h:64
FloatInfo::FloatType FloatType
Definition FloatPacker.h:62
FloatInfo::PackedType PackedType
Definition FloatPacker.h:61
FloatType Decode(PackedType Value) const