From patchwork Thu Apr 27 11:13:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 88200 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp188890vqo; Thu, 27 Apr 2023 04:13:52 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ47OLZcKCruugx78O7wsoWKTeWIMPsnoKi9ly/wM3nfGG8y60LelAnYQZBVVeMAjQMlT8L0 X-Received: by 2002:aa7:d987:0:b0:506:9220:b149 with SMTP id u7-20020aa7d987000000b005069220b149mr987515eds.26.1682594032621; Thu, 27 Apr 2023 04:13:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682594032; cv=none; d=google.com; s=arc-20160816; b=I32uzF5zMfZXo+UhPIdUqomlc38YhOKX5oaxEmmgd70MDvKmIHMd1JTIFg90emvbLI yZOWOAgPrZeZOkQhXo61mzPdtiRQ6oKYBtO7ORjjVSgqIMwEt+k1jo3g88sJOT8/KjxR rpYRTmwnF8t5MHkLd9+Kb+ByiVWcdGArBl92uMVi1CxXnRq5OXtHBYev6iV0YSfles7z Q6kbkbAC4U/IrhIwZXCSGXzVe5LlezqOSbe7ocRZyC29HgdPPhmmXuUFhglT4iaQX1K5 DlXS2wVU2E/KuPjvgYnEq5vCY0+FU0hCpX0QgiGF+HY/nh8ip7RNRbT1DA16Olg5os2v 1daQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:in-reply-to:mime-version:references:message-id :subject:cc:to:date:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=QCrY2jT6jr6RoSLjhVHTgiFm89db3XqjNi4YrTWfq74=; b=m4tW6yLxe7Jb8KwbkEQi9r5LOU2Mh6IDmGKD+6mgEeNjRiNlpYVXHx5QYAv8mCtRep c4V2XwM2aNL90o+j/4HEMMIbeScbpNsRyxNih5c+DUUMkEFDXG1oWlfWtdOIKIPFQb1a ME5O2VwRUfoM21uvhFuq4cax8wVpaNW0WpzQ562nu4ZjOJnY23d5/A8Cn9sEpKqmvM2G cara6p6ysYA0NGxSupORNTW4Ahmv0g2L4li3jHFcxoP1y67o9YYHebvwBQ7sRymn8jqq I5EY6JiNo+Ov3HTA1GzrKstg1KZBkHdaK5txJXJuIO2OBqpbS6wKLInm49SXQLp92GeK 71eg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=P2mUmTXi; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id r22-20020aa7cfd6000000b0050847f2103fsi13356674edy.687.2023.04.27.04.13.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Apr 2023 04:13:52 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=P2mUmTXi; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E9368385841E for ; Thu, 27 Apr 2023 11:13:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E9368385841E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682594030; bh=QCrY2jT6jr6RoSLjhVHTgiFm89db3XqjNi4YrTWfq74=; h=Date:To:Cc:Subject:References:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=P2mUmTXi+9BrY3/Ptth1jFTZiUNZy18O5wNk2kEhcqLyIBy3YfXIarfpYlg195LDD MyIZVt6NIf1B5ZsuW2J+mtDRM3Kpn2uID8RCu+9bAFc4Cb1tpE5Sak2je0W3/YzFz+ t90VXSL6/8pzPmIPoi2ovrIG5vA1A9uPdeWY1dTY= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id D60543858D38 for ; Thu, 27 Apr 2023 11:13:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D60543858D38 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-149-_f2ZS46JMKSoWYp-o3UuLA-1; Thu, 27 Apr 2023 07:13:05 -0400 X-MC-Unique: _f2ZS46JMKSoWYp-o3UuLA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B938A109DCE7 for ; Thu, 27 Apr 2023 11:13:04 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.194.156]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 65B14C15BA0; Thu, 27 Apr 2023 11:13:04 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 33RBD2ie2757415 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 27 Apr 2023 13:13:02 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 33RBD1bK2757414; Thu, 27 Apr 2023 13:13:01 +0200 Date: Thu, 27 Apr 2023 13:13:01 +0200 To: Aldy Hernandez Cc: GCC patches , Andrew MacLeod Subject: [PATCH] v2: Implement range-op entry for sin/cos Message-ID: References: <20230418131250.310916-1-aldyh@redhat.com> MIME-Version: 1.0 In-Reply-To: <20230418131250.310916-1-aldyh@redhat.com> X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1764327719989399012?= X-GMAIL-MSGID: =?utf-8?q?1764327719989399012?= Hi! On Tue, Apr 18, 2023 at 03:12:50PM +0200, Aldy Hernandez wrote: > [I don't know why I keep poking at floats. I must really like the pain. > Jakub, are you OK with this patch for trunk?] > > This is the range-op entry for sin/cos. It is meant to serve as an > example of what we can do for glibc math functions. It is by no means > exhaustive, just a stub to restrict the return range from sin/cos to > [-1.0, 1.0] with appropriate smarts of NANs. > > As can be seen in the testcase, we see sin() as well as > __builtin_sin() in the IL, and can resolve the resulting range > accordingly. > > gcc/ChangeLog: > > * gimple-range-op.cc (class cfn_sincos): New. > (gimple_range_op_handler::maybe_builtin_call): Add case for sin/cos. > > gcc/testsuite/ChangeLog: > > * gcc.dg/tree-ssa/range-sincos.c: New test. Here is an updated version of the patch on top of the v2: Add targetm.libm_function_max_error patch with all my comments incorporated into your patch (but still no handling of sin/cos ranges shorter than 2*M_PI). So far tested on the new testcase with RUNTESTFLAGS='--target_board=unix\{,-frounding-math} tree-ssa.exp=range-sincos.c' where expectedly without -frounding-math it PASSes and with it fails (as the hooks return 4ulps in that case for the boundaries). Ok for trunk if it passes bootstrap/regtest? I'll defer tweaks to frange_nextafter for ulps or whatever you want to do with it. 2023-04-27 Aldy Hernandez Jakub Jelinek * value-range.h (frange_nextafter): Declare. * gimple-range-op.cc (class cfn_sincos): New. (op_cfn_sin, op_cfn_cos): New variables. (gimple_range_op_handler::maybe_builtin_call): Handle CASE_CFN_{SIN,COS}{,_FN}. * gcc.dg/tree-ssa/range-sincos.c: New test. Jakub --- gcc/value-range.h.jj 2023-04-27 10:17:46.504485376 +0200 +++ gcc/value-range.h 2023-04-27 12:07:09.891147869 +0200 @@ -1388,4 +1388,7 @@ frange::nan_signbit_p (bool &signbit) co return true; } +void frange_nextafter (enum machine_mode, REAL_VALUE_TYPE &, + const REAL_VALUE_TYPE &); + #endif // GCC_VALUE_RANGE_H --- gcc/gimple-range-op.cc.jj 2023-04-22 10:23:26.942812958 +0200 +++ gcc/gimple-range-op.cc 2023-04-27 11:57:09.865879982 +0200 @@ -400,6 +400,89 @@ public: } } op_cfn_copysign; +class cfn_sincos : public range_operator_float +{ +public: + using range_operator_float::fold_range; + using range_operator_float::op1_range; + cfn_sincos (combined_fn cfn) { m_cfn = cfn; } + virtual bool fold_range (frange &r, tree type, + const frange &lh, const frange &, + relation_trio) const final override + { + if (lh.undefined_p ()) + return false; + if (lh.known_isnan () || lh.known_isinf ()) + { + r.set_nan (type); + return true; + } + unsigned bulps = targetm.libm_function_max_error (m_cfn, TYPE_MODE (type), + true); + if (bulps == ~0U) + r.set_varying (type); + else if (bulps == 0) + r.set (type, dconstm1, dconst1); + else + { + REAL_VALUE_TYPE boundmin, boundmax; + boundmax = dconst1; + while (bulps--) + frange_nextafter (TYPE_MODE (type), boundmax, dconstinf); + real_arithmetic (&boundmin, NEGATE_EXPR, &boundmax, NULL); + r.set (type, boundmin, boundmax); + } + if (!lh.maybe_isnan () && !lh.maybe_isinf ()) + r.clear_nan (); + return true; + } + virtual bool op1_range (frange &r, tree type, + const frange &lhs, const frange &, + relation_trio) const final override + { + if (lhs.undefined_p ()) + return false; + + // A known NAN means the input is [-INF,-INF][+INF,+INF] U +-NAN, + // which we can't currently represent. + if (lhs.known_isnan ()) + { + r.set_varying (type); + return true; + } + + // Results outside of [-1.0, +1.0] are impossible. + REAL_VALUE_TYPE lb = lhs.lower_bound (); + REAL_VALUE_TYPE ub = lhs.upper_bound (); + if (real_less (&ub, &dconstm1) || real_less (&dconst1, &lb)) + { + if (!lhs.maybe_isnan ()) + r.set_undefined (); + else + /* If lhs could be NAN and finite result is impossible, + the range is like lhs.known_isnan () above, + [-INF,-INF][+INF,+INF] U +-NAN. */ + r.set_varying (type); + return true; + } + + if (!lhs.maybe_isnan ()) + { + // If NAN is not valid result, the input cannot include either + // a NAN nor a +-INF. + lb = real_min_representable (type); + ub = real_max_representable (type); + r.set (type, lb, ub, nan_state (false, false)); + return true; + } + + r.set_varying (type); + return true; + } +private: + combined_fn m_cfn; +} op_cfn_sin (CFN_SIN), op_cfn_cos (CFN_COS); + // Implement range operator for CFN_BUILT_IN_TOUPPER and CFN_BUILT_IN_TOLOWER. class cfn_toupper_tolower : public range_operator { @@ -878,6 +961,20 @@ gimple_range_op_handler::maybe_builtin_c m_valid = true; break; + CASE_CFN_SIN: + CASE_CFN_SIN_FN: + m_op1 = gimple_call_arg (call, 0); + m_float = &op_cfn_sin; + m_valid = true; + break; + + CASE_CFN_COS: + CASE_CFN_COS_FN: + m_op1 = gimple_call_arg (call, 0); + m_float = &op_cfn_cos; + m_valid = true; + break; + case CFN_BUILT_IN_TOUPPER: case CFN_BUILT_IN_TOLOWER: // Only proceed If the argument is compatible with the LHS. --- gcc/testsuite/gcc.dg/tree-ssa/range-sincos.c.jj 2023-04-27 10:27:08.135348612 +0200 +++ gcc/testsuite/gcc.dg/tree-ssa/range-sincos.c 2023-04-27 12:14:50.788437504 +0200 @@ -0,0 +1,43 @@ +// { dg-do compile } +// { dg-options "-O2 -fdump-tree-evrp -fno-thread-jumps" } + +#include + +void use (double); +void link_error (); + +void +foo (double x) +{ + if (__builtin_isnan (x)) + __builtin_unreachable (); + x = sin (x); + if (x < -1.0 || x > 1.0) + link_error (); + use (x); +} + +void +bar (double x) +{ + if (!__builtin_isnan (sin (x))) + { + if (__builtin_isnan (x)) + link_error (); + if (__builtin_isinf (x)) + link_error (); + } +} + +void +stool (double x) +{ + double res1 = sin (x); + double res2 = __builtin_sin (x); + if (res1 < -1.0 || res2 < -1.0) + link_error (); + if (res1 > 1.0 || res2 > 1.0) + link_error (); +} + +// { dg-final { scan-tree-dump-not "link_error" "evrp" { target { { *-*-linux* } && { glibc } } } } }