7c0489
commit 3db85a9814784a74536a1f0e7b7ddbfef7dc84bb
7c0489
Author: Paul A. Clarke <pc@us.ibm.com>
7c0489
Date:   Thu Jun 20 11:57:18 2019 -0500
7c0489
7c0489
    powerpc: Use faster means to access FPSCR when possible in some cases
7c0489
    
7c0489
    Using 'mffs' instruction to read the Floating Point Status Control Register
7c0489
    (FPSCR) can force a processor flush in some cases, with undesirable
7c0489
    performance impact.  If the values of the bits in the FPSCR which force the
7c0489
    flush are not needed, an instruction that is new to POWER9 (ISA version 3.0),
7c0489
    'mffsl' can be used instead.
7c0489
    
7c0489
    Cases included:  get_rounding_mode, fegetround, fegetmode, fegetexcept.
7c0489
    
7c0489
            * sysdeps/powerpc/bits/fenvinline.h (__fegetround): Use
7c0489
            __fegetround_ISA300() or __fegetround_ISA2() as appropriate.
7c0489
            (__fegetround_ISA300) New.
7c0489
            (__fegetround_ISA2) New.
7c0489
            * sysdeps/powerpc/fpu_control.h (IS_ISA300): New.
7c0489
            (_FPU_MFFS): Move implementation...
7c0489
            (_FPU_GETCW): Here.
7c0489
            (_FPU_MFFSL): Move implementation....
7c0489
            (_FPU_GET_RC_ISA300): Here. New.
7c0489
            (_FPU_GET_RC): Use _FPU_GET_RC_ISA300() or _FPU_GETCW() as appropriate.
7c0489
            * sysdeps/powerpc/fpu/fenv_libc.h (fegetenv_status_ISA300): New.
7c0489
            (fegetenv_status): New.
7c0489
            * sysdeps/powerpc/fpu/fegetmode.c (fegetmode): Use fegetenv_status()
7c0489
            instead of fegetenv_register().
7c0489
            * sysdeps/powerpc/fpu/fegetexcept.c (__fegetexcept): Likewise.
7c0489
    
7c0489
    Reviewed-by: Tulio Magno Quites Machado Filho <tuliom@linux.ibm.com>
7c0489
7c0489
diff --git a/sysdeps/powerpc/bits/fenvinline.h b/sysdeps/powerpc/bits/fenvinline.h
7c0489
index 41316386ba75e903..caec8ead6e17219d 100644
7c0489
--- a/sysdeps/powerpc/bits/fenvinline.h
7c0489
+++ b/sysdeps/powerpc/bits/fenvinline.h
7c0489
@@ -18,13 +18,36 @@
7c0489
 
7c0489
 #if defined __GNUC__ && !defined _SOFT_FLOAT && !defined __NO_FPRS__
7c0489
 
7c0489
-/* Inline definition for fegetround.  */
7c0489
-# define __fegetround() \
7c0489
-  (__extension__  ({ int __fegetround_result;				      \
7c0489
-		     __asm__ __volatile__				      \
7c0489
-		       ("mcrfs 7,7 ; mfcr %0"				      \
7c0489
-			: "=r"(__fegetround_result) : : "cr7");		      \
7c0489
-		     __fegetround_result & 3; }))
7c0489
+/* Inline definitions for fegetround.  */
7c0489
+# define __fegetround_ISA300()						\
7c0489
+  (__extension__  ({							\
7c0489
+    union { double __d; unsigned long long __ll; } __u;			\
7c0489
+    __asm__ __volatile__ (						\
7c0489
+      ".machine push; .machine \"power9\"; mffsl %0; .machine pop"	\
7c0489
+      : "=f" (__u.__d));						\
7c0489
+    __u.__ll & 0x0000000000000003LL;					\
7c0489
+  }))
7c0489
+
7c0489
+# define __fegetround_ISA2()						\
7c0489
+  (__extension__  ({							\
7c0489
+     int __fegetround_result;						\
7c0489
+     __asm__ __volatile__ ("mcrfs 7,7 ; mfcr %0"			\
7c0489
+			   : "=r"(__fegetround_result) : : "cr7");	\
7c0489
+     __fegetround_result & 3;						\
7c0489
+  }))
7c0489
+
7c0489
+# ifdef _ARCH_PWR9
7c0489
+#  define __fegetround() __fegetround_ISA300()
7c0489
+# elif defined __BUILTIN_CPU_SUPPORTS__
7c0489
+#  define __fegetround()						\
7c0489
+  (__glibc_likely (__builtin_cpu_supports ("arch_3_00"))		\
7c0489
+   ? __fegetround_ISA300()						\
7c0489
+   : __fegetround_ISA2()						\
7c0489
+  )
7c0489
+# else
7c0489
+#  define __fegetround() __fegetround_ISA2()
7c0489
+# endif
7c0489
+
7c0489
 # define fegetround() __fegetround ()
7c0489
 
7c0489
 # ifndef __NO_MATH_INLINES
7c0489
diff --git a/sysdeps/powerpc/fpu/fegetexcept.c b/sysdeps/powerpc/fpu/fegetexcept.c
7c0489
index a053a32bfe11c0d4..9d77adea59939ece 100644
7c0489
--- a/sysdeps/powerpc/fpu/fegetexcept.c
7c0489
+++ b/sysdeps/powerpc/fpu/fegetexcept.c
7c0489
@@ -25,7 +25,7 @@ __fegetexcept (void)
7c0489
   fenv_union_t fe;
7c0489
   int result = 0;
7c0489
 
7c0489
-  fe.fenv = fegetenv_register ();
7c0489
+  fe.fenv = fegetenv_status ();
7c0489
 
7c0489
   if (fe.l & (1 << (31 - FPSCR_XE)))
7c0489
       result |= FE_INEXACT;
7c0489
diff --git a/sysdeps/powerpc/fpu/fegetmode.c b/sysdeps/powerpc/fpu/fegetmode.c
7c0489
index b83dc9f625d2248a..75493e5f24c8b05b 100644
7c0489
--- a/sysdeps/powerpc/fpu/fegetmode.c
7c0489
+++ b/sysdeps/powerpc/fpu/fegetmode.c
7c0489
@@ -21,6 +21,6 @@
7c0489
 int
7c0489
 fegetmode (femode_t *modep)
7c0489
 {
7c0489
-  *modep = fegetenv_register ();
7c0489
+  *modep = fegetenv_status ();
7c0489
   return 0;
7c0489
 }
7c0489
diff --git a/sysdeps/powerpc/fpu/fenv_libc.h b/sysdeps/powerpc/fpu/fenv_libc.h
7c0489
index d6945903b525748e..cc00df033da47c1a 100644
7c0489
--- a/sysdeps/powerpc/fpu/fenv_libc.h
7c0489
+++ b/sysdeps/powerpc/fpu/fenv_libc.h
7c0489
@@ -35,6 +35,27 @@ extern const fenv_t *__fe_mask_env (void) attribute_hidden;
7c0489
 #define fegetenv_register() \
7c0489
         ({ fenv_t env; asm volatile ("mffs %0" : "=f" (env)); env; })
7c0489
 
7c0489
+/* Equivalent to fegetenv_register, but only returns bits for
7c0489
+   status, exception enables, and mode.  */
7c0489
+
7c0489
+#define fegetenv_status_ISA300()					\
7c0489
+  ({register double __fr;						\
7c0489
+    __asm__ __volatile__ (						\
7c0489
+      ".machine push; .machine \"power9\"; mffsl %0; .machine pop"	\
7c0489
+      : "=f" (__fr));							\
7c0489
+    __fr;								\
7c0489
+  })
7c0489
+
7c0489
+#ifdef _ARCH_PWR9
7c0489
+# define fegetenv_status() fegetenv_status_ISA300()
7c0489
+#else
7c0489
+# define fegetenv_status()						\
7c0489
+  (__glibc_likely (__builtin_cpu_supports ("arch_3_00"))		\
7c0489
+   ? fegetenv_status_ISA300()						\
7c0489
+   : fegetenv_register()						\
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_control.h b/sysdeps/powerpc/fpu_control.h
7c0489
index 90063d77bbbf794f..e0ee622e246c0d61 100644
7c0489
--- a/sysdeps/powerpc/fpu_control.h
7c0489
+++ b/sysdeps/powerpc/fpu_control.h
7c0489
@@ -96,35 +96,37 @@ extern fpu_control_t __fpu_control;
7c0489
 typedef unsigned int fpu_control_t;
7c0489
 
7c0489
 /* Macros for accessing the hardware control word.  */
7c0489
-# define __FPU_MFFS()						\
7c0489
-  ({register double __fr;					\
7c0489
-    __asm__ __volatile__("mffs %0" : "=f" (__fr));		\
7c0489
-    __fr;							\
7c0489
-  })
7c0489
-
7c0489
 # define _FPU_GETCW(cw)						\
7c0489
   ({union { double __d; unsigned long long __ll; } __u;		\
7c0489
-    __u.__d = __FPU_MFFS();					\
7c0489
+    __asm__ __volatile__("mffs %0" : "=f" (__u.__d));		\
7c0489
     (cw) = (fpu_control_t) __u.__ll;				\
7c0489
     (fpu_control_t) __u.__ll;					\
7c0489
   })
7c0489
 
7c0489
-#ifdef _ARCH_PWR9
7c0489
-# define __FPU_MFFSL()						\
7c0489
-  ({register double __fr;					\
7c0489
-    __asm__ __volatile__("mffsl %0" : "=f" (__fr));		\
7c0489
-    __fr;							\
7c0489
+# define _FPU_GET_RC_ISA300()						\
7c0489
+  ({union { double __d; unsigned long long __ll; } __u;			\
7c0489
+    __asm__ __volatile__(						\
7c0489
+      ".machine push; .machine \"power9\"; mffsl %0; .machine pop" 	\
7c0489
+      : "=f" (__u.__d));						\
7c0489
+    (fpu_control_t) (__u.__ll & _FPU_MASK_RC);				\
7c0489
   })
7c0489
-#else
7c0489
-# define __FPU_MFFSL() __FPU_MFFS()
7c0489
-#endif
7c0489
-    
7c0489
-# define _FPU_GET_RC()						\
7c0489
-  ({union { double __d; unsigned long long __ll; } __u;		\
7c0489
-    __u.__d = __FPU_MFFSL();					\
7c0489
-    __u.__ll &= _FPU_MASK_RC;					\
7c0489
-    (fpu_control_t) __u.__ll;					\
7c0489
+
7c0489
+# ifdef _ARCH_PWR9
7c0489
+#  define _FPU_GET_RC() _FPU_GET_RC_ISA300()
7c0489
+# elif defined __BUILTIN_CPU_SUPPORTS__
7c0489
+#  define _FPU_GET_RC()							\
7c0489
+  ({fpu_control_t __rc;							\
7c0489
+    __rc = __glibc_likely (__builtin_cpu_supports ("arch_3_00"))	\
7c0489
+      ? _FPU_GET_RC_ISA300 ()						\
7c0489
+      : _FPU_GETCW (__rc) & _FPU_MASK_RC;				\
7c0489
+    __rc;								\
7c0489
+  })
7c0489
+# else
7c0489
+#  define _FPU_GET_RC()						\
7c0489
+  ({fpu_control_t __rc = _FPU_GETCW (__rc) & _FPU_MASK_RC;	\
7c0489
+    __rc;							\
7c0489
   })
7c0489
+# endif
7c0489
 
7c0489
 # define _FPU_SETCW(cw)						\
7c0489
   { union { double __d; unsigned long long __ll; } __u;		\