[fpc-devel] Unexpected range check error (64-bit only)

Jonas Maebe jonas at freepascal.org
Sat Jan 1 13:20:55 CET 2022

On 01/01/2022 13:01, Bart via fpc-devel wrote:
> OK, let me see if I understand it correctly now:
> 1. (Signed) Int64 :=  unsigned(32-16-8bit) + unsigned(32-16-8-bit) +
> signed(32-16-8-bit)
> gets evaluated with unsigned 64-bit arithmetic and then the test for
> <high(signed 64-bit) fails.
> 2. (Signed) Int64 := unsigned(32-16-8-bit) + signed(32-16-8-bit)
> gets eval with signed arithmetic.


> How to workaround (1) then, other then turning range checking off?
> (Since I hardly ever trust myself, almost all of my applications ahve
> range checking on even in release mode.)

Add an explicit typecast to longint or int64 for the first term.

Pascal evaluates expressions independent of how the result is used. Even 
if it did take the final use into account, we'd end up with tons of 
peculiar cases related to overloads and intrinsics like writeln.

> And the consequence is that even if we eventually have native 128-bit
> integer support, the problem will persist but now for Int128?

Yes, unless we would only support/use it for intermediate results.


More information about the fpc-devel mailing list