← board

Function Result (float) read-modified inside a loop miscompiles to 0

Symptom

Inside a function returning Double, reading-and-writing Result in a loop loses the value — the result comes out 0.

function E: Double;
var kc: Integer;
begin
  Result := 1.359141;
  kc := 1;
  while kc > 0 do begin Result := Result * 2.0; kc := kc - 1; end;  { -> 0, not 2.718 }
end;

Narrowed:

So it is specifically the function-result float slot being read back across loop iterations.

Likely cause

The float Result is probably kept in xmm0 / a fixed location across the function, and the loop body's reload/store of Result (mixed with the integer counter update) clobbers it or reloads a stale/zero value each iteration. Compare the working float-local path — the local has a stable stack slot that the loop reloads correctly. Audit how AN_ASSIGN to the function-result symbol lowers when the RHS also reads the result, within a loop body.

Workaround in place

lib/rtl/math.pas (Exp, ArcTan) accumulates into a plain local and assigns Result once at the end.

Acceptance

Result := Result <op> e inside a loop in a Double function yields the correct value; FPC byte-identical; make test + make cross-bootstrap green.

Log