[CLOSED] Optimization at function TFPReaderBMP.ShiftCount lazarus / 64-FPC development ---------------------------------------------------------------------- lagprogramming · 2024-01-09 11:43 packages/fcl-image/src/fpreadbmp.pp has Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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. ---------------------------------------------------------------------- Zvoni · 2024-01-09 12:04 Addition: 1) Line 16+17 can be coalesced into one line 2) Why use a tmp-var in the first place? ---------------------------------------------------------------------- RayoGlauco · 2024-01-09 12:28 My own version may be like this (I agree Zvoni's opinion): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     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; ---------------------------------------------------------------------- AlexTP · 2024-01-10 14:49 Posted and applied: https://gitlab.com/freepascal.org/fpc/source/-/issues/40590