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