UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
VVMHeapInt.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3/*
4 * Copyright (C) 2017 Caio Lima <ticaiolima@gmail.com>
5 * Copyright (C) 2019-2022 Apple Inc. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#pragma once
30
31#if WITH_VERSE_VM || defined(__INTELLISENSE__)
32
33#include "CoreTypes.h"
34#include "HAL/Platform.h"
37#include "VVMType.h"
38#include "VerseVM/VVMFloat.h"
39#include <cstdint>
40
41namespace Verse
42{
43enum class EValueStringFormat;
44
45struct VHeapInt final : VHeapValue
46{
49
50 using Digit = uint32;
51
52 static VHeapInt& New(FAllocationContext Context, uint32 NumWords)
53 {
54 return *CreateWithLength(Context, NumWords);
55 }
56
57 static VHeapInt& New(FAllocationContext Context, bool Sign, TArrayView<Digit> Digits)
58 {
60 }
61
62 FORCENOINLINE static VHeapInt& FromInt64(FAllocationContext Context, int64 Int64)
63 {
64 return *CreateFrom(Context, Int64);
65 }
66
67 COREUOBJECT_API bool IsInt32() const;
68 COREUOBJECT_API int32 AsInt32() const;
69
70 COREUOBJECT_API bool IsInt64() const;
71 COREUOBJECT_API int64 AsInt64() const;
72
73 COREUOBJECT_API VFloat ConvertToFloat() const;
74
75 COREUOBJECT_API static bool Equals(VHeapInt& X, VHeapInt& Y);
76
77 COREUOBJECT_API static VHeapInt* CreateZero(FAllocationContext Context);
78
79 COREUOBJECT_API static VHeapInt* Add(FAllocationContext, VHeapInt& X, VHeapInt& Y);
80 COREUOBJECT_API static VHeapInt* Sub(FAllocationContext, VHeapInt& X, VHeapInt& Y);
81 COREUOBJECT_API static VHeapInt* Multiply(FAllocationContext, VHeapInt& X, VHeapInt& Y);
82 COREUOBJECT_API static VHeapInt* Divide(FAllocationContext, VHeapInt& X, VHeapInt& Y, bool* bOutHasNonZeroRemainder = nullptr);
83 COREUOBJECT_API static VHeapInt* Modulo(FAllocationContext, VHeapInt& X, VHeapInt& Y);
84 // Note, modulo result is always positive, even where `Modulo` would produce a negative result.
85 COREUOBJECT_API static TTuple<VHeapInt*, Digit> DivideModulo(FAllocationContext, VHeapInt&, Digit);
86 COREUOBJECT_API static VHeapInt* UnaryMinus(FAllocationContext, VHeapInt& X);
87
88 enum class ComparisonResult
89 {
90 Equal,
94 };
95
97
98 inline bool IsZero() const
99 {
100 check(GetLength() || !GetSign());
101 return GetLength() == 0;
102 }
103
104 // false means positive, true means negative
105 bool GetSign() const { return static_cast<bool>(Sign); }
106 Digit GetDigit(const uint32 Index) const;
107 unsigned int GetLength() const { return Length; }
108
109 COREUOBJECT_API void AppendDecimalToString(FUtf8StringBuilderBase& Builder, FAllocationContext Context);
111
112 COREUOBJECT_API void AppendToStringImpl(FAllocationContext Context, FUtf8StringBuilderBase& Builder, EValueStringFormat Format, TSet<const void*>& VisitedObjects, uint32 RecursionDepth);
114
115 static constexpr bool SerializeIdentity = false;
116 static void SerializeLayout(FAllocationContext Context, VHeapInt*& This, FStructuredArchiveVisitor& Visitor);
117 void SerializeImpl(FAllocationContext Context, FStructuredArchiveVisitor& Visitor);
118
119 enum class InitializationType
120 {
121 None,
123 };
125 void SetDigit(const uint32 Index, Digit Value); // Use only when initializing.
126 static VHeapInt* CreateWithLength(FAllocationContext, uint32 length);
127 static VHeapInt* Copy(FAllocationContext, const VHeapInt& X);
128 inline Digit* DataStorage()
129 {
130 return Digits;
131 }
132
133 // The maximum length that the current implementation supports would be
134 // MaxInt / DigitBits. However, we use A lower limit for now, because
135 // raising it later is easier than lowering it.
136 // Support up to 1 million bits.
137 static constexpr unsigned BitsPerByte = 8;
138 static constexpr unsigned DigitBits = sizeof(Digit) * BitsPerByte;
139 static constexpr unsigned MaxLengthBits = 1024 * 1024;
140 static constexpr unsigned MaxLength = MaxLengthBits / DigitBits;
141 static_assert(MaxLengthBits % DigitBits == 0);
142
143private:
144 explicit VHeapInt(FAllocationContext Context, uint32 NumWords)
146 , Length(NumWords)
147 {
148 }
149
150 COREUOBJECT_API static VHeapInt* CreateFrom(FAllocationContext, int64 Value);
151 static VHeapInt* CreateWithDigits(FAllocationContext, bool Sign, TArrayView<Digit> Digits);
152
153 void SetSign(bool NewSign) { Sign = NewSign; }
154
155 VHeapInt* RightTrim(FAllocationContext);
156
157 static VHeapInt* CreateFromImpl(FAllocationContext, uint64 Value, bool sign);
158
159 static constexpr unsigned HalfDigitBits = DigitBits / 2;
160 static constexpr Digit HalfDigitMask = (1ull << HalfDigitBits) - 1;
161 static constexpr int MaxInt = 0x7FFFFFFF;
162
163 static ComparisonResult AbsoluteCompare(const VHeapInt& X, const VHeapInt& Y);
164 static ComparisonResult AbsoluteCompare(const VHeapInt&, Digit);
165 static void MultiplyAccumulate(const VHeapInt& Multiplicand, Digit Multiplier, VHeapInt* Accumulator, uint32 AccumulatorIndex);
166
167 // Digit arithmetic helpers.
168 static Digit DigitAdd(Digit A, Digit B, Digit& Carry);
169 static Digit DigitSub(Digit A, Digit B, Digit& Borrow);
170 static Digit DigitMul(Digit A, Digit B, Digit& High);
171
172 static VHeapInt* AbsoluteAdd(FAllocationContext, VHeapInt& X, VHeapInt& Y, bool ResultSign);
173 static VHeapInt* AbsoluteSub(FAllocationContext, VHeapInt& X, VHeapInt& Y, bool ResultSign);
174
175 Digit AbsoluteInplaceAdd(const VHeapInt& Summand, uint32 StartIndex);
176 Digit AbsoluteInplaceSub(const VHeapInt& Subtrahend, uint32 StartIndex);
177 void InplaceRightShift(uint32 Shift);
178
179 static bool AbsoluteDivWithDigitDivisor(FAllocationContext Context, const VHeapInt& X, Digit Divisor, VHeapInt** Quotient, Digit& Remainder);
180 static void AbsoluteDivWithHeapIntDivisor(FAllocationContext Context, const VHeapInt& Dividend, const VHeapInt& Divisor, VHeapInt** Quotient, VHeapInt** Remainder, bool* bOutHasNonZeroRemainder = nullptr);
181 inline static Digit DigitDiv(Digit high, Digit low, Digit divisor, Digit& remainder);
182
183 enum class LeftShiftMode
184 {
187 };
188
189 static VHeapInt* AbsoluteLeftShiftAlwaysCopy(FAllocationContext Context, const VHeapInt& X, uint32 Shift, LeftShiftMode Mode);
190 inline static bool ProductGreaterThan(Digit Factor1, Digit Factor2, Digit High, Digit Low);
191
192 static void InternalMultiplyAdd(const VHeapInt& Source, Digit Factor, Digit Summand, uint32 N, VHeapInt* Result);
193
194 inline const Digit* DataStorage() const
195 {
196 return Digits;
197 }
198
199 const uint32 Length;
200 uint8 Sign{false}; // false means positive, true means negative
201 Digit Digits[];
202};
203
204inline VHeapInt::Digit VHeapInt::GetDigit(const uint32 Index) const
205{
206 check(Index < GetLength());
207 return DataStorage()[Index];
208}
209
210inline void VHeapInt::SetDigit(uint32 Index, Digit Value)
211{
212 check(Index < GetLength());
213 DataStorage()[Index] = Value;
214}
215
216inline uint32 GetTypeHash(const VHeapInt& HeapInt)
217{
218 uint32 Result = ::GetTypeHash(HeapInt.GetSign());
219
220 for (uint32 I = 0; I < HeapInt.GetLength(); I++)
221 {
222 Result = ::HashCombineFast(Result, ::GetTypeHash(HeapInt.GetDigit(I)));
223 }
224
225 return Result;
226}
227} // namespace Verse
228#endif // WITH_VERSE_VM
#define FORCENOINLINE
Definition AndroidPlatform.h:142
#define check(expr)
Definition AssertionMacros.h:314
FPlatformTypes::int64 int64
A 64-bit signed integer.
Definition Platform.h:1127
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define X(Name, Desc)
Definition FormatStringSan.h:47
T * New(FMemStackBase &Mem, int32 Count=1, int32 Align=DEFAULT_ALIGNMENT)
Definition MemStack.h:259
constexpr uint32 HashCombineFast(uint32 A, uint32 B)
Definition TypeHash.h:74
@ Multiplier
uint8_t uint8
Definition binka_ue_file_header.h:8
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition JsonObject.h:23
Definition ArrayView.h:139
Definition UnrealString.h.inl:34
Definition SharedPointer.h:692
Definition StringBuilder.h:79
constexpr bool Compare(const InAT &InputA, const InBT &InputB, ProjectionT Projection, PredicateT Predicate)
Definition Compare.h:15
FORCEINLINE T * Get(const FObjectPtr &ObjectPtr)
Definition ObjectPtr.h:426
bool SerializeImpl(const UScriptStruct *InSourceEventType, const void *InSourceEventData, FLiveLinkSerializedFrameData &OutSerializedData)
Definition LiveLinkCompression.cpp:126
UE_STRING_CLASS Result(Forward< LhsType >(Lhs), RhsLen)
Definition String.cpp.inl:732
@ MaxLength
[PropertyMetadata] Used for FString and FText properties. Indicates the maximum length of the value t...
Definition ObjectMacros.h:1449
Definition Archive.h:36
EValueStringFormat
Definition VVMValuePrinting.h:17
void AppendDecimalToString(FUtf8StringBuilderBase &Builder, VFloat Float, EFloatStringFormat Format)
Definition VVMFloatPrinting.cpp:25
uint32 GetTypeHash(TPtrVariant< Ts... > Ptr)
Definition VVMPtrVariant.h:83
U16 Index
Definition radfft.cpp:71
Definition Tuple.h:652