[fpc-pascal]mixed cardinal/signed operations

Jonas Maebe jonas at zeus.rug.ac.be
Wed Dec 13 19:53:00 CET 2000


Hello,

As of tomorrow, the fixes branch snapshot (and as such also the 
soon-to-be-released 1.0.4) will evaluate divisions which contain a 
cardinal (unsigned 32bit integer) and a signed expression by first 
converting both arguments to int64 and then performing the operation with 
64bits of precision. The result of such an operation will be an int64 as 
well.

The reason is that if you have

var
  l: longint;
  d: cardinal;
begin
  l := -1;
  d := 1;
  writeln(l div d);
end.

the program will currently terminate with a division by zero error (the 
actual error is a division overflow, but the errorcode is the same).

This 64bit evaluation will also be used for addition, substraction and 
multiplication (if the above operand conditions are fulfilled) when range 
and/or overflow checking is used. A warning will be issued in all cases.

In general, you won't notice this (except that this 64bit operation is 
obviously a lot slower than a normal 32bit one), except if you typecast 
the result back to a pointer, because an int64 can't be converted to a 
pointer. In that case, either add another typecast to dword/longint in 
between, or (preferably) typecase all operands of the oepration to either 
longint or dword so no 64bit evaluation is necessary.

BTW: if someone thinks this 64bit evaluation should be the default for 
all mathematical operations regardless of whether range and/or overflow 
checking is enabled, please speak up, because we aren't 100% sure 
ourselves of how this should be handled.


Jonas




More information about the fpc-pascal mailing list