Simplify (_Float16) sqrtf((float) a) to .SQRT(a) when a is a _Float16 value.
authorliuhongt <hongtao.liu@intel.com>
Mon, 25 Oct 2021 02:51:33 +0000 (10:51 +0800)
committerliuhongt <hongtao.liu@intel.com>
Mon, 25 Oct 2021 08:53:55 +0000 (16:53 +0800)
Similar for sqrt/sqrtl.

gcc/ChangeLog:

PR target/102464
* match.pd: Simplify (_Float16) sqrtf((float) a) to .SQRT(a)
when direct_internal_fn_supported_p, similar for sqrt/sqrtl.

gcc/testsuite/ChangeLog:

PR target/102464
* gcc.target/i386/pr102464-sqrtph.c: New test.
* gcc.target/i386/pr102464-sqrtsh.c: New test.

gcc/match.pd
gcc/testsuite/gcc.target/i386/pr102464-sqrtph.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/pr102464-sqrtsh.c [new file with mode: 0644]

index 5bed2e1..43d1c1b 100644 (file)
@@ -6228,14 +6228,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
            BUILT_IN_ROUNDEVENL BUILT_IN_ROUNDEVEN BUILT_IN_ROUNDEVENF
            BUILT_IN_ROUNDL BUILT_IN_ROUND BUILT_IN_ROUNDF
            BUILT_IN_NEARBYINTL BUILT_IN_NEARBYINT BUILT_IN_NEARBYINTF
-           BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF)
+           BUILT_IN_RINTL BUILT_IN_RINT BUILT_IN_RINTF
+           BUILT_IN_SQRTL BUILT_IN_SQRT BUILT_IN_SQRTF)
      tos (IFN_TRUNC IFN_TRUNC IFN_TRUNC
          IFN_FLOOR IFN_FLOOR IFN_FLOOR
          IFN_CEIL IFN_CEIL IFN_CEIL
          IFN_ROUNDEVEN IFN_ROUNDEVEN IFN_ROUNDEVEN
          IFN_ROUND IFN_ROUND IFN_ROUND
          IFN_NEARBYINT IFN_NEARBYINT IFN_NEARBYINT
-         IFN_RINT IFN_RINT IFN_RINT)
+         IFN_RINT IFN_RINT IFN_RINT
+         IFN_SQRT IFN_SQRT IFN_SQRT)
  /* (_Float16) round ((doube) x) -> __built_in_roundf16 (x), etc.,
     if x is a _Float16.  */
  (simplify
diff --git a/gcc/testsuite/gcc.target/i386/pr102464-sqrtph.c b/gcc/testsuite/gcc.target/i386/pr102464-sqrtph.c
new file mode 100644 (file)
index 0000000..8bd19c6
--- /dev/null
@@ -0,0 +1,27 @@
+/* PR target/102464.  */
+/* { dg-do compile } */
+/* { dg-options "-O2 -mavx512fp16 -mavx512vl -ffast-math -ftree-vectorize" } */
+
+#include<math.h>
+void foo1 (_Float16* __restrict a, _Float16* b)
+{
+  for (int i = 0; i != 8; i++)
+    a[i] =  sqrtf (b[i]);
+}
+
+void foo2 (_Float16* __restrict a, _Float16* b)
+{
+  for (int i = 0; i != 8; i++)
+    a[i] =  sqrt (b[i]);
+}
+
+void foo3 (_Float16* __restrict a, _Float16* b)
+{
+  for (int i = 0; i != 8; i++)
+    a[i] =  sqrtl (b[i]);
+}
+
+/* { dg-final { scan-assembler-not "vcvtsh2s\[sd\]" } } */
+/* { dg-final { scan-assembler-not "vcvtph2p\[sd\]" } } */
+/* { dg-final { scan-assembler-not "extendhfxf" } } */
+/* { dg-final { scan-assembler-times "vsqrtph\[^\n\r\]*xmm\[0-9\]" 3 } } */
diff --git a/gcc/testsuite/gcc.target/i386/pr102464-sqrtsh.c b/gcc/testsuite/gcc.target/i386/pr102464-sqrtsh.c
new file mode 100644 (file)
index 0000000..4cf0089
--- /dev/null
@@ -0,0 +1,23 @@
+/* PR target/102464.  */
+/* { dg-do compile } */
+/* { dg-options "-O2 -mavx512fp16 -ffast-math" } */
+
+#include<math.h>
+_Float16 foo1 (_Float16 a)
+{
+  return sqrtf (a);
+}
+
+_Float16 foo2 (_Float16 a)
+{
+  return sqrt (a);
+}
+
+_Float16 foo3 (_Float16 a)
+{
+  return sqrtl (a);
+}
+
+/* { dg-final { scan-assembler-not "vcvtsh2s\[sd\]" } } */
+/* { dg-final { scan-assembler-not "extendhfxf" } } */
+/* { dg-final { scan-assembler-times "vsqrtsh\[^\n\r\]*xmm\[0-9\]" 3 } } */