Forum > General

optimization will generate wrong result

(1/1)

powerpcer:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---j:=edit_distance_bit('agtcaaaagtcagtcagtcagtcagtcacagtcagaaggcatccaaccga','ccgttagtcagaaacagtcagtcagtcagtcagtccagtcttaggcccgga');

Alextp:
FPC 3.2 fixes; Linux x64.

-O0: 14
-O1: 14
-O2: 16

bytebites:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---fillbyte(Peq,0,sizeof(Peq));
Your code is wrong. The second argument of fillchar is count, the last is value.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Pv :=Pv or (ONE shl i);
Pv is not initialised.

powerpcer:
thanks,
after fixed, -O3 optimize faster little, from 40ms to 30ms