7c0489
commit f1c56cdff09f650ad721fae026eb6a3651631f3d
7c0489
Author: Paul A. Clarke <pc@us.ibm.com>
7c0489
Date:   Thu Sep 19 08:35:16 2019 -0500
7c0489
7c0489
    [powerpc] SET_RESTORE_ROUND optimizations and bug fix
7c0489
    
7c0489
    SET_RESTORE_ROUND brackets a block of code, temporarily setting and
7c0489
    restoring the rounding mode and letting everything else, including
7c0489
    exceptions generated within the block, pass through.
7c0489
    
7c0489
    On powerpc, the current code clears the exception enables, which will hide
7c0489
    exceptions generated within the block.  This issue was introduced by me
7c0489
    in commit e905212627350d54b58426214b5a54ddc852b0c9.
7c0489
    
7c0489
    Fix this by not clearing exception enable bits in the prologue.
7c0489
    
7c0489
    Also, since we are no longer changing the enable bits in either the
7c0489
    prologue or the epilogue, there is no need to test for entering/exiting
7c0489
    non-stop mode.
7c0489
    
7c0489
    Also, optimize the prologue get/save/set rounding mode operations for
7c0489
    POWER9 and later by using 'mffscrn' when possible.
7c0489
    
7c0489
    Suggested-by: Paul E. Murphy <murphyp@linux.ibm.com>
7c0489
    Reviewed-by: Paul E. Murphy <murphyp@linux.ibm.com>
7c0489
    Fixes: e905212627350d54b58426214b5a54ddc852b0c9
7c0489
    
7c0489
    2019-09-19  Paul A. Clarke  <pc@us.ibm.com>
7c0489
    
7c0489
            * sysdeps/powerpc/fpu/fenv_libc.h (fegetenv_and_set_rn): New.
7c0489
            (__fe_mffscrn): New.
7c0489
            * sysdeps/powerpc/fpu/fenv_private.h (libc_feholdsetround_ppc_ctx):
7c0489
            Do not clear enable bits, remove obsolete code, use
7c0489
            fegetenv_and_set_rn.
7c0489
            (libc_feresetround_ppc): Remove obsolete code, use
7c0489
            fegetenv_and_set_rn.
7c0489
7c0489
diff --git a/sysdeps/powerpc/fpu/fenv_libc.h b/sysdeps/powerpc/fpu/fenv_libc.h
7c0489
index e8d40ea256b6c5bc..b10b6a141ded4bfd 100644
7c0489
--- a/sysdeps/powerpc/fpu/fenv_libc.h
7c0489
+++ b/sysdeps/powerpc/fpu/fenv_libc.h
7c0489
@@ -49,6 +49,38 @@ extern const fenv_t *__fe_mask_env (void) attribute_hidden;
7c0489
     __fr;								\
7c0489
   })
7c0489
 
7c0489
+#define __fe_mffscrn(rn)						\
7c0489
+  ({register fenv_union_t __fr;						\
7c0489
+    if (__builtin_constant_p (rn))					\
7c0489
+      __asm__ __volatile__ (						\
7c0489
+        ".machine push; .machine \"power9\"; mffscrni %0,%1; .machine pop" \
7c0489
+        : "=f" (__fr.fenv) : "i" (rn));					\
7c0489
+    else								\
7c0489
+    {									\
7c0489
+      __fr.l = (rn);							\
7c0489
+      __asm__ __volatile__ (						\
7c0489
+        ".machine push; .machine \"power9\"; mffscrn %0,%1; .machine pop" \
7c0489
+        : "=f" (__fr.fenv) : "f" (__fr.fenv));				\
7c0489
+    }									\
7c0489
+    __fr.fenv;								\
7c0489
+  })
7c0489
+
7c0489
+/* Like fegetenv_status, but also sets the rounding mode.  */
7c0489
+#ifdef _ARCH_PWR9
7c0489
+#define fegetenv_and_set_rn(rn) __fe_mffscrn (rn)
7c0489
+#else
7c0489
+/* 'mffscrn' will decode to 'mffs' on ARCH < 3_00, which is still necessary
7c0489
+   but not sufficient, because it does not set the rounding mode.
7c0489
+   Explicitly set the rounding mode when 'mffscrn' actually doesn't.  */
7c0489
+#define fegetenv_and_set_rn(rn)						\
7c0489
+  ({register fenv_union_t __fr;						\
7c0489
+    __fr.fenv = __fe_mffscrn (rn);					\
7c0489
+    if (__glibc_unlikely (!(GLRO(dl_hwcap2) & PPC_FEATURE2_ARCH_3_00)))	\
7c0489
+      __fesetround_inline (rn);						\
7c0489
+    __fr.fenv;								\
7c0489
+  })
7c0489
+#endif
7c0489
+
7c0489
 /* Equivalent to fesetenv, but takes a fenv_t instead of a pointer.  */
7c0489
 #define fesetenv_register(env) \
7c0489
 	do { \
7c0489
diff --git a/sysdeps/powerpc/fpu/fenv_private.h b/sysdeps/powerpc/fpu/fenv_private.h
7c0489
index b0149aa243e69f5a..30df92c9a4700dee 100644
7c0489
--- a/sysdeps/powerpc/fpu/fenv_private.h
7c0489
+++ b/sysdeps/powerpc/fpu/fenv_private.h
7c0489
@@ -133,16 +133,7 @@ static __always_inline void
7c0489
 libc_feresetround_ppc (fenv_t *envp)
7c0489
 {
7c0489
   fenv_union_t new = { .fenv = *envp };
7c0489
-
7c0489
-  /* If the old env has no enabled exceptions and the new env has any enabled
7c0489
-     exceptions, then unmask SIGFPE in the MSR FE0/FE1 bits.  This will put the
7c0489
-     hardware into "precise mode" and may cause the FPU to run slower on some
7c0489
-     hardware.  */
7c0489
-  if ((new.l & _FPU_ALL_TRAPS) != 0)
7c0489
-    (void) __fe_nomask_env_priv ();
7c0489
-
7c0489
-  /* Atomically enable and raise (if appropriate) exceptions set in `new'.  */
7c0489
-  fesetenv_mode (new.fenv);
7c0489
+  fegetenv_and_set_rn (new.l & FPSCR_RN_MASK);
7c0489
 }
7c0489
 
7c0489
 static __always_inline int
7c0489
@@ -184,22 +175,10 @@ libc_feupdateenv_ppc (fenv_t *e)
7c0489
 static __always_inline void
7c0489
 libc_feholdsetround_ppc_ctx (struct rm_ctx *ctx, int r)
7c0489
 {
7c0489
-  fenv_union_t old, new;
7c0489
+  fenv_union_t old;
7c0489
 
7c0489
-  old.fenv = fegetenv_status ();
7c0489
-
7c0489
-  new.l = (old.l & ~(FPSCR_ENABLES_MASK|FPSCR_RN_MASK)) | r;
7c0489
-
7c0489
-  ctx->env = old.fenv;
7c0489
-  if (__glibc_unlikely (new.l != old.l))
7c0489
-    {
7c0489
-      if ((old.l & _FPU_ALL_TRAPS) != 0)
7c0489
-	(void) __fe_mask_env ();
7c0489
-      fesetenv_mode (new.fenv);
7c0489
-      ctx->updated_status = true;
7c0489
-    }
7c0489
-  else
7c0489
-    ctx->updated_status = false;
7c0489
+  ctx->env = old.fenv = fegetenv_and_set_rn (r);
7c0489
+  ctx->updated_status = (r != (old.l & FPSCR_RN_MASK));
7c0489
 }
7c0489
 
7c0489
 static __always_inline void