Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
Int128.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 "Serialization/Archive.h"
7
9{
10private:
11 /** Internal values representing this number */
12 uint64 Hi;
13 uint64 Lo;
14
15 FORCEINLINE uint32 AddInternal(uint32 A, uint32 B, uint32& Carry)
16 {
17 uint64 Result = (uint64)A + (uint64)B + (uint64)Carry;
18 Carry = (Result >> 32) & 1;
19 return (uint32)Result;
20 }
21
22 FORCEINLINE uint32 MultiplyInternal(uint32 Multiplicand, uint32 Multiplier, uint32& Carry)
23 {
24 uint64 Result;
25 Result = ((uint64)Multiplicand * (uint64)Multiplier) + (uint64)Carry;
26 Carry = Result >> 32;
27 return (uint32)Result;
28 }
29
30 FORCEINLINE uint32 DivideInternal(uint32 Dividend, uint32 Divisor, uint32& Remainder)
31 {
32 uint64 Value = ((uint64)Remainder << 32) | Dividend;
33 Remainder = Value % Divisor;
34 return (uint32)(Value / Divisor);
35 }
36
37public:
38 /** Gets internal quad parts. */
39 FORCEINLINE uint32 GetQuadPart(uint32 Part)
40 {
41 switch (Part)
42 {
43 case 3: return Hi >> 32;
44 case 2: return (uint32)Hi;
45 case 1: return Lo >> 32;
46 case 0: return (uint32)Lo;
47 default: check(0);
48 }
49 return 0;
50 }
51
52 /** Sets internal quad parts. */
53 FORCEINLINE void SetQuadPart(uint32 Part, uint32 Value)
54 {
55 switch (Part)
56 {
57 case 3: Hi = (Hi & 0x00000000ffffffffull) | ((uint64)Value << 32); break;
58 case 2: Hi = (Hi & 0xffffffff00000000ull) | Value; break;
59 case 1: Lo = (Lo & 0x00000000ffffffffull) | ((uint64)Value << 32); break;
60 case 0: Lo = (Lo & 0xffffffff00000000ull) | Value; break;
61 default: check(0);
62 }
63 }
64
65 /** Sets this number to 0. */
67 {
68 Hi = Lo = 0;
69 }
70
71 /** Initializes this number with a pair of 64 bit integer values. */
72 FORCEINLINE void Set(uint64 InHi, uint64 InLo)
73 {
74 Hi = InHi;
75 Lo = InLo;
76 }
77
78 /** Default constructors. */
79 FORCEINLINE FUInt128(const FUInt128&) = default;
83
84 /** Default constructor. Initializes the number to zero. */
86 : Hi(0)
87 , Lo(0)
88 {}
89
90 /** Constructor. Initializes this uint128 with a uint64 value. */
92 : Hi(0)
93 , Lo(A)
94 {}
95
96 /** Constructor. Initializes this uint128 with two uint64 values. */
97 FORCEINLINE FUInt128(uint64 A, uint64 B)
98 : Hi(A)
99 , Lo(B)
100 {}
101
102 /** Constructor. Initializes this uint128 with four uint32 values. */
103 FORCEINLINE FUInt128(uint32 A, uint32 B, uint32 C, uint32 D)
104 : Hi(((uint64)A << 32) | B)
105 , Lo(((uint64)C << 32) | D)
106 {}
107
108 /** this > Other */
109 FORCEINLINE bool IsGreater(const FUInt128& Other) const
110 {
111 if (Hi == Other.Hi)
112 {
113 return Lo > Other.Lo;
114 }
115 return Hi > Other.Hi;
116 }
117
118 /** this >= Other */
119 FORCEINLINE bool IsGreaterOrEqual(const FUInt128& Other) const
120 {
121 if (Hi == Other.Hi)
122 {
123 return Lo >= Other.Lo;
124 }
125 return Hi >= Other.Hi;
126 }
127
128 /** this < Other */
129 FORCEINLINE bool IsLess(const FUInt128& Other) const
130 {
131 if (Hi == Other.Hi)
132 {
133 return Lo < Other.Lo;
134 }
135 return Hi < Other.Hi;
136 }
137
138 /** this <= Other */
139 FORCEINLINE bool IsLessOrEqual(const FUInt128& Other) const
140 {
141 if (Hi == Other.Hi)
142 {
143 return Lo <= Other.Lo;
144 }
145 return Hi <= Other.Hi;
146 }
147
148 /** this == Other */
149 FORCEINLINE bool IsEqual(const FUInt128& Other) const
150 {
151 return (Hi == Other.Hi) && (Lo == Other.Lo);
152 }
153
154 /** Add an unsigned 32bit value */
156 {
157 uint32 Carry = 0;
158
159 FUInt128 Result;
164 return Result;
165 }
166
167 /* Subtract an unsigned 32bit value */
169 {
170 uint32 Carry = 0;
171
172 uint32 AddValue = ~Value + 1;
173 uint32 SignExtend = (AddValue >> 31) ? ~0 : 0;
174
175 FUInt128 Result;
176 Result.SetQuadPart(0, AddInternal(GetQuadPart(0), AddValue, Carry));
177 Result.SetQuadPart(1, AddInternal(GetQuadPart(1), SignExtend, Carry));
178 Result.SetQuadPart(2, AddInternal(GetQuadPart(2), SignExtend, Carry));
179 Result.SetQuadPart(3, AddInternal(GetQuadPart(3), SignExtend, Carry));
180 return Result;
181 }
182
183 /** Multiply by an unsigned 32bit value */
184 FORCEINLINE FUInt128 Multiply(uint32 Multiplier)
185 {
186 uint32 Carry = 0;
187
188 FUInt128 Result;
189 Result.SetQuadPart(0, MultiplyInternal(GetQuadPart(0), Multiplier, Carry));
190 Result.SetQuadPart(1, MultiplyInternal(GetQuadPart(1), Multiplier, Carry));
191 Result.SetQuadPart(2, MultiplyInternal(GetQuadPart(2), Multiplier, Carry));
192 Result.SetQuadPart(3, MultiplyInternal(GetQuadPart(3), Multiplier, Carry));
193 return Result;
194 }
195
196 /** Divide by an unsigned 32bit value */
197 FORCEINLINE FUInt128 Divide(uint32 Divisor, uint32& Remainder)
198 {
199 Remainder = 0;
200
201 FUInt128 Result;
202 Result.SetQuadPart(3, DivideInternal(GetQuadPart(3), Divisor, Remainder));
203 Result.SetQuadPart(2, DivideInternal(GetQuadPart(2), Divisor, Remainder));
204 Result.SetQuadPart(1, DivideInternal(GetQuadPart(1), Divisor, Remainder));
205 Result.SetQuadPart(0, DivideInternal(GetQuadPart(0), Divisor, Remainder));
206 return Result;
207 }
208
209 /**
210 * Comparison operators
211 */
212 FORCEINLINE bool operator>(const FUInt128& Other) const
213 {
214 return IsGreater(Other);
215 }
216
217 FORCEINLINE bool operator>=(const FUInt128& Other) const
218 {
219 return IsGreaterOrEqual(Other);
220 }
221
222 FORCEINLINE bool operator==(const FUInt128& Other) const
223 {
224 return IsEqual(Other);
225 }
226
227 FORCEINLINE bool operator<(const FUInt128& Other) const
228 {
229 return IsLess(Other);
230 }
231
232 FORCEINLINE bool operator<=(const FUInt128& Other) const
233 {
234 return IsLessOrEqual(Other);
235 }
236
237 FORCEINLINE bool IsZero() const
238 {
239 return !(Hi | Lo);
240 }
241
243 {
244 return !IsZero();
245 }
246
247 /**
248 * Arithmetic operators
249 */
251 {
252 *this = Add(Other);
253 return *this;
254 }
255
257 {
258 *this = Sub(Other);
259 return *this;
260 }
261
263 {
264 *this = Multiply(Other);
265 return *this;
266 }
267
269 {
270 uint32 Remainder;
271 *this = Divide(Other, Remainder);
272 return *this;
273 }
274
275 /**
276 * Serialization
277 */
278 friend FArchive& operator<<(FArchive& Ar, FUInt128& Value)
279 {
280 return Ar << Value.Hi << Value.Lo;
281 }
282};
#define check(expr)
#define FORCEINLINE
Definition Platform.h:644
FORCEINLINE FUInt128 & operator=(FUInt128 const &)=default
FORCEINLINE void Set(uint64 InHi, uint64 InLo)
Definition Int128.h:72
uint64 Hi
Definition Int128.h:12
FORCEINLINE uint32 MultiplyInternal(uint32 Multiplicand, uint32 Multiplier, uint32 &Carry)
Definition Int128.h:22
FORCEINLINE FUInt128 Add(uint32 Value)
Definition Int128.h:155
FORCEINLINE FUInt128 & operator=(FUInt128 &&)=default
FORCEINLINE FUInt128(uint64 A, uint64 B)
Definition Int128.h:97
FORCEINLINE bool operator<(const FUInt128 &Other) const
Definition Int128.h:227
FORCEINLINE FUInt128 Divide(uint32 Divisor, uint32 &Remainder)
Definition Int128.h:197
FORCEINLINE bool IsLess(const FUInt128 &Other) const
Definition Int128.h:129
FORCEINLINE bool IsZero() const
Definition Int128.h:237
FORCEINLINE bool IsGreaterOrEqual(const FUInt128 &Other) const
Definition Int128.h:119
FORCEINLINE FUInt128()
Definition Int128.h:85
FORCEINLINE bool operator==(const FUInt128 &Other) const
Definition Int128.h:222
FORCEINLINE FUInt128 & operator-=(uint32 Other)
Definition Int128.h:256
FORCEINLINE bool IsGreater(const FUInt128 &Other) const
Definition Int128.h:109
FORCEINLINE FUInt128 & operator*=(uint32 Other)
Definition Int128.h:262
FORCEINLINE void Zero()
Definition Int128.h:66
uint64 Lo
Definition Int128.h:13
FORCEINLINE bool operator<=(const FUInt128 &Other) const
Definition Int128.h:232
FORCEINLINE bool operator>(const FUInt128 &Other) const
Definition Int128.h:212
FORCEINLINE bool IsLessOrEqual(const FUInt128 &Other) const
Definition Int128.h:139
FORCEINLINE void SetQuadPart(uint32 Part, uint32 Value)
Definition Int128.h:53
FORCEINLINE uint32 DivideInternal(uint32 Dividend, uint32 Divisor, uint32 &Remainder)
Definition Int128.h:30
FORCEINLINE FUInt128(uint32 A, uint32 B, uint32 C, uint32 D)
Definition Int128.h:103
FORCEINLINE bool IsGreaterThanZero() const
Definition Int128.h:242
FORCEINLINE FUInt128 Multiply(uint32 Multiplier)
Definition Int128.h:184
FORCEINLINE uint32 AddInternal(uint32 A, uint32 B, uint32 &Carry)
Definition Int128.h:15
FORCEINLINE bool operator>=(const FUInt128 &Other) const
Definition Int128.h:217
FORCEINLINE FUInt128 & operator/=(uint32 Other)
Definition Int128.h:268
FORCEINLINE FUInt128(FUInt128 &&)=default
FORCEINLINE uint32 GetQuadPart(uint32 Part)
Definition Int128.h:39
FORCEINLINE FUInt128 & operator+=(uint32 Other)
Definition Int128.h:250
FORCEINLINE bool IsEqual(const FUInt128 &Other) const
Definition Int128.h:149
FORCEINLINE FUInt128 Sub(uint32 Value)
Definition Int128.h:168
FORCEINLINE FUInt128(uint64 A)
Definition Int128.h:91
FORCEINLINE FUInt128(const FUInt128 &)=default