possible fpc bug lazarus / 62-General ---------------------------------------------------------------------- paulvanderlinden · 2023-03-21 14:12 got the following program: 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 program project1; {$mode objfpc}{$H+} var ind:integer=0;const vals:array[0..1] of integer=(1052909725,1092805937); function GetLong:integer;begin  Result := vals[ind];  inc(ind);end; function GetDouble: double;type TpDoubleRec=packed record v1,v2:integer;end;var V:TpDoubleRec absolute Result;begin  V.v1 := GetLong;  V.v2 := GetLong;end; var r:double;begin  r := GetDouble;  if round(r*100000)<>61916062257 then    writeln('wrong')  else    writeln('ok');end.  Compiling on windows as i386-win32:   without optimizing or with -O3: writes ok to console Compiling on linux as x86_63-linux:   without optimizing: writes ok to console   with -O3: writes wrong to console (and r is widely off, its roughly 2.15320923002915E-317) Am I doing something wrong here, or is it indeed a bug? Paul ---------------------------------------------------------------------- Fred vS · 2023-03-21 14:58 Hello. I confirm, here on x86_64-linux, with fpc 3.2.2, using -O- and -O1, the result is "ok". But with -O2, -O3, -O4 the result is "wrong". [EDIT] Tested with last trunk fpc 3.3.1 and same result as fpc 3.2.2. ---------------------------------------------------------------------- Laksen · 2023-03-21 15:25 It's a bug yes. It's not handling the absolute reference to the implicit result variable correctly Edit: And probably easy to fix since the following works: Code: [Select]function GetDouble: double; type TpDoubleRec=packed record v1,v2:integer;end;      PpDoubleRec = ^TpDoubleRec; var V:PpDoubleRec; begin   v:=@result;   V^.v1 := GetLong;   V^.v2 := GetLong; end; ---------------------------------------------------------------------- jamie · 2023-03-21 21:24 Works fine in all optimizers 0..4 with fpc 3.0.4 of course that is on Windows 64 bit. ---------------------------------------------------------------------- tetrastes · 2023-03-21 22:20 Quote from: Laksen on March 21, 2023, 04:25:32 pmIt's a bug yes. It's not handling the absolute reference to the implicit result variable correctly The matter is not in the implicit result. This code works with -O2/3/4 (fpc 3.2.2 x86_64-linux): 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 GetDouble: double;type TpDoubleRec=packed record v1,v2:integer;end;var    V: TpDoubleRec;    d: double absolute V;begin    V.v1 := GetLong;    V.v2 := GetLong;    Result := d;end;  But this does not: 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 GetDouble: double;type TpDoubleRec=packed record v1,v2:integer;end;var    d: double;     V: TpDoubleRec absolute d;begin    V.v1 := GetLong;    V.v2 := GetLong;    Result := d;end; ---------------------------------------------------------------------- Laksen · 2023-03-21 22:54 Quote from: tetrastes on March 21, 2023, 11:20:47 pmThe matter is not in the implicit result. It is. Your example shows the same thing just that d and result are getting coalesced or treated the same way. The bug is that the handling of the absolute variable should mark whatever value it is pointing at as not regable so the variable will be forced on the stack. ---------------------------------------------------------------------- tetrastes · 2023-03-21 23:11 Quote from: Laksen on March 21, 2023, 11:54:07 pmQuote from: tetrastes on March 21, 2023, 11:20:47 pmThe matter is not in the implicit result. It is. Your example shows the same thing just that d and result are getting coalesced or treated the same way. My example shows that the problem is with any variable, not only implicit result, as you had written. And most importantly, it is related to the order in which variables are declared. ---------------------------------------------------------------------- tetrastes · 2023-03-21 23:31 In fact the problem is related to packed record somehow, because this 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 function GetDouble: double;//type TpDoubleRec=packed record v1,v2:integer;end;type TpDoubleRec=array[0..1] of integer;var    d: double;     V: TpDoubleRec absolute d;begin    V[0] := GetLong;    V[1] := GetLong;    Result := d;end; and (naturally) this 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 GetDouble: double;//type TpDoubleRec=packed record v1,v2:integer;end;type TpDoubleRec=array[0..1] of integer;var    V: TpDoubleRec absolute Result;begin    V[0] := GetLong;    V[1] := GetLong;end; work with -O2/3/4. ---------------------------------------------------------------------- paulvanderlinden · 2023-03-22 07:54 Thanks guys for the confirmation and workarounds. Filed a bug here: https://gitlab.com/freepascal.org/fpc/source/-/issues/40212 btw, is there any list of isssues of fpc where things go silently wrong (like f.e. this case)? That way I can proactively try to find those cases in my source instead of bumping into them when manual testing... (unfortunately test-coverage is pretty low) ---------------------------------------------------------------------- MathMan · 2023-03-22 10:05 @paulvanderlinde Beside the point about the FPC compiler error, I wonder what you want to achieve and why you implemented it the way you did. One obvious issue with this is, that it will fail on little endian machines. I personally would have implemented a variant record e.g. like (beware, unchecked) Code: [Select]type   record case byte of     0: f64: double;     1: U64: UInt64;     2:     {$ifdef ENDIAN_LITTLE}       Hi: UInt32;       Lo: UInt32;     {$else}       Lo: UInt32;       Hi: UInt32;     {$endif}   end; Cheers, MathMan ---------------------------------------------------------------------- paulvanderlinden · 2023-03-22 10:19 well, its part of my WKBReader where I read coordinates from WKB. In my code I do consider the BigEndian/LittleEndian cases properly but I wanted to make my example as small as possible. ---------------------------------------------------------------------- MathMan · 2023-03-22 10:25 Quote from: paulvanderlinden on March 22, 2023, 11:19:48 am... In my code I do consider the BigEndian/LittleEndian cases properly but I wanted to make my example as small as possible. Thanks for the clarification - actually the Big/Little issue was my main concern. ---------------------------------------------------------------------- rasberryrabbit · 2023-05-09 00:02 It's bug in absolute. I made patch for this bug and two issue solved. https://gitlab.com/freepascal.org/fpc/source/-/issues/40239