[fpc-devel] no exceptions from arithmetical operations on AArch64

Jonas Maebe jonas.maebe at elis.ugent.be
Fri May 29 08:37:04 CEST 2015

Edmund Grimley Evans wrote:
> Looking through the tests that fail on aarch64-linux I found ten that
> depend on an arithmetical operation causing an exception, usually
> division by zero:
> tbs/tb0262
> test/texception4
> test/units/math/tmask
> test/units/math/tmask2
> webtbs/tw3157
> webtbs/tw3160a
> webtbs/tw3160b
> webtbs/tw3160c
> webtbs/tw3161
> webtbs/tw4100
> In AArch64 the instruction for integer division never causes an
> exception,

The compiler adds an explicit comparison to ensure that in case of an 
integer division by zero, a run time error is raised anyway. The same is 
done on PowerPC, which doesn't trigger an exception for integer division 
by zero either.

> and floating-point arithmetic can only cause an exception
> if an optional part of the architecture is enabled; most hardware
> doesn't even have the option.

It turns out that this is also optional in the regular ARM architecture, 
although there most implementations do seem to support it. An AArch64 
cpu without the support running plain ARM code won't trigger floating 
point exceptions either though.

> Clearly the compiler could generate code
> to check for exceptions but it would be very inefficient when those
> exceptions are not required.

The idea is to add a code generation option for this, so the programmer 
can decide whether they want the exceptions (and associated performance 
overhead) or not.

> I can't find an explicit statement in the documentation but this
> example suggests that exceptions from arithmetical operations are not
> required by the language definition:
> http://www.freepascal.org/docs-html/ref/refse101.html#x212-22200017.1

While the example does not have the intention of demonstrating that, it 
is in fact true that according to the ISO Extended Pascal standard 
nothing special needs to happen in case of a division by zero:
a) "A term of the form i div j shall be an error if j is zero" 
(http://www.pascal-central.com/docs/iso10206.pdf , section — the 
same goes for "i / j", which is the floating point division).
b) definition of Error: "A violation by a program of the requirements of 
this International Standard that a processor is permitted to leave 
undetected" (section 3.2)

However, we generally mainly aim for Turbo Pascal and Delphi 
compatibility, and most code written for those compilers (and for FPC on 
other platforms) assumes that it is at least possible to configure the 
platform to raise floating point exceptions.

> So, can those tests be disabled on AArch64, by adding
> { %skipcpu=aarch64 }?

I don't think that's the best approach. Here are two possible alternatives:
a) we add the "math" unit to the uses clause, check whether floating 
point exceptions can be enabled (see 
http://lists.freepascal.org/pipermail/fpc-devel/2015-February/035397.html for 
the details) and if not, skip the test (that would also cover running 
the tests when compiled for ARM)
b) we leave them as they are until support has been added to the code 
generator to generate explicit exception checks after each floating 
point operation that may trigger one (and then specify that this option 
should be used when compiling those tests; the compiler will ignore it 
on platforms that don't need it)

My preference goes to option b). I haven't started working on that yet 


More information about the fpc-devel mailing list