bca718
commit c95b3011018893fcc473279768a67a24a73bbef2
bca718
Author: Wilco <wdijkstr@arm.com>
bca718
Date:   Mon Jun 2 12:20:17 2014 +0100
bca718
bca718
    [AArch64] Rewrite feupdateenv (BZ 17009).
bca718
bca718
diff --git a/ports/sysdeps/aarch64/fpu/feupdateenv.c b/ports/sysdeps/aarch64/fpu/feupdateenv.c
bca718
index 6d64a9b..ac2f6fe 100644
bca718
--- a/ports/sysdeps/aarch64/fpu/feupdateenv.c
bca718
+++ b/ports/sysdeps/aarch64/fpu/feupdateenv.c
bca718
@@ -22,16 +22,65 @@
bca718
 int
bca718
 feupdateenv (const fenv_t *envp)
bca718
 {
bca718
+  fpu_control_t fpcr;
bca718
+  fpu_control_t fpcr_new;
bca718
+  fpu_control_t updated_fpcr;
bca718
   fpu_fpsr_t fpsr;
bca718
+  fpu_fpsr_t fpsr_new;
bca718
+  int excepts;
bca718
 
bca718
-  /* Get the current exception state.  */
bca718
+  _FPU_GETCW (fpcr);
bca718
   _FPU_GETFPSR (fpsr);
bca718
+  excepts = fpsr & FE_ALL_EXCEPT;
bca718
 
bca718
-  /* Install new environment.  */
bca718
-  fesetenv (envp);
bca718
+  if ((envp != FE_DFL_ENV) && (envp != FE_NOMASK_ENV))
bca718
+    {
bca718
+      fpcr_new = envp->__fpcr;
bca718
+      fpsr_new = envp->__fpsr | excepts;
bca718
 
bca718
-  /* Raise the saved exceptions.  */
bca718
-  feraiseexcept (fpsr & FE_ALL_EXCEPT);
bca718
+      if (fpcr != fpcr_new)
bca718
+        _FPU_SETCW (fpcr_new);
bca718
+
bca718
+      if (fpsr != fpsr_new)
bca718
+        _FPU_SETFPSR (fpsr_new);
bca718
+
bca718
+      if (excepts & (fpcr_new >> FE_EXCEPT_SHIFT))
bca718
+        return feraiseexcept (excepts);
bca718
+
bca718
+      return 0;
bca718
+    }
bca718
+
bca718
+  fpcr_new = fpcr & _FPU_RESERVED;
bca718
+  fpsr_new = fpsr & (_FPU_FPSR_RESERVED | FE_ALL_EXCEPT);
bca718
+
bca718
+  if (envp == FE_DFL_ENV)
bca718
+    {
bca718
+      fpcr_new |= _FPU_DEFAULT;
bca718
+      fpsr_new |= _FPU_FPSR_DEFAULT;
bca718
+    }
bca718
+  else
bca718
+    {
bca718
+      fpcr_new |= _FPU_FPCR_IEEE;
bca718
+      fpsr_new |= _FPU_FPSR_IEEE;
bca718
+    }
bca718
+
bca718
+  _FPU_SETFPSR (fpsr_new);
bca718
+
bca718
+  if (fpcr != fpcr_new)
bca718
+    {
bca718
+      _FPU_SETCW (fpcr_new);
bca718
+
bca718
+      /* Trapping exceptions are optional in AArch64; the relevant enable
bca718
+	 bits in FPCR are RES0 hence the absence of support can be detected
bca718
+	 by reading back the FPCR and comparing with the required value.  */
bca718
+      _FPU_GETCW (updated_fpcr);
bca718
+
bca718
+      if (fpcr_new & ~updated_fpcr)
bca718
+        return 1;
bca718
+    }
bca718
+
bca718
+  if (excepts & (fpcr_new >> FE_EXCEPT_SHIFT))
bca718
+    return feraiseexcept (excepts);
bca718
 
bca718
   return 0;
bca718
 }