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