From 7ba4e67564dafdbe02ccd872845fbdc3792b05d3 Mon Sep 17 00:00:00 2001
From: sergei <gorelkin@nanoreflex.ru>
Date: Thu, 20 Mar 2014 01:59:06 +0000
Subject: [PATCH] * Fixed Linux FPU exception mapping for x86_64 and MIPS,
 might be still incomplete, but at least operations yielding NaNs no longer
 produce EAccessViolation.

git-svn-id: trunk@27194 -
---
 rtl/linux/mips/sighnd.inc   |  8 ++------
 rtl/linux/x86_64/sighnd.inc | 10 +++++-----
 2 files changed, 7 insertions(+), 11 deletions(-)

diff --git a/rtl/linux/mips/sighnd.inc b/rtl/linux/mips/sighnd.inc
index c2d8938b1f..cd4279d101 100644
--- a/rtl/linux/mips/sighnd.inc
+++ b/rtl/linux/mips/sighnd.inc
@@ -31,19 +31,15 @@ begin
           
           case  siginfo^.si_code of
             FPE_INTDIV:
-              res:=200;
+              res:=200;  // maps to EDivByZero
             FPE_INTOVF:
               res:=215;
             FPE_FLTDIV:
-              res:=200;
+              res:=208;  // maps to EZeroDivide
             FPE_FLTOVF:
               res:=205;
             FPE_FLTUND:
               res:=206;
-            FPE_FLTRES,
-            FPE_FLTINV,
-            FPE_FLTSUB:
-              res:=216;
             else
               res:=207;
           end;
diff --git a/rtl/linux/x86_64/sighnd.inc b/rtl/linux/x86_64/sighnd.inc
index cbab9aea05..e8fa3cac0c 100644
--- a/rtl/linux/x86_64/sighnd.inc
+++ b/rtl/linux/x86_64/sighnd.inc
@@ -22,9 +22,11 @@ const
 function GetFPUState(const SigContext : TSigContext) : word;
   begin
     if assigned(SigContext.fpstate) then
-      GetfpuState:=SigContext.fpstate^.swd;
+      GetfpuState:=SigContext.fpstate^.swd
+    else
+      GetFPUState:=0;
   {$ifdef SYSTEM_DEBUG}
-    writeln('xx:',sigcontext.en_tw,' ',sigcontext.en_cw);
+    writeln('xx:',sigcontext.twd,' ',sigcontext.cwd);
   {$endif SYSTEM_DEBUG}
   {$ifdef SYSTEM_DEBUG}
     Writeln(stderr,'FpuState = ',result);
@@ -55,10 +57,8 @@ procedure SignalToRunerror(sig : longint; SigInfo: PSigInfo; SigContext: PSigCon
                 res:=206
               else if (FpuState and FPU_Denormal)<>0 then
                 res:=216
-              else if (FpuState and (FPU_StackOverflow or FPU_StackUnderflow))<>0 Then
+              else if (FpuState and (FPU_StackOverflow or FPU_StackUnderflow or FPU_Invalid))<>0 Then
                 res:=207
-              else if (FpuState and FPU_Invalid)<>0 then
-                res:=216
               else
                 res:=207;  {'Coprocessor Error'}
             end;