optimization will generate wrong result lazarus / 62-General ---------------------------------------------------------------------- powerpcer · 2021-05-11 16:26 for function below, if i change the optimization to -O2, the result will be wrong 11, the correct one is 18. i test this code on win10 x64 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 edit_distance_bit(str1,str2:string):integer;        var          Peq:array[0..255] of Uint64;                Pv, Eq, Xv, Xh, Ph, Mh, Mv, ONE:Uint64;                i,j,m,n,Score:integer;                p1,p2:pbyte;                s:string;  begin    if (length(str1) < length(str2)) then    begin      p1:=@str2[1];                p2:=@str1[1];      m := length(str2);      n := length(str1);    end              else                begin      p1:=@str1[1];              p2:=@str2[1];      m := length(str1);      n := length(str2);              end;     ONE := 1;                fillbyte(Peq,0,sizeof(Peq));    Mv  := 0;    Score := m;     for i := 0 to m do                begin        Peq[p1[i]] :=Peq[p1[i]] or ONE shl i;        Pv :=Pv or (ONE shl i);    end;    for j := 0 to n do              begin        Eq := Peq[p2[j]];        Xv := Eq or Mv;      Xh := (((Eq and Pv) + Pv) xor Pv) or Eq;        Ph := Mv or (not (Xh or Pv));        Mh := Pv and Xh;        if (Ph and (ONE shl (m - 1)))<>0 then                                        inc(Score)        else                                 if (Mh and (ONE shl (m - 1)))<>0 then                                        dec(Score);        Ph :=Ph shl ONE;        Pv := (Mh shl ONE) or (not (Xv or Ph));        Mv := Ph and Xv;    end;    result := Score;  end;with testing code 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 j:=edit_dis tance_bit('agtcaaaagtcagtcagtcagtcagtcacagtcagaaggcatccaaccga','ccgtta gtcagaaacagtcagtcagtcagtcagtccagtcttaggcccgga'); ---------------------------------------------------------------------- AlexTP · 2021-05-11 16:31 FPC 3.2 fixes; Linux x64. -O0: 14 -O1: 14 -O2: 16 ---------------------------------------------------------------------- bytebites · 2021-05-11 16:42 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 fillbyte(Peq,0,sizeof(Peq)); Your code is wrong. The second argument of fillchar is count, the last is value. 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 Pv :=Pv or (ONE shl i); Pv is not initialised. ---------------------------------------------------------------------- powerpcer · 2021-05-12 01:13 thanks, after fixed, -O3 optimize faster little, from 40ms to 30ms