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