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

2015-05-29 Thread Edmund Grimley Evans
 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.


 As jumps in many cases are slow due to queue issues, this might be a severe
 performance hit.

It's an interesting problem. There may be cases in which you don't
need to know about the exception immediately, in which case you could
avoid conditional branches by doing a conditional select in the loop,
say, and only testing after the loop whether a division by zero has
occurred. However, most branch predictors can cope quite well with a
branch that is almost never taken.

In the case of floating-point operations, the AArch64 hardware does
something like that for you: it sets a sticky bit when the exceptional
thing has happened. However, testing whether that bit has been set is
probably more expensive than checking whether the divisor is zero so
you'd want to put off the test until after several operations.

How much freedom do you have in the choice of which exceptions you
detect and when?

Just for a check list, the AArch64 cumulative exception bits are:

IXC : Inexact
UFC : Underflow
OFC : Overflow
DZC : Division by Zero
IOC : Invalid Operation

(I don't have much practical experience of programming with
floating-point operations, though I have at times studied what AArch64
instructions do.)

Edmund
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


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

2015-05-29 Thread Michael Schnell

On 05/29/2015 08:37 AM, Jonas Maebe wrote:


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.


As jumps in many cases are slow due to queue issues, this might be a 
severe performance hit.


How much slower is the code after introducing the check ?

Can the check be disable for high performance code snippets ?

What does gcc work  on that behave ?

-Michael
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


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

2015-05-29 Thread Michael Schnell

On 05/29/2015 10:22 AM, Edmund Grimley Evans wrote:
There may be cases in which you don't need to know about the exception 
immediately.


In fact  - especially with high-performance applications - there are 
cases where the algorithm you are doing guarantees that the divisor is 
not zero, so testing does not make sense.


-Michael
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


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

2015-05-29 Thread Jonas Maebe


Edmund Grimley Evans wrote on Fri, 29 May 2015:


How much freedom do you have in the choice of which exceptions you
detect and when?


In theory: all the freedom in the world. E.g., when using the x87  
instructions on x86 hardware, floating point exceptions are  
(currently) only thrown when the next x87 instruction starts executing  
following the one that caused the exception. This can be in a  
completely different part of the program, if you're unlucky, and is  
due to the origins of the x87 as an independent/off-chip co-processor.  
For a long time, I've had the idea of adding a compiler switch that  
optionally emits an fwait instruction after every x87 instruction  
that might cause an exception, but never got around it.


In practice, I think that throwing the exception at the point where it  
occurs is best. It's true that it's not ideal from a performance  
perspective, but in that case you can just disable the generation of  
exception-generating code. We already have an architecture-independent  
way to raise an exception if the FPU detected an error while its  
exceptions were masked (using the Math unit as described in my  
previously referenced post). Therefore you can perform manual checking  
only at the points you care about in that case (with the caveat that's  
also mentioned in that previous post, namely that currently this kind  
of checking is only implemented on AArch64).



Just for a check list, the AArch64 cumulative exception bits are:

IXC : Inexact
UFC : Underflow
OFC : Overflow
DZC : Division by Zero
IOC : Invalid Operation


These are more or less the same on all architectures. The reason is  
probably that they're mandated by the IEEE 754 standard:  
http://en.wikipedia.org/wiki/IEEE_floating_point#Exception_handling



Jonas
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


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

2015-05-29 Thread Jonas Maebe

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 6.8.3.2 — 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 
though.



Jonas
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


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

2015-05-28 Thread Edmund Grimley Evans
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, 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. Clearly the compiler could generate code
to check for exceptions but it would be very inefficient when those
exceptions are not required.

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

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

Edmund
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel