Changeset 52


Ignore:
Timestamp:
Nov 16, 2023, 2:01:59 PM (3 months ago)
Author:
chronos
Message:
  • Modified: BigInt implementation improvements.
Location:
branches/ByteArray
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/ByteArray/BigInt.pas

    r51 r52  
    4747    class operator GreaterThanOrEqual(A, B: TBigInt): Boolean;
    4848    procedure Shrink;
     49    function IsNegative: Boolean;
     50    function IsZero: Boolean;
     51    function IsPositive: Boolean;
    4952    procedure SetByteArray(AData: TArrayOfByte; ASize: TBigIntSize);
    5053    procedure GetByteArray(var AData: TArrayOfByte; ASize: TBigIntSize);
    5154    function Copy(Size: TBigIntSize): TBigInt;
    52     property Size: Byte read GetSize write SetSize;
     55    property Size: TBigIntSize read GetSize write SetSize;
    5356    property Data[Index: TBigIntSize]: Byte read GetData write SetData;
    5457  end;
     
    8588procedure TBigInt.SetData(Index: TBigIntSize; AValue: Byte);
    8689begin
    87   if Index > Size - 1 then Size := Index + 1;
     90  if Index > Integer(Size) - 1 then Size := Index + 1;
    8891  FData[Index] := AValue;
    8992end;
     
    241244  Carry: Byte;
    242245begin
    243   Result.Size := A.Size;
     246  Result.Size := Max(A.Size, B.Size);
    244247  Carry := 0;
    245248  I := 0;
     
    259262begin
    260263  Result := A.Size = B.Size;
     264  if not Result then Exit;
    261265  for I := 0 to A.Size - 1 do
    262266    if A.FData[I] <> B.FData[I] then begin
     
    271275begin
    272276  Result := A.Size = B.Size;
     277  if not Result then Exit;
    273278  for I := 0 to A.Size - 1 do
    274279    if A.FData[I] <> B.FData[I] then begin
     
    280285
    281286class operator TBigInt.LessThan(A, B: TBigInt): Boolean;
    282 begin
    283 
     287var
     288  Temp: TBigInt;
     289begin
     290  Temp := A - B;
     291  Result := Temp.IsNegative;
    284292end;
    285293
    286294class operator TBigInt.LessThanOrEqual(A, B: TBigInt): Boolean;
    287 begin
    288 
     295var
     296  Temp: TBigInt;
     297begin
     298  Temp := A - B;
     299  Result := Temp.IsNegative or Temp.IsZero;
    289300end;
    290301
    291302class operator TBigInt.GreaterThan(A, B: TBigInt): Boolean;
    292 begin
    293 
     303var
     304  Temp: TBigInt;
     305begin
     306  Temp := A - B;
     307  Result := Temp.IsPositive and not Temp.IsZero;
    294308end;
    295309
    296310class operator TBigInt.GreaterThanOrEqual(A, B: TBigInt): Boolean;
    297 begin
    298 
     311var
     312  Temp: TBigInt;
     313begin
     314  Temp := A - B;
     315  Result := Temp.IsPositive;
    299316end;
    300317
     
    307324    Dec(I);
    308325  Size := I + 1;
     326end;
     327
     328function TBigInt.IsNegative: Boolean;
     329begin
     330  Result := (FData[Size - 1] and $80) = $80;
     331end;
     332
     333function TBigInt.IsZero: Boolean;
     334var
     335  I: TBigIntSize;
     336begin
     337  Result := True;
     338  for I := 0 to Size - 1 do
     339    if FData[I] <> 0 then begin
     340      Result := False;
     341      Break;
     342    end;
     343end;
     344
     345function TBigInt.IsPositive: Boolean;
     346begin
     347  Result := (FData[Size - 1] and $80) = 0;
    309348end;
    310349
     
    329368    4: PDWord(@Result.FData[0])^ := PDWord(@FData[0])^;
    330369    8: PQWord(@Result.FData[0])^ := PQWord(@FData[0])^;
     370    else Move(PByte(@FData[0])^, PByte(@Result.FData[0])^, Size);
    331371  end;
    332372end;
  • branches/ByteArray/Cpu.pas

    r50 r52  
    101101    IO: TChannel;
    102102    function Read(Size: TBigIntSize): TBigInt;
     103    function ReadSize: TBigIntSize;
    103104    function ReadRegIndex: TRegIndex;
    104105    procedure Write(Size: TBigIntSize; Value: TBigInt);
     
    173174  DataSize: TBigIntSize;
    174175begin
    175   DataSize := Read(SizeOf(TBigIntSize));
     176  DataSize := ReadSize;
    176177  RegIndex := ReadRegIndex;
    177178  Regs[RegIndex] := Read(DataSize);
     
    194195  RegIndex2: TRegIndex;
    195196begin
    196   DataSize := Read(SizeOf(TBigIntSize));
     197  DataSize := ReadSize;
    197198  RegIndex := ReadRegIndex;
    198199  RegIndex2 := ReadRegIndex;
     
    217218  RegIndex: TRegIndex;
    218219begin
    219   DataSize := Read(SizeOf(TBigIntSize));
     220  DataSize := ReadSize;
    220221  AddressSize := Read(SizeOf(TBigIntSize));
    221222  RegIndex := ReadRegIndex;
     
    241242  RegIndex: TRegIndex;
    242243begin
    243   DataSize := Read(SizeOf(TBigIntSize));
     244  DataSize := ReadSize;
    244245  AddressSize := Read(SizeOf(TBigIntSize));
    245246  Address := Read(AddressSize);
     
    279280  AddressSize: TBigIntSize;
    280281begin
    281   DataSize := Read(SizeOf(TBigIntSize));
     282  DataSize := ReadSize;
    282283  AddressSize := Read(SizeOf(TBigIntSize));
    283284  RegIndex := ReadRegIndex;
     
    305306  AddressSize: TBigIntSize;
    306307begin
    307   DataSize := Read(SizeOf(TBigIntSize));
     308  DataSize := ReadSize;
    308309  AddressSize := Read(SizeOf(TBigIntSize));
    309310  RegIndex := ReadRegIndex;
     
    322323  AddressSize: TBigIntSize;
    323324begin
    324   AddressSize := Read(SizeOf(TBigIntSize));
     325  AddressSize := ReadSize;
    325326  PC := PC + Read(AddressSize);
    326327end;
     
    336337  AddressSize: TBigIntSize;
    337338begin
    338   AddressSize := Read(SizeOf(TBigIntSize));
     339  AddressSize := ReadSize;
    339340  Push(PC, AddressSize);
    340341  PC := Read(AddressSize);
     
    350351  AddressSize: TBigIntSize;
    351352begin
    352   AddressSize := Read(SizeOf(TBigIntSize));
     353  AddressSize := ReadSize;
    353354  PC := Pop(AddressSize);
    354355end;
     
    367368  RegIndex: TRegIndex;
    368369begin
    369   DataSize := Read(SizeOf(TBigIntSize));
     370  DataSize := ReadSize;
    370371  RegIndex := ReadRegIndex;
    371372  Push(Regs[RegIndex], DataSize);
     
    385386  RegIndex: TRegIndex;
    386387begin
    387   DataSize := Read(SizeOf(TBigIntSize));
     388  DataSize := ReadSize;
    388389  RegIndex := ReadRegIndex;
    389390  Regs[RegIndex] := Pop(DataSize);
     
    406407  DataSize: TBigIntSize;
    407408begin
    408   DataSize := Read(SizeOf(TBigIntSize));
     409  DataSize := ReadSize;
    409410  RegIndex := ReadRegIndex;
    410411  RegIndex2 := ReadRegIndex;
     
    428429  DataSize: TBigIntSize;
    429430begin
    430   DataSize := Read(SizeOf(TBigIntSize));
     431  DataSize := ReadSize;
    431432  RegIndex := ReadRegIndex;
    432433  RegIndex2 := ReadRegIndex;
     
    447448  DataSize: TBigIntSize;
    448449begin
    449   DataSize := Read(SizeOf(TBigIntSize));
     450  DataSize := ReadSize;
    450451  RegIndex := ReadRegIndex;
    451452  Regs[RegIndex] := Regs[RegIndex].Copy(DataSize) + 1;
     
    465466  DataSize: TBigIntSize;
    466467begin
    467   DataSize := Read(SizeOf(TBigIntSize));
     468  DataSize := ReadSize;
    468469  RegIndex := ReadRegIndex;
    469470  Regs[RegIndex] := Regs[RegIndex].Copy(DataSize) - 1;
     
    519520  Result := Memory.Read(PC, Size);
    520521  PC := PC + Size;
     522end;
     523
     524function TCpu.ReadSize: TBigIntSize;
     525begin
     526  Result := Read(SizeOf(TBigIntSize));
    521527end;
    522528
  • branches/ByteArray/Disassembler.pas

    r46 r52  
    114114end;
    115115
    116 
    117116end.
    118117
Note: See TracChangeset for help on using the changeset viewer.