Fixed array init in BigInt shift and replaced Move

This commit is contained in:
Stefan Müller 2024-05-16 17:08:05 +02:00
parent df8b5c32fd
commit 7630bdddeb
1 changed files with 12 additions and 6 deletions

View File

@ -161,7 +161,7 @@ end;
class function TBigInt.AddAbsoluteValues(constref AA, AB: TBigInt; const AReturnNegative: Boolean): TBigInt; class function TBigInt.AddAbsoluteValues(constref AA, AB: TBigInt; const AReturnNegative: Boolean): TBigInt;
var var
i, lenA, lenB, len, shorter: Integer; i, j, lenA, lenB, len, shorter: Integer;
carry: Cardinal; carry: Cardinal;
begin begin
lenA := Length(AA.FDigits); lenA := Length(AA.FDigits);
@ -208,9 +208,11 @@ begin
// carry-overs. This avoids additional tests for finding the shorter digit array. // carry-overs. This avoids additional tests for finding the shorter digit array.
if (i < lenA) or (i < lenB) then if (i < lenA) or (i < lenB) then
if lenA >= lenB then if lenA >= lenB then
Move(AA.FDigits[i], Result.FDigits[i], CDigitSize * (len - i)) for j := i to len - 1 do
Result.FDigits[j] := AA.FDigits[j]
else else
Move(AB.FDigits[i], Result.FDigits[i], CDigitSize * (len - i)); for j := i to len - 1 do
Result.FDigits[j] := AB.FDigits[j];
// Applies the remaining carry-overs until the end of the prepared result digit array. // Applies the remaining carry-overs until the end of the prepared result digit array.
while (carry > 0) and (i < len) do while (carry > 0) and (i < len) do
@ -235,7 +237,7 @@ class function TBigInt.SubtractAbsoluteValues(constref AA, AB: TBigInt; const AR
var var
a, b: TBigInt; a, b: TBigInt;
carry: Cardinal; carry: Cardinal;
i, lastNonZeroDigitIndex, len: Integer; i, j, lastNonZeroDigitIndex, len: Integer;
begin begin
// Establishes the operand order, such that Abs(a) is not less than Abs(b). // Establishes the operand order, such that Abs(a) is not less than Abs(b).
if (AA.CompareToAbsoluteValues(AB) >= 0) then if (AA.CompareToAbsoluteValues(AB) >= 0) then
@ -300,7 +302,8 @@ begin
// Copies the missing unchanged digits from the longer operand to the result, if any. If there are none, then no trim // Copies the missing unchanged digits from the longer operand to the result, if any. If there are none, then no trim
// needs to occur because the most significant digit is not zero. // needs to occur because the most significant digit is not zero.
if i < len then if i < len then
Move(a.FDigits[i], Result.FDigits[i], CDigitSize * (len - i)) for j := i to len - 1 do
Result.FDigits[j] := a.FDigits[j]
else if (lastNonZeroDigitIndex + 1 < len) then else if (lastNonZeroDigitIndex + 1 < len) then
// Trims leading zeros from the digits array. // Trims leading zeros from the digits array.
Delete(Result.FDigits, lastNonZeroDigitIndex + 1, len - lastNonZeroDigitIndex - 1); Delete(Result.FDigits, lastNonZeroDigitIndex + 1, len - lastNonZeroDigitIndex - 1);
@ -605,7 +608,10 @@ begin
// Performs full digit shifts by copy if there are no bit shifts. // Performs full digit shifts by copy if there are no bit shifts.
len := Length(A.FDigits); len := Length(A.FDigits);
SetLength(Result.FDigits, len + digitShifts); SetLength(Result.FDigits, len + digitShifts);
Move(A.FDigits[0], Result.FDigits[digitShifts], CDigitSize * len); for i := 0 to digitShifts - 1 do
Result.FDigits[i] := 0;
for i := 0 to len - 1 do
Result.FDigits[i + digitShifts] := A.FDigits[i];
end; end;
Result.FIsNegative := A.IsNegative; Result.FIsNegative := A.IsNegative;