]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
Rewrite feupdateenv
authorWilco <wdijkstr@arm.com>
Tue, 24 Jun 2014 13:53:04 +0000 (13:53 +0000)
committerWilco <wdijkstr@arm.com>
Tue, 24 Jun 2014 13:53:04 +0000 (13:53 +0000)
This patch rewrites feupdateenv to improve performance by avoiding
unnecessary FPSCR reads/writes. It fixes bug 16918 by passing the
correct return value.

2014-06-24  Wilco  <wdijkstr@arm.com>

[BZ #16918]
* sysdeps/arm/feupdateenv.c (feupdateenv):
Rewrite to reduce FPSCR accesses and fix return value.

ChangeLog
sysdeps/arm/feupdateenv.c

index 0b0f61affa695b672ce64f0ca92dfa88a5f0d781..3330b0b98b43fd069d921b8b681d643997e819d0 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2014-06-24  Wilco  <wdijkstr@arm.com>
+
+       [BZ #16918]
+       * sysdeps/arm/feupdateenv.c (feupdateenv):
+       Rewrite to reduce FPSCR accesses and fix return value.
+
 2014-06-24  Wilco  <wdijkstr@arm.com>
 
        * sysdeps/arm/fclrexcpt.c (feclearexcept):
index 55a15025c6da4986ecd51a7c592cb346ef38f846..d8116789d0bcada1fcf1e20a7dff5bfdc1025e14 100644 (file)
    <http://www.gnu.org/licenses/>.  */
 
 #include <fenv.h>
-#include <fpu_control.h>
 #include <arm-features.h>
 
 
 int
 feupdateenv (const fenv_t *envp)
 {
-  fpu_control_t fpscr;
+  fpu_control_t fpscr, new_fpscr, updated_fpscr;
+  int excepts;
 
   /* Fail if a VFP unit isn't present.  */
   if (!ARM_HAVE_VFP)
     return 1;
 
   _FPU_GETCW (fpscr);
+  excepts = fpscr & FE_ALL_EXCEPT;
 
-  /* Install new environment.  */
-  fesetenv (envp);
+  if ((envp != FE_DFL_ENV) && (envp != FE_NOMASK_ENV))
+    {
+      /* Merge current exception flags with the saved fenv.  */
+      new_fpscr = envp->__cw | excepts;
+
+      /* Write new FPSCR if different (ignoring NZCV flags).  */
+      if (((fpscr ^ new_fpscr) & ~_FPU_MASK_NZCV) != 0)
+       _FPU_SETCW (new_fpscr);
+
+      /* Raise the exceptions if enabled in the new FP state.  */
+      if (excepts & (new_fpscr >> FE_EXCEPT_SHIFT))
+       return feraiseexcept (excepts);
+
+      return 0;
+    }
+
+  /* Preserve the reserved FPSCR flags.  */
+  new_fpscr = fpscr & (_FPU_RESERVED | FE_ALL_EXCEPT);
+  new_fpscr |= (envp == FE_DFL_ENV) ? _FPU_DEFAULT : _FPU_IEEE;
+
+  if (((new_fpscr ^ fpscr) & ~_FPU_MASK_NZCV) != 0)
+    {
+      _FPU_SETCW (new_fpscr);
+
+      /* Not all VFP architectures support trapping exceptions, so
+        test whether the relevant bits were set and fail if not.  */
+      _FPU_GETCW (updated_fpscr);
+
+      if (new_fpscr & ~updated_fpscr)
+       return 1;
+    }
+
+  /* Raise the exceptions if enabled in the new FP state.  */
+  if (excepts & (new_fpscr >> FE_EXCEPT_SHIFT))
+    return feraiseexcept (excepts);
 
-  /* Raise the saved exceptions.  */
-  feraiseexcept (fpscr & FE_ALL_EXCEPT);
   return 0;
 }
 libm_hidden_def (feupdateenv)