public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Rafael Tsuha <rafael.tsuha@usp.br>
To: gcc-patches@gcc.gnu.org
Cc: mateus.carmo.barbosa@usp.br, giuliano.belinassi@usp.br
Subject: [PATCH] Simplify sinh (x) / cosh (x)
Date: Tue, 22 Jan 2019 19:31:00 -0000	[thread overview]
Message-ID: <CAHMCd+VFfSGKHOqQwWK7k9+ycA-p8vJU5bLQ2LuRxJ7b+vZX0A@mail.gmail.com> (raw)

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

This patch simplifies the expression sinh (x) / cosh (x) to tanh (x).
This rule is mathematically valid.

There's a slight difference in the result when applying this
optimization with x in the interval 0 < x <= 1e-4951. With the
optimization, the result using long double is -0 and without the
optimization, the result is +0.

When running the testsuite, the test gfortran.dg/pr79966.f90 failed,
but it seems unrelated to this patch

My architecture is x86_64.

gcc/ChangeLog
2019-01-22  Rafael Tsuha  <rafael.tsuha@usp.br>

        * match.pd (sinh (x) / cosh (x)): New simplification rule.

gcc/testsuite/ChangeLog
2019-01-22  Rafael Tsuha  <rafael.tsuha@usp.br>

        * gcc.dg/sinhovercosh-1.c: New test.

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

Index: gcc/match.pd
===================================================================
--- gcc/match.pd	(revision 267671)
+++ gcc/match.pd	(working copy)
@@ -4484,6 +4484,11 @@
   (rdiv (SIN:s @0) (COS:s @0))
    (TAN @0))
 
+ /* Simplify sinh(x) / cosh(x) -> tanh(x). */
+ (simplify
+  (rdiv (SINH:s @0) (COSH:s @0))
+   (TANH @0))
+
  /* Simplify cos(x) / sin(x) -> 1 / tan(x). */
  (simplify
   (rdiv (COS:s @0) (SIN:s @0))
Index: gcc/testsuite/gcc.dg/sinhovercosh-1.c
===================================================================
--- gcc/testsuite/gcc.dg/sinhovercosh-1.c	(nonexistent)
+++ gcc/testsuite/gcc.dg/sinhovercosh-1.c	(working copy)
@@ -0,0 +1,45 @@
+/* { dg-do compile } */
+/* { dg-options "-Ofast -fdump-tree-optimized" } */
+
+extern float sinhf (float);
+extern float coshf (float);
+extern float tanhf (float);
+extern float sqrtf (float);
+extern double sinh (double);
+extern double cosh (double);
+extern double sqrt (double);
+extern double tanh (double);
+extern long double sinhl (long double);
+extern long double coshl (long double);
+extern long double tanhl (long double);
+extern long double sqrtl (long double);
+
+double __attribute__ ((noinline))
+sinhovercosh_ (double x)
+{
+  return sinh (x) / cosh (x);
+}
+
+float __attribute__ ((noinline))
+sinhfovercoshf_(float x)
+{
+  return sinhf (x) / coshf (x);
+}
+
+long double __attribute__ ((noinline))
+sinhlovercoshl_ (long double x)
+{
+  return sinhl (x) / coshl (x);
+}
+
+/* There must be no calls to sinh, cosh, or atanh */
+/* {dg-final { scan-tree-dump-not "sinh " "optimized" } } */
+/* {dg-final { scan-tree-dump-not "cosh " "optimized" } } */
+/* {dg-final { scan-tree-dump-not "sinfh " "optimized" } } */
+/* {dg-final { scan-tree-dump-not "cosfh " "optimized" } } */
+/* {dg-final { scan-tree-dump-not "sinlh " "optimized" } } */
+/* {dg-final { scan-tree-dump-not "coslh " "optimized" } } */
+/* {dg-final { scan-tree-dump-times "tanh " "1" "optimized" }} */
+/* {dg-final { scan-tree-dump-times "tanhl " "1" "optimized" }} */
+/* {dg-final { scan-tree-dump-times "tanhf " "1" "optimized" }} */
+

             reply	other threads:[~2019-01-22 19:31 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-22 19:31 Rafael Tsuha [this message]
2019-04-29 21:23 ` Jeff Law
2019-09-04 18:16   ` Rafael Tsuha
2019-09-09 18:44     ` Jeff Law
2019-09-10  7:37       ` Uros Bizjak
2019-09-10 15:23         ` Jeff Law
2019-10-02 18:08           ` Rafael Tsuha
2019-10-04 18:31             ` Jeff Law

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAHMCd+VFfSGKHOqQwWK7k9+ycA-p8vJU5bLQ2LuRxJ7b+vZX0A@mail.gmail.com \
    --to=rafael.tsuha@usp.br \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=giuliano.belinassi@usp.br \
    --cc=mateus.carmo.barbosa@usp.br \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).