public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: [PATCH] Add sinh(tanh(x)) and cosh(tanh(x)) rules
@ 2018-10-18 23:22 Wilco Dijkstra
  2018-10-19 13:39 ` Giuliano Augusto Faulin Belinassi
  0 siblings, 1 reply; 37+ messages in thread
From: Wilco Dijkstra @ 2018-10-18 23:22 UTC (permalink / raw)
  To: GCC Patches, giuliano.belinassi; +Cc: nd, Jeff Law

Hi,

> Well, I compared the results before and after the simplifications with a 512-bit
> precise mpfr value. Unfortunately, I found that sometimes the error is very
> noticeable :-( .

Did you enable FMA? I'd expect 1 - x*x to be accurate with FMA, so the relative error
should be much better. If there is no FMA, 2*(1-fabs(x)) - (1-fabs(x))^2 should be
more accurate when abs(x)>0.5 and still much faster.

Wilco

    

^ permalink raw reply	[flat|nested] 37+ messages in thread
* [PATCH] Add sinh(tanh(x)) and cosh(tanh(x)) rules
@ 2018-08-07 20:00 Giuliano Augusto Faulin Belinassi
  2018-08-07 20:29 ` Paul Koning
  2018-10-12  4:55 ` Jeff Law
  0 siblings, 2 replies; 37+ messages in thread
From: Giuliano Augusto Faulin Belinassi @ 2018-08-07 20:00 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 616 bytes --]

Related with bug 86829, but for hyperbolic trigonometric functions.
This patch adds substitution rules to both sinh(tanh(x)) -> x / sqrt(1
- x*x) and cosh(tanh(x)) -> 1 / sqrt(1 - x*x). Notice that the both
formulas has division by 0, but it causes no harm because 1/(+0) ->
+infinity, thus the math is still safe.

Changelog:
2018-08-07  Giuliano Belinassi <giuliano.belinassi@usp.br>

    * match.pd: add simplification rules to sinh(atanh(x)) and cosh(atanh(x)).

All tests added by this patch runs without errors in trunk, however,
there are tests unrelated with this patch that fails in my x86_64
Ubuntu 18.04.

[-- Attachment #2: sinhatanh.patch --]
[-- Type: text/x-patch, Size: 3522 bytes --]

Index: gcc/match.pd
===================================================================
--- gcc/match.pd	(revisão 263359)
+++ gcc/match.pd	(cópia de trabalho)
@@ -4219,6 +4219,25 @@
   (mult:c (TAN:s @0) (COS:s @0))
    (SIN @0))
 
+ /* Simplify sinh(atanh(x)) -> x / sqrt(1 - x*x). */
+ (for sins (SINH)
+      atans (ATANH)
+      sqrts (SQRT)
+  (simplify
+   (sins (atans:s @0))
+   (rdiv @0 (sqrts (minus {build_one_cst (type);} 
+       (mult @0 @0))))))
+ 
+ /* Simplify cosh(atanh(x)) -> 1 / sqrt(1 - x*x). */
+ (for coss (COSH)
+      atans (ATANH)
+      sqrts (SQRT)
+  (simplify
+   (coss (atans:s @0))
+   (rdiv {build_one_cst (type);} 
+       (sqrts (minus {build_one_cst (type);} 
+        (mult @0 @0))))))
+
  /* Simplify x * pow(x,c) -> pow(x,c+1). */
  (simplify
   (mult:c @0 (POW:s @0 REAL_CST@1))
Index: gcc/testsuite/gcc.dg/sinhtanh-1.c
===================================================================
--- gcc/testsuite/gcc.dg/sinhtanh-1.c	(nonexistent)
+++ gcc/testsuite/gcc.dg/sinhtanh-1.c	(cópia de trabalho)
@@ -0,0 +1,15 @@
+/* { dg-do compile } */
+/* { dg-options "-O3 -ffast-math -fdump-tree-optimized" } */
+
+extern double sinh(double x);
+extern double atanh(double x);
+
+double __attribute__ ((noinline)) 
+sinhatanh_(double x)
+{
+    return sinh(atanh(x));
+}
+
+/* There should be no calls to sinh nor atanh */
+/* { dg-final { scan-tree-dump-not "sinh " "optimized" } } */
+/* { dg-final { scan-tree-dump-not "atanh " "optimized" } } */
Index: gcc/testsuite/gcc.dg/sinhtanh-2.c
===================================================================
--- gcc/testsuite/gcc.dg/sinhtanh-2.c	(nonexistent)
+++ gcc/testsuite/gcc.dg/sinhtanh-2.c	(cópia de trabalho)
@@ -0,0 +1,15 @@
+/* { dg-do compile } */
+/* { dg-options "-O3 -ffast-math -fdump-tree-optimized" } */
+
+extern double cosh(double x);
+extern double atanh(double x);
+
+double __attribute__ ((noinline)) 
+coshatanh_(double x)
+{
+    return cosh(atanh(x));
+}
+
+/* There should be no calls to cosh nor atanh */
+/* { dg-final { scan-tree-dump-not "cosh " "optimized" } } */
+/* { dg-final { scan-tree-dump-not "atanh " "optimized" } } */
Index: gcc/testsuite/gcc.dg/sinhtanh-3.c
===================================================================
--- gcc/testsuite/gcc.dg/sinhtanh-3.c	(nonexistent)
+++ gcc/testsuite/gcc.dg/sinhtanh-3.c	(cópia de trabalho)
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+/* { dg-options "-O3 -ffast-math -fdump-tree-optimized" } */
+
+extern double sinh(double x);
+extern double atanh(double x);
+
+double __attribute__ ((noinline)) 
+sinhatanh_(double x)
+{
+    double atgh = atanh(x);
+    return sinh(atgh) + atgh;
+}
+
+/* There should be calls to both sinh and atanh */
+/* { dg-final { scan-tree-dump "sinh " "optimized" } } */
+/* { dg-final { scan-tree-dump "atanh " "optimized" } } */
Index: gcc/testsuite/gcc.dg/sinhtanh-4.c
===================================================================
--- gcc/testsuite/gcc.dg/sinhtanh-4.c	(nonexistent)
+++ gcc/testsuite/gcc.dg/sinhtanh-4.c	(cópia de trabalho)
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+/* { dg-options "-O3 -ffast-math -fdump-tree-optimized" } */
+
+extern double cosh(double x);
+extern double atanh(double x);
+
+double __attribute__ ((noinline)) 
+coshatanh_(double x)
+{
+    double atgh = atanh(x);
+    return cosh(atgh) + atgh;
+}
+
+/* There should be calls to both cosh and atanh */
+/* { dg-final { scan-tree-dump "cosh " "optimized" } } */
+/* { dg-final { scan-tree-dump "atanh " "optimized" } } */

^ permalink raw reply	[flat|nested] 37+ messages in thread

end of thread, other threads:[~2018-12-02 14:37 UTC | newest]

Thread overview: 37+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-10-18 23:22 [PATCH] Add sinh(tanh(x)) and cosh(tanh(x)) rules Wilco Dijkstra
2018-10-19 13:39 ` Giuliano Augusto Faulin Belinassi
2018-10-19 13:46   ` Wilco Dijkstra
2018-10-19 14:49     ` Jakub Jelinek
2018-10-19 15:35       ` Wilco Dijkstra
2018-10-19 14:03   ` Segher Boessenkool
2018-10-19 15:10     ` Giuliano Augusto Faulin Belinassi
2018-10-19 17:28       ` Wilco Dijkstra
2018-10-20 19:06         ` Giuliano Augusto Faulin Belinassi
2018-10-22 20:58           ` Jeff Law
2018-10-23  9:52             ` Richard Biener
2018-10-23 11:31               ` Wilco Dijkstra
2018-10-23 11:35                 ` Jakub Jelinek
2018-10-23 12:02                   ` Wilco Dijkstra
2018-11-07 22:21               ` Jeff Law
2018-11-07 22:34                 ` Wilco Dijkstra
2018-11-08  1:44                   ` Segher Boessenkool
2018-11-08 13:33                     ` Wilco Dijkstra
2018-11-09 20:04                       ` Jeff Law
2018-11-09 22:04                         ` Giuliano Augusto Faulin Belinassi
2018-11-10  5:36                         ` Segher Boessenkool
2018-11-12 14:17                           ` Richard Biener
2018-12-02 14:37                       ` Segher Boessenkool
  -- strict thread matches above, loose matches on Subject: below --
2018-08-07 20:00 Giuliano Augusto Faulin Belinassi
2018-08-07 20:29 ` Paul Koning
2018-08-07 20:42   ` Giuliano Augusto Faulin Belinassi
2018-08-08 13:59     ` Paul Koning
2018-08-08 18:57       ` Giuliano Augusto Faulin Belinassi
2018-08-08 19:55         ` Paul Koning
2018-10-12  4:55 ` Jeff Law
2018-10-12 14:55   ` Giuliano Augusto Faulin Belinassi
2018-10-16 21:16     ` Jeff Law
2018-10-17 21:43       ` Giuliano Augusto Faulin Belinassi
2018-10-17 22:45         ` Jeff Law
2018-10-18  5:38           ` Giuliano Augusto Faulin Belinassi
2018-10-18 17:10             ` Jeff Law
2018-10-18 22:46               ` Giuliano Belinassi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).