UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
Float16.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"
8#include "Math/Float32.h"
10
11template <typename T> struct TCanBulkSerialize;
12
34{
35public:
36
37 /* Float16 can store values in [-MaxF16Float,MaxF16Float] */
38 constexpr static float MaxF16Float = 65504.f;
39
41
43 [[nodiscard]] FFloat16() = default;
44
46 [[nodiscard]] FFloat16(const FFloat16& FP16Value) = default;
47
49 [[nodiscard]] FFloat16(float FP32Value);
50
53
56
58 operator float() const;
59
62 void Set(float FP32Value);
63
64 /*Convert from Fp32 to Fp16, round-to-nearest-even. (RTNE)
65 Clamps values out of range as +-MaxF16Float */
70
74 void CORE_API SetTruncate(float FP32Value);
75
77 void SetZero()
78 {
79 Encoded = 0;
80 }
81
83 void SetOne()
84 {
85 Encoded = 0x3c00;
86 }
87
90
93
95 [[nodiscard]] float GetFloat() const;
96
99 [[nodiscard]] bool IsNegative() const
100 {
101 // negative if sign bit is on
102 // can be tested with int compare
103 return (int16)Encoded < 0;
104 }
105
115 {
116 return Ar << V.Encoded;
117 }
118};
119template<> struct TCanBulkSerialize<FFloat16> { enum { Value = true }; };
120
122
127
128
130{
131 Set(FP32Value);
132 return *this;
133}
134
135
136UE_FORCEINLINE_HINT FFloat16::operator float() const
137{
138 return GetFloat();
139}
140
141
142// NOTE: Set() on values out of F16 max range store them as +-Inf
144{
145 // FPlatformMath::StoreHalf follows RTNE (round-to-nearest-even) rounding default convention
146 FPlatformMath::StoreHalf(&Encoded, FP32Value);
147}
148
149
150
152{
153 return FPlatformMath::LoadHalf(&Encoded);
154}
155
156
159{
160 FFloat16 ReturnValue;
161
162 if ( Encoded < 0x7c00 ) // normal and non-negative, just pass through
163 ReturnValue.Encoded = Encoded;
164 else if ( Encoded == 0x7c00 ) // infinity turns into largest normal
165 ReturnValue.Encoded = 0x7bff;
166 else // NaNs or anything negative turns into 0
167 ReturnValue.Encoded = 0;
168
169 return ReturnValue;
170}
171
172
175{
176 FFloat16 ReturnValue;
177
178 if ( (Encoded&0x7c00) == 0x7c00 )
179 {
180 // inf or nan
181 if ( Encoded == 0x7C00 ) //+inf
182 {
183 ReturnValue.Encoded = 0x7bff; // max finite
184 }
185 else if ( Encoded == 0xFC00 ) //-inf
186 {
187 ReturnValue.Encoded = 0xfbff; // max finite negative
188 }
189 else
190 {
191 // nan
192 ReturnValue.Encoded = 0;
193 }
194 }
195 else
196 {
197 ReturnValue.Encoded = Encoded;
198 }
199
200 return ReturnValue;
201}
FPlatformTypes::int16 int16
A 16-bit signed integer.
Definition Platform.h:1123
#define UE_FORCEINLINE_HINT
Definition Platform.h:723
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define DECLARE_INTRINSIC_TYPE_LAYOUT(T)
Definition MemoryLayout.h:760
USkinnedMeshComponent float
Definition SkinnedMeshComponent.h:60
uint16_t uint16
Definition binka_ue_file_header.h:7
Definition Archive.h:1208
Definition Float16.h:34
FFloat16(const FFloat16 &FP16Value)=default
static constexpr float MaxF16Float
Definition Float16.h:38
void SetZero()
Definition Float16.h:77
void CORE_API SetTruncate(float FP32Value)
Definition Float16.cpp:5
void SetClamped(float FP32Value)
Definition Float16.h:66
FFloat16 GetClampedNonNegativeAndFinite() const
Definition Float16.h:158
FFloat16()=default
FFloat16 GetClampedFinite() const
Definition Float16.h:174
uint16 Encoded
Definition Float16.h:40
FFloat16 & operator=(const FFloat16 &FP16Value)=default
FFloat16 & operator=(float FP32Value)
Definition Float16.h:129
friend FArchive & operator<<(FArchive &Ar, FFloat16 &V)
Definition Float16.h:114
float GetFloat() const
Definition Float16.h:151
void Set(float FP32Value)
Definition Float16.h:143
bool IsNegative() const
Definition Float16.h:99
void SetOne()
Definition Float16.h:83
static constexpr UE_FORCEINLINE_HINT T Clamp(const T X, const T MinValue, const T MaxValue)
Definition UnrealMathUtility.h:592
Definition Array.h:45
@ Value
Definition Array.h:46