Sun's OBP and Apple's OpenFirmware have different behaviours from each other when a divide-by-zero error is encountered. Since we can't reenter Forth from a trap on all platforms, detect this case before performing the division and allow an optional defer word to be executed instead.
Signed-off-by: Mark Cave-Ayland mark.cave-ayland@ilande.co.uk --- openbios-devel/forth/bootstrap/bootstrap.fs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)
diff --git a/openbios-devel/forth/bootstrap/bootstrap.fs b/openbios-devel/forth/bootstrap/bootstrap.fs index 6878449..dc20caf 100644 --- a/openbios-devel/forth/bootstrap/bootstrap.fs +++ b/openbios-devel/forth/bootstrap/bootstrap.fs @@ -445,10 +445,23 @@ variable leaves 0 leaves ! \ 7.3.2.3 double number arithmetic \
+defer (div-by-zero-handler) + : s>d dup 0 < ; : dnegate 0 0 2swap d- ; : dabs dup 0 < if dnegate then ; -: um/mod mu/mod drop ; +: um/mod + dup 0 = if + \ Allow optional override of divide by zero handling + ['] (div-by-zero-handler) /n + @ 0 <> if + (div-by-zero-handler) drop + else + mu/mod drop + then + else + mu/mod drop + then + ;
\ symmetric division : sm/rem ( d n -- rem quot )
As reported by Amadeusz Sławiński using a real Mac, if anything is divided by zero then we should return zero:
0 > 2 0 / ok 1 > u. 0 ok
Signed-off-by: Mark Cave-Ayland mark.cave-ayland@ilande.co.uk --- openbios-devel/arch/ppc/ppc.fs | 7 +++++++ 1 file changed, 7 insertions(+)
diff --git a/openbios-devel/arch/ppc/ppc.fs b/openbios-devel/arch/ppc/ppc.fs index 6995ba9..212894d 100644 --- a/openbios-devel/arch/ppc/ppc.fs +++ b/openbios-devel/arch/ppc/ppc.fs @@ -53,5 +53,12 @@ \ other \ -------------------------------------------------------------------------
+\ Override divide by zero handling +\ Tests indicate that Apple's OF implementation returns a single value of 0 + +: (ppc-div-by-zero-handler) 3drop 0 0 0 ; + +['] (ppc-div-by-zero-handler) to (div-by-zero-handler) + \ Set by BootX when booting Mac OS X defer spin
As reported by Artyom Tarasenko, if anything is divided by zero then we should return no value:
0 > 4 2 / u. 2 ok 0 > 2 0 / ok
Signed-off-by: Mark Cave-Ayland mark.cave-ayland@ilande.co.uk --- openbios-devel/arch/sparc32/init.fs | 7 +++++++ openbios-devel/arch/sparc64/init.fs | 7 +++++++ 2 files changed, 14 insertions(+)
diff --git a/openbios-devel/arch/sparc32/init.fs b/openbios-devel/arch/sparc32/init.fs index 7306eab..7d20d4d 100644 --- a/openbios-devel/arch/sparc32/init.fs +++ b/openbios-devel/arch/sparc32/init.fs @@ -45,3 +45,10 @@ device-end : obmem ( -- space ) 0 ; + +\ Override divide by zero handling +\ Tests indicate that Sun's OBP implementation returns no value in this case + +: (sparc-div-by-zero-handler) 3drop 0 0 ; + +['] (sparc-div-by-zero-handler) to (div-by-zero-handler) diff --git a/openbios-devel/arch/sparc64/init.fs b/openbios-devel/arch/sparc64/init.fs index a1cadc1..535b7d2 100644 --- a/openbios-devel/arch/sparc64/init.fs +++ b/openbios-devel/arch/sparc64/init.fs @@ -53,3 +53,10 @@ device-end : rmap@ ( virt -- rmentry ) drop 0 ; + +\ Override divide by zero handling +\ Tests indicate that Sun's OBP implementation returns no value in this case + +: (sparc-div-by-zero-handler) 3drop 0 0 ; + +['] (sparc-div-by-zero-handler) to (div-by-zero-handler)
Hi Mark,
On Mon, Jan 7, 2013 at 4:07 PM, Mark Cave-Ayland mark.cave-ayland@ilande.co.uk wrote:
As reported by Artyom Tarasenko, if anything is divided by zero then we should return no value:
0 > 4 2 / u. 2 ok 0 > 2 0 / ok
NAK. Either my memory is cheating on me or there is some other Artyom Tarasenko who reported it. OBP produces an exception in case of division by zero.
(This time adding you to cc, since the mailing list bounces my mails saying my emai is graylisted - whatever it means).
Signed-off-by: Mark Cave-Ayland mark.cave-ayland@ilande.co.uk
openbios-devel/arch/sparc32/init.fs | 7 +++++++ openbios-devel/arch/sparc64/init.fs | 7 +++++++ 2 files changed, 14 insertions(+)
diff --git a/openbios-devel/arch/sparc32/init.fs b/openbios-devel/arch/sparc32/init.fs index 7306eab..7d20d4d 100644 --- a/openbios-devel/arch/sparc32/init.fs +++ b/openbios-devel/arch/sparc32/init.fs @@ -45,3 +45,10 @@ device-end : obmem ( -- space ) 0 ;
+\ Override divide by zero handling +\ Tests indicate that Sun's OBP implementation returns no value in this case
+: (sparc-div-by-zero-handler) 3drop 0 0 ;
+['] (sparc-div-by-zero-handler) to (div-by-zero-handler) diff --git a/openbios-devel/arch/sparc64/init.fs b/openbios-devel/arch/sparc64/init.fs index a1cadc1..535b7d2 100644 --- a/openbios-devel/arch/sparc64/init.fs +++ b/openbios-devel/arch/sparc64/init.fs @@ -53,3 +53,10 @@ device-end : rmap@ ( virt -- rmentry ) drop 0 ;
+\ Override divide by zero handling +\ Tests indicate that Sun's OBP implementation returns no value in this case
+: (sparc-div-by-zero-handler) 3drop 0 0 ;
+['] (sparc-div-by-zero-handler) to (div-by-zero-handler)
1.7.10.4
-- OpenBIOS http://openbios.org/ Mailinglist: http://lists.openbios.org/mailman/listinfo Free your System - May the Forth be with you