UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
Arithmetic.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
7#include <cstdint>
8#include <cfloat>
9#include <math.h>
10
11namespace uLang
12{
17
22
23constexpr float Float32Min = FLT_MIN;
24constexpr float Float32Max = FLT_MAX;
25constexpr double Float64Min = DBL_MIN;
26constexpr double Float64Max = DBL_MAX;
27
28//
29// todo: implement the rest of the types (int32_t, int16_t, ...), and then also
30// provide platform specific optimized versions of this. in particular,
31// platforms that use the Clang and GCC compilers have intrinsics
32// available such as '__builtin_add_overflow' that will perform the operation
33// and return whether or not overflow happened, which should yield more
34// optimal code.
35//
37{
38 const bool bDidOverflow = (Rhs == Int64Min);
39 *OutResult = static_cast<int64_t>(~static_cast<uint64_t>(Rhs) + 1);
40 return !bDidOverflow;
41}
42
44{
45 int64_t Result = 0;
46 const bool bDidOverflow = (Rhs == Int64Min);
47
48 if (!bDidOverflow)
49 {
50 Result = (Rhs >= 0) ? Rhs : -Rhs;
51 }
52
53 *OutResult = Result;
54 return !bDidOverflow;
55}
56
58{
59 const int32_t Result = (int32_t)Rhs;
60 const bool bDidOverflow = (Rhs < Int32Min) || (Rhs > Int32Max);
61
62 *OutResult = Result;
63 return !bDidOverflow;
64}
65
66constexpr bool CheckedI64Add(int64_t Lhs, int64_t Rhs, int64_t* OutResult)
67{
68 // note it is important per the C++ spec to do the addition
69 // as unsigned, which is well defined to work as 'modulo 2^64'
70 // in the face of overflow, whereas signed addition is assumed
71 // to not overflow and invokes undefined behavior
72 const int64_t Result = (int64_t)((uint64_t)Lhs + (uint64_t)Rhs);
73
74 const bool bDidOverflow = ((Rhs >= 0) && (Result < Lhs)) ||
75 ((Lhs < 0) && (Result > Rhs));
76
77 *OutResult = Result;
78 return !bDidOverflow;
79}
80
82{
83 const int64_t Result = (int64_t)((uint64_t)Lhs - (uint64_t)Rhs);
84
85 const bool bDidOverflow = ((Rhs < 0) && (Result < Lhs)) ||
86 ((Rhs >= 0) && (Result > Lhs));
87
88 *OutResult = Result;
89 return !bDidOverflow;
90}
91
93{
94 // do it as sign magnitude
95 const bool bIsLhsNegative = Lhs < 0;
96 const bool bIsRhsNegative = Rhs < 0;
98
101
102 const uint64_t LhsLow = (uint64_t)(LhsMagnitude & 0xFFFFFFFF);
103 const uint64_t LhsHigh = (uint64_t)(LhsMagnitude >> 32);
104
105 const uint64_t RhsLow = (uint64_t)(RhsMagnitude & 0xFFFFFFFF);
106 const uint64_t RhsHigh = (uint64_t)(RhsMagnitude >> 32);
107
108 const uint64_t LowLow = LhsLow * RhsLow;
109 const uint64_t HighLow = LhsHigh * RhsLow;
110 const uint64_t LowHigh = LhsLow * RhsHigh;
112
113 // do long addition of the parts discarding the
114 // irrelevant lower bits because we are only
115 // interested in detecting whether the solution
116 // overflows or not
120 DetectOverflow >>= 32;
122
123 // assuming it does fit in 64 bits, this is the magnitude...
125
126 if (bIsResultNegative && (SolveLowMagnitude > 9223372036854775808ULL))
127 {
128 DetectOverflow = 1;
129 }
130 else if (!bIsResultNegative && (SolveLowMagnitude > 9223372036854775807ULL))
131 {
132 DetectOverflow = 1;
133 }
134
136
137 *OutResult = Result;
138 return (DetectOverflow == 0);
139}
140
142{
143 int64_t Result = 0;
144
145 const bool bDidOverflow = (Lhs == Int64Min) && (Rhs == -1LL);
146
147 if (!bDidOverflow)
148 {
149 Result = Lhs / Rhs;
150 }
151
152 *OutResult = Result;
153 return !bDidOverflow;
154}
155
157{
158 int64_t Result = 0;
159
160 const bool bDidOverflow = (Lhs == Int64Min) && (Rhs == -1LL);
161
162 if (!bDidOverflow)
163 {
164 Result = Lhs % Rhs;
165 }
166
167 *OutResult = Result;
168 return !bDidOverflow;
169}
170
172 return !(Right % Left);
173}
174
176 return (Left ^ Right) >= 0;
177}
178
180 if (Left == Int64Min && Right == -1)
181 {
182 return {};
183 }
185 bool bSameSign = SameSign(Left, Right);
186 return (Left / Right) + (bIsNotFactor & bSameSign);
187}
188
190 if (Left == Int64Min && Right == -1)
191 {
192 return {};
193 }
196 return (Left / Right) - (bIsNotFactor & bNotSameSign);
197}
198}
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
Definition VVMEngineEnvironment.h:23
constexpr float Float32Min
Definition Arithmetic.h:23
constexpr bool CheckedI64Divide(int64_t Lhs, int64_t Rhs, int64_t *OutResult)
Definition Arithmetic.h:141
constexpr double Float64Max
Definition Arithmetic.h:26
constexpr uint64_t Int64MaxMagnitude
Definition Arithmetic.h:20
constexpr bool CheckedI64Add(int64_t Lhs, int64_t Rhs, int64_t *OutResult)
Definition Arithmetic.h:66
constexpr bool CheckedI64Subtract(int64_t Lhs, int64_t Rhs, int64_t *OutResult)
Definition Arithmetic.h:81
constexpr int32_t Int32Min
Definition Arithmetic.h:13
constexpr int64_t Int64Min
Definition Arithmetic.h:18
TOptional< int64_t > CheckedI64DivideAndRoundUp(int64_t Left, int64_t Right)
Definition Arithmetic.h:179
constexpr uint32_t Int32MaxMagnitude
Definition Arithmetic.h:15
constexpr bool SameSign(int64_t Left, int64_t Right)
Definition Arithmetic.h:175
constexpr bool CheckedConvertI32I64(int64_t Rhs, int32_t *OutResult)
Definition Arithmetic.h:57
constexpr bool CheckedI64Abs(int64_t Rhs, int64_t *OutResult)
Definition Arithmetic.h:43
constexpr uint32_t UInt32Max
Definition Arithmetic.h:16
constexpr double Float64Min
Definition Arithmetic.h:25
constexpr bool CheckedI64Multiply(int64_t Lhs, int64_t Rhs, int64_t *OutResult)
Definition Arithmetic.h:92
constexpr bool IsFactor(int64_t Left, int64_t Right)
Definition Arithmetic.h:171
constexpr int64_t Int64Max
Definition Arithmetic.h:19
constexpr uint64_t UInt64Max
Definition Arithmetic.h:21
TOptional< int64_t > CheckedI64DivideAndRoundDown(int64_t Left, int64_t Right)
Definition Arithmetic.h:189
constexpr int32_t Int32Max
Definition Arithmetic.h:14
constexpr bool CheckedI64Modulo(int64_t Lhs, int64_t Rhs, int64_t *OutResult)
Definition Arithmetic.h:156
constexpr bool CheckedI64Negate(int64_t Rhs, int64_t *OutResult)
Definition Arithmetic.h:36
constexpr float Float32Max
Definition Arithmetic.h:24
Definition Optional.h:23