FillInteger Implementation in Delphi/Object Pascal


This post gives a few C++ implementations that fill an array of integers by every 4 byte. Please note that the function FillChar works on the byte level (filling each byte).

The following gives the simple translation to Delphi/Object Pascal Code. If it is 32 bit, then you can use the following:

procedure FillInteger(var Dest; Count: Integer; What: Integer); assembler ;
asm
  PUSH EDI
    MOV EDI, Dest
    MOV EAX, What
    MOV ECX, Count
    CLD
    REP STOSD
  POP EDI
end;

In 64-bit Delphi, a pure Pascal Code is pretty optimised, so instead of using assembly, you can write it in a cleaner way.

procedure FillInteger(var Dest; Count: Integer; What: Integer);
var
  p: ^Integer;
  i: integer;
begin
  p := @Dest;
  for i := 0 to Count - 1 do
  begin
    p[i] := What;
  end;
end;

Alternatively, we can use pointer arithmetic to do the tricks.

procedure FillInteger(var Dest; Count: Integer; What: Integer);
var
  i: integer;
  p: ^Integer;
begin
  p := @Dest;
  for i := 0 to count - 1 do
  begin
    p^ := What;
    Inc(p, 4);
  end;
end;

You can use {$IFDEF WIN32} {$ELSE} {$ENDIF} to combine both that works both on 32 and 64 bit.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
256 words
Last Post: Passing String or StringBuilder to Function in C# (By Value or By Reference)
Next Post: How to Test Upload/Download Speed of Your Web Server?

The Permanent URL is: FillInteger Implementation in Delphi/Object Pascal

Leave a Reply