From patchwork Tue Nov 27 19:38:04 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Giuliano Belinassi X-Patchwork-Id: 1003988 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-491025-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=usp.br Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="ADQykfyq"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=usp-br.20150623.gappssmtp.com header.i=@usp-br.20150623.gappssmtp.com header.b="Y2RLDxl5"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 434Dbt6JsTz9s1c for ; Wed, 28 Nov 2018 06:38:33 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:from:date:message-id:subject:to:content-type; q= dns; s=default; b=U33J4kZtEQiQwkEQt5YSUlbkmKaysxw7TSROFBukGhwxpv n6Bxwe+EMx+IB0peOWYAA6zDznfw+7QyFsYo6Kf/ouwhzHg2Iwepr0xTT1NLyMHr D+UJekOjUuGgkkCKMYV2/ZXFZvgiEpp45Kz9gfR/XlASjiFPUAhqetRTlhccM= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:from:date:message-id:subject:to:content-type; s= default; bh=1u/VlN7edO0YP1zg8oua7OgVnoU=; b=ADQykfyqKDm6leVsqHRT rYuxE4D3iBwCDPozTXsP/OgZ8+r3mLbib2sErPpyl5B+6wmDR3aPMI02BWxBBkM2 B2FFsDnDfD5e0ryrIJ2NKMmeLWFr5usv8c5ij7zUKL/O1ChWDa9w5XUMQ7aKR9yu d5585Bc8YPDiKucRDVgIhSY= Received: (qmail 72695 invoked by alias); 27 Nov 2018 19:38:25 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 72668 invoked by uid 89); 27 Nov 2018 19:38:23 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-11.1 required=5.0 tests=BAYES_00, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=D*1, noinline, H*F:D*usp.br, H*F:D*br X-HELO: mail-ot1-f50.google.com Received: from mail-ot1-f50.google.com (HELO mail-ot1-f50.google.com) (209.85.210.50) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 27 Nov 2018 19:38:18 +0000 Received: by mail-ot1-f50.google.com with SMTP id s5so21233152oth.7 for ; Tue, 27 Nov 2018 11:38:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=usp-br.20150623.gappssmtp.com; s=20150623; h=mime-version:from:date:message-id:subject:to; bh=50CalktSVOLGKyG136czYLYLkvLJE9QWx9M7YejjrGc=; b=Y2RLDxl5Bdm58QUIZapgmKxTzs7pRFjJchp1ABdJHeu9y04UpciDIS83wVuhuCrLBR a4QXzS3qKkj/Gm2uGjPAflRecPShk7PAWO6d9m3XtD4CivfahVoKnFCkYeZn1n17lL7S 7ucHLDq51TI2sCyHNSC+ArYyYexYMIZ52ULbh5TJTXYIXNrqL5tjPxpaQC3TgYgFKfK6 vaHrkxrjWkihXMqSClHIAxgJc25GOI7jrfEBFRT2FP8/c+RExq4edWimidAXcl5xifa8 /SedKFaHxX3HQPgU4WgzmgvJlagM1DpABYlzJ0i+4rcMad3Tjo3dYctV+LI6dnE3ykkp 9NXg== MIME-Version: 1.0 From: Giuliano Augusto Faulin Belinassi Date: Tue, 27 Nov 2018 17:38:04 -0200 Message-ID: Subject: [PATCH v4] Add sinh(atanh(x)) and cosh(atanh(x)) optimizations To: GCC Patches , Wilco.Dijkstra@arm.com, Richard Biener X-IsSubscribed: yes Only do this optimization if funsafe-math and -fno-math-errno are enabled, as pointed in the previous iteration. Also added one more test case to ensure that fno-math-errno is required for the optimization. Special thanks for Wilco Dijsktra for all his help :-) gcc/ChangeLog 2018-11-27 Giuliano Belinassi * match.pd (sinh (atanh (x))): New simplification rules. (cosh (atanh (x))): Likewise. gcc/testsuite/ChangeLog 2018-11-27 Giuliano Belinassi * gcc.dg/sinhatanh-1.c: New test. * gcc.dg/sinhatanh-2.c: New test. * gcc.dg/sinhatanh-3.c: New test. Index: gcc/match.pd =================================================================== --- gcc/match.pd (revision 266469) +++ gcc/match.pd (working copy) @@ -4342,6 +4342,25 @@ (rdiv { t_one; } (sqrts (plus (mult @0 @0) { t_one; }))) (copysigns { t_zero; } @0)))))) + (if (!flag_errno_math) + /* Simplify sinh(atanh(x)) -> x / sqrt((1 - x)*(1 + x)). */ + (for sinhs (SINH) + atanhs (ATANH) + sqrts (SQRT) + (simplify + (sinhs (atanhs:s @0)) + (with { tree t_one = build_one_cst (type); } + (rdiv @0 (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0))))))) + + /* Simplify cosh(atanh(x)) -> 1 / sqrt((1 - x)*(1 + x)) */ + (for coshs (COSH) + atanhs (ATANH) + sqrts (SQRT) + (simplify + (coshs (atanhs:s @0)) + (with { tree t_one = build_one_cst (type); } + (rdiv { t_one; } (sqrts (mult (minus { t_one; } @0) (plus { t_one; } @0)))))))) + /* cabs(x+0i) or cabs(0+xi) -> abs(x). */ (simplify (CABS (complex:C @0 real_zerop@1)) Index: gcc/testsuite/gcc.dg/sinhatanh-1.c =================================================================== --- gcc/testsuite/gcc.dg/sinhatanh-1.c (nonexistent) +++ gcc/testsuite/gcc.dg/sinhatanh-1.c (working copy) @@ -0,0 +1,62 @@ +/* { dg-do compile } */ +/* { dg-options "-Ofast -fdump-tree-optimized" } */ + +extern float sinhf (float); +extern float coshf (float); +extern float atanhf (float); +extern float sqrtf (float); +extern double sinh (double); +extern double cosh (double); +extern double atanh (double); +extern double sqrt (double); +extern long double sinhl (long double); +extern long double coshl (long double); +extern long double atanhl (long double); +extern long double sqrtl (long double); + +double __attribute__ ((noinline)) +sinhatanh_ (double x) +{ + return sinh (atanh (x)); +} + +double __attribute__ ((noinline)) +coshatanh_ (double x) +{ + return cosh (atanh (x)); +} + +float __attribute__ ((noinline)) +sinhatanhf_(float x) +{ + return sinhf (atanhf (x)); +} + +float __attribute__ ((noinline)) +coshatanhf_(float x) +{ + return coshf (atanhf (x)); +} + +long double __attribute__ ((noinline)) +sinhatanhl_ (long double x) +{ + return sinhl (atanhl (x)); +} + +long double __attribute__ ((noinline)) +coshatanhl_ (long double x) +{ + return coshl (atanhl (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 "atanh " "optimized" }} */ +/* {dg-final { scan-tree-dump-not "sinfh " "optimized" } } */ +/* {dg-final { scan-tree-dump-not "cosfh " "optimized" } } */ +/* {dg-final { scan-tree-dump-not "atanfh " "optimized" }} */ +/* {dg-final { scan-tree-dump-not "sinlh " "optimized" } } */ +/* {dg-final { scan-tree-dump-not "coslh " "optimized" } } */ +/* {dg-final { scan-tree-dump-not "atanlh " "optimized" }} */ Index: gcc/testsuite/gcc.dg/sinhatanh-2.c =================================================================== --- gcc/testsuite/gcc.dg/sinhatanh-2.c (nonexistent) +++ gcc/testsuite/gcc.dg/sinhatanh-2.c (working copy) @@ -0,0 +1,68 @@ +/* { dg-do compile } */ +/* { dg-options "-Ofast -fdump-tree-optimized" } */ + +extern float sinhf (float); +extern float coshf (float); +extern float atanhf (float); +extern float sqrtf (float); +extern double sinh (double); +extern double cosh (double); +extern double atanh (double); +extern double sqrt (double); +extern long double sinhl (long double); +extern long double coshl (long double); +extern long double atanhl (long double); +extern long double sqrtl (long double); + +float __attribute__ ((noinline)) +coshatanhf_(float x) +{ + float atg = atanhf(x); + return coshf(atg) + atg; +} + +double __attribute__ ((noinline)) +cosatan_(double x) +{ + double atg = atanh(x); + return cosh(atg) + atg; +} + +long double __attribute__ ((noinline)) +cosatanl_(long double x) +{ + long double atg = atanhl(x); + return coshl(atg) + atg; +} + +float __attribute__ ((noinline)) +sinatanf_(float x) +{ + float atg = atanhf(x); + return sinhf(atg) + atg; +} + +double __attribute__ ((noinline)) +sinatan_(double x) +{ + double atg = atanh(x); + return sinh(atg) + atg; +} + +long double __attribute__ ((noinline)) +sinatanl_(long double x) +{ + long double atg = atanhl(x); + return sinhl(atg) + atg; +} + +/* There should be calls to sinh, cosh and atanh */ +/* { dg-final { scan-tree-dump "cosh " "optimized" } } */ +/* { dg-final { scan-tree-dump "sinh " "optimized" } } */ +/* { dg-final { scan-tree-dump "atanh " "optimized" } } */ +/* { dg-final { scan-tree-dump "coshf " "optimized" } } */ +/* { dg-final { scan-tree-dump "sinhf " "optimized" } } */ +/* { dg-final { scan-tree-dump "atanhf " "optimized" } } */ +/* { dg-final { scan-tree-dump "coshl " "optimized" } } */ +/* { dg-final { scan-tree-dump "sinhl " "optimized" } } */ +/* { dg-final { scan-tree-dump "atanhl " "optimized" } } */ Index: gcc/testsuite/gcc.dg/sinhatanh-3.c =================================================================== --- gcc/testsuite/gcc.dg/sinhatanh-3.c (nonexistent) +++ gcc/testsuite/gcc.dg/sinhatanh-3.c (working copy) @@ -0,0 +1,62 @@ +/* { dg-do compile } */ +/* { dg-options "-Ofast -fmath-errno -fdump-tree-optimized" } */ + +extern float sinhf (float); +extern float coshf (float); +extern float atanhf (float); +extern float sqrtf (float); +extern double sinh (double); +extern double cosh (double); +extern double atanh (double); +extern double sqrt (double); +extern long double sinhl (long double); +extern long double coshl (long double); +extern long double atanhl (long double); +extern long double sqrtl (long double); + +double __attribute__ ((noinline)) +sinhatanh_ (double x) +{ + return sinh (atanh (x)); +} + +double __attribute__ ((noinline)) +coshatanh_ (double x) +{ + return cosh (atanh (x)); +} + +float __attribute__ ((noinline)) +sinhatanhf_(float x) +{ + return sinhf (atanhf (x)); +} + +float __attribute__ ((noinline)) +coshatanhf_(float x) +{ + return coshf (atanhf (x)); +} + +long double __attribute__ ((noinline)) +sinhatanhl_ (long double x) +{ + return sinhl (atanhl (x)); +} + +long double __attribute__ ((noinline)) +coshatanhl_ (long double x) +{ + return coshl (atanhl (x)); +} + +/* There should be calls to sinh, cosh and atanh */ +/* { dg-final { scan-tree-dump "cosh " "optimized" } } */ +/* { dg-final { scan-tree-dump "sinh " "optimized" } } */ +/* { dg-final { scan-tree-dump "atanh " "optimized" } } */ +/* { dg-final { scan-tree-dump "coshf " "optimized" } } */ +/* { dg-final { scan-tree-dump "sinhf " "optimized" } } */ +/* { dg-final { scan-tree-dump "atanhf " "optimized" } } */ +/* { dg-final { scan-tree-dump "coshl " "optimized" } } */ +/* { dg-final { scan-tree-dump "sinhl " "optimized" } } */ +/* { dg-final { scan-tree-dump "atanhl " "optimized" } } */