From patchwork Thu Oct 1 13:51:52 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Sandiford X-Patchwork-Id: 524986 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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 6AE8E140B0D for ; Thu, 1 Oct 2015 23:52:07 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=WgbEx3Pa; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id:mime-version:content-type :content-transfer-encoding; q=dns; s=default; b=kq2LPoyqcf13bXRr Bx+ti/F+bO/j8W94ge2yaZiJIxroziI7g4UVIvbDMLKXIPntKJtV5dtit8ALJTCr RNkUFZoWTnCkGWuOuS+KbZdplmkrmHZn4Zja3huMTplubI2s2suIpneZg8MAOvXz iZyHNPUojs4/1RslHm4b8xgWt/4= 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:from :to:subject:date:message-id:mime-version:content-type :content-transfer-encoding; s=default; bh=W35aKIj9nvYojYNgocsAWZ GVb9I=; b=WgbEx3PaTaCSmeuSPgkyWJGdiSaChR0t68c+GPC+0TbweJMmAFmKr5 1AhdNk2kJHWuMH+9kaYa0U0CmN6YaTSxMJqUDLkCPZi64uzy5d1fhJ016+CtCsrt 3Nwlfkurvo4Rce5hgzasv+qdG+pKBzz+UnuVbM/KvAD6GtP3gN94E= Received: (qmail 42855 invoked by alias); 1 Oct 2015 13:52:01 -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 42838 invoked by uid 89); 1 Oct 2015 13:52:00 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.4 required=5.0 tests=AWL, BAYES_00, SPF_PASS autolearn=ham version=3.3.2 X-HELO: eu-smtp-delivery-143.mimecast.com Received: from eu-smtp-delivery-143.mimecast.com (HELO eu-smtp-delivery-143.mimecast.com) (146.101.78.143) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 01 Oct 2015 13:51:58 +0000 Received: from cam-owa1.Emea.Arm.com (fw-tnat.cambridge.arm.com [217.140.96.140]) by eu-smtp-1.mimecast.com with ESMTP id uk-mta-17-DW--2T_7RISfQoY-1ESbGg-1; Thu, 01 Oct 2015 14:51:52 +0100 Received: from localhost ([10.1.2.79]) by cam-owa1.Emea.Arm.com with Microsoft SMTPSVC(6.0.3790.3959); Thu, 1 Oct 2015 14:51:52 +0100 From: Richard Sandiford To: gcc-patches@gcc.gnu.org Mail-Followup-To: gcc-patches@gcc.gnu.org, richard.sandiford@arm.com Subject: Cache reals for 1/4, 1/6 and 1/9 Date: Thu, 01 Oct 2015 14:51:52 +0100 Message-ID: <87twqay9vr.fsf@e105548-lin.cambridge.arm.com> User-Agent: Gnus/5.130012 (Ma Gnus v0.12) Emacs/24.3 (gnu/linux) MIME-Version: 1.0 X-MC-Unique: DW--2T_7RISfQoY-1ESbGg-1 We have a global 1/2 and a cached 1/3, but recalculate 1/4, 1/6 and 1/9 each time we need them. That seems a bit arbitrary and makes the folding code more noisy (especially once it's moved to match.pd). This patch caches the other three constants too. Bootstrapped & regression-tested on x86_64-linux-gnu. OK to install? Thanks, Richard gcc/ * real.h (dconst_quarter, dconst_sixth, dconst_ninth): New macros. (dconst_quarter_ptr, dconst_sixth_ptr, dconst_ninth_ptr): Declare. * real.c (CACHED_FRACTION(: New helper macro. (dconst_third_ptr): Use it. (dconst_quarter_ptr, dconst_sixth_ptr, dconst_ninth_ptr): New. * builtins.c (fold_builtin_sqrt): Use dconst_quarter and dconst_sixth. (fold_builtin_cbrt): Use dconst_sixth and dconst_ninth. diff --git a/gcc/builtins.c b/gcc/builtins.c index 1751b37..63724b9 100644 --- a/gcc/builtins.c +++ b/gcc/builtins.c @@ -7743,20 +7743,10 @@ fold_builtin_sqrt (location_t loc, tree arg, tree type) if (powfn) { tree arg0 = CALL_EXPR_ARG (arg, 0); - tree tree_root; - /* The inner root was either sqrt or cbrt. */ - /* This was a conditional expression but it triggered a bug - in Sun C 5.5. */ - REAL_VALUE_TYPE dconstroot; - if (BUILTIN_SQRT_P (fcode)) - dconstroot = dconsthalf; - else - dconstroot = dconst_third (); - - /* Adjust for the outer root. */ - SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1); - tree_root = build_real_truncate (type, dconstroot); - return build_call_expr_loc (loc, powfn, 2, arg0, tree_root); + tree arg1 = (BUILTIN_SQRT_P (fcode) + ? build_real (type, dconst_quarter ()) + : build_real_truncate (type, dconst_sixth ())); + return build_call_expr_loc (loc, powfn, 2, arg0, arg1); } } @@ -7816,11 +7806,7 @@ fold_builtin_cbrt (location_t loc, tree arg, tree type) if (powfn) { tree arg0 = CALL_EXPR_ARG (arg, 0); - tree tree_root; - REAL_VALUE_TYPE dconstroot = dconst_third (); - - SET_REAL_EXP (&dconstroot, REAL_EXP (&dconstroot) - 1); - tree_root = build_real_truncate (type, dconstroot); + tree tree_root = build_real_truncate (type, dconst_sixth ()); return build_call_expr_loc (loc, powfn, 2, arg0, tree_root); } } @@ -7835,12 +7821,7 @@ fold_builtin_cbrt (location_t loc, tree arg, tree type) if (powfn) { - tree tree_root; - REAL_VALUE_TYPE dconstroot; - - real_arithmetic (&dconstroot, MULT_EXPR, - dconst_third_ptr (), dconst_third_ptr ()); - tree_root = build_real_truncate (type, dconstroot); + tree tree_root = build_real_truncate (type, dconst_ninth ()); return build_call_expr_loc (loc, powfn, 2, arg0, tree_root); } } diff --git a/gcc/real.c b/gcc/real.c index c1ff78d..78f3623 100644 --- a/gcc/real.c +++ b/gcc/real.c @@ -2379,21 +2379,26 @@ dconst_e_ptr (void) return &value; } -/* Returns the special REAL_VALUE_TYPE corresponding to 1/3. */ - -const REAL_VALUE_TYPE * -dconst_third_ptr (void) -{ - static REAL_VALUE_TYPE value; - - /* Initialize mathematical constants for constant folding builtins. - These constants need to be given to at least 160 bits precision. */ - if (value.cl == rvc_zero) - { - real_arithmetic (&value, RDIV_EXPR, &dconst1, real_digit (3)); - } - return &value; -} +/* Returns a cached REAL_VALUE_TYPE corresponding to 1/n, for various n. */ + +#define CACHED_FRACTION(NAME, N) \ + const REAL_VALUE_TYPE * \ + NAME (void) \ + { \ + static REAL_VALUE_TYPE value; \ + \ + /* Initialize mathematical constants for constant folding builtins. \ + These constants need to be given to at least 160 bits \ + precision. */ \ + if (value.cl == rvc_zero) \ + real_arithmetic (&value, RDIV_EXPR, &dconst1, real_digit (N)); \ + return &value; \ + } + +CACHED_FRACTION (dconst_third_ptr, 3) +CACHED_FRACTION (dconst_quarter_ptr, 4) +CACHED_FRACTION (dconst_sixth_ptr, 6) +CACHED_FRACTION (dconst_ninth_ptr, 9) /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */ diff --git a/gcc/real.h b/gcc/real.h index 455d853..5d8c92c 100644 --- a/gcc/real.h +++ b/gcc/real.h @@ -414,15 +414,21 @@ extern REAL_VALUE_TYPE dconst2; extern REAL_VALUE_TYPE dconstm1; extern REAL_VALUE_TYPE dconsthalf; -#define dconst_e() (*dconst_e_ptr ()) -#define dconst_third() (*dconst_third_ptr ()) -#define dconst_sqrt2() (*dconst_sqrt2_ptr ()) +#define dconst_e() (*dconst_e_ptr ()) +#define dconst_third() (*dconst_third_ptr ()) +#define dconst_quarter() (*dconst_quarter_ptr ()) +#define dconst_sixth() (*dconst_sixth_ptr ()) +#define dconst_ninth() (*dconst_ninth_ptr ()) +#define dconst_sqrt2() (*dconst_sqrt2_ptr ()) /* Function to return the real value special constant 'e'. */ extern const REAL_VALUE_TYPE * dconst_e_ptr (void); -/* Returns the special REAL_VALUE_TYPE corresponding to 1/3. */ -extern const REAL_VALUE_TYPE * dconst_third_ptr (void); +/* Returns a cached REAL_VALUE_TYPE corresponding to 1/n, for various n. */ +extern const REAL_VALUE_TYPE *dconst_third_ptr (void); +extern const REAL_VALUE_TYPE *dconst_quarter_ptr (void); +extern const REAL_VALUE_TYPE *dconst_sixth_ptr (void); +extern const REAL_VALUE_TYPE *dconst_ninth_ptr (void); /* Returns the special REAL_VALUE_TYPE corresponding to sqrt(2). */ extern const REAL_VALUE_TYPE * dconst_sqrt2_ptr (void);