588e70
commit 56c81132ccc6f468fa4fc29c536db060e18e9d87
588e70
Author: Raphael Moreira Zinsly <rzinsly@linux.ibm.com>
588e70
Date:   Tue Feb 23 14:14:37 2021 -0300
588e70
588e70
    powerpc: Add optimized ilogb* for POWER9
588e70
    
588e70
    The instructions xsxexpdp and xsxexpqp introduced on POWER9 extract
588e70
    the exponent from a double-precision and quad-precision floating-point
588e70
    respectively, thus they can be used to improve ilogb, ilogbf and ilogbf128.
588e70
588e70
diff --git a/sysdeps/powerpc/fpu/math_private.h b/sysdeps/powerpc/fpu/math_private.h
588e70
index e642d6c8237578ea..5bbc468829062a48 100644
588e70
--- a/sysdeps/powerpc/fpu/math_private.h
588e70
+++ b/sysdeps/powerpc/fpu/math_private.h
588e70
@@ -26,7 +26,28 @@
588e70
 
588e70
 #include_next <math_private.h>
588e70
 
588e70
-#if defined _ARCH_PWR9 && __HAVE_DISTINCT_FLOAT128
588e70
+#ifdef _ARCH_PWR9
588e70
+
588e70
+#if __GNUC_PREREQ (8, 0)
588e70
+# define _GL_HAS_BUILTIN_ILOGB 1
588e70
+#elif defined __has_builtin
588e70
+# define _GL_HAS_BUILTIN_ILOGB __has_builtin (__builtin_vsx_scalar_extract_exp)
588e70
+#else
588e70
+# define _GL_HAS_BUILTIN_ILOGB 0
588e70
+#endif
588e70
+
588e70
+#define __builtin_test_dc_ilogbf __builtin_test_dc_ilogb
588e70
+#define __builtin_ilogbf __builtin_ilogb
588e70
+
588e70
+#define __builtin_test_dc_ilogb(x, y) \
588e70
+        __builtin_vsx_scalar_test_data_class_dp(x, y)
588e70
+#define __builtin_ilogb(x) __builtin_vsx_scalar_extract_exp(x) - 0x3ff
588e70
+
588e70
+#define __builtin_test_dc_ilogbf128(x, y) \
588e70
+        __builtin_vsx_scalar_test_data_class_qp(x, y)
588e70
+#define __builtin_ilogbf128(x) __builtin_vsx_scalar_extract_expq(x) - 0x3fff
588e70
+
588e70
+#if __HAVE_DISTINCT_FLOAT128
588e70
 extern __always_inline _Float128
588e70
 __ieee754_sqrtf128 (_Float128 __x)
588e70
 {
588e70
@@ -35,6 +56,9 @@ __ieee754_sqrtf128 (_Float128 __x)
588e70
   return __z;
588e70
 }
588e70
 #endif
588e70
+#else /* !_ARCH_PWR9 */
588e70
+#define _GL_HAS_BUILTIN_ILOGB 0
588e70
+#endif
588e70
 
588e70
 #if defined _ARCH_PWR5X
588e70
 
588e70
diff --git a/sysdeps/powerpc/powerpc64/le/fpu/w_ilogb_template.c b/sysdeps/powerpc/powerpc64/le/fpu/w_ilogb_template.c
588e70
new file mode 100644
588e70
index 0000000000000000..b5c1c0aa9db86f3d
588e70
--- /dev/null
588e70
+++ b/sysdeps/powerpc/powerpc64/le/fpu/w_ilogb_template.c
588e70
@@ -0,0 +1,30 @@
588e70
+#include <math.h>
588e70
+#include <errno.h>
588e70
+#include <limits.h>
588e70
+#include <math_private.h>
588e70
+#include <fenv.h>
588e70
+
588e70
+#if _GL_HAS_BUILTIN_ILOGB
588e70
+int
588e70
+M_DECL_FUNC (__ilogb) (FLOAT x)
588e70
+{
588e70
+  int r;
588e70
+  /* Check for exceptional cases.  */
588e70
+  if (! M_SUF(__builtin_test_dc_ilogb) (x, 0x7f))
588e70
+    r = M_SUF (__builtin_ilogb) (x);
588e70
+  else
588e70
+    /* Fallback to the generic ilogb if x is NaN, Inf or subnormal.  */
588e70
+    r = M_SUF (__ieee754_ilogb) (x);
588e70
+  if (__builtin_expect (r == FP_ILOGB0, 0)
588e70
+      || __builtin_expect (r == FP_ILOGBNAN, 0)
588e70
+      || __builtin_expect (r == INT_MAX, 0))
588e70
+    {
588e70
+      __set_errno (EDOM);
588e70
+      __feraiseexcept (FE_INVALID);
588e70
+    }
588e70
+  return r;
588e70
+}
588e70
+declare_mgen_alias (__ilogb, ilogb)
588e70
+#else
588e70
+#include <math/w_ilogb_template.c>
588e70
+#endif
588e70
diff --git a/sysdeps/powerpc/powerpc64/le/fpu/w_ilogbl.c b/sysdeps/powerpc/powerpc64/le/fpu/w_ilogbl.c
588e70
new file mode 100644
588e70
index 0000000000000000..205f154f0089a269
588e70
--- /dev/null
588e70
+++ b/sysdeps/powerpc/powerpc64/le/fpu/w_ilogbl.c
588e70
@@ -0,0 +1,4 @@
588e70
+/* Skip the optimization for long double as ibm128 does not provide an
588e70
+   optimized builtin. */
588e70
+#include <math-type-macros-ldouble.h>
588e70
+#include <math/w_ilogb_template.c>