Skip to content

Constant integer division broken

Summary

Integer division gives a wrong result when inlined

System Information

  • Operating system: Linux
  • Processor architecture: x86-64
  • Compiler version: trunk b9dc428e

Example Project

function fquotient(a, b: integer): integer; inline; //= floor (a/b)
begin
  result := a div b;
{  if a < 0 then begin
    if result * b = a then exit;
    result -= 1;
  end}
end;

procedure test;
var seconds, min, hour: integer;
begin
  seconds := 3600 + 60 * 40 + 52;
  min      := fquotient(seconds, 60);
  seconds := seconds - min * 60;
  hour     := fquotient(min, 60);
  min := min - hour * 60;
  writeln(hour, ':',min, ':',seconds);
end;

What is the current bug behavior?

It prints 2:-209:11392

What is the expected (correct) behavior?

It should print 1:40:52

Possible fixes

Perhaps it is caused by one of @CuriousKit's optimizations

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information