7c0489
commit e905212627350d54b58426214b5a54ddc852b0c9
7c0489
Author: Paul A. Clarke <pc@us.ibm.com>
7c0489
Date:   Fri Aug 2 22:47:57 2019 -0400
7c0489
7c0489
    [powerpc] SET_RESTORE_ROUND improvements
7c0489
    
7c0489
    SET_RESTORE_ROUND uses libc_feholdsetround_ppc_ctx and
7c0489
    libc_feresetround_ppc_ctx to bracket a block of code where the floating point
7c0489
    rounding mode must be set to a certain value.
7c0489
    
7c0489
    For the *prologue*, libc_feholdsetround_ppc_ctx is used and performs:
7c0489
    1. Read/save FPSCR.
7c0489
    2. Create new value for FPSCR with new rounding mode and enables cleared.
7c0489
    3. If new value is different than current value,
7c0489
       a. If transitioning from a state where some exceptions enabled,
7c0489
          enter "ignore exceptions / non-stop" mode.
7c0489
       b. Write new value to FPSCR.
7c0489
       c. Put a mark on the wall indicating the FPSCR was changed.
7c0489
    
7c0489
    (1) uses the 'mffs' instruction.  On POWER9, the lighter weight 'mffsl'
7c0489
    instruction can be used, but it doesn't return all of the bits in the FPSCR.
7c0489
    fegetenv_status uses 'mffsl' on POWER9, 'mffs' otherwise, and can thus be
7c0489
    used instead of fegetenv_register.
7c0489
    (3b) uses 'mtfsf 0b11111111' to write the entire FPSCR, so it must
7c0489
    instead use 'mtfsf 0b00000011' to write just the enables and the mode,
7c0489
    because some of the rest of the bits are not valid if 'mffsl' was used.
7c0489
    fesetenv_mode uses 'mtfsf 0b00000011' on POWER9, 'mtfsf 0b11111111'
7c0489
    otherwise.
7c0489
    
7c0489
    For the *epilogue*, libc_feresetround_ppc_ctx checks the mark on the wall, then
7c0489
    calls libc_feresetround_ppc, which just calls __libc_femergeenv_ppc with
7c0489
    parameters such that it performs:
7c0489
    1. Retreive saved value of FPSCR, saved in prologue above.
7c0489
    2. Read FPSCR.
7c0489
    3. Create new value of FPSCR where:
7c0489
       - Summary bits and exception indicators = current OR saved.
7c0489
       - Rounding mode and enables = saved.
7c0489
       - Status bits = current.
7c0489
    4. If transitioning from some exceptions enabled to none,
7c0489
       enter "ignore exceptions / non-stop" mode.
7c0489
    5. If transitioning from no exceptions enabled to some,
7c0489
       enter "catch exceptions" mode.
7c0489
    6. Write new value to FPSCR.
7c0489
    
7c0489
    The summary bits are hardwired to the exception indicators, so there is no
7c0489
    need to restore any saved summary bits.
7c0489
    The exception indicator bits, which are sticky and remain set unless
7c0489
    explicitly cleared, would only need to be restored if the code block
7c0489
    might explicitly clear any of them.  This is certainly not expected.
7c0489
    
7c0489
    So, the only bits that need to be restored are the enables and the mode.
7c0489
    If it is the case that only those bits are to be restored, there is no need to
7c0489
    read the FPSCR.  Steps (2) and (3) are unnecessary, and step (6) only needs to
7c0489
    write the bits being restored.
7c0489
    
7c0489
    We know we are transitioning out of "ignore exceptions" mode, so step (4) is
7c0489
    unnecessary, and in step (6), we only need to check the state we are
7c0489
    entering.
7c0489
7c0489
diff --git a/sysdeps/powerpc/fpu/fenv_private.h b/sysdeps/powerpc/fpu/fenv_private.h
7c0489
index 945ab98018450092..b0149aa243e69f5a 100644
7c0489
--- a/sysdeps/powerpc/fpu/fenv_private.h
7c0489
+++ b/sysdeps/powerpc/fpu/fenv_private.h
7c0489
@@ -132,7 +132,17 @@ libc_fesetenv_ppc (const fenv_t *envp)
7c0489
 static __always_inline void
7c0489
 libc_feresetround_ppc (fenv_t *envp)
7c0489
 {
7c0489
-  __libc_femergeenv_ppc (envp, _FPU_MASK_TRAPS_RN, _FPU_MASK_FRAC_INEX_RET_CC);
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
 }
7c0489
 
7c0489
 static __always_inline int
7c0489
@@ -176,9 +186,30 @@ libc_feholdsetround_ppc_ctx (struct rm_ctx *ctx, int r)
7c0489
 {
7c0489
   fenv_union_t old, new;
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
+}
7c0489
+
7c0489
+static __always_inline void
7c0489
+libc_feholdsetround_noex_ppc_ctx (struct rm_ctx *ctx, int r)
7c0489
+{
7c0489
+  fenv_union_t old, new;
7c0489
+
7c0489
   old.fenv = fegetenv_register ();
7c0489
 
7c0489
-  new.l = (old.l & _FPU_MASK_TRAPS_RN) | r;
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
@@ -218,6 +249,9 @@ libc_feresetround_ppc_ctx (struct rm_ctx *ctx)
7c0489
 #define libc_feholdsetround_ctx          libc_feholdsetround_ppc_ctx
7c0489
 #define libc_feholdsetroundf_ctx         libc_feholdsetround_ppc_ctx
7c0489
 #define libc_feholdsetroundl_ctx         libc_feholdsetround_ppc_ctx
7c0489
+#define libc_feholdsetround_noex_ctx     libc_feholdsetround_noex_ppc_ctx
7c0489
+#define libc_feholdsetround_noexf_ctx    libc_feholdsetround_noex_ppc_ctx
7c0489
+#define libc_feholdsetround_noexl_ctx    libc_feholdsetround_noex_ppc_ctx
7c0489
 #define libc_feresetround_ctx            libc_feresetround_ppc_ctx
7c0489
 #define libc_feresetroundf_ctx           libc_feresetround_ppc_ctx
7c0489
 #define libc_feresetroundl_ctx           libc_feresetround_ppc_ctx