On Dec 21, 2012, at 6:27 AM, Mark Cave-Ayland wrote:
On 21/12/12 11:01, Amadeusz Sławiński wrote:
10.2 ones show that there is need for some solution regarding division by 0, some notes from my talks with Mark:
My g5 machine returns 0 when dividing by 0, no warnings are printed: 0> 0 0 / ok 1> . ok 0> 0
0> 2 0 / ok 1> . ok 0> 0
For some more context, the reason we need to allow divide by zero for OpenBIOS PPC is because of this amazing piece of brain-dead programming in BootX's DrawSplashScreen() function:
if (gBootFileType != kNetworkDeviceType) { SpinInit(0, 0, NULL, 0, 0, 0, 0, 0); }
where SpinInit() invokes the Forth slw_spin_init word via CIF that looks like this:
: slw_spin_init dup FFFF and to cursorH 10 >> drop dup FFFF and to cursorW 10 >> to cursorPixelSize dup FFFF and to cursorY 10 >> d# 1000 swap / to cursorDelay dup FFFF and to cursorX 10 >> to cursorFrames to cursorAddr to screenIH ['] slw_spin to spin ;
It's fairly easy to see that if you invoke slw_spin_init with all its parameters set to zero, you'll get a divide by zero error.
Question is how to implement it, or maybe just use gsoc patch?
For reference, the GSOC patch is this: http://lists.openbios.org/pipermail/openbios/2011-August/006651.html. I'm not sure this is the best way to do it though.
I'm currently thinking the best way would be to alter the "/" Forth word using throw/catch and have it throw an exception with a zero denominator which calls a "divide-by-zero" defer word. This behaviour can then be specified on a per-architecture basis.
I also like still allowing internal C divisions to invoke the processor divide by zero traps, as it's very useful at pointing out when you've forgotten to set something rather than carrying on with an erroneus result. Do people think this is an acceptable solution?
What if someone made their program knowing that division by zero is equal to zero? This is the behavior that Apple's Open Firmware follows. I think we should follow it for the PowerPC target.