Skip to content

Optimization at function TFPReaderBMP.ShiftCount

from 'lagprogramming'.

packages/fcl-image/src/fpreadbmp.pp has

    function TFPReaderBMP.ShiftCount(Mask : longword) : shortint;
    var tmp : shortint;
    begin
      tmp:=0;
      if Mask=0 then
      begin
        Result:=0;
        exit;
      end;
     
      while (Mask mod 2)=0 do { rightmost bit is 0 }
      begin
        inc(tmp);
        Mask:= Mask shr 1;
      end;
      tmp:=tmp-(8-popcnt(byte(Mask and $FF)));
      Result:=tmp;
    end;

The following patch replaces "while (Mask mod 2)=0 do" with the faster "while (Mask and 1)=0 do" line. patch.diff

Another forum user:

Addition:

  1. Line 16+17 can be coalesced into one line
  2. Why use a tmp-var in the first place?

Forum user 'RayoGlauco':

My own version may be like this

function TFPReaderBMP.ShiftCount(Mask : longword) : shortint;
begin
  Result:=0;
  if Mask=0 then
    exit;
 
  while (Mask and 1)=0 do { rightmost bit is 0 }
  begin
    inc(Result);
    Mask:= Mask shr 1;
  end;
  Result:= Result-(8-popcnt(byte(Mask and $FF)));
end;
Edited by Alexey Torgashin
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information