There was an error in the code i supplied; the variable 'i' obviously 
has to be integer as corrected below. I show another routine (IsZero2) 
which is pretty much as I would have coded without any input. I couldn't 
hazard a guess as to which routine is faster.

Francois Piette suggested using assembler eg. the SCAS instruction with 
REP prefix. This would certainly be the fastest on a 486 but AFIR, when 
the Pentium arrived  it turned out that coding the loop 'manually' was 
faster.

Sorry about the error. I'm away the rest of the week and so have 
/really/ terminated:-)

-malcolm

function IsZero(const AddrOfDataToSearch:Pointer; const 
LengthInBytes:cardinal) : boolean;
var
  p:pLongWord; //pLongWord is defined System unit in D7
  i:integer;
begin
  p:=pLongWord(AddrOfDataToSearch);
  result:=false;
  for i:=0 to ((LengthInBytes div 4)-1) do
    if  p^ <> 0 then
      exit
    else
      inc(p);
  case LengthInBytes mod 4 of
    0: result:=true;
    1: if pByte(p)^=0 then result:=true;
    2: if pWord(p)^=0 then result:=true;
    3: if ((pWord(p)^=0) and (PByte(LongWord(p)+2)^=0)) then result:=true;
  end;
end;//IsZero
(*
;p:=pLongWord(AddrOfDataToSearch);
      mov   [esp],eax
;result:=false;
      xor   ecx,ecx
;for i:=0 to ((LengthInBytes div 4)-1) do
      mov   eax,edx
      shr   eax,2
      dec   eax
      test  eax,eax
      jb    $2
      inc   eax
;if  p^ <> 0 then exit
  $1  mov   ebx,[esp]
      cmp   dword ptr [ebx],0
      jnz   exit
;inc(p);
      add   dword ptr [esp], 4
;for i:=0 to ((LengthInBytes div 4)-1) do
      dec   eax
      jnz   $1
  $2
*)

function IsZero2(const AddrOfDataToSearch:Pointer; const 
LengthInBytes:cardinal) : boolean;
var
  p,pend:pLongWord; //pLongWord is defined System unit in D7
begin
  p:=pLongWord(AddrOfDataToSearch);
  LongWord(pend):=LongWord(p)+LengthInBytes div 4 * 4;
//This is probably faster but is less transparent:
//LongWord(pend):=LongWord(p)+LengthInBytes and not 3;
  result:=false;
  while LongWord(p)<LongWord(pend) do
    if  p^ <> 0 then
      exit
    else
      inc(p);
  case LengthInBytes mod 4 of
    0: result:=true;
    1: if pByte(p)^=0 then result:=true;
    2: if pWord(p)^=0 then result:=true;
    3: if ((pWord(p)^=0) and (PByte(LongWord(p)+2)^=0)) then result:=true;
  end;
end;//IsZero2

(*
;p:=pLongWord(AddrOfDataToSearch);
      mov   [esi],eax
;LongWord(pend):=LongWord(p)+LengthInBytes div 4 * 4;
      mov   ecx,edx
      shr   ecx,2
      shl   ecx,2
      add   ecx,[esi]
      mov   eax,ecx
;result:=false;
      xor   ecx,ecx
      jmp   @2
;if  p^ <> 0 then exit
  @1  mov   ebx,[esi]
      cmp   dword ptr [ebx],0
      jnz   exit
;inc(p);
      add   dword ptr [esi],4
;while LongWord(p)<LongWord(pend) do
  @2  mov   ebx,[esi]
      cmp   ebx,eax
      jb    @1
*)

_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi

Reply via email to