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